// ==================================== // AJAX // ==================================== /* Test multiple ajax submission - check if the success ajax works to display a notification, multiple of it - Ajax Complete - Ajax Error - Ajax Success - Ajax Get - Ajax GetJSON - Ajax GetScript */ var leAjax = { // send_ajax: [], // count_ajax: 0, ajaxFlag: false, ajaxQueue: [], init: function(){ }, isJsonString: function(str) { try { JSON.parse(str); } catch (e) { return false; } return true; }, buttonText: false, buttonLoad: function(form){ var button; // check if the form is already a button if($(form).is('button') || $(form).is('input')){ button = $(form); if($(form).is('button') && !leAjax.buttonText) leAjax.buttonText = button.html(); else if($(form).is('input') && !leAjax.buttonText) leAjax.buttonText = button.val(); }else{ button = $(form).find('button[type=submit]'); if(!button.length){ button = $(form).find('input[type=submit]'); if(!leAjax.buttonText) leAjax.buttonText = button.val(); }else{ if(!leAjax.buttonText) leAjax.buttonText = button.html(); } } var newButton = false; var modalID; if(button.is('[data-mtb]')){ modalID = $(button).attr('data-modalid'); newButton = $('#'+modalID).find('.modal-footer #'+button.attr('data-mtb')); } // not loading if(!button.hasClass('loading')){ button.addClass('loading'); button.text('Processing..'); button.val('Processing..'); button.css('cursor','wait'); button.prop('disabled',true); if(newButton){ newButton.addClass('loading'); newButton.text('Processing..'); newButton.val('Processing..'); newButton.css('cursor','wait'); newButton.prop('disabled',true); } } // loading else{ button.removeClass('loading'); button.html(leAjax.buttonText); button.val(leAjax.buttonText); button.css('cursor','pointer'); leAjax.buttonText = false; button.prop('disabled',false); if(newButton){ newButton.removeClass('loading'); newButton.text(button.text()); newButton.val(button.text()); newButton.css('cursor','pointer'); newButton.prop('disabled',false); } } }, push: function(settings){ if(leAjax.isJsonString(settings)){ settings = JSON.parse(settings); } if(!settings.type) settings.type = "post"; var defaults = { async: true, type: settings.type, dataType: "json", }; var options = $.extend({}, defaults, settings); // do some checkings if(options.url == undefined){ alert('Please provide url for ajax'); return false; } else if(options.callback == undefined){ alert('Please provide the callback success function'); return false; } else if(options.data == undefined){ alert('Please provide the data for ajax. Token is required.'); return false; } // a flag for the server side that this is ajax options.data.ajax = true; // count_ajax = leAjax.count_ajax++; options.complete = function(zxc){ // leAjax.send_ajax[count_ajax] = null; leAjax.ajaxFlag = false; if(leAjax.ajaxQueue.length > 0){ leAjax.push(leAjax.ajaxQueue.shift()); } } options.error = function(error){ leAjax.ajaxFlag = false; if(error.status == 200){ error.message = error.responseText + '


If the message in this box looks funny, please snapshot the screen and send it to the developer. Thank you!'; options.callback(error); } console.log(error); } options.success = function(results){ leAjax.ajaxFlag = false; options.callback(results); } if(leAjax.ajaxFlag === false){ leAjax.ajaxFlag = $.ajax(options); }else{ leAjax.ajaxQueue.push(options); } }, //end submit // get the only token getToken: function(){ return $('#site_token').text(); }, // get the proc master url // if site is true, return the site url getUrl: function(site){ if(!!site){ return $('#site_token').attr('data-url') + "s_proc/master.proc.php"; }else{ return $('#site_token').attr('data-url') + "c_proc/master.proc.php"; } } } $(document).ready(function(){ leAjax.init(); });