-
Notifications
You must be signed in to change notification settings - Fork 16
/
jquery.defer.min.js
3 lines (3 loc) · 3.61 KB
/
jquery.defer.min.js
1
2
3
/*! JsDefer v1.0.0-alpha: http://github.com/BorisMoore/jsdefer and http://jsviews.com/jsviews */
jQuery.defer||function(n,t){function u(n,t){return t.indexOf("//")===-1&&(t=n+t),y.href=t}function a(n){return n.slice(0,n.lastIndexOf("/")+1)}function o(n){return typeof n=="string"?[n]:n}function v(n,t){if(!n)return 0;n=o(n);for(var r,f=n.length;f--;)r=n[f],i[r]||(n[f]=u(t,r));return n}function f(n,t){var f,e=i[n];return e?e:(t=u(a(t||""),n),f=t.toLowerCase(),r[f]||(r[f]={url:t}))}var c=t.document,y=c.createElement("a"),s,i,h,r={},l=[],e;n.extend({defer:function(scriptName,options,basePath){function run(){var t,i=runCb||scriptDef.runCb;if(!runWait--){if(asyncLoad.state()!=="rejected"){if(parentPromise)asyncLoad.resolve(result||(scriptDef.result=result=i.call(promise,n,options)));else if(contains){for(e=[],i.call(promise,n,options),t=contains.length;t--;)f(contains[t],url).runCb=e[t].run;e=0,hasRun.resolve(),loadDependencies(contains,[],function(){for(t=contains.length,result=[];t--;)result.push(f(contains[t],url).result);asyncLoad.resolve(result)})}else asyncLoad.resolve(result||(scriptDef.result=result=bare?"bare":i.call(promise,n,options)));result=multiple?0:result}delayDomReady&&h(!0)}}function reject(){asyncLoad.reject("fail",url)}function loadDependencies(t,r,u){r=r||[];for(var f=t&&t.length;f--;)r.push(i(t[f],options,url));n.when.apply(n,r).fail(reject).done(u||run)}function getScript(){return n.ajax({url:loadUrl,dataType:"script",timeout:settings.timeout,cache:!settings.noCache,crossDomain:!0}).fail(reject).done(function(){var t=bare?0:l.shift();if(!t){bare||reject(),run();return}runCb=scriptDef.runCb=t.run,t.def&&n.deferDef(t.def,url),depends=o(t.depends)||[],prepareSubDefs(!contains&&t.contains),depends.length&&(runWait++,loadDependencies(depends))})}function prepareSubDefs(t){if(t)for(hasRun=n.Deferred(),hasRunPromise=[hasRun.promise()],contains=o(t),i=contains.length;i--;)f(contains[i],url).prntPrms=hasRunPromise}options=options||{};var i,readyName,asyncLoad,result,prevPromise,runWait=0,delayDomReady=options.delayDomReady||s.delayDomReady,min=options.min||s.min,scriptDef=f(scriptName,basePath),url=scriptDef.url,loadUrl=min&&scriptDef.minUrl||scriptDef.url,settings=n.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=n.Deferred(),loaded&&eval(loaded))return asyncLoad.resolve().promise();delayDomReady&&n.readyWait++,prevPromise=promise,asyncLoad=n.Deferred(),promise=scriptDef.promise=asyncLoad.promise(),bare?loadDependencies(depends,0,getScript):prevPromise?loadDependencies(0,[prevPromise]):parentPromise?loadDependencies(depends,parentPromise):(prepareSubDefs(contains),loadDependencies(depends,[getScript().promise()]))}return(readyName=options.readyName||scriptDef.name)&&(h[readyName]=promise),promise},deferDef:function(t,e){function y(t,f){typeof f=="string"&&(f={url:f});var h=f.minUrl,e=u(o,f.url),s=r[e]||function(){var r=Array.prototype.slice.call(arguments,0);return r.unshift(t),i.apply(n,r)};f.url=e,h&&(f.minUrl=u(o,h)),n.extend(s,f),r[e]=i[t]=s,s.name=t}var s,o,h,l=c.getElementsByTagName("script");if(l=l[l.length-1],o=a(e||l.src),typeof t=="string")return i[t]||r[u(o,t)];for(s in t)y(s,t[s]);for(s in t)h=f(s),h.depends=v(h.depends,o),h.contains=v(h.contains,o)},deferSettings:{delayDomReady:!1,min:!0}}),t.$deferRun=function(n,t){t=o(t),t=t&&t.length?{depends:t}:t||{},t.run=n,(e||l).push(t)},s=n.deferSettings,i=n.defer,h=n.ready}(jQuery,window);
//@ sourceMappingURL=jquery.defer.min.js.map