ocLazyLoad.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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,
  252. m;
  253. if (typeof path === 'object') {
  254. file_type = path.type;
  255. path = path.path;
  256. }
  257. cachePromise = filesCache.get(path);
  258. if (angular.isUndefined(cachePromise) || params.cache === false) {
  259. // always check for requirejs syntax just in case
  260. if ((m = /^(css|less|html|htm|js)?(?=!)/.exec(path)) !== null) { // Detect file type using preceding type declaration (ala requireJS)
  261. file_type = m[1];
  262. path = path.substr(m[1].length + 1, path.length); // Strip the type from the path
  263. }
  264. if (!file_type) {
  265. if ((m = /[.](css|less|html|htm|js)?(\?.*)?$/.exec(path)) !== null) { // Detect file type via file extension
  266. file_type = m[1];
  267. } else if (!jsLoader.hasOwnProperty('ocLazyLoadLoader') && jsLoader.hasOwnProperty('load')) { // requirejs
  268. file_type = 'js';
  269. } else {
  270. $log.error('File type could not be determined. ' + path);
  271. return;
  272. }
  273. }
  274. if ((file_type === 'css' || file_type === 'less') && cssFiles.indexOf(path) === -1) {
  275. cssFiles.push(path);
  276. } else if ((file_type === 'html' || file_type === 'htm') && templatesFiles.indexOf(path) === -1) {
  277. templatesFiles.push(path);
  278. } else if ((file_type === 'js') || jsFiles.indexOf(path) === -1) {
  279. jsFiles.push(path);
  280. } else {
  281. $log.error('File type is not valid. ' + path);
  282. }
  283. } else if (cachePromise) {
  284. promises.push(cachePromise);
  285. }
  286. };
  287. if (params.serie) {
  288. pushFile(params.files.shift());
  289. } else {
  290. angular.forEach(params.files, function(path) {
  291. pushFile(path);
  292. });
  293. }
  294. if (cssFiles.length > 0) {
  295. var cssDeferred = $q.defer();
  296. cssLoader(cssFiles, function(err) {
  297. if (angular.isDefined(err) && cssLoader.hasOwnProperty('ocLazyLoadLoader')) {
  298. $log.error(err);
  299. cssDeferred.reject(err);
  300. } else {
  301. cssDeferred.resolve();
  302. }
  303. }, params);
  304. promises.push(cssDeferred.promise);
  305. }
  306. if (templatesFiles.length > 0) {
  307. var templatesDeferred = $q.defer();
  308. templatesLoader(templatesFiles, function(err) {
  309. if (angular.isDefined(err) && templatesLoader.hasOwnProperty('ocLazyLoadLoader')) {
  310. $log.error(err);
  311. templatesDeferred.reject(err);
  312. } else {
  313. templatesDeferred.resolve();
  314. }
  315. }, params);
  316. promises.push(templatesDeferred.promise);
  317. }
  318. if (jsFiles.length > 0) {
  319. var jsDeferred = $q.defer();
  320. jsLoader(jsFiles, function(err) {
  321. if (angular.isDefined(err) && jsLoader.hasOwnProperty('ocLazyLoadLoader')) {
  322. $log.error(err);
  323. jsDeferred.reject(err);
  324. } else {
  325. jsDeferred.resolve();
  326. }
  327. }, params);
  328. promises.push(jsDeferred.promise);
  329. }
  330. if (params.serie && params.files.length > 0) {
  331. return $q.all(promises).then(function success() {
  332. return filesLoader(config, params);
  333. });
  334. } else {
  335. return $q.all(promises).finally(function(res) {
  336. recordDeclarations.pop(); // stop watching angular.module calls
  337. return res;
  338. });
  339. }
  340. };
  341. return {
  342. /**
  343. * Let you get a module config object
  344. * @param moduleName String the name of the module
  345. * @returns {*}
  346. */
  347. getModuleConfig: function(moduleName) {
  348. if (!angular.isString(moduleName)) {
  349. throw new Error('You need to give the name of the module to get');
  350. }
  351. if (!modules[moduleName]) {
  352. return null;
  353. }
  354. return modules[moduleName];
  355. },
  356. /**
  357. * Let you define a module config object
  358. * @param moduleConfig Object the module config object
  359. * @returns {*}
  360. */
  361. setModuleConfig: function(moduleConfig) {
  362. if (!angular.isObject(moduleConfig)) {
  363. throw new Error('You need to give the module config object to set');
  364. }
  365. modules[moduleConfig.name] = moduleConfig;
  366. return moduleConfig;
  367. },
  368. /**
  369. * Returns the list of loaded modules
  370. * @returns {string[]}
  371. */
  372. getModules: function() {
  373. return regModules;
  374. },
  375. /**
  376. * Let you check if a module has been loaded into Angular or not
  377. * @param modulesNames String/Object a module name, or a list of module names
  378. * @returns {boolean}
  379. */
  380. isLoaded: function(modulesNames) {
  381. var moduleLoaded = function(module) {
  382. var isLoaded = regModules.indexOf(module) > -1;
  383. if (!isLoaded) {
  384. isLoaded = !!moduleExists(module);
  385. }
  386. return isLoaded;
  387. };
  388. if (angular.isString(modulesNames)) {
  389. modulesNames = [modulesNames];
  390. }
  391. if (angular.isArray(modulesNames)) {
  392. var i, len;
  393. for (i = 0, len = modulesNames.length; i < len; i++) {
  394. if (!moduleLoaded(modulesNames[i])) {
  395. return false;
  396. }
  397. }
  398. return true;
  399. } else {
  400. throw new Error('You need to define the module(s) name(s)');
  401. }
  402. },
  403. /**
  404. * Load a module or a list of modules into Angular
  405. * @param module Mixed the name of a predefined module config object, or a module config object, or an array of either
  406. * @param params Object optional parameters
  407. * @returns promise
  408. */
  409. load: function(module, params) {
  410. var self = this,
  411. config = null,
  412. moduleCache = [],
  413. deferredList = [],
  414. deferred = $q.defer(),
  415. errText;
  416. if (angular.isUndefined(params)) {
  417. params = {};
  418. }
  419. // If module is an array, break it down
  420. if (angular.isArray(module)) {
  421. // Resubmit each entry as a single module
  422. angular.forEach(module, function(m) {
  423. if (m) {
  424. deferredList.push(self.load(m, params));
  425. }
  426. });
  427. // Resolve the promise once everything has loaded
  428. $q.all(deferredList).then(function success() {
  429. deferred.resolve(module);
  430. }, function error(err) {
  431. deferred.reject(err);
  432. });
  433. return deferred.promise;
  434. }
  435. // Get or Set a configuration depending on what was passed in
  436. if (typeof module === 'string') {
  437. config = self.getModuleConfig(module);
  438. if (!config) {
  439. config = {
  440. files: [module]
  441. };
  442. }
  443. } else if (typeof module === 'object') {
  444. if (angular.isDefined(module.path) && angular.isDefined(module.type)) { // case {type: 'js', path: lazyLoadUrl + 'testModule.fakejs'}
  445. config = {
  446. files: [module]
  447. };
  448. } else {
  449. config = self.setModuleConfig(module);
  450. }
  451. }
  452. if (config === null) {
  453. var moduleName = getModuleName(module);
  454. errText = 'Module "' + (moduleName || 'unknown') + '" is not configured, cannot load.';
  455. $log.error(errText);
  456. deferred.reject(new Error(errText));
  457. return deferred.promise;
  458. } else {
  459. // deprecated
  460. if (angular.isDefined(config.template)) {
  461. if (angular.isUndefined(config.files)) {
  462. config.files = [];
  463. }
  464. if (angular.isString(config.template)) {
  465. config.files.push(config.template);
  466. } else if (angular.isArray(config.template)) {
  467. config.files.concat(config.template);
  468. }
  469. }
  470. }
  471. moduleCache.push = function(value) {
  472. if (this.indexOf(value) === -1) {
  473. Array.prototype.push.apply(this, arguments);
  474. }
  475. };
  476. var localParams = {};
  477. angular.extend(localParams, params, config);
  478. var loadDependencies = function loadDependencies(module) {
  479. var moduleName,
  480. loadedModule,
  481. requires,
  482. diff,
  483. promisesList = [];
  484. moduleName = getModuleName(module);
  485. if (moduleName === null) {
  486. return $q.when();
  487. } else {
  488. try {
  489. loadedModule = getModule(moduleName);
  490. } catch (e) {
  491. var deferred = $q.defer();
  492. $log.error(e.message);
  493. deferred.reject(e);
  494. return deferred.promise;
  495. }
  496. requires = getRequires(loadedModule);
  497. }
  498. angular.forEach(requires, function(requireEntry) {
  499. // If no configuration is provided, try and find one from a previous load.
  500. // If there isn't one, bail and let the normal flow run
  501. if (typeof requireEntry === 'string') {
  502. var config = self.getModuleConfig(requireEntry);
  503. if (config === null) {
  504. moduleCache.push(requireEntry); // We don't know about this module, but something else might, so push it anyway.
  505. return;
  506. }
  507. requireEntry = config;
  508. }
  509. // Check if this dependency has been loaded previously
  510. if (moduleExists(requireEntry.name)) {
  511. if (typeof module !== 'string') {
  512. // compare against the already loaded module to see if the new definition adds any new files
  513. diff = requireEntry.files.filter(function(n) {
  514. return self.getModuleConfig(requireEntry.name).files.indexOf(n) < 0;
  515. });
  516. // If the module was redefined, advise via the console
  517. if (diff.length !== 0) {
  518. $log.warn('Module "', moduleName, '" attempted to redefine configuration for dependency. "', requireEntry.name, '"\n Additional Files Loaded:', diff);
  519. }
  520. // Push everything to the file loader, it will weed out the duplicates.
  521. promisesList.push(filesLoader(requireEntry.files, localParams).then(function() {
  522. return loadDependencies(requireEntry);
  523. }));
  524. }
  525. return;
  526. } else if (angular.isArray(requireEntry)) {
  527. requireEntry = {
  528. files: requireEntry
  529. };
  530. } else if (typeof requireEntry === 'object') {
  531. if (requireEntry.hasOwnProperty('name') && requireEntry['name']) {
  532. // The dependency doesn't exist in the module cache and is a new configuration, so store and push it.
  533. self.setModuleConfig(requireEntry);
  534. moduleCache.push(requireEntry['name']);
  535. }
  536. }
  537. // Check if the dependency has any files that need to be loaded. If there are, push a new promise to the promise list.
  538. if (requireEntry.hasOwnProperty('files') && requireEntry.files.length !== 0) {
  539. if (requireEntry.files) {
  540. promisesList.push(filesLoader(requireEntry, localParams).then(function() {
  541. return loadDependencies(requireEntry);
  542. }));
  543. }
  544. }
  545. });
  546. // Create a wrapper promise to watch the promise list and resolve it once everything is done.
  547. return $q.all(promisesList);
  548. };
  549. // if someone loaded the module file with something else and called the load function with just the module name
  550. if (angular.isUndefined(config.files) && angular.isDefined(config.name) && moduleExists(config.name)) {
  551. recordDeclarations.push(true); // start watching angular.module calls
  552. addToLoadList(config.name);
  553. recordDeclarations.pop();
  554. }
  555. filesLoader(config, localParams).then(function success() {
  556. if (modulesToLoad.length === 0) {
  557. deferred.resolve(module);
  558. } else {
  559. var loadNext = function loadNext(moduleName) {
  560. moduleCache.push(moduleName);
  561. loadDependencies(moduleName).then(function success() {
  562. try {
  563. justLoaded = [];
  564. register(providers, moduleCache, localParams);
  565. } catch (e) {
  566. $log.error(e.message);
  567. deferred.reject(e);
  568. return;
  569. }
  570. if (modulesToLoad.length > 0) {
  571. loadNext(modulesToLoad.shift()); // load the next in list
  572. } else {
  573. deferred.resolve(module); // everything has been loaded, resolve
  574. }
  575. }, function error(err) {
  576. deferred.reject(err);
  577. });
  578. };
  579. // load the first in list
  580. loadNext(modulesToLoad.shift());
  581. }
  582. }, function error(err) {
  583. deferred.reject(err);
  584. });
  585. return deferred.promise;
  586. }
  587. };
  588. }];
  589. this.config = function(config) {
  590. if (angular.isDefined(config.jsLoader) || angular.isDefined(config.asyncLoader)) {
  591. if (!angular.isFunction(config.jsLoader || config.asyncLoader)) {
  592. throw ('The js loader needs to be a function');
  593. }
  594. jsLoader = config.jsLoader || config.asyncLoader;
  595. }
  596. if (angular.isDefined(config.cssLoader)) {
  597. if (!angular.isFunction(config.cssLoader)) {
  598. throw ('The css loader needs to be a function');
  599. }
  600. cssLoader = config.cssLoader;
  601. }
  602. if (angular.isDefined(config.templatesLoader)) {
  603. if (!angular.isFunction(config.templatesLoader)) {
  604. throw ('The template loader needs to be a function');
  605. }
  606. templatesLoader = config.templatesLoader;
  607. }
  608. // If we want to define modules configs
  609. if (angular.isDefined(config.modules)) {
  610. if (angular.isArray(config.modules)) {
  611. angular.forEach(config.modules, function(moduleConfig) {
  612. modules[moduleConfig.name] = moduleConfig;
  613. });
  614. } else {
  615. modules[config.modules.name] = config.modules;
  616. }
  617. }
  618. if (angular.isDefined(config.debug)) {
  619. debug = config.debug;
  620. }
  621. if (angular.isDefined(config.events)) {
  622. events = config.events;
  623. }
  624. };
  625. }
  626. ]);
  627. ocLazyLoad.directive('ocLazyLoad', ['$ocLazyLoad', '$compile', '$animate', '$parse',
  628. function($ocLazyLoad, $compile, $animate, $parse) {
  629. return {
  630. restrict: 'A',
  631. terminal: true,
  632. priority: 1000,
  633. compile: function(element, attrs) {
  634. // we store the content and remove it before compilation
  635. var content = element[0].innerHTML;
  636. element.html('');
  637. return function($scope, $element, $attr) {
  638. var model = $parse($attr.ocLazyLoad);
  639. $scope.$watch(function() {
  640. // it can be a module name (string), an object, an array, or a scope reference to any of this
  641. return model($scope) || $attr.ocLazyLoad;
  642. }, function(moduleName) {
  643. if (angular.isDefined(moduleName)) {
  644. $ocLazyLoad.load(moduleName).then(function success(moduleConfig) {
  645. $animate.enter($compile(content)($scope), null, $element);
  646. });
  647. }
  648. }, true);
  649. };
  650. }
  651. };
  652. }
  653. ]);
  654. /**
  655. * Get the list of required modules/services/... for this module
  656. * @param module
  657. * @returns {Array}
  658. */
  659. function getRequires(module) {
  660. var requires = [];
  661. angular.forEach(module.requires, function(requireModule) {
  662. if (regModules.indexOf(requireModule) === -1) {
  663. requires.push(requireModule);
  664. }
  665. });
  666. return requires;
  667. }
  668. /**
  669. * Check if a module exists and returns it if it does
  670. * @param moduleName
  671. * @returns {boolean}
  672. */
  673. function moduleExists(moduleName) {
  674. if (!angular.isString(moduleName)) {
  675. return false;
  676. }
  677. try {
  678. return ngModuleFct(moduleName);
  679. } catch (e) {
  680. if (/No module/.test(e) || (e.message.indexOf('$injector:nomod') > -1)) {
  681. return false;
  682. }
  683. }
  684. }
  685. function getModule(moduleName) {
  686. try {
  687. return ngModuleFct(moduleName);
  688. } catch (e) {
  689. // this error message really suxx
  690. if (/No module/.test(e) || (e.message.indexOf('$injector:nomod') > -1)) {
  691. e.message = 'The module "' + moduleName + '" that you are trying to load does not exist. ' + e.message
  692. }
  693. throw e;
  694. }
  695. }
  696. function invokeQueue(providers, queue, moduleName, reconfig) {
  697. if (!queue) {
  698. return;
  699. }
  700. var i, len, args, provider;
  701. for (i = 0, len = queue.length; i < len; i++) {
  702. args = queue[i];
  703. if (angular.isArray(args)) {
  704. if (providers !== null) {
  705. if (providers.hasOwnProperty(args[0])) {
  706. provider = providers[args[0]];
  707. } else {
  708. throw new Error('unsupported provider ' + args[0]);
  709. }
  710. }
  711. var isNew = registerInvokeList(args, moduleName);
  712. if (args[1] !== 'invoke') {
  713. if (isNew && angular.isDefined(provider)) {
  714. provider[args[1]].apply(provider, args[2]);
  715. }
  716. } else { // config block
  717. var callInvoke = function(fct) {
  718. var invoked = regConfigs.indexOf(moduleName + '-' + fct);
  719. if (invoked === -1 || reconfig) {
  720. if (invoked === -1) {
  721. regConfigs.push(moduleName + '-' + fct);
  722. }
  723. if (angular.isDefined(provider)) {
  724. provider[args[1]].apply(provider, args[2]);
  725. }
  726. }
  727. };
  728. if (angular.isFunction(args[2][0])) {
  729. callInvoke(args[2][0]);
  730. } else if (angular.isArray(args[2][0])) {
  731. for (var j = 0, jlen = args[2][0].length; j < jlen; j++) {
  732. if (angular.isFunction(args[2][0][j])) {
  733. callInvoke(args[2][0][j]);
  734. }
  735. }
  736. }
  737. }
  738. }
  739. }
  740. }
  741. /**
  742. * Register a new module and load it
  743. * @param providers
  744. * @param registerModules
  745. * @returns {*}
  746. */
  747. function register(providers, registerModules, params) {
  748. if (registerModules) {
  749. var k, r, moduleName, moduleFn, tempRunBlocks = [];
  750. for (k = registerModules.length - 1; k >= 0; k--) {
  751. moduleName = registerModules[k];
  752. if (typeof moduleName !== 'string') {
  753. moduleName = getModuleName(moduleName);
  754. }
  755. if (!moduleName || justLoaded.indexOf(moduleName) !== -1) {
  756. continue;
  757. }
  758. var newModule = regModules.indexOf(moduleName) === -1;
  759. moduleFn = ngModuleFct(moduleName);
  760. if (newModule) { // new module
  761. regModules.push(moduleName);
  762. register(providers, moduleFn.requires, params);
  763. }
  764. if (moduleFn._runBlocks.length > 0) {
  765. // new run blocks detected! Replace the old ones (if existing)
  766. runBlocks[moduleName] = [];
  767. while (moduleFn._runBlocks.length > 0) {
  768. runBlocks[moduleName].push(moduleFn._runBlocks.shift());
  769. }
  770. }
  771. if (angular.isDefined(runBlocks[moduleName]) && (newModule || params.rerun)) {
  772. tempRunBlocks = tempRunBlocks.concat(runBlocks[moduleName]);
  773. }
  774. invokeQueue(providers, moduleFn._invokeQueue, moduleName, params.reconfig);
  775. invokeQueue(providers, moduleFn._configBlocks, moduleName, params.reconfig); // angular 1.3+
  776. broadcast(newModule ? 'ocLazyLoad.moduleLoaded' : 'ocLazyLoad.moduleReloaded', moduleName);
  777. registerModules.pop();
  778. justLoaded.push(moduleName);
  779. }
  780. // execute the run blocks at the end
  781. var instanceInjector = providers.getInstanceInjector();
  782. angular.forEach(tempRunBlocks, function(fn) {
  783. instanceInjector.invoke(fn);
  784. });
  785. }
  786. }
  787. /**
  788. * Register an invoke
  789. * @param args
  790. * @param moduleName
  791. * @returns {boolean}
  792. */
  793. function registerInvokeList(args, moduleName) {
  794. var invokeList = args[2][0],
  795. type = args[1],
  796. newInvoke = false;
  797. if (angular.isUndefined(regInvokes[moduleName])) {
  798. regInvokes[moduleName] = {};
  799. }
  800. if (angular.isUndefined(regInvokes[moduleName][type])) {
  801. regInvokes[moduleName][type] = {};
  802. }
  803. var onInvoke = function(invokeName, signature) {
  804. newInvoke = true;
  805. regInvokes[moduleName][type][invokeName].push(signature);
  806. broadcast('ocLazyLoad.componentLoaded', [moduleName, type, invokeName]);
  807. };
  808. var signature = function signature(data) {
  809. if (angular.isArray(data)) { // arrays are objects, we need to test for it first
  810. return hashCode(data.toString());
  811. } else if (angular.isObject(data)) { // constants & values for example
  812. return hashCode(stringify(data));
  813. } else {
  814. if (angular.isDefined(data) && data !== null) {
  815. return hashCode(data.toString());
  816. } else { // null & undefined constants
  817. return data;
  818. }
  819. }
  820. };
  821. if (angular.isString(invokeList)) {
  822. if (angular.isUndefined(regInvokes[moduleName][type][invokeList])) {
  823. regInvokes[moduleName][type][invokeList] = [];
  824. }
  825. if (regInvokes[moduleName][type][invokeList].indexOf(signature(args[2][1])) === -1) {
  826. onInvoke(invokeList, signature(args[2][1]));
  827. }
  828. } else if (angular.isObject(invokeList)) { // decorators for example
  829. angular.forEach(invokeList, function(invoke) {
  830. if (angular.isString(invoke)) {
  831. if (angular.isUndefined(regInvokes[moduleName][type][invoke])) {
  832. regInvokes[moduleName][type][invoke] = [];
  833. }
  834. if (regInvokes[moduleName][type][invoke].indexOf(signature(invokeList[1])) === -1) {
  835. onInvoke(invoke, signature(invokeList[1]));
  836. }
  837. }
  838. });
  839. } else {
  840. return false;
  841. }
  842. return newInvoke;
  843. }
  844. function getModuleName(module) {
  845. var moduleName = null;
  846. if (angular.isString(module)) {
  847. moduleName = module;
  848. } else if (angular.isObject(module) && module.hasOwnProperty('name') && angular.isString(module.name)) {
  849. moduleName = module.name;
  850. }
  851. return moduleName;
  852. }
  853. /**
  854. * Get the list of existing registered modules
  855. * @param element
  856. */
  857. function init(element) {
  858. if (modulesToLoad.length === 0) {
  859. var elements = [element],
  860. names = ['ng:app', 'ng-app', 'x-ng-app', 'data-ng-app'],
  861. NG_APP_CLASS_REGEXP = /\sng[:\-]app(:\s*([\w\d_]+);?)?\s/,
  862. append = function append(elm) {
  863. return (elm && elements.push(elm));
  864. };
  865. angular.forEach(names, function(name) {
  866. names[name] = true;
  867. append(document.getElementById(name));
  868. name = name.replace(':', '\\:');
  869. if (element[0].querySelectorAll) {
  870. angular.forEach(element[0].querySelectorAll('.' + name), append);
  871. angular.forEach(element[0].querySelectorAll('.' + name + '\\:'), append);
  872. angular.forEach(element[0].querySelectorAll('[' + name + ']'), append);
  873. }
  874. });
  875. angular.forEach(elements, function(elm) {
  876. if (modulesToLoad.length === 0) {
  877. var className = ' ' + element.className + ' ';
  878. var match = NG_APP_CLASS_REGEXP.exec(className);
  879. if (match) {
  880. modulesToLoad.push((match[2] || '').replace(/\s+/g, ','));
  881. } else {
  882. angular.forEach(elm.attributes, function(attr) {
  883. if (modulesToLoad.length === 0 && names[attr.name]) {
  884. modulesToLoad.push(attr.value);
  885. }
  886. });
  887. }
  888. }
  889. });
  890. }
  891. if (modulesToLoad.length === 0 && !((window.jasmine || window.mocha) && angular.isDefined(angular.mock))) {
  892. 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.');
  893. }
  894. var addReg = function addReg(moduleName) {
  895. if (regModules.indexOf(moduleName) === -1) {
  896. // register existing modules
  897. regModules.push(moduleName);
  898. var mainModule = angular.module(moduleName);
  899. // register existing components (directives, services, ...)
  900. invokeQueue(null, mainModule._invokeQueue, moduleName);
  901. invokeQueue(null, mainModule._configBlocks, moduleName); // angular 1.3+
  902. angular.forEach(mainModule.requires, addReg);
  903. }
  904. };
  905. angular.forEach(modulesToLoad, function(moduleName) {
  906. addReg(moduleName);
  907. });
  908. modulesToLoad = []; // reset for next bootstrap
  909. recordDeclarations.pop(); // wait for the next lazy load
  910. }
  911. var bootstrapFct = angular.bootstrap;
  912. angular.bootstrap = function(element, modules, config) {
  913. // we use slice to make a clean copy
  914. angular.forEach(modules.slice(), function(module) {
  915. addToLoadList(module);
  916. });
  917. return bootstrapFct(element, modules, config);
  918. };
  919. var addToLoadList = function addToLoadList(name) {
  920. if (recordDeclarations.length > 0 && angular.isString(name) && modulesToLoad.indexOf(name) === -1) {
  921. modulesToLoad.push(name);
  922. }
  923. };
  924. var ngModuleFct = angular.module;
  925. angular.module = function(name, requires, configFn) {
  926. addToLoadList(name);
  927. return ngModuleFct(name, requires, configFn);
  928. };
  929. var hashCode = function hashCode(str) {
  930. var hash = 0,
  931. i, chr, len;
  932. if (str.length == 0) return hash;
  933. for (i = 0, len = str.length; i < len; i++) {
  934. chr = str.charCodeAt(i);
  935. hash = ((hash << 5) - hash) + chr;
  936. hash |= 0; // Convert to 32bit integer
  937. }
  938. return hash;
  939. };
  940. var stringify = function stringify(obj) {
  941. var cache = [];
  942. return JSON.stringify(obj, function(key, value) {
  943. if (typeof value === 'object' && value !== null) {
  944. if (cache.indexOf(value) !== -1) {
  945. // Circular reference found, discard key
  946. return;
  947. }
  948. // Store value in our collection
  949. cache.push(value);
  950. }
  951. return value;
  952. });
  953. };
  954. // Array.indexOf polyfill for IE8
  955. if (!Array.prototype.indexOf) {
  956. Array.prototype.indexOf = function(searchElement, fromIndex) {
  957. var k;
  958. // 1. Let O be the result of calling ToObject passing
  959. // the this value as the argument.
  960. if (this == null) {
  961. throw new TypeError('"this" is null or not defined');
  962. }
  963. var O = Object(this);
  964. // 2. Let lenValue be the result of calling the Get
  965. // internal method of O with the argument "length".
  966. // 3. Let len be ToUint32(lenValue).
  967. var len = O.length >>> 0;
  968. // 4. If len is 0, return -1.
  969. if (len === 0) {
  970. return -1;
  971. }
  972. // 5. If argument fromIndex was passed let n be
  973. // ToInteger(fromIndex); else let n be 0.
  974. var n = +fromIndex || 0;
  975. if (Math.abs(n) === Infinity) {
  976. n = 0;
  977. }
  978. // 6. If n >= len, return -1.
  979. if (n >= len) {
  980. return -1;
  981. }
  982. // 7. If n >= 0, then Let k be n.
  983. // 8. Else, n<0, Let k be len - abs(n).
  984. // If k is less than 0, then let k be 0.
  985. k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
  986. // 9. Repeat, while k < len
  987. while (k < len) {
  988. // a. Let Pk be ToString(k).
  989. // This is implicit for LHS operands of the in operator
  990. // b. Let kPresent be the result of calling the
  991. // HasProperty internal method of O with argument Pk.
  992. // This step can be combined with c
  993. // c. If kPresent is true, then
  994. // i. Let elementK be the result of calling the Get
  995. // internal method of O with the argument ToString(k).
  996. // ii. Let same be the result of applying the
  997. // Strict Equality Comparison Algorithm to
  998. // searchElement and elementK.
  999. // iii. If same is true, return k.
  1000. if (k in O && O[k] === searchElement) {
  1001. return k;
  1002. }
  1003. k++;
  1004. }
  1005. return -1;
  1006. };
  1007. }
  1008. })();