-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.process.min.js
16 lines (15 loc) · 3.23 KB
/
jquery.process.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/** jQueryProcess (https://github.com/nothrem/jQueryProcess) (c) 2015 */
(function($){if(!$){return;}
$.Process=function(init){if(!(this instanceof $.Process)){throw"Cannnot use Process constructor as a function!";}
$.ProcessFactory.call(this,{},init);};$.ProcessFactory=function(source,init){var process;if(this instanceof $.Process){process=this;}
else{process=new $.Process(init);process.promise(source);return process;}
var tuples=[["resolve","done",$.Callbacks("once memory"),"resolved",$.Callbacks("once memory")],["reject","fail",$.Callbacks("once memory"),"rejected",$.Callbacks("once memory")],["notify","progress",$.Callbacks("memory"),undefined,$.Callbacks("memory")]],events=['on','one','off','trigger','triggerHandler'],state="pending",promise={state:function(){return state;},always:function(){process.done(arguments).fail(arguments);return this;},sync:function(process){promise.done(process.resolve).fail(process.reject).progress(process.notify);return source;},promise:function(obj){source=promise.promiseWith(obj);return source;},promiseWith:function(obj){return obj?$.extend(obj,promise):source;},ajax:function(obj){promise.promise(obj);return promise.ajaxWith(obj);},ajaxWith:function(obj){var jqXHR=promise.promiseWith(obj);jqXHR.success=jqXHR.done;jqXHR.error=jqXHR.fail;return jqXHR;},all:function(){var promises=[];$.each(arguments,function(){var me=this;if(!me||'function'!==typeof me.then){return;}
promises.push(me);me.then(function(){var pos=promises.indexOf(me);(pos<0||promises.splice(pos,1));(promises.length||process.resolve());},process.reject);});return this;},race:function(){$.each(arguments,function(){(!this||'function'!=typeof this.then||this.then(process.resolve,process.reject));});return this;},then:function(done,fail,progress){(!done||this.done(done));(!fail||this.fail(fail));(!progress||this.progress(progress));return this;},'catch':function(onRejected){this.fail(onRejected);return this;}};$.each(events,function(i,method){promise[method]=function(){var me=(this===process?process:source);$.fn[method].apply($(me),arguments);return this;};});$.each(tuples,function(i,tuple){var list=tuple[2],listProc=tuple[4],stateString=tuple[3];promise[tuple[1]]=function(){if(this===process){listProc.add.apply(listProc,arguments);}
else{list.add.apply(list,arguments);}
return this;};if(stateString){list.add(function(){state=stateString;},tuples[i^1][2].disable,tuples[2][2].lock);}
process[tuple[0]]=function(){process[tuple[0]+"With"](source,arguments);return this;};process[tuple[0]+"Ajax"]=function(){var args=arguments,jqXHR=promise.ajax();if('resolve'===tuple[0]){args=[args[0],args[1],jqXHR];}
else if('reject'===tuple[0]){args=[jqXHR,args[0],args[1]];}
window.setTimeout(function(){process[tuple[0]+"With"](jqXHR,args);},1);return this;};process[tuple[0]+"With"]=function(obj,args){if(state!=="pending"){return;}
if('notify'===tuple[0]&&'string'===typeof args[0]){args[0]=args[0].replace(' ','_');$.fn.trigger.apply($(process),args);$.fn.trigger.apply($(obj),args);}
listProc.fireWith(process,args);list.fireWith(obj,args);return this;};});process.onCallback=function(event){return function(){process.notify(event,arguments);};};promise.promiseWith(process);promise.promise(source||{});if(init){init.call(process,process);}
return process;};})(window.jQuery);