8a6dfe491930bd588bf21f27af49b487535867c5
[moodle.git] / lib / amd / build / ajax.min.js
1 define(["jquery","core/config","core/log","core/yui","core/url"],function(a,b,c,d,e){var f=!1,g=function(a){var b,c,f=this,g=null,h=0;if(a.error)for(;h<f.length;h++)b=f[h],b.deferred.reject(a);else{for(h=0;h<f.length;h++){if(b=f[h],c=a[h],"undefined"==typeof c){g=new Error("missing response");break}if(c.error!==!1){g=c.exception;break}b.deferred.resolve(c.data)}if(null!==g)if("servicerequireslogin"===g.errorcode)d.use("moodle-core-formchangechecker",function(){if(!M.core_formchangechecker.get_form_dirty_state()){var a=e.relativeUrl("/login/index.php");window.location.replace(a)}});else for(;h<f.length;h++)b=f[h],b.deferred.reject(g)}},h=function(a,b,d){var e=this,g=0;for(g=0;g<e.length;g++){var h=e[g];f?(c.error("Page unloaded."),c.error(d)):h.deferred.reject(d)}};return{call:function(c,d,e){a(window).bind("beforeunload",function(){f=!0});var i,j=[],k=[],l=[],m="";for("undefined"==typeof e&&(e=!0),"undefined"==typeof d&&(d=!0),i=0;i<c.length;i++){var n=c[i];j.push({index:i,methodname:n.methodname,args:n.args}),n.deferred=a.Deferred(),k.push(n.deferred.promise()),"undefined"!=typeof n.done&&n.deferred.done(n.done),"undefined"!=typeof n.fail&&n.deferred.fail(n.fail),n.index=i,l.push(n.methodname)}m=l.length<=5?l.sort().join():l.length+"-method-calls",j=JSON.stringify(j);var o={type:"POST",data:j,context:c,dataType:"json",processData:!1,async:d,contentType:"application/json"},p="service.php";e||(p="service-nologin.php");var q=b.wwwroot+"/lib/ajax/"+p+"?sesskey="+b.sesskey+"&info="+m;return d?a.ajax(q,o).done(g).fail(h):(o.success=g,o.error=h,a.ajax(q,o)),k}}});