forked from BorisMoore/jsdefer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsdefer.min.js
1 lines (1 loc) · 4.72 KB
/
jsdefer.min.js
1
window.jsDefer||window.jQuery&&jQuery.defer||function(d,s){var a,b=d.document,v=b.createElement("a"),j,c,n,l,g,f={},p=[],i,m="then done fail isResolved isRejected promise".split(" "),o=[].slice;if(d.jQuery){a=jQuery;a(function(){g.resolveWith(b,[a])})}else{d.jsDefer=d.$=a=function(a){return g.done(a)};a.extend=function(c,b){if(b===s){b=c;c=a}for(var d in b)c[d]=b[d];return c};a.extend({readyWait:1,ready:function(c){if(c===true)a.readyWait--;(!a.readyWait||c!==true&&!a.isReady)&&g.resolveWith(b,[a])},isFunction:function(a){return typeof a==="function"},_Deferred:function(){var d=[],b,e,f,c={done:function(){if(!f){var j=arguments,h,i,e,k,g;if(b){g=b;b=0}for(h=0,i=j.length;h<i;h++){e=j[h];if(e instanceof Array)c.done.apply(c,e);else a.isFunction(e)&&d.push(e)}g&&c.resolveWith(g[0],g[1])}return this},resolveWith:function(c,a){if(!f&&!b&&!e){a=a||[];e=1;try{while(d[0])d.shift().apply(c,a)}finally{b=[c,a];e=0}}return this},resolve:function(){c.resolveWith(this,arguments);return this},isResolved:function(){return!!(e||b)},cancel:function(){f=1;d=[];return this}};return c},Deferred:function(e){var b=a._Deferred(),c=a._Deferred(),d;a.extend(b,{then:function(a,c){b.done(a).fail(c);return this},fail:c.done,rejectWith:c.resolveWith,reject:c.resolve,isRejected:c.isResolved,promise:function(a,c){if(a==null){if(d)return d;d=a={}}c=m.length;while(c--)a[m[c]]=b[m[c]];return a}});b.done(c.cancel).fail(b.cancel);delete b.cancel;e&&e.call(b,b);return b},when:function(e){var c=arguments,d=0,f=c.length,g=f,b=f<=1&&e&&a.isFunction(e.promise)?e:a.Deferred();function h(a){return function(d){c[a]=arguments.length>1?o.call(arguments,0):d;!--g&&b.resolveWith(b,o.call(c,0))}}if(f>1){for(;d<f;d++)if(c[d]&&a.isFunction(c[d].promise))c[d].promise().then(h(d),b.reject);else--g;!g&&b.resolveWith(b,c)}else b!==e&&b.resolveWith(b,f?[e]:[]);return b.promise()}});function u(f){var e=a.Deferred(),d=b.getElementsByTagName("head")[0]||b.documentElement,c=b.createElement("script");c.src=f.url;c.onload=c.onreadystatechange=function(){if(!c.readyState||/loaded|complete/.test(c.readyState)){c.onload=c.onreadystatechange=null;d&&c.parentNode&&d.removeChild(c);c=s;e.resolve()}};d.insertBefore(c,d.firstChild);return e}}function h(b,a){if(a.indexOf("://")===-1)a=b+a;return v.href=a}function q(a){return a.slice(0,a.lastIndexOf("/")+1)}function k(a){return typeof a==="string"?[a]:a}function r(a,e){if(!a)return 0;a=k(a);var b,d=a.length;while(d--){b=a[d];if(!c[b])a[d]=h(e,b)}return a}function e(e,a){var b,d=c[e];if(d)return d;a=h(q(a||""),e);b=a.toLowerCase();return f[b]||(f[b]={url:a})}a.extend({defer:function(L,f,M){f=f||{};var A,F,h,m,x,H=0,C=f.delayDomReady||j.delayDomReady,O=f.min||j.min,b=e(L,M),s=b.url,N=O&&b.minUrl||b.url,t=a.extend(b,f),v=t.bare,g=t.contains,I=t.loaded,q=t.depends,G=t.multiple,w=b.prntPrms,o=b.promise,J,P,y,D;function K(){var c,d=J||b.runCb;if(!H--){if(!h.isRejected()){if(w)h.resolve(m||(b.result=m=d.call(o,a,f)));else if(g){i=[];d.call(o,a,f);c=g.length;while(c--)e(g[c],s).runCb=i[c].run;i=0;y.resolve();r(g,[],function(){c=g.length;m=[];while(c--)m.push(e(g[c],s).result);h.resolve(m)})}else h.resolve(m||(b.result=m=v?"bare":d.call(o,a,f)));m=G?0:m}C&&l(true)}}function z(){h.reject("fail",s)}function r(d,b,g){b=b||[];var e=d&&d.length;while(e--)b.push(c(d[e],f,s));a.when.apply(a,b).fail(z).done(g||K)}function E(){return(a.ajax||u)({url:N,dataType:"script",timeout:f.timeout,cache:!f.noCache,crossDomain:true}).fail(z).done(function(){var e=v?0:p.shift();if(!e){!v&&z();if(d.jQuery&&a!==jQuery)a=jQuery.extend({defer:c,deferSettings:j,deferDef:n});K();return}J=b.runCb=e.run;e.def&&n(e.def,s);q=k(e.depends)||[];B(!g&&e.contains);if(q.length){H++;r(q)}})}function B(b){if(b){y=a.Deferred();D=[y.promise()];g=k(b);A=g.length;while(A--)e(g[A],s).prntPrms=D}}if(G||!o){h=a.Deferred();if(I&&eval(I))return h.resolve().promise();if(C)a.readyWait++;x=o;h=a.Deferred();o=b.promise=h.promise();if(v)r(q,0,E);else if(x)r(q,[x]);else if(w)r(q,w);else{B(g);r(q,[E().promise()])}}if(F=f.readyName||b.name)l[F]=o;return o},deferDef:function(d,m){var i,g,j,k=b.getElementsByTagName("script");function l(e,b){if(typeof b==="string")b={url:b};var j=b.minUrl,i=h(g,b.url),d=f[i]||function(){var b=o.call(arguments,0);b.unshift(e);return c.apply(a,b)};b.url=i;if(j)b.minUrl=h(g,j);a.extend(d,b);f[i]=c[e]=d;d.name=e}k=k[k.length-1];g=q(m||k.src);if(typeof d==="string")return c[d]||f[h(g,d)];for(i in d)l(i,d[i]);for(i in d){j=e(i);j.depends=r(j.depends,g);j.contains=r(j.contains,g)}},deferSettings:{delayDomReady:false,min:true}});d.$deferRun=function(b,a){a=k(a);a=a&&a.length?{depends:a}:a||{};a.run=b;(i||p).push(a)};j=a.deferSettings;c=a.defer;n=a.deferDef;l=a.ready;g=a.Deferred();g.promise(l);if(!d.jQuery){function t(){if(!b.body)return setTimeout(function(){t()},1);a.isReady=true;a.ready(true)}t()}}(window);