-
Notifications
You must be signed in to change notification settings - Fork 16
/
jsdefer.min.js
3 lines (3 loc) · 5.83 KB
/
jsdefer.min.js
1
2
3
/*! JsDefer v1.0.0-alpha: http://github.com/BorisMoore/jsdefer and http://jsviews.com/jsviews */
window.jsDefer||window.jQuery&&jQuery.defer||function(n,t){function o(n,t){return t.indexOf("://")===-1&&(t=n+t),g.href=t}function b(n){return n.slice(0,n.lastIndexOf("/")+1)}function a(n){return typeof n=="string"?[n]:n}function k(n,t){if(!n)return 0;n=a(n);for(var i,r=n.length;r--;)i=n[r],u[i]||(n[r]=o(t,i));return n}function s(n,t){var i,r=u[n];return r?r:(t=o(b(t||""),n),i=t.toLowerCase(),e[i]||(e[i]={url:t}))}var i,r=n.document,g=r.createElement("a"),h,u,v,c,f,e={},w=[],l,y="then done fail state promise".split(" "),p=[].slice;if(n.jQuery)i=jQuery,i(function(){f.resolveWith(r,[i])});else{n.jsDefer=n.$=i=function(n){return f.done(n)},i.extend=function(n,r){r===t&&(r=n,n=i);for(var u in r)n[u]=r[u];return n},i.extend({readyWait:1,ready:function(n){n===!0&&i.readyWait--,i.readyWait&&(n===!0||i.isReady)||f.resolveWith(r,[i])},isFunction:function(n){return typeof n=="function"},_Deferred:function(){var r=[],n,u,f,t={done:function(){if(!f){var s=arguments,e,h,u,o;for(n&&(o=n,n=0),e=0,h=s.length;e<h;e++)u=s[e],u instanceof Array?t.done.apply(t,u):i.isFunction(u)&&r.push(u);o&&t.resolveWith(o[0],o[1])}return this},resolveWith:function(t,i){if(!f&&!n&&!u){i=i||[],u=1;try{while(r[0])r.shift().apply(t,i)}finally{n=[t,i],u=0}}return this},resolve:function(){return t.resolveWith(this,arguments),this},isResolved:function(){return!!(u||n)},cancel:function(){return f=1,r=[],this}};return t},Deferred:function(n){var t=i._Deferred(),r=i._Deferred(),u;return i.extend(t,{then:function(n,i){return t.done(n).fail(i),this},fail:r.done,rejectWith:r.resolveWith,reject:r.resolve,isRejected:r.isResolved,promise:function(n,i){if(n==null){if(u)return u;u=n={}}for(i=y.length;i--;)n[y[i]]=t[y[i]];return n}}),t.done(r.cancel).fail(t.cancel),delete t.cancel,n&&n.call(t,t),t},when:function(n){function o(n){return function(i){r[n]=arguments.length>1?p.call(arguments,0):i,--e||t.resolveWith(t,p.call(r,0))}}var r=arguments,u=0,f=r.length,e=f,t=f<=1&&n&&i.isFunction(n.promise)?n:i.Deferred();if(f>1){for(;u<f;u++)r[u]&&i.isFunction(r[u].promise)?r[u].promise().then(o(u),t.reject):--e;e||t.resolveWith(t,r)}else t!==n&&t.resolveWith(t,f?[n]:[]);return t.promise()}});function getAjax(n){var e=i.Deferred(),f=r.getElementsByTagName("head")[0]||r.documentElement,u=r.createElement("script");return u.src=n.url,u.onload=u.onreadystatechange=function(){(!u.readyState||/loaded|complete/.test(u.readyState))&&(u.onload=u.onreadystatechange=null,f&&u.parentNode&&f.removeChild(u),u=t,e.resolve())},f.insertBefore(u,f.firstChild),e}}if(i.extend({defer:function(scriptName,options,basePath){function run(){var n,t=runCb||scriptDef.runCb;if(!runWait--){if(!asyncLoad.isRejected()){if(parentPromise)asyncLoad.resolve(result||(scriptDef.result=result=t.call(promise,i,options)));else if(contains){for(l=[],t.call(promise,i,options),n=contains.length;n--;)s(contains[n],url).runCb=l[n].run;l=0,hasRun.resolve(),loadDependencies(contains,[],function(){for(n=contains.length,result=[];n--;)result.push(s(contains[n],url).result);asyncLoad.resolve(result)})}else asyncLoad.resolve(result||(scriptDef.result=result=bare?"bare":t.call(promise,i,options)));result=multiple?0:result}delayDomReady&&c(!0)}}function reject(){asyncLoad.reject("fail",url)}function loadDependencies(n,t,r){t=t||[];for(var f=n&&n.length;f--;)t.push(u(n[f],options,url));i.when.apply(i,t).fail(reject).done(r||run)}function getScript(){return(i.ajax||getAjax)({url:loadUrl,dataType:"script",timeout:options.timeout,cache:!options.noCache,crossDomain:!0}).fail(reject).done(function(){var t=bare?0:w.shift();if(!t){bare||reject(),n.jQuery&&i!==jQuery&&(i=jQuery.extend({defer:u,deferSettings:h,deferDef:v})),run();return}runCb=scriptDef.runCb=t.run,t.def&&v(t.def,url),depends=a(t.depends)||[],prepareSubDefs(!contains&&t.contains),depends.length&&(runWait++,loadDependencies(depends))})}function prepareSubDefs(n){if(n)for(hasRun=i.Deferred(),hasRunPromise=[hasRun.promise()],contains=a(n),i=contains.length;i--;)s(contains[i],url).prntPrms=hasRunPromise}options=options||{};var i,readyName,asyncLoad,result,prevPromise,runWait=0,delayDomReady=options.delayDomReady||h.delayDomReady,min=options.min||h.min,scriptDef=s(scriptName,basePath),url=scriptDef.url,loadUrl=min&&scriptDef.minUrl||scriptDef.url,settings=i.extend(scriptDef,options),bare=settings.bare,contains=settings.contains,loaded=settings.loaded,depends=settings.depends,multiple=settings.multiple,parentPromise=scriptDef.prntPrms,promise=scriptDef.promise,runCb,thisPromise,hasRun,hasRunPromise;if(multiple||!promise){if(asyncLoad=i.Deferred(),loaded&&eval(loaded))return asyncLoad.resolve().promise();delayDomReady&&i.readyWait++,prevPromise=promise,asyncLoad=i.Deferred(),promise=scriptDef.promise=asyncLoad.promise(),bare?loadDependencies(depends,0,getScript):prevPromise?loadDependencies(depends,[prevPromise]):parentPromise?loadDependencies(depends,parentPromise):(prepareSubDefs(contains),loadDependencies(depends,[getScript().promise()]))}return(readyName=options.readyName||scriptDef.name)&&(c[readyName]=promise),promise},deferDef:function(n,t){function a(n,t){typeof t=="string"&&(t={url:t});var h=t.minUrl,r=o(f,t.url),s=e[r]||function(){var t=p.call(arguments,0);return t.unshift(n),u.apply(i,t)};t.url=r,h&&(t.minUrl=o(f,h)),i.extend(s,t),e[r]=u[n]=s,s.name=n}var h,f,c,l=r.getElementsByTagName("script");if(l=l[l.length-1],f=b(t||l.src),typeof n=="string")return u[n]||e[o(f,n)];for(h in n)a(h,n[h]);for(h in n)c=s(h),c.depends=k(c.depends,f),c.contains=k(c.contains,f)},deferSettings:{delayDomReady:!1,min:!0}}),n.$deferRun=function(n,t){t=a(t),t=t&&t.length?{depends:t}:t||{},t.run=n,(l||w).push(t)},h=i.deferSettings,u=i.defer,v=i.deferDef,c=i.ready,f=i.Deferred(),f.promise(c),!n.jQuery){function d(){if(!r.body)return setTimeout(function(){d()},1);i.isReady=!0,i.ready(!0)}d()}}(window);
//@ sourceMappingURL=jsdefer.min.js.map