ocLazyLoad.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. /**
  2. * oclazyload - Load modules on demand (lazy load) with angularJS
  3. * @version v0.6.3
  4. * @link https://github.com/ocombe/ocLazyLoad
  5. * @license MIT
  6. * @author Olivier Combe <olivier.combe@gmail.com>
  7. */
  8. (function() {
  9. 'use strict';
  10. var regModules = ['ng'],
  11. regInvokes = {},
  12. regConfigs = [],
  13. justLoaded = [],
  14. runBlocks = {},
  15. ocLazyLoad = angular.module('oc.lazyLoad', ['ng']),
  16. broadcast = angular.noop,
  17. modulesToLoad = [],
  18. recordDeclarations = [true];
  19. ocLazyLoad.provider('$ocLazyLoad', ['$controllerProvider', '$provide', '$compileProvider', '$filterProvider', '$injector', '$animateProvider',
  20. function($controllerProvider, $provide, $compileProvider, $filterProvider, $injector, $animateProvider) {
  21. var modules = {},
  22. providers = {
  23. $controllerProvider: $controllerProvider,
  24. $compileProvider: $compileProvider,
  25. $filterProvider: $filterProvider,
  26. $provide: $provide, // other things
  27. $injector: $injector,
  28. $animateProvider: $animateProvider
  29. },
  30. anchor = document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0],
  31. jsLoader, cssLoader, templatesLoader,
  32. debug = false,
  33. events = false;
  34. // Let's get the list of loaded modules & components
  35. init(angular.element(window.document));
  36. this.$get = ['$log', '$q', '$templateCache', '$http', '$rootElement', '$rootScope', '$cacheFactory', '$interval', function($log, $q, $templateCache, $http, $rootElement, $rootScope, $cacheFactory, $interval) {
  37. var instanceInjector,
  38. filesCache = $cacheFactory('ocLazyLoad'),
  39. uaCssChecked = false,
  40. useCssLoadPatch = false;
  41. if(!debug) {
  42. $log = {};
  43. $log['error'] = angular.noop;
  44. $log['warn'] = angular.noop;
  45. $log['info'] = angular.noop;
  46. }
  47. // Make this lazy because at the moment that $get() is called the instance injector hasn't been assigned to the rootElement yet
  48. providers.getInstanceInjector = function() {
  49. return (instanceInjector) ? instanceInjector : (instanceInjector = ($rootElement.data('$injector') || angular.injector()));
  50. };
  51. broadcast = function broadcast(eventName, params) {
  52. if(events) {
  53. $rootScope.$broadcast(eventName, params);
  54. }
  55. if(debug) {
  56. $log.info(eventName, params);
  57. }
  58. };
  59. /**
  60. * Load a js/css file
  61. * @param type
  62. * @param path
  63. * @param params
  64. * @returns promise
  65. */
  66. var buildElement = function buildElement(type, path, params) {
  67. var deferred = $q.defer(),
  68. el, loaded,
  69. cacheBuster = function cacheBuster(url) {
  70. var dc = new Date().getTime();
  71. if(url.indexOf('?') >= 0) {
  72. if(url.substring(0, url.length - 1) === '&') {
  73. return url + '_dc=' + dc;
  74. }
  75. return url + '&_dc=' + dc;
  76. } else {
  77. return url + '?_dc=' + dc;
  78. }
  79. };
  80. // Store the promise early so the file load can be detected by other parallel lazy loads
  81. // (ie: multiple routes on one page) a 'true' value isn't sufficient
  82. // as it causes false positive load results.
  83. if(angular.isUndefined(filesCache.get(path))) {
  84. filesCache.put(path, deferred.promise);
  85. }
  86. // Switch in case more content types are added later
  87. switch(type) {
  88. case 'css':
  89. el = document.createElement('link');
  90. el.type = 'text/css';
  91. el.rel = 'stylesheet';
  92. el.href = params.cache === false ? cacheBuster(path) : path;
  93. break;
  94. case 'js':
  95. el = document.createElement('script');
  96. el.src = params.cache === false ? cacheBuster(path) : path;
  97. break;
  98. default:
  99. deferred.reject(new Error('Requested type "' + type + '" is not known. Could not inject "' + path + '"'));
  100. break;
  101. }
  102. el.onload = el['onreadystatechange'] = function(e) {
  103. if((el['readyState'] && !(/^c|loade/.test(el['readyState']))) || loaded) return;
  104. el.onload = el['onreadystatechange'] = null;
  105. loaded = 1;
  106. broadcast('ocLazyLoad.fileLoaded', path);
  107. deferred.resolve();
  108. };
  109. el.onerror = function() {
  110. deferred.reject(new Error('Unable to load ' + path));
  111. };
  112. el.async = params.serie ? 0 : 1;
  113. var insertBeforeElem = anchor.lastChild;
  114. if(params.insertBefore) {
  115. var element = angular.element(params.insertBefore);
  116. if(element && element.length > 0) {
  117. insertBeforeElem = element[0];
  118. }
  119. }
  120. anchor.insertBefore(el, insertBeforeElem);
  121. /*
  122. The event load or readystatechange doesn't fire in:
  123. - iOS < 6 (default mobile browser)
  124. - Android < 4.4 (default mobile browser)
  125. - Safari < 6 (desktop browser)
  126. */
  127. if(type == 'css') {
  128. if(!uaCssChecked) {
  129. var ua = navigator.userAgent.toLowerCase();
  130. // iOS < 6
  131. if(/iP(hone|od|ad)/.test(navigator.platform)) {
  132. var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
  133. var iOSVersion = parseFloat([parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)].join('.'));
  134. useCssLoadPatch = iOSVersion < 6;
  135. } else if(ua.indexOf("android") > -1) { // Android < 4.4
  136. var androidVersion = parseFloat(ua.slice(ua.indexOf("android") + 8));
  137. useCssLoadPatch = androidVersion < 4.4;
  138. } else if(ua.indexOf('safari') > -1 && ua.indexOf('chrome') == -1) {
  139. var safariVersion = parseFloat(ua.match(/version\/([\.\d]+)/i)[1]);
  140. useCssLoadPatch = safariVersion < 6;
  141. }
  142. }
  143. if(useCssLoadPatch) {
  144. var tries = 1000; // * 20 = 20000 miliseconds
  145. var interval = $interval(function() {
  146. try {
  147. el.sheet.cssRules;
  148. $interval.cancel(interval);
  149. el.onload();
  150. } catch(e) {
  151. if(--tries <= 0) {
  152. el.onerror();
  153. }
  154. }
  155. }, 20);
  156. }
  157. }
  158. return deferred.promise;
  159. };
  160. if(angular.isUndefined(jsLoader)) {
  161. /**
  162. * jsLoader function
  163. * @type Function
  164. * @param paths array list of js files to load
  165. * @param callback to call when everything is loaded. We use a callback and not a promise
  166. * @param params object config parameters
  167. * because the user can overwrite jsLoader and it will probably not use promises :(
  168. */
  169. jsLoader = function(paths, callback, params) {
  170. var promises = [];
  171. angular.forEach(paths, function loading(path) {
  172. promises.push(buildElement('js', path, params));
  173. });
  174. $q.all(promises).then(function success() {
  175. callback();
  176. }, function error(err) {
  177. callback(err);
  178. });
  179. };
  180. jsLoader.ocLazyLoadLoader = true;
  181. }
  182. if(angular.isUndefined(cssLoader)) {
  183. /**
  184. * cssLoader function
  185. * @type Function
  186. * @param paths array list of css files to load
  187. * @param callback to call when everything is loaded. We use a callback and not a promise
  188. * @param params object config parameters
  189. * because the user can overwrite cssLoader and it will probably not use promises :(
  190. */
  191. cssLoader = function(paths, callback, params) {
  192. var promises = [];
  193. angular.forEach(paths, function loading(path) {
  194. promises.push(buildElement('css', path, params));
  195. });
  196. $q.all(promises).then(function success() {
  197. callback();
  198. }, function error(err) {
  199. callback(err);
  200. });
  201. };
  202. cssLoader.ocLazyLoadLoader = true;
  203. }
  204. if(angular.isUndefined(templatesLoader)) {
  205. /**
  206. * templatesLoader function
  207. * @type Function
  208. * @param paths array list of css files to load
  209. * @param callback to call when everything is loaded. We use a callback and not a promise
  210. * @param params object config parameters for $http
  211. * because the user can overwrite templatesLoader and it will probably not use promises :(
  212. */
  213. templatesLoader = function(paths, callback, params) {
  214. var promises = [];
  215. angular.forEach(paths, function(url) {
  216. var deferred = $q.defer();
  217. promises.push(deferred.promise);
  218. $http.get(url, params).success(function(data) {
  219. if(angular.isString(data) && data.length > 0) {
  220. angular.forEach(angular.element(data), function(node) {
  221. if(node.nodeName === 'SCRIPT' && node.type === 'text/ng-template') {
  222. $templateCache.put(node.id, node.innerHTML);
  223. }
  224. });
  225. }
  226. if(angular.isUndefined(filesCache.get(url))) {
  227. filesCache.put(url, true);
  228. }
  229. deferred.resolve();
  230. }).error(function(err) {
  231. deferred.reject(new Error('Unable to load template file "' + url + '": ' + err));
  232. });
  233. });
  234. return $q.all(promises).then(function success() {
  235. callback();
  236. }, function error(err) {
  237. callback(err);
  238. });
  239. };
  240. templatesLoader.ocLazyLoadLoader = true;
  241. }
  242. var filesLoader = function filesLoader(config, params) {
  243. var cssFiles = [],
  244. templatesFiles = [],
  245. jsFiles = [],
  246. promises = [],
  247. cachePromise = null;
  248. recordDeclarations.push(true); // start watching angular.module calls
  249. angular.extend(params || {}, config);
  250. var pushFile = function(path) {
  251. var file_type = null, m;
  252. if (typeof path === 'object') {
  253. file_type = path.type;
  254. path = path.path;
  255. }
  256. cachePromise = filesCache.get(path);
  257. if(angular.isUndefined(cachePromise) || params.cache === false) {
  258. // always check for requirejs syntax just in case
  259. if ((m = /^(css|less|html|htm|js)?(?=!)/.exec(path)) !== null) { // Detect file type using preceding type declaration (ala requireJS)
  260. file_type = m[1];
  261. path = path.substr(m[1].length + 1, path.length); // Strip the type from the path
  262. }
  263. if (!file_type) {
  264. if ((m = /[.](css|less|html|htm|js)?(\?.*)?$/.exec(path)) !== null) { // Detect file type via file extension
  265. file_type = m[1];
  266. } else if(!jsLoader.hasOwnProperty('ocLazyLoadLoader') && jsLoader.hasOwnProperty('load')) { // requirejs
  267. file_type = 'js';
  268. } else {
  269. $log.error('File type could not be determined. ' + path);
  270. return;
  271. }
  272. }
  273. if((file_type === 'css' || file_type === 'less') && cssFiles.indexOf(path) === -1) {
  274. cssFiles.push(path);
  275. } else if ((file_type === 'html' || file_type === 'htm') && templatesFiles.indexOf(path) === -1) {
  276. templatesFiles.push(path);
  277. } else if((file_type === 'js') || jsFiles.indexOf(path) === -1) {
  278. jsFiles.push(path);
  279. } else {
  280. $log.error('File type is not valid. ' + path);
  281. }
  282. } else if(cachePromise) {
  283. promises.push(cachePromise);
  284. }
  285. };
  286. if(params.serie) {
  287. pushFile(params.files.shift());
  288. } else {
  289. angular.forEach(params.files, function(path) {
  290. pushFile(path);
  291. });
  292. }
  293. if(cssFiles.length > 0) {
  294. var cssDeferred = $q.defer();
  295. cssLoader(cssFiles, function(err) {
  296. if(angular.isDefined(err) && cssLoader.hasOwnProperty('ocLazyLoadLoader')) {
  297. $log.error(err);
  298. cssDeferred.reject(err);
  299. } else {
  300. cssDeferred.resolve();
  301. }
  302. }, params);
  303. promises.push(cssDeferred.promise);
  304. }
  305. if(templatesFiles.length > 0) {
  306. var templatesDeferred = $q.defer();
  307. templatesLoader(templatesFiles, function(err) {
  308. if(angular.isDefined(err) && templatesLoader.hasOwnProperty('ocLazyLoadLoader')) {
  309. $log.error(err);
  310. templatesDeferred.reject(err);
  311. } else {
  312. templatesDeferred.resolve();
  313. }
  314. }, params);
  315. promises.push(templatesDeferred.promise);
  316. }
  317. if(jsFiles.length > 0) {
  318. var jsDeferred = $q.defer();
  319. jsLoader(jsFiles, function(err) {
  320. if(angular.isDefined(err) && jsLoader.hasOwnProperty('ocLazyLoadLoader')) {
  321. $log.error(err);
  322. jsDeferred.reject(err);
  323. } else {
  324. jsDeferred.resolve();
  325. }
  326. }, params);
  327. promises.push(jsDeferred.promise);
  328. }
  329. if(params.serie && params.files.length > 0) {
  330. return $q.all(promises).then(function success() {
  331. return filesLoader(config, params);
  332. });
  333. } else {
  334. return $q.all(promises).finally(function(res) {
  335. recordDeclarations.pop(); // stop watching angular.module calls
  336. return res;
  337. });
  338. }
  339. };
  340. return {
  341. /**
  342. * Let you get a module config object
  343. * @param moduleName String the name of the module
  344. * @returns {*}
  345. */
  346. getModuleConfig: function(moduleName) {
  347. if(!angular.isString(moduleName)) {
  348. throw new Error('You need to give the name of the module to get');
  349. }
  350. if(!modules[moduleName]) {
  351. return null;
  352. }
  353. return modules[moduleName];
  354. },
  355. /**
  356. * Let you define a module config object
  357. * @param moduleConfig Object the module config object
  358. * @returns {*}
  359. */
  360. setModuleConfig: function(moduleConfig) {
  361. if(!angular.isObject(moduleConfig)) {
  362. throw new Error('You need to give the module config object to set');
  363. }
  364. modules[moduleConfig.name] = moduleConfig;
  365. return moduleConfig;
  366. },
  367. /**
  368. * Returns the list of loaded modules
  369. * @returns {string[]}
  370. */
  371. getModules: function() {
  372. return regModules;
  373. },
  374. /**
  375. * Let you check if a module has been loaded into Angular or not
  376. * @param modulesNames String/Object a module name, or a list of module names
  377. * @returns {boolean}
  378. */
  379. isLoaded: function(modulesNames) {
  380. var moduleLoaded = function(module) {
  381. var isLoaded = regModules.indexOf(module) > -1;
  382. if(!isLoaded) {
  383. isLoaded = !!moduleExists(module);
  384. }
  385. return isLoaded;
  386. };
  387. if(angular.isString(modulesNames)) {
  388. modulesNames = [modulesNames];
  389. }
  390. if(angular.isArray(modulesNames)) {
  391. var i, len;
  392. for(i = 0, len = modulesNames.length; i < len; i++) {
  393. if(!moduleLoaded(modulesNames[i])) {
  394. return false;
  395. }
  396. }
  397. return true;
  398. } else {
  399. throw new Error('You need to define the module(s) name(s)');
  400. }
  401. },
  402. /**
  403. * Load a module or a list of modules into Angular
  404. * @param module Mixed the name of a predefined module config object, or a module config object, or an array of either
  405. * @param params Object optional parameters
  406. * @returns promise
  407. */
  408. load: function(module, params) {
  409. var self = this,
  410. config = null,
  411. moduleCache = [],
  412. deferredList = [],
  413. deferred = $q.defer(),
  414. errText;
  415. if(angular.isUndefined(params)) {
  416. params = {};
  417. }
  418. // If module is an array, break it down
  419. if(angular.isArray(module)) {
  420. // Resubmit each entry as a single module
  421. angular.forEach(module, function(m) {
  422. if(m) {
  423. deferredList.push(self.load(m, params));
  424. }
  425. });
  426. // Resolve the promise once everything has loaded
  427. $q.all(deferredList).then(function success() {
  428. deferred.resolve(module);
  429. }, function error(err) {
  430. deferred.reject(err);
  431. });
  432. return deferred.promise;
  433. }
  434. // Get or Set a configuration depending on what was passed in
  435. if(typeof module === 'string') {
  436. config = self.getModuleConfig(module);
  437. if(!config) {
  438. config = {
  439. files: [module]
  440. };
  441. }
  442. } else if(typeof module === 'object') {
  443. if(angular.isDefined(module.path) && angular.isDefined(module.type)) { // case {type: 'js', path: lazyLoadUrl + 'testModule.fakejs'}
  444. config = {
  445. files: [module]
  446. };
  447. } else {
  448. config = self.setModuleConfig(module);
  449. }
  450. }
  451. if(config === null) {
  452. var moduleName = getModuleName(module);
  453. errText = 'Module "' + (moduleName || 'unknown') + '" is not configured, cannot load.';
  454. $log.error(errText);
  455. deferred.reject(new Error(errText));
  456. return deferred.promise;
  457. } else {
  458. // deprecated
  459. if(angular.isDefined(config.template)) {
  460. if(angular.isUndefined(config.files)) {
  461. config.files = [];
  462. }
  463. if(angular.isString(config.template)) {
  464. config.files.push(config.template);
  465. } else if(angular.isArray(config.template)) {
  466. config.files.concat(config.template);
  467. }
  468. }
  469. }
  470. moduleCache.push = function(value) {
  471. if(this.indexOf(value) === -1) {
  472. Array.prototype.push.apply(this, arguments);
  473. }
  474. };
  475. var localParams = {};
  476. angular.extend(localParams, params, config);
  477. var loadDependencies = function loadDependencies(module) {
  478. var moduleName,
  479. loadedModule,
  480. requires,
  481. diff,
  482. promisesList = [];
  483. moduleName = getModuleName(module);
  484. if(moduleName === null) {
  485. return $q.when();
  486. } else {
  487. try {
  488. loadedModule = getModule(moduleName);
  489. } catch(e) {
  490. var deferred = $q.defer();
  491. $log.error(e.message);
  492. deferred.reject(e);
  493. return deferred.promise;
  494. }
  495. requires = getRequires(loadedModule);
  496. }
  497. angular.forEach(requires, function(requireEntry) {
  498. // If no configuration is provided, try and find one from a previous load.
  499. // If there isn't one, bail and let the normal flow run
  500. if(typeof requireEntry === 'string') {
  501. var config = self.getModuleConfig(requireEntry);
  502. if(config === null) {
  503. moduleCache.push(requireEntry); // We don't know about this module, but something else might, so push it anyway.
  504. return;
  505. }
  506. requireEntry = config;
  507. }
  508. // Check if this dependency has been loaded previously
  509. if(moduleExists(requireEntry.name)) {
  510. if(typeof module !== 'string') {
  511. // compare against the already loaded module to see if the new definition adds any new files
  512. diff = requireEntry.files.filter(function(n) {
  513. return self.getModuleConfig(requireEntry.name).files.indexOf(n) < 0;
  514. });
  515. // If the module was redefined, advise via the console
  516. if(diff.length !== 0) {
  517. $log.warn('Module "', moduleName, '" attempted to redefine configuration for dependency. "', requireEntry.name, '"\n Additional Files Loaded:', diff);
  518. }
  519. // Push everything to the file loader, it will weed out the duplicates.
  520. promisesList.push(filesLoader(requireEntry.files, localParams).then(function() {
  521. return loadDependencies(requireEntry);
  522. }));
  523. }
  524. return;
  525. } else if(angular.isArray(requireEntry)) {
  526. requireEntry = {
  527. files: requireEntry
  528. };
  529. } else if(typeof requireEntry === 'object') {
  530. if(requireEntry.hasOwnProperty('name') && requireEntry['name']) {
  531. // The dependency doesn't exist in the module cache and is a new configuration, so store and push it.
  532. self.setModuleConfig(requireEntry);
  533. moduleCache.push(requireEntry['name']);
  534. }
  535. }
  536. // Check if the dependency has any files that need to be loaded. If there are, push a new promise to the promise list.
  537. if(requireEntry.hasOwnProperty('files') && requireEntry.files.length !== 0) {
  538. if(requireEntry.files) {
  539. promisesList.push(filesLoader(requireEntry, localParams).then(function() {
  540. return loadDependencies(requireEntry);
  541. }));
  542. }
  543. }
  544. });
  545. // Create a wrapper promise to watch the promise list and resolve it once everything is done.
  546. return $q.all(promisesList);
  547. };
  548. // if someone loaded the module file with something else and called the load function with just the module name
  549. if(angular.isUndefined(config.files) && angular.isDefined(config.name) && moduleExists(config.name)) {
  550. recordDeclarations.push(true); // start watching angular.module calls
  551. addToLoadList(config.name);
  552. recordDeclarations.pop();
  553. }
  554. filesLoader(config, localParams).then(function success() {
  555. if(modulesToLoad.length === 0) {
  556. deferred.resolve(module);
  557. } else {
  558. var loadNext = function loadNext(moduleName) {
  559. moduleCache.push(moduleName);
  560. loadDependencies(moduleName).then(function success() {
  561. try {
  562. justLoaded = [];
  563. register(providers, moduleCache, localParams);
  564. } catch(e) {
  565. $log.error(e.message);
  566. deferred.reject(e);
  567. return;
  568. }
  569. if(modulesToLoad.length > 0) {
  570. loadNext(modulesToLoad.shift()); // load the next in list
  571. } else {
  572. deferred.resolve(module); // everything has been loaded, resolve
  573. }
  574. }, function error(err) {
  575. deferred.reject(err);
  576. });
  577. };
  578. // load the first in list
  579. loadNext(modulesToLoad.shift());
  580. }
  581. }, function error(err) {
  582. deferred.reject(err);
  583. });
  584. return deferred.promise;
  585. }
  586. };
  587. }];
  588. this.config = function(config) {
  589. if(angular.isDefined(config.jsLoader) || angular.isDefined(config.asyncLoader)) {
  590. if(!angular.isFunction(config.jsLoader || config.asyncLoader)) {
  591. throw('The js loader needs to be a function');
  592. }
  593. jsLoader = config.jsLoader || config.asyncLoader;
  594. }
  595. if(angular.isDefined(config.cssLoader)) {
  596. if(!angular.isFunction(config.cssLoader)) {
  597. throw('The css loader needs to be a function');
  598. }
  599. cssLoader = config.cssLoader;
  600. }
  601. if(angular.isDefined(config.templatesLoader)) {
  602. if(!angular.isFunction(config.templatesLoader)) {
  603. throw('The template loader needs to be a function');
  604. }
  605. templatesLoader = config.templatesLoader;
  606. }
  607. // If we want to define modules configs
  608. if(angular.isDefined(config.modules)) {
  609. if(angular.isArray(config.modules)) {
  610. angular.forEach(config.modules, function(moduleConfig) {
  611. modules[moduleConfig.name] = moduleConfig;
  612. });
  613. } else {
  614. modules[config.modules.name] = config.modules;
  615. }
  616. }
  617. if(angular.isDefined(config.debug)) {
  618. debug = config.debug;
  619. }
  620. if(angular.isDefined(config.events)) {
  621. events = config.events;
  622. }
  623. };
  624. }]);
  625. ocLazyLoad.directive('ocLazyLoad', ['$ocLazyLoad', '$compile', '$animate', '$parse',
  626. function($ocLazyLoad, $compile, $animate, $parse) {
  627. return {
  628. restrict: 'A',
  629. terminal: true,
  630. priority: 1000,
  631. compile: function(element, attrs) {
  632. // we store the content and remove it before compilation
  633. var content = element[0].innerHTML;
  634. element.html('');
  635. return function($scope, $element, $attr) {
  636. var model = $parse($attr.ocLazyLoad);
  637. $scope.$watch(function() {
  638. // it can be a module name (string), an object, an array, or a scope reference to any of this
  639. return model($scope) || $attr.ocLazyLoad;
  640. }, function(moduleName) {
  641. if(angular.isDefined(moduleName)) {
  642. $ocLazyLoad.load(moduleName).then(function success(moduleConfig) {
  643. $animate.enter($compile(content)($scope), null, $element);
  644. });
  645. }
  646. }, true);
  647. };
  648. }
  649. };
  650. }]);
  651. /**
  652. * Get the list of required modules/services/... for this module
  653. * @param module
  654. * @returns {Array}
  655. */
  656. function getRequires(module) {
  657. var requires = [];
  658. angular.forEach(module.requires, function(requireModule) {
  659. if(regModules.indexOf(requireModule) === -1) {
  660. requires.push(requireModule);
  661. }
  662. });
  663. return requires;
  664. }
  665. /**
  666. * Check if a module exists and returns it if it does
  667. * @param moduleName
  668. * @returns {boolean}
  669. */
  670. function moduleExists(moduleName) {
  671. if(!angular.isString(moduleName)) {
  672. return false;
  673. }
  674. try {
  675. return ngModuleFct(moduleName);
  676. } catch(e) {
  677. if(/No module/.test(e) || (e.message.indexOf('$injector:nomod') > -1)) {
  678. return false;
  679. }
  680. }
  681. }
  682. function getModule(moduleName) {
  683. try {
  684. return ngModuleFct(moduleName);
  685. } catch(e) {
  686. // this error message really suxx
  687. if(/No module/.test(e) || (e.message.indexOf('$injector:nomod') > -1)) {
  688. e.message = 'The module "' + moduleName + '" that you are trying to load does not exist. ' + e.message
  689. }
  690. throw e;
  691. }
  692. }
  693. function invokeQueue(providers, queue, moduleName, reconfig) {
  694. if(!queue) {
  695. return;
  696. }
  697. var i, len, args, provider;
  698. for(i = 0, len = queue.length; i < len; i++) {
  699. args = queue[i];
  700. if(angular.isArray(args)) {
  701. if(providers !== null) {
  702. if(providers.hasOwnProperty(args[0])) {
  703. provider = providers[args[0]];
  704. } else {
  705. throw new Error('unsupported provider ' + args[0]);
  706. }
  707. }
  708. var isNew = registerInvokeList(args, moduleName);
  709. if(args[1] !== 'invoke') {
  710. if(isNew && angular.isDefined(provider)) {
  711. provider[args[1]].apply(provider, args[2]);
  712. }
  713. } else { // config block
  714. var callInvoke = function(fct) {
  715. var invoked = regConfigs.indexOf(moduleName + '-' + fct);
  716. if(invoked === -1 || reconfig) {
  717. if(invoked === -1) {
  718. regConfigs.push(moduleName + '-' + fct);
  719. }
  720. if(angular.isDefined(provider)) {
  721. provider[args[1]].apply(provider, args[2]);
  722. }
  723. }
  724. };
  725. if(angular.isFunction(args[2][0])) {
  726. callInvoke(args[2][0]);
  727. } else if(angular.isArray(args[2][0])) {
  728. for(var j = 0, jlen = args[2][0].length; j < jlen; j++) {
  729. if(angular.isFunction(args[2][0][j])) {
  730. callInvoke(args[2][0][j]);
  731. }
  732. }
  733. }
  734. }
  735. }
  736. }
  737. }
  738. /**
  739. * Register a new module and load it
  740. * @param providers
  741. * @param registerModules
  742. * @returns {*}
  743. */
  744. function register(providers, registerModules, params) {
  745. if(registerModules) {
  746. var k, r, moduleName, moduleFn, tempRunBlocks = [];
  747. for(k = registerModules.length - 1; k >= 0; k--) {
  748. moduleName = registerModules[k];
  749. if(typeof moduleName !== 'string') {
  750. moduleName = getModuleName(moduleName);
  751. }
  752. if(!moduleName || justLoaded.indexOf(moduleName) !== -1) {
  753. continue;
  754. }
  755. var newModule = regModules.indexOf(moduleName) === -1;
  756. moduleFn = ngModuleFct(moduleName);
  757. if(newModule) { // new module
  758. regModules.push(moduleName);
  759. register(providers, moduleFn.requires, params);
  760. }
  761. if(moduleFn._runBlocks.length > 0) {
  762. // new run blocks detected! Replace the old ones (if existing)
  763. runBlocks[moduleName] = [];
  764. while(moduleFn._runBlocks.length > 0) {
  765. runBlocks[moduleName].push(moduleFn._runBlocks.shift());
  766. }
  767. }
  768. if(angular.isDefined(runBlocks[moduleName]) && (newModule || params.rerun)) {
  769. tempRunBlocks = tempRunBlocks.concat(runBlocks[moduleName]);
  770. }
  771. invokeQueue(providers, moduleFn._invokeQueue, moduleName, params.reconfig);
  772. invokeQueue(providers, moduleFn._configBlocks, moduleName, params.reconfig); // angular 1.3+
  773. broadcast(newModule ? 'ocLazyLoad.moduleLoaded' : 'ocLazyLoad.moduleReloaded', moduleName);
  774. registerModules.pop();
  775. justLoaded.push(moduleName);
  776. }
  777. // execute the run blocks at the end
  778. var instanceInjector = providers.getInstanceInjector();
  779. angular.forEach(tempRunBlocks, function(fn) {
  780. instanceInjector.invoke(fn);
  781. });
  782. }
  783. }
  784. /**
  785. * Register an invoke
  786. * @param args
  787. * @param moduleName
  788. * @returns {boolean}
  789. */
  790. function registerInvokeList(args, moduleName) {
  791. var invokeList = args[2][0],
  792. type = args[1],
  793. newInvoke = false;
  794. if(angular.isUndefined(regInvokes[moduleName])) {
  795. regInvokes[moduleName] = {};
  796. }
  797. if(angular.isUndefined(regInvokes[moduleName][type])) {
  798. regInvokes[moduleName][type] = {};
  799. }
  800. var onInvoke = function(invokeName, signature) {
  801. newInvoke = true;
  802. regInvokes[moduleName][type][invokeName].push(signature);
  803. broadcast('ocLazyLoad.componentLoaded', [moduleName, type, invokeName]);
  804. };
  805. var signature = function signature(data) {
  806. if(angular.isArray(data)) { // arrays are objects, we need to test for it first
  807. return hashCode(data.toString());
  808. } else if(angular.isObject(data)) { // constants & values for example
  809. return hashCode(stringify(data));
  810. } else {
  811. if(angular.isDefined(data) && data !== null) {
  812. return hashCode(data.toString());
  813. } else { // null & undefined constants
  814. return data;
  815. }
  816. }
  817. };
  818. if(angular.isString(invokeList)) {
  819. if(angular.isUndefined(regInvokes[moduleName][type][invokeList])) {
  820. regInvokes[moduleName][type][invokeList] = [];
  821. }
  822. if(regInvokes[moduleName][type][invokeList].indexOf(signature(args[2][1])) === -1) {
  823. onInvoke(invokeList, signature(args[2][1]));
  824. }
  825. } else if(angular.isObject(invokeList)) { // decorators for example
  826. angular.forEach(invokeList, function(invoke) {
  827. if(angular.isString(invoke)) {
  828. if(angular.isUndefined(regInvokes[moduleName][type][invoke])) {
  829. regInvokes[moduleName][type][invoke] = [];
  830. }
  831. if(regInvokes[moduleName][type][invoke].indexOf(signature(invokeList[1])) === -1) {
  832. onInvoke(invoke, signature(invokeList[1]));
  833. }
  834. }
  835. });
  836. } else {
  837. return false;
  838. }
  839. return newInvoke;
  840. }
  841. function getModuleName(module) {
  842. var moduleName = null;
  843. if(angular.isString(module)) {
  844. moduleName = module;
  845. } else if(angular.isObject(module) && module.hasOwnProperty('name') && angular.isString(module.name)) {
  846. moduleName = module.name;
  847. }
  848. return moduleName;
  849. }
  850. /**
  851. * Get the list of existing registered modules
  852. * @param element
  853. */
  854. function init(element) {
  855. if(modulesToLoad.length === 0) {
  856. var elements = [element],
  857. names = ['ng:app', 'ng-app', 'x-ng-app', 'data-ng-app'],
  858. NG_APP_CLASS_REGEXP = /\sng[:\-]app(:\s*([\w\d_]+);?)?\s/,
  859. append = function append(elm) {
  860. return (elm && elements.push(elm));
  861. };
  862. angular.forEach(names, function(name) {
  863. names[name] = true;
  864. append(document.getElementById(name));
  865. name = name.replace(':', '\\:');
  866. if(element[0].querySelectorAll) {
  867. angular.forEach(element[0].querySelectorAll('.' + name), append);
  868. angular.forEach(element[0].querySelectorAll('.' + name + '\\:'), append);
  869. angular.forEach(element[0].querySelectorAll('[' + name + ']'), append);
  870. }
  871. });
  872. angular.forEach(elements, function(elm) {
  873. if(modulesToLoad.length === 0) {
  874. var className = ' ' + element.className + ' ';
  875. var match = NG_APP_CLASS_REGEXP.exec(className);
  876. if(match) {
  877. modulesToLoad.push((match[2] || '').replace(/\s+/g, ','));
  878. } else {
  879. angular.forEach(elm.attributes, function(attr) {
  880. if(modulesToLoad.length === 0 && names[attr.name]) {
  881. modulesToLoad.push(attr.value);
  882. }
  883. });
  884. }
  885. }
  886. });
  887. }
  888. if(modulesToLoad.length === 0 && !((window.jasmine || window.mocha) && angular.isDefined(angular.mock))) {
  889. console.error('No module found during bootstrap, unable to init ocLazyLoad. You should always use the ng-app directive or angular.boostrap when you use ocLazyLoad.');
  890. }
  891. var addReg = function addReg(moduleName) {
  892. if(regModules.indexOf(moduleName) === -1) {
  893. // register existing modules
  894. regModules.push(moduleName);
  895. var mainModule = angular.module(moduleName);
  896. // register existing components (directives, services, ...)
  897. invokeQueue(null, mainModule._invokeQueue, moduleName);
  898. invokeQueue(null, mainModule._configBlocks, moduleName); // angular 1.3+
  899. angular.forEach(mainModule.requires, addReg);
  900. }
  901. };
  902. angular.forEach(modulesToLoad, function(moduleName) {
  903. addReg(moduleName);
  904. });
  905. modulesToLoad = []; // reset for next bootstrap
  906. recordDeclarations.pop(); // wait for the next lazy load
  907. }
  908. var bootstrapFct = angular.bootstrap;
  909. angular.bootstrap = function(element, modules, config) {
  910. // we use slice to make a clean copy
  911. angular.forEach(modules.slice(), function(module) {
  912. addToLoadList(module);
  913. });
  914. return bootstrapFct(element, modules, config);
  915. };
  916. var addToLoadList = function addToLoadList(name) {
  917. if(recordDeclarations.length > 0 && angular.isString(name) && modulesToLoad.indexOf(name) === -1) {
  918. modulesToLoad.push(name);
  919. }
  920. };
  921. var ngModuleFct = angular.module;
  922. angular.module = function(name, requires, configFn) {
  923. addToLoadList(name);
  924. return ngModuleFct(name, requires, configFn);
  925. };
  926. var hashCode = function hashCode(str) {
  927. var hash = 0, i, chr, len;
  928. if (str.length == 0) return hash;
  929. for (i = 0, len = str.length; i < len; i++) {
  930. chr = str.charCodeAt(i);
  931. hash = ((hash << 5) - hash) + chr;
  932. hash |= 0; // Convert to 32bit integer
  933. }
  934. return hash;
  935. };
  936. var stringify = function stringify(obj) {
  937. var cache = [];
  938. return JSON.stringify(obj, function(key, value) {
  939. if (typeof value === 'object' && value !== null) {
  940. if (cache.indexOf(value) !== -1) {
  941. // Circular reference found, discard key
  942. return;
  943. }
  944. // Store value in our collection
  945. cache.push(value);
  946. }
  947. return value;
  948. });
  949. };
  950. // Array.indexOf polyfill for IE8
  951. if(!Array.prototype.indexOf) {
  952. Array.prototype.indexOf = function(searchElement, fromIndex) {
  953. var k;
  954. // 1. Let O be the result of calling ToObject passing
  955. // the this value as the argument.
  956. if(this == null) {
  957. throw new TypeError('"this" is null or not defined');
  958. }
  959. var O = Object(this);
  960. // 2. Let lenValue be the result of calling the Get
  961. // internal method of O with the argument "length".
  962. // 3. Let len be ToUint32(lenValue).
  963. var len = O.length >>> 0;
  964. // 4. If len is 0, return -1.
  965. if(len === 0) {
  966. return -1;
  967. }
  968. // 5. If argument fromIndex was passed let n be
  969. // ToInteger(fromIndex); else let n be 0.
  970. var n = +fromIndex || 0;
  971. if(Math.abs(n) === Infinity) {
  972. n = 0;
  973. }
  974. // 6. If n >= len, return -1.
  975. if(n >= len) {
  976. return -1;
  977. }
  978. // 7. If n >= 0, then Let k be n.
  979. // 8. Else, n<0, Let k be len - abs(n).
  980. // If k is less than 0, then let k be 0.
  981. k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
  982. // 9. Repeat, while k < len
  983. while(k < len) {
  984. // a. Let Pk be ToString(k).
  985. // This is implicit for LHS operands of the in operator
  986. // b. Let kPresent be the result of calling the
  987. // HasProperty internal method of O with argument Pk.
  988. // This step can be combined with c
  989. // c. If kPresent is true, then
  990. // i. Let elementK be the result of calling the Get
  991. // internal method of O with the argument ToString(k).
  992. // ii. Let same be the result of applying the
  993. // Strict Equality Comparison Algorithm to
  994. // searchElement and elementK.
  995. // iii. If same is true, return k.
  996. if(k in O && O[k] === searchElement) {
  997. return k;
  998. }
  999. k++;
  1000. }
  1001. return -1;
  1002. };
  1003. }
  1004. })();