From 31157941ccabfa8f8c55edc00dc2b5758bf073b2 Mon Sep 17 00:00:00 2001 From: ocombe Date: Mon, 10 Nov 2014 00:31:09 +0100 Subject: [PATCH] fix: extend config to params for the register method Fixes #89 --- dist/ocLazyLoad.js | 34 ++++++++++++++++-------------- dist/ocLazyLoad.min.js | 2 +- examples/example1/js/ocLazyLoad.js | 34 ++++++++++++++++-------------- examples/example2/js/ocLazyLoad.js | 34 ++++++++++++++++-------------- src/ocLazyLoad.js | 34 ++++++++++++++++-------------- 5 files changed, 73 insertions(+), 65 deletions(-) diff --git a/dist/ocLazyLoad.js b/dist/ocLazyLoad.js index e9d6d2b..99bf7fb 100644 --- a/dist/ocLazyLoad.js +++ b/dist/ocLazyLoad.js @@ -254,14 +254,13 @@ templatesFiles = [], jsFiles = [], promises = [], - cachePromise = null, - localParams = {}; + cachePromise = null; - angular.extend(localParams, params || {}, config); + angular.extend(params || {}, config); var pushFile = function(path) { cachePromise = filesCache.get(path); - if(angular.isUndefined(cachePromise) || localParams.cache === false) { + if(angular.isUndefined(cachePromise) || params.cache === false) { if(/\.css[^\.]*$/.test(path) && cssFiles.indexOf(path) === -1) { cssFiles.push(path); } else if(/\.(htm|html)[^\.]*$/.test(path) && templatesFiles.indexOf(path) === -1) { @@ -274,10 +273,10 @@ } } - if(localParams.serie) { - pushFile(localParams.files.shift()); + if(params.serie) { + pushFile(params.files.shift()); } else { - angular.forEach(localParams.files, function(path) { + angular.forEach(params.files, function(path) { pushFile(path); }); } @@ -291,7 +290,7 @@ } else { cssDeferred.resolve(); } - }, localParams); + }, params); promises.push(cssDeferred.promise); } @@ -304,7 +303,7 @@ } else { templatesDeferred.resolve(); } - }, localParams); + }, params); promises.push(templatesDeferred.promise); } @@ -317,11 +316,11 @@ } else { jsDeferred.resolve(); } - }, localParams); + }, params); promises.push(jsDeferred.promise); } - if(localParams.serie && localParams.files.length > 0) { + if(params.serie && params.files.length > 0) { return $q.all(promises).then(function success() { return filesLoader(config, params); }); @@ -484,6 +483,9 @@ } } + var localParams = {}; + angular.extend(localParams, params, config); + var loadDependencies = function loadDependencies(module) { var moduleName, loadedModule, @@ -532,7 +534,7 @@ } // Push everything to the file loader, it will weed out the duplicates. - promisesList.push(filesLoader(requireEntry.files, params).then(function() { + promisesList.push(filesLoader(requireEntry.files, localParams).then(function() { return loadDependencies(requireEntry); })); } @@ -548,7 +550,7 @@ if(requireEntry.hasOwnProperty('css') && requireEntry['css'].length !== 0) { // Locate the document insertion point angular.forEach(requireEntry['css'], function(path) { - buildElement('css', path, params); + buildElement('css', path, localParams); }); } // CSS End. @@ -557,7 +559,7 @@ // Check if the dependency has any files that need to be loaded. If there are, push a new promise to the promise list. if(requireEntry.hasOwnProperty('files') && requireEntry.files.length !== 0) { if(requireEntry.files) { - promisesList.push(filesLoader(requireEntry, params).then(function() { + promisesList.push(filesLoader(requireEntry, localParams).then(function() { return loadDependencies(requireEntry); })); } @@ -568,7 +570,7 @@ return $q.all(promisesList); } - filesLoader(config, params).then(function success() { + filesLoader(config, localParams).then(function success() { if(moduleName === null) { deferred.resolve(module); } else { @@ -576,7 +578,7 @@ loadDependencies(moduleName).then(function success() { try { justLoaded = []; - register(providers, moduleCache, params); + register(providers, moduleCache, localParams); } catch(e) { $log.error(e.message); deferred.reject(e); diff --git a/dist/ocLazyLoad.min.js b/dist/ocLazyLoad.min.js index 21ed102..c97b7c6 100644 --- a/dist/ocLazyLoad.min.js +++ b/dist/ocLazyLoad.min.js @@ -5,4 +5,4 @@ * @license MIT * @author Olivier Combe */ -!function(){"use strict";function e(e){var n=[];return angular.forEach(e.requires,function(e){-1===l.indexOf(e)&&n.push(e)}),n}function n(e){try{return angular.module(e)}catch(n){if(/No module/.test(n)||n.message.indexOf("$injector:nomod")>-1)return!1}}function r(e){try{return angular.module(e)}catch(n){throw(/No module/.test(n)||n.message.indexOf("$injector:nomod")>-1)&&(n.message='The module "'+e+'" that you are trying to load does not exist. '+n.message),n}}function a(e,n,r,a){if(n){var o,i,u,l;for(o=0,i=n.length;i>o;o++)if(u=n[o],angular.isArray(u)){if(null!==e){if(!e.hasOwnProperty(u[0]))throw new Error("unsupported provider "+u[0]);l=e[u[0]]}var s=t(u,r);if("invoke"!==u[1])s&&angular.isDefined(l)&&l[u[1]].apply(l,u[2]);else{var c=function(e){var n=f.indexOf(r+"-"+e);(-1===n||a)&&(-1===n&&f.push(r+"-"+e),angular.isDefined(l)&&l[u[1]].apply(l,u[2]))};if(angular.isFunction(u[2][0]))c(u[2][0]);else if(angular.isArray(u[2][0]))for(var d=0,g=u[2][0].length;g>d;d++)angular.isFunction(u[2][0][d])&&c(u[2][0][d])}}}}function o(e,n,r){if(n){var t,u,s,f=[];for(t=n.length-1;t>=0;t--)if(u=n[t],"string"!=typeof u&&(u=i(u)),u&&-1===c.indexOf(u)){var g=-1===l.indexOf(u);if(s=angular.module(u),g&&(l.push(u),o(e,s.requires,r)),s._runBlocks.length>0)for(d[u]=[];s._runBlocks.length>0;)d[u].push(s._runBlocks.shift());angular.isDefined(d[u])&&(g||r.rerun)&&(f=f.concat(d[u])),a(e,s._invokeQueue,u,r.reconfig),a(e,s._configBlocks,u,r.reconfig),h(g?"ocLazyLoad.moduleLoaded":"ocLazyLoad.moduleReloaded",u),n.pop(),c.push(u)}var p=e.getInstanceInjector();angular.forEach(f,function(e){p.invoke(e)})}}function t(e,n){var r=e[2][0],a=e[1],o=!1;angular.isUndefined(s[n])&&(s[n]={}),angular.isUndefined(s[n][a])&&(s[n][a]=[]);var t=function(e){o=!0,s[n][a].push(e),h("ocLazyLoad.componentLoaded",[n,a,e])};if(angular.isString(r)&&-1===s[n][a].indexOf(r))t(r);else{if(!angular.isObject(r))return!1;angular.forEach(r,function(e){angular.isString(e)&&-1===s[n][a].indexOf(e)&&t(e)})}return o}function i(e){if(null===e)return null;var n=null;return"string"==typeof e?n=e:"object"==typeof e&&e.hasOwnProperty("name")&&"string"==typeof e.name&&(n=e.name),n}function u(e){function n(e){return e&&t.push(e)}var r,o,t=[e],i=["ng:app","ng-app","x-ng-app","data-ng-app"],u=/\sng[:\-]app(:\s*([\w\d_]+);?)?\s/;angular.forEach(i,function(r){i[r]=!0,n(document.getElementById(r)),r=r.replace(":","\\:"),e[0].querySelectorAll&&(angular.forEach(e[0].querySelectorAll("."+r),n),angular.forEach(e[0].querySelectorAll("."+r+"\\:"),n),angular.forEach(e[0].querySelectorAll("["+r+"]"),n))}),angular.forEach(t,function(n){if(!r){var a=" "+e.className+" ",t=u.exec(a);t?(r=n,o=(t[2]||"").replace(/\s+/g,",")):angular.forEach(n.attributes,function(e){!r&&i[e.name]&&(r=n,o=e.value)})}}),r&&!function s(e){if(-1===l.indexOf(e)){l.push(e);var n=angular.module(e);a(null,n._invokeQueue,e),a(null,n._configBlocks,e),angular.forEach(n.requires,s)}}(o)}var l=["ng"],s={},f=[],c=[],d={},g=angular.module("oc.lazyLoad",["ng"]),h=angular.noop;g.provider("$ocLazyLoad",["$controllerProvider","$provide","$compileProvider","$filterProvider","$injector","$animateProvider",function(a,t,s,f,d,g){var p,m,v,y={},L={$controllerProvider:a,$compileProvider:s,$filterProvider:f,$provide:t,$injector:d,$animateProvider:g},w=document.getElementsByTagName("head")[0]||document.getElementsByTagName("body")[0],E=!1,O=!1;u(angular.element(window.document)),this.$get=["$timeout","$log","$q","$templateCache","$http","$rootElement","$rootScope","$cacheFactory","$interval",function(a,t,u,s,f,d,g,$,j){var x,b=$("ocLazyLoad"),D=!1,P=!1;E||(t={},t.error=angular.noop,t.warn=angular.noop,t.info=angular.noop),L.getInstanceInjector=function(){return x?x:x=d.data("$injector")},h=function(e,n){O&&g.$broadcast(e,n),E&&t.info(e,n)};var z=function(e,n,r){var a,o,t=u.defer(),i=function(e){var n=(new Date).getTime();return e.indexOf("?")>=0?"&"===e.substring(0,e.length-1)?e+"_dc="+n:e+"&_dc="+n:e+"?_dc="+n};switch(angular.isUndefined(b.get(n))&&b.put(n,t.promise),e){case"css":a=document.createElement("link"),a.type="text/css",a.rel="stylesheet",a.href=r.cache===!1?i(n):n;break;case"js":a=document.createElement("script"),a.src=r.cache===!1?i(n):n;break;default:t.reject(new Error('Requested type "'+e+'" is not known. Could not inject "'+n+'"'))}if(a.onload=a.onreadystatechange=function(){a.readyState&&!/^c|loade/.test(a.readyState)||o||(a.onload=a.onreadystatechange=null,o=1,h("ocLazyLoad.fileLoaded",n),t.resolve())},a.onerror=function(){t.reject(new Error("Unable to load "+n))},a.async=1,w.insertBefore(a,w.lastChild),"css"==e){if(!D){var l=navigator.userAgent.toLowerCase();if(/iP(hone|od|ad)/.test(navigator.platform)){var s=navigator.appVersion.match(/OS (\d+)_(\d+)_?(\d+)?/),f=parseFloat([parseInt(s[1],10),parseInt(s[2],10),parseInt(s[3]||0,10)].join("."));P=6>f}else if(l.indexOf("android")>-1){var c=parseFloat(l.slice(l.indexOf("android")+8));P=4.4>c}else if(l.indexOf("safari")>-1&&-1==l.indexOf("chrome")){var d=parseFloat(l.match(/version\/([\.\d]+)/i)[1]);P=6>d}}if(P)var g=1e3,p=j(function(){try{a.sheet.cssRules,j.cancel(p),a.onload()}catch(e){--g<=0&&a.onerror()}},20)}return t.promise};angular.isUndefined(p)&&(p=function(e,n,r){var a=[];angular.forEach(e,function(e){a.push(z("js",e,r))}),u.all(a).then(function(){n()},function(e){n(e)})},p.ocLazyLoadLoader=!0),angular.isUndefined(m)&&(m=function(e,n,r){var a=[];angular.forEach(e,function(e){a.push(z("css",e,r))}),u.all(a).then(function(){n()},function(e){n(e)})},m.ocLazyLoadLoader=!0),angular.isUndefined(v)&&(v=function(e,n,r){angular.isString(e)&&(e=[e]);var a=[];return angular.forEach(e,function(e){var n=u.defer();a.push(n.promise),f.get(e,r).success(function(r){angular.forEach(angular.element(r),function(e){"SCRIPT"===e.nodeName&&"text/ng-template"===e.type&&s.put(e.id,e.innerHTML)}),angular.isUndefined(b.get(e))&&b.put(e,!0),n.resolve()}).error(function(r){var a='Error load template "'+e+'": '+r;t.error(a),n.reject(new Error(a))})}),u.all(a).then(function(){n()},function(e){n(e)})},v.ocLazyLoadLoader=!0);var M=function(e,n){var r=[],a=[],o=[],i=[],l=null,s={};angular.extend(s,n||{},e);var f=function(e){l=b.get(e),angular.isUndefined(l)||s.cache===!1?/\.css[^\.]*$/.test(e)&&-1===r.indexOf(e)?r.push(e):/\.(htm|html)[^\.]*$/.test(e)&&-1===a.indexOf(e)?a.push(e):-1===o.indexOf(e)&&o.push(e):l&&i.push(l)};if(s.serie?f(s.files.shift()):angular.forEach(s.files,function(e){f(e)}),r.length>0){var c=u.defer();m(r,function(e){angular.isDefined(e)&&m.hasOwnProperty("ocLazyLoadLoader")?(t.error(e),c.reject(e)):c.resolve()},s),i.push(c.promise)}if(a.length>0){var d=u.defer();v(a,function(e){angular.isDefined(e)&&v.hasOwnProperty("ocLazyLoadLoader")?(t.error(e),d.reject(e)):d.resolve()},s),i.push(d.promise)}if(o.length>0){var g=u.defer();p(o,function(e){angular.isDefined(e)&&p.hasOwnProperty("ocLazyLoadLoader")?(t.error(e),g.reject(e)):g.resolve()},s),i.push(g.promise)}return s.serie&&s.files.length>0?u.all(i).then(function(){return M(e,n)}):u.all(i)};return{getModuleConfig:function(e){if(!angular.isString(e))throw new Error("You need to give the name of the module to get");return y[e]?y[e]:null},setModuleConfig:function(e){if(!angular.isObject(e))throw new Error("You need to give the module config object to set");return y[e.name]=e,e},getModules:function(){return l},isLoaded:function(e){var r=function(e){var r=l.indexOf(e)>-1;return r||(r=!!n(e)),r};if(angular.isString(e)&&(e=[e]),angular.isArray(e)){var a,o;for(a=0,o=e.length;o>a;a++)if(!r(e[a]))return!1;return!0}throw new Error("You need to define the module(s) name(s)")},load:function(s,f){var d,g,h=this,p=null,m=[],v=[],y=u.defer();if(angular.isUndefined(f)&&(f={}),angular.isArray(s))return angular.forEach(s,function(e){e&&v.push(h.load(e,f))}),u.all(v).then(function(){y.resolve(s)},function(e){y.reject(e)}),y.promise;if(d=i(s),"string"==typeof s?(p=h.getModuleConfig(s),p||(p={files:[s]},d=null)):"object"==typeof s&&(p=h.setModuleConfig(s)),null===p?(g='Module "'+d+'" is not configured, cannot load.',t.error(g),y.reject(new Error(g))):angular.isDefined(p.template)&&(angular.isUndefined(p.files)&&(p.files=[]),angular.isString(p.template)?p.files.push(p.template):angular.isArray(p.template)&&p.files.concat(p.template)),m.push=function(e){-1===this.indexOf(e)&&Array.prototype.push.apply(this,arguments)},angular.isDefined(d)&&n(d)&&-1!==l.indexOf(d)&&(m.push(d),angular.isUndefined(p.files)))return y.resolve(),y.promise;var w=function E(a){var o,l,s,c,d=[];if(o=i(a),null===o)return u.when();try{l=r(o)}catch(g){var p=u.defer();return t.error(g.message),p.reject(g),p.promise}return s=e(l),angular.forEach(s,function(e){if("string"==typeof e){var r=h.getModuleConfig(e);if(null===r)return void m.push(e);e=r}return n(e.name)?void("string"!=typeof a&&(c=e.files.filter(function(n){return h.getModuleConfig(e.name).files.indexOf(n)<0}),0!==c.length&&t.warn('Module "',o,'" attempted to redefine configuration for dependency. "',e.name,'"\n Additional Files Loaded:',c),d.push(M(e.files,f).then(function(){return E(e)})))):("object"==typeof e&&(e.hasOwnProperty("name")&&e.name&&(h.setModuleConfig(e),m.push(e.name)),e.hasOwnProperty("css")&&0!==e.css.length&&angular.forEach(e.css,function(e){z("css",e,f)})),void(e.hasOwnProperty("files")&&0!==e.files.length&&e.files&&d.push(M(e,f).then(function(){return E(e)}))))}),u.all(d)};return M(p,f).then(function(){null===d?y.resolve(s):(m.push(d),w(d).then(function(){try{c=[],o(L,m,f)}catch(e){return t.error(e.message),void y.reject(e)}a(function(){y.resolve(s)})},function(e){a(function(){y.reject(e)})}))},function(e){y.reject(e)}),y.promise}}}],this.config=function(e){if((angular.isDefined(e.jsLoader)||angular.isDefined(e.asyncLoader))&&(p=e.jsLoader||e.asyncLoader,!angular.isFunction(p)))throw"The js loader needs to be a function";if(angular.isDefined(e.cssLoader)&&(m=e.cssLoader,!angular.isFunction(m)))throw"The css loader needs to be a function";if(angular.isDefined(e.templatesLoader)&&(v=e.templatesLoader,!angular.isFunction(v)))throw"The template loader needs to be a function";if(angular.isDefined(e.loadedModules)){var n=function(e){l.indexOf(e)<0&&(l.push(e),angular.forEach(angular.module(e).requires,n))};angular.forEach(e.loadedModules,n)}angular.isDefined(e.modules)&&(angular.isArray(e.modules)?angular.forEach(e.modules,function(e){y[e.name]=e}):y[e.modules.name]=e.modules),angular.isDefined(e.debug)&&(E=e.debug),angular.isDefined(e.events)&&(O=e.events)}}]),g.directive("ocLazyLoad",["$ocLazyLoad","$compile","$animate","$parse",function(e,n,r,a){return{restrict:"A",terminal:!0,priority:1e3,compile:function(o){var t=o[0].innerHTML;return o.html(""),function(o,i,u){var l=a(u.ocLazyLoad);o.$watch(function(){return l(o)||u.ocLazyLoad},function(a){angular.isDefined(a)&&e.load(a).then(function(){r.enter(n(t)(o),null,i)})},!0)}}}}]),Array.prototype.indexOf||(Array.prototype.indexOf=function(e,n){var r;if(null==this)throw new TypeError('"this" is null or not defined');var a=Object(this),o=a.length>>>0;if(0===o)return-1;var t=+n||0;if(1/0===Math.abs(t)&&(t=0),t>=o)return-1;for(r=Math.max(t>=0?t:o-Math.abs(t),0);o>r;){if(r in a&&a[r]===e)return r;r++}return-1})}(); \ No newline at end of file +!function(){"use strict";function e(e){var n=[];return angular.forEach(e.requires,function(e){-1===l.indexOf(e)&&n.push(e)}),n}function n(e){try{return angular.module(e)}catch(n){if(/No module/.test(n)||n.message.indexOf("$injector:nomod")>-1)return!1}}function r(e){try{return angular.module(e)}catch(n){throw(/No module/.test(n)||n.message.indexOf("$injector:nomod")>-1)&&(n.message='The module "'+e+'" that you are trying to load does not exist. '+n.message),n}}function a(e,n,r,a){if(n){var o,i,u,l;for(o=0,i=n.length;i>o;o++)if(u=n[o],angular.isArray(u)){if(null!==e){if(!e.hasOwnProperty(u[0]))throw new Error("unsupported provider "+u[0]);l=e[u[0]]}var s=t(u,r);if("invoke"!==u[1])s&&angular.isDefined(l)&&l[u[1]].apply(l,u[2]);else{var c=function(e){var n=f.indexOf(r+"-"+e);(-1===n||a)&&(-1===n&&f.push(r+"-"+e),angular.isDefined(l)&&l[u[1]].apply(l,u[2]))};if(angular.isFunction(u[2][0]))c(u[2][0]);else if(angular.isArray(u[2][0]))for(var d=0,g=u[2][0].length;g>d;d++)angular.isFunction(u[2][0][d])&&c(u[2][0][d])}}}}function o(e,n,r){if(n){var t,u,s,f=[];for(t=n.length-1;t>=0;t--)if(u=n[t],"string"!=typeof u&&(u=i(u)),u&&-1===c.indexOf(u)){var g=-1===l.indexOf(u);if(s=angular.module(u),g&&(l.push(u),o(e,s.requires,r)),s._runBlocks.length>0)for(d[u]=[];s._runBlocks.length>0;)d[u].push(s._runBlocks.shift());angular.isDefined(d[u])&&(g||r.rerun)&&(f=f.concat(d[u])),a(e,s._invokeQueue,u,r.reconfig),a(e,s._configBlocks,u,r.reconfig),h(g?"ocLazyLoad.moduleLoaded":"ocLazyLoad.moduleReloaded",u),n.pop(),c.push(u)}var p=e.getInstanceInjector();angular.forEach(f,function(e){p.invoke(e)})}}function t(e,n){var r=e[2][0],a=e[1],o=!1;angular.isUndefined(s[n])&&(s[n]={}),angular.isUndefined(s[n][a])&&(s[n][a]=[]);var t=function(e){o=!0,s[n][a].push(e),h("ocLazyLoad.componentLoaded",[n,a,e])};if(angular.isString(r)&&-1===s[n][a].indexOf(r))t(r);else{if(!angular.isObject(r))return!1;angular.forEach(r,function(e){angular.isString(e)&&-1===s[n][a].indexOf(e)&&t(e)})}return o}function i(e){if(null===e)return null;var n=null;return"string"==typeof e?n=e:"object"==typeof e&&e.hasOwnProperty("name")&&"string"==typeof e.name&&(n=e.name),n}function u(e){function n(e){return e&&t.push(e)}var r,o,t=[e],i=["ng:app","ng-app","x-ng-app","data-ng-app"],u=/\sng[:\-]app(:\s*([\w\d_]+);?)?\s/;angular.forEach(i,function(r){i[r]=!0,n(document.getElementById(r)),r=r.replace(":","\\:"),e[0].querySelectorAll&&(angular.forEach(e[0].querySelectorAll("."+r),n),angular.forEach(e[0].querySelectorAll("."+r+"\\:"),n),angular.forEach(e[0].querySelectorAll("["+r+"]"),n))}),angular.forEach(t,function(n){if(!r){var a=" "+e.className+" ",t=u.exec(a);t?(r=n,o=(t[2]||"").replace(/\s+/g,",")):angular.forEach(n.attributes,function(e){!r&&i[e.name]&&(r=n,o=e.value)})}}),r&&!function s(e){if(-1===l.indexOf(e)){l.push(e);var n=angular.module(e);a(null,n._invokeQueue,e),a(null,n._configBlocks,e),angular.forEach(n.requires,s)}}(o)}var l=["ng"],s={},f=[],c=[],d={},g=angular.module("oc.lazyLoad",["ng"]),h=angular.noop;g.provider("$ocLazyLoad",["$controllerProvider","$provide","$compileProvider","$filterProvider","$injector","$animateProvider",function(a,t,s,f,d,g){var p,m,v,y={},L={$controllerProvider:a,$compileProvider:s,$filterProvider:f,$provide:t,$injector:d,$animateProvider:g},w=document.getElementsByTagName("head")[0]||document.getElementsByTagName("body")[0],E=!1,O=!1;u(angular.element(window.document)),this.$get=["$timeout","$log","$q","$templateCache","$http","$rootElement","$rootScope","$cacheFactory","$interval",function(a,t,u,s,f,d,g,$,j){var x,b=$("ocLazyLoad"),D=!1,P=!1;E||(t={},t.error=angular.noop,t.warn=angular.noop,t.info=angular.noop),L.getInstanceInjector=function(){return x?x:x=d.data("$injector")},h=function(e,n){O&&g.$broadcast(e,n),E&&t.info(e,n)};var z=function(e,n,r){var a,o,t=u.defer(),i=function(e){var n=(new Date).getTime();return e.indexOf("?")>=0?"&"===e.substring(0,e.length-1)?e+"_dc="+n:e+"&_dc="+n:e+"?_dc="+n};switch(angular.isUndefined(b.get(n))&&b.put(n,t.promise),e){case"css":a=document.createElement("link"),a.type="text/css",a.rel="stylesheet",a.href=r.cache===!1?i(n):n;break;case"js":a=document.createElement("script"),a.src=r.cache===!1?i(n):n;break;default:t.reject(new Error('Requested type "'+e+'" is not known. Could not inject "'+n+'"'))}if(a.onload=a.onreadystatechange=function(){a.readyState&&!/^c|loade/.test(a.readyState)||o||(a.onload=a.onreadystatechange=null,o=1,h("ocLazyLoad.fileLoaded",n),t.resolve())},a.onerror=function(){t.reject(new Error("Unable to load "+n))},a.async=1,w.insertBefore(a,w.lastChild),"css"==e){if(!D){var l=navigator.userAgent.toLowerCase();if(/iP(hone|od|ad)/.test(navigator.platform)){var s=navigator.appVersion.match(/OS (\d+)_(\d+)_?(\d+)?/),f=parseFloat([parseInt(s[1],10),parseInt(s[2],10),parseInt(s[3]||0,10)].join("."));P=6>f}else if(l.indexOf("android")>-1){var c=parseFloat(l.slice(l.indexOf("android")+8));P=4.4>c}else if(l.indexOf("safari")>-1&&-1==l.indexOf("chrome")){var d=parseFloat(l.match(/version\/([\.\d]+)/i)[1]);P=6>d}}if(P)var g=1e3,p=j(function(){try{a.sheet.cssRules,j.cancel(p),a.onload()}catch(e){--g<=0&&a.onerror()}},20)}return t.promise};angular.isUndefined(p)&&(p=function(e,n,r){var a=[];angular.forEach(e,function(e){a.push(z("js",e,r))}),u.all(a).then(function(){n()},function(e){n(e)})},p.ocLazyLoadLoader=!0),angular.isUndefined(m)&&(m=function(e,n,r){var a=[];angular.forEach(e,function(e){a.push(z("css",e,r))}),u.all(a).then(function(){n()},function(e){n(e)})},m.ocLazyLoadLoader=!0),angular.isUndefined(v)&&(v=function(e,n,r){angular.isString(e)&&(e=[e]);var a=[];return angular.forEach(e,function(e){var n=u.defer();a.push(n.promise),f.get(e,r).success(function(r){angular.forEach(angular.element(r),function(e){"SCRIPT"===e.nodeName&&"text/ng-template"===e.type&&s.put(e.id,e.innerHTML)}),angular.isUndefined(b.get(e))&&b.put(e,!0),n.resolve()}).error(function(r){var a='Error load template "'+e+'": '+r;t.error(a),n.reject(new Error(a))})}),u.all(a).then(function(){n()},function(e){n(e)})},v.ocLazyLoadLoader=!0);var M=function(e,n){var r=[],a=[],o=[],i=[],l=null;angular.extend(n||{},e);var s=function(e){l=b.get(e),angular.isUndefined(l)||n.cache===!1?/\.css[^\.]*$/.test(e)&&-1===r.indexOf(e)?r.push(e):/\.(htm|html)[^\.]*$/.test(e)&&-1===a.indexOf(e)?a.push(e):-1===o.indexOf(e)&&o.push(e):l&&i.push(l)};if(n.serie?s(n.files.shift()):angular.forEach(n.files,function(e){s(e)}),r.length>0){var f=u.defer();m(r,function(e){angular.isDefined(e)&&m.hasOwnProperty("ocLazyLoadLoader")?(t.error(e),f.reject(e)):f.resolve()},n),i.push(f.promise)}if(a.length>0){var c=u.defer();v(a,function(e){angular.isDefined(e)&&v.hasOwnProperty("ocLazyLoadLoader")?(t.error(e),c.reject(e)):c.resolve()},n),i.push(c.promise)}if(o.length>0){var d=u.defer();p(o,function(e){angular.isDefined(e)&&p.hasOwnProperty("ocLazyLoadLoader")?(t.error(e),d.reject(e)):d.resolve()},n),i.push(d.promise)}return n.serie&&n.files.length>0?u.all(i).then(function(){return M(e,n)}):u.all(i)};return{getModuleConfig:function(e){if(!angular.isString(e))throw new Error("You need to give the name of the module to get");return y[e]?y[e]:null},setModuleConfig:function(e){if(!angular.isObject(e))throw new Error("You need to give the module config object to set");return y[e.name]=e,e},getModules:function(){return l},isLoaded:function(e){var r=function(e){var r=l.indexOf(e)>-1;return r||(r=!!n(e)),r};if(angular.isString(e)&&(e=[e]),angular.isArray(e)){var a,o;for(a=0,o=e.length;o>a;a++)if(!r(e[a]))return!1;return!0}throw new Error("You need to define the module(s) name(s)")},load:function(s,f){var d,g,h=this,p=null,m=[],v=[],y=u.defer();if(angular.isUndefined(f)&&(f={}),angular.isArray(s))return angular.forEach(s,function(e){e&&v.push(h.load(e,f))}),u.all(v).then(function(){y.resolve(s)},function(e){y.reject(e)}),y.promise;if(d=i(s),"string"==typeof s?(p=h.getModuleConfig(s),p||(p={files:[s]},d=null)):"object"==typeof s&&(p=h.setModuleConfig(s)),null===p?(g='Module "'+d+'" is not configured, cannot load.',t.error(g),y.reject(new Error(g))):angular.isDefined(p.template)&&(angular.isUndefined(p.files)&&(p.files=[]),angular.isString(p.template)?p.files.push(p.template):angular.isArray(p.template)&&p.files.concat(p.template)),m.push=function(e){-1===this.indexOf(e)&&Array.prototype.push.apply(this,arguments)},angular.isDefined(d)&&n(d)&&-1!==l.indexOf(d)&&(m.push(d),angular.isUndefined(p.files)))return y.resolve(),y.promise;var w={};angular.extend(w,f,p);var E=function O(a){var o,l,s,f,c=[];if(o=i(a),null===o)return u.when();try{l=r(o)}catch(d){var g=u.defer();return t.error(d.message),g.reject(d),g.promise}return s=e(l),angular.forEach(s,function(e){if("string"==typeof e){var r=h.getModuleConfig(e);if(null===r)return void m.push(e);e=r}return n(e.name)?void("string"!=typeof a&&(f=e.files.filter(function(n){return h.getModuleConfig(e.name).files.indexOf(n)<0}),0!==f.length&&t.warn('Module "',o,'" attempted to redefine configuration for dependency. "',e.name,'"\n Additional Files Loaded:',f),c.push(M(e.files,w).then(function(){return O(e)})))):("object"==typeof e&&(e.hasOwnProperty("name")&&e.name&&(h.setModuleConfig(e),m.push(e.name)),e.hasOwnProperty("css")&&0!==e.css.length&&angular.forEach(e.css,function(e){z("css",e,w)})),void(e.hasOwnProperty("files")&&0!==e.files.length&&e.files&&c.push(M(e,w).then(function(){return O(e)}))))}),u.all(c)};return M(p,w).then(function(){null===d?y.resolve(s):(m.push(d),E(d).then(function(){try{c=[],o(L,m,w)}catch(e){return t.error(e.message),void y.reject(e)}a(function(){y.resolve(s)})},function(e){a(function(){y.reject(e)})}))},function(e){y.reject(e)}),y.promise}}}],this.config=function(e){if((angular.isDefined(e.jsLoader)||angular.isDefined(e.asyncLoader))&&(p=e.jsLoader||e.asyncLoader,!angular.isFunction(p)))throw"The js loader needs to be a function";if(angular.isDefined(e.cssLoader)&&(m=e.cssLoader,!angular.isFunction(m)))throw"The css loader needs to be a function";if(angular.isDefined(e.templatesLoader)&&(v=e.templatesLoader,!angular.isFunction(v)))throw"The template loader needs to be a function";if(angular.isDefined(e.loadedModules)){var n=function(e){l.indexOf(e)<0&&(l.push(e),angular.forEach(angular.module(e).requires,n))};angular.forEach(e.loadedModules,n)}angular.isDefined(e.modules)&&(angular.isArray(e.modules)?angular.forEach(e.modules,function(e){y[e.name]=e}):y[e.modules.name]=e.modules),angular.isDefined(e.debug)&&(E=e.debug),angular.isDefined(e.events)&&(O=e.events)}}]),g.directive("ocLazyLoad",["$ocLazyLoad","$compile","$animate","$parse",function(e,n,r,a){return{restrict:"A",terminal:!0,priority:1e3,compile:function(o){var t=o[0].innerHTML;return o.html(""),function(o,i,u){var l=a(u.ocLazyLoad);o.$watch(function(){return l(o)||u.ocLazyLoad},function(a){angular.isDefined(a)&&e.load(a).then(function(){r.enter(n(t)(o),null,i)})},!0)}}}}]),Array.prototype.indexOf||(Array.prototype.indexOf=function(e,n){var r;if(null==this)throw new TypeError('"this" is null or not defined');var a=Object(this),o=a.length>>>0;if(0===o)return-1;var t=+n||0;if(1/0===Math.abs(t)&&(t=0),t>=o)return-1;for(r=Math.max(t>=0?t:o-Math.abs(t),0);o>r;){if(r in a&&a[r]===e)return r;r++}return-1})}(); \ No newline at end of file diff --git a/examples/example1/js/ocLazyLoad.js b/examples/example1/js/ocLazyLoad.js index e9d6d2b..99bf7fb 100644 --- a/examples/example1/js/ocLazyLoad.js +++ b/examples/example1/js/ocLazyLoad.js @@ -254,14 +254,13 @@ templatesFiles = [], jsFiles = [], promises = [], - cachePromise = null, - localParams = {}; + cachePromise = null; - angular.extend(localParams, params || {}, config); + angular.extend(params || {}, config); var pushFile = function(path) { cachePromise = filesCache.get(path); - if(angular.isUndefined(cachePromise) || localParams.cache === false) { + if(angular.isUndefined(cachePromise) || params.cache === false) { if(/\.css[^\.]*$/.test(path) && cssFiles.indexOf(path) === -1) { cssFiles.push(path); } else if(/\.(htm|html)[^\.]*$/.test(path) && templatesFiles.indexOf(path) === -1) { @@ -274,10 +273,10 @@ } } - if(localParams.serie) { - pushFile(localParams.files.shift()); + if(params.serie) { + pushFile(params.files.shift()); } else { - angular.forEach(localParams.files, function(path) { + angular.forEach(params.files, function(path) { pushFile(path); }); } @@ -291,7 +290,7 @@ } else { cssDeferred.resolve(); } - }, localParams); + }, params); promises.push(cssDeferred.promise); } @@ -304,7 +303,7 @@ } else { templatesDeferred.resolve(); } - }, localParams); + }, params); promises.push(templatesDeferred.promise); } @@ -317,11 +316,11 @@ } else { jsDeferred.resolve(); } - }, localParams); + }, params); promises.push(jsDeferred.promise); } - if(localParams.serie && localParams.files.length > 0) { + if(params.serie && params.files.length > 0) { return $q.all(promises).then(function success() { return filesLoader(config, params); }); @@ -484,6 +483,9 @@ } } + var localParams = {}; + angular.extend(localParams, params, config); + var loadDependencies = function loadDependencies(module) { var moduleName, loadedModule, @@ -532,7 +534,7 @@ } // Push everything to the file loader, it will weed out the duplicates. - promisesList.push(filesLoader(requireEntry.files, params).then(function() { + promisesList.push(filesLoader(requireEntry.files, localParams).then(function() { return loadDependencies(requireEntry); })); } @@ -548,7 +550,7 @@ if(requireEntry.hasOwnProperty('css') && requireEntry['css'].length !== 0) { // Locate the document insertion point angular.forEach(requireEntry['css'], function(path) { - buildElement('css', path, params); + buildElement('css', path, localParams); }); } // CSS End. @@ -557,7 +559,7 @@ // Check if the dependency has any files that need to be loaded. If there are, push a new promise to the promise list. if(requireEntry.hasOwnProperty('files') && requireEntry.files.length !== 0) { if(requireEntry.files) { - promisesList.push(filesLoader(requireEntry, params).then(function() { + promisesList.push(filesLoader(requireEntry, localParams).then(function() { return loadDependencies(requireEntry); })); } @@ -568,7 +570,7 @@ return $q.all(promisesList); } - filesLoader(config, params).then(function success() { + filesLoader(config, localParams).then(function success() { if(moduleName === null) { deferred.resolve(module); } else { @@ -576,7 +578,7 @@ loadDependencies(moduleName).then(function success() { try { justLoaded = []; - register(providers, moduleCache, params); + register(providers, moduleCache, localParams); } catch(e) { $log.error(e.message); deferred.reject(e); diff --git a/examples/example2/js/ocLazyLoad.js b/examples/example2/js/ocLazyLoad.js index e9d6d2b..99bf7fb 100644 --- a/examples/example2/js/ocLazyLoad.js +++ b/examples/example2/js/ocLazyLoad.js @@ -254,14 +254,13 @@ templatesFiles = [], jsFiles = [], promises = [], - cachePromise = null, - localParams = {}; + cachePromise = null; - angular.extend(localParams, params || {}, config); + angular.extend(params || {}, config); var pushFile = function(path) { cachePromise = filesCache.get(path); - if(angular.isUndefined(cachePromise) || localParams.cache === false) { + if(angular.isUndefined(cachePromise) || params.cache === false) { if(/\.css[^\.]*$/.test(path) && cssFiles.indexOf(path) === -1) { cssFiles.push(path); } else if(/\.(htm|html)[^\.]*$/.test(path) && templatesFiles.indexOf(path) === -1) { @@ -274,10 +273,10 @@ } } - if(localParams.serie) { - pushFile(localParams.files.shift()); + if(params.serie) { + pushFile(params.files.shift()); } else { - angular.forEach(localParams.files, function(path) { + angular.forEach(params.files, function(path) { pushFile(path); }); } @@ -291,7 +290,7 @@ } else { cssDeferred.resolve(); } - }, localParams); + }, params); promises.push(cssDeferred.promise); } @@ -304,7 +303,7 @@ } else { templatesDeferred.resolve(); } - }, localParams); + }, params); promises.push(templatesDeferred.promise); } @@ -317,11 +316,11 @@ } else { jsDeferred.resolve(); } - }, localParams); + }, params); promises.push(jsDeferred.promise); } - if(localParams.serie && localParams.files.length > 0) { + if(params.serie && params.files.length > 0) { return $q.all(promises).then(function success() { return filesLoader(config, params); }); @@ -484,6 +483,9 @@ } } + var localParams = {}; + angular.extend(localParams, params, config); + var loadDependencies = function loadDependencies(module) { var moduleName, loadedModule, @@ -532,7 +534,7 @@ } // Push everything to the file loader, it will weed out the duplicates. - promisesList.push(filesLoader(requireEntry.files, params).then(function() { + promisesList.push(filesLoader(requireEntry.files, localParams).then(function() { return loadDependencies(requireEntry); })); } @@ -548,7 +550,7 @@ if(requireEntry.hasOwnProperty('css') && requireEntry['css'].length !== 0) { // Locate the document insertion point angular.forEach(requireEntry['css'], function(path) { - buildElement('css', path, params); + buildElement('css', path, localParams); }); } // CSS End. @@ -557,7 +559,7 @@ // Check if the dependency has any files that need to be loaded. If there are, push a new promise to the promise list. if(requireEntry.hasOwnProperty('files') && requireEntry.files.length !== 0) { if(requireEntry.files) { - promisesList.push(filesLoader(requireEntry, params).then(function() { + promisesList.push(filesLoader(requireEntry, localParams).then(function() { return loadDependencies(requireEntry); })); } @@ -568,7 +570,7 @@ return $q.all(promisesList); } - filesLoader(config, params).then(function success() { + filesLoader(config, localParams).then(function success() { if(moduleName === null) { deferred.resolve(module); } else { @@ -576,7 +578,7 @@ loadDependencies(moduleName).then(function success() { try { justLoaded = []; - register(providers, moduleCache, params); + register(providers, moduleCache, localParams); } catch(e) { $log.error(e.message); deferred.reject(e); diff --git a/src/ocLazyLoad.js b/src/ocLazyLoad.js index ed49451..697279a 100644 --- a/src/ocLazyLoad.js +++ b/src/ocLazyLoad.js @@ -247,14 +247,13 @@ templatesFiles = [], jsFiles = [], promises = [], - cachePromise = null, - localParams = {}; + cachePromise = null; - angular.extend(localParams, params || {}, config); + angular.extend(params || {}, config); var pushFile = function(path) { cachePromise = filesCache.get(path); - if(angular.isUndefined(cachePromise) || localParams.cache === false) { + if(angular.isUndefined(cachePromise) || params.cache === false) { if(/\.css[^\.]*$/.test(path) && cssFiles.indexOf(path) === -1) { cssFiles.push(path); } else if(/\.(htm|html)[^\.]*$/.test(path) && templatesFiles.indexOf(path) === -1) { @@ -267,10 +266,10 @@ } } - if(localParams.serie) { - pushFile(localParams.files.shift()); + if(params.serie) { + pushFile(params.files.shift()); } else { - angular.forEach(localParams.files, function(path) { + angular.forEach(params.files, function(path) { pushFile(path); }); } @@ -284,7 +283,7 @@ } else { cssDeferred.resolve(); } - }, localParams); + }, params); promises.push(cssDeferred.promise); } @@ -297,7 +296,7 @@ } else { templatesDeferred.resolve(); } - }, localParams); + }, params); promises.push(templatesDeferred.promise); } @@ -310,11 +309,11 @@ } else { jsDeferred.resolve(); } - }, localParams); + }, params); promises.push(jsDeferred.promise); } - if(localParams.serie && localParams.files.length > 0) { + if(params.serie && params.files.length > 0) { return $q.all(promises).then(function success() { return filesLoader(config, params); }); @@ -477,6 +476,9 @@ } } + var localParams = {}; + angular.extend(localParams, params, config); + var loadDependencies = function loadDependencies(module) { var moduleName, loadedModule, @@ -525,7 +527,7 @@ } // Push everything to the file loader, it will weed out the duplicates. - promisesList.push(filesLoader(requireEntry.files, params).then(function() { + promisesList.push(filesLoader(requireEntry.files, localParams).then(function() { return loadDependencies(requireEntry); })); } @@ -541,7 +543,7 @@ if(requireEntry.hasOwnProperty('css') && requireEntry['css'].length !== 0) { // Locate the document insertion point angular.forEach(requireEntry['css'], function(path) { - buildElement('css', path, params); + buildElement('css', path, localParams); }); } // CSS End. @@ -550,7 +552,7 @@ // Check if the dependency has any files that need to be loaded. If there are, push a new promise to the promise list. if(requireEntry.hasOwnProperty('files') && requireEntry.files.length !== 0) { if(requireEntry.files) { - promisesList.push(filesLoader(requireEntry, params).then(function() { + promisesList.push(filesLoader(requireEntry, localParams).then(function() { return loadDependencies(requireEntry); })); } @@ -561,7 +563,7 @@ return $q.all(promisesList); } - filesLoader(config, params).then(function success() { + filesLoader(config, localParams).then(function success() { if(moduleName === null) { deferred.resolve(module); } else { @@ -569,7 +571,7 @@ loadDependencies(moduleName).then(function success() { try { justLoaded = []; - register(providers, moduleCache, params); + register(providers, moduleCache, localParams); } catch(e) { $log.error(e.message); deferred.reject(e);