1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102 |
- /**
- * oclazyload - Load modules on demand (lazy load) with angularJS
- * @version v0.6.3
- * @link https://github.com/ocombe/ocLazyLoad
- * @license MIT
- * @author Olivier Combe <olivier.combe@gmail.com>
- */
- (function() {
- 'use strict';
- var regModules = ['ng'],
- regInvokes = {},
- regConfigs = [],
- justLoaded = [],
- runBlocks = {},
- ocLazyLoad = angular.module('oc.lazyLoad', ['ng']),
- broadcast = angular.noop,
- modulesToLoad = [],
- recordDeclarations = [true];
- ocLazyLoad.provider('$ocLazyLoad', ['$controllerProvider', '$provide', '$compileProvider', '$filterProvider', '$injector', '$animateProvider',
- function($controllerProvider, $provide, $compileProvider, $filterProvider, $injector, $animateProvider) {
- var modules = {},
- providers = {
- $controllerProvider: $controllerProvider,
- $compileProvider: $compileProvider,
- $filterProvider: $filterProvider,
- $provide: $provide, // other things
- $injector: $injector,
- $animateProvider: $animateProvider
- },
- anchor = document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0],
- jsLoader, cssLoader, templatesLoader,
- debug = false,
- events = false;
- // Let's get the list of loaded modules & components
- init(angular.element(window.document));
- this.$get = ['$log', '$q', '$templateCache', '$http', '$rootElement', '$rootScope', '$cacheFactory', '$interval', function($log, $q, $templateCache, $http, $rootElement, $rootScope, $cacheFactory, $interval) {
- var instanceInjector,
- filesCache = $cacheFactory('ocLazyLoad'),
- uaCssChecked = false,
- useCssLoadPatch = false;
- if(!debug) {
- $log = {};
- $log['error'] = angular.noop;
- $log['warn'] = angular.noop;
- $log['info'] = angular.noop;
- }
- // Make this lazy because at the moment that $get() is called the instance injector hasn't been assigned to the rootElement yet
- providers.getInstanceInjector = function() {
- return (instanceInjector) ? instanceInjector : (instanceInjector = ($rootElement.data('$injector') || angular.injector()));
- };
- broadcast = function broadcast(eventName, params) {
- if(events) {
- $rootScope.$broadcast(eventName, params);
- }
- if(debug) {
- $log.info(eventName, params);
- }
- };
- /**
- * Load a js/css file
- * @param type
- * @param path
- * @param params
- * @returns promise
- */
- var buildElement = function buildElement(type, path, params) {
- var deferred = $q.defer(),
- el, loaded,
- cacheBuster = function cacheBuster(url) {
- var dc = new Date().getTime();
- if(url.indexOf('?') >= 0) {
- if(url.substring(0, url.length - 1) === '&') {
- return url + '_dc=' + dc;
- }
- return url + '&_dc=' + dc;
- } else {
- return url + '?_dc=' + dc;
- }
- };
- // Store the promise early so the file load can be detected by other parallel lazy loads
- // (ie: multiple routes on one page) a 'true' value isn't sufficient
- // as it causes false positive load results.
- if(angular.isUndefined(filesCache.get(path))) {
- filesCache.put(path, deferred.promise);
- }
- // Switch in case more content types are added later
- switch(type) {
- case 'css':
- el = document.createElement('link');
- el.type = 'text/css';
- el.rel = 'stylesheet';
- el.href = params.cache === false ? cacheBuster(path) : path;
- break;
- case 'js':
- el = document.createElement('script');
- el.src = params.cache === false ? cacheBuster(path) : path;
- break;
- default:
- deferred.reject(new Error('Requested type "' + type + '" is not known. Could not inject "' + path + '"'));
- break;
- }
- el.onload = el['onreadystatechange'] = function(e) {
- if((el['readyState'] && !(/^c|loade/.test(el['readyState']))) || loaded) return;
- el.onload = el['onreadystatechange'] = null;
- loaded = 1;
- broadcast('ocLazyLoad.fileLoaded', path);
- deferred.resolve();
- };
- el.onerror = function() {
- deferred.reject(new Error('Unable to load ' + path));
- };
- el.async = params.serie ? 0 : 1;
- var insertBeforeElem = anchor.lastChild;
- if(params.insertBefore) {
- var element = angular.element(params.insertBefore);
- if(element && element.length > 0) {
- insertBeforeElem = element[0];
- }
- }
- anchor.insertBefore(el, insertBeforeElem);
- /*
- The event load or readystatechange doesn't fire in:
- - iOS < 6 (default mobile browser)
- - Android < 4.4 (default mobile browser)
- - Safari < 6 (desktop browser)
- */
- if(type == 'css') {
- if(!uaCssChecked) {
- var ua = navigator.userAgent.toLowerCase();
- // iOS < 6
- if(/iP(hone|od|ad)/.test(navigator.platform)) {
- var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
- var iOSVersion = parseFloat([parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)].join('.'));
- useCssLoadPatch = iOSVersion < 6;
- } else if(ua.indexOf("android") > -1) { // Android < 4.4
- var androidVersion = parseFloat(ua.slice(ua.indexOf("android") + 8));
- useCssLoadPatch = androidVersion < 4.4;
- } else if(ua.indexOf('safari') > -1 && ua.indexOf('chrome') == -1) {
- var safariVersion = parseFloat(ua.match(/version\/([\.\d]+)/i)[1]);
- useCssLoadPatch = safariVersion < 6;
- }
- }
- if(useCssLoadPatch) {
- var tries = 1000; // * 20 = 20000 miliseconds
- var interval = $interval(function() {
- try {
- el.sheet.cssRules;
- $interval.cancel(interval);
- el.onload();
- } catch(e) {
- if(--tries <= 0) {
- el.onerror();
- }
- }
- }, 20);
- }
- }
- return deferred.promise;
- };
- if(angular.isUndefined(jsLoader)) {
- /**
- * jsLoader function
- * @type Function
- * @param paths array list of js files to load
- * @param callback to call when everything is loaded. We use a callback and not a promise
- * @param params object config parameters
- * because the user can overwrite jsLoader and it will probably not use promises :(
- */
- jsLoader = function(paths, callback, params) {
- var promises = [];
- angular.forEach(paths, function loading(path) {
- promises.push(buildElement('js', path, params));
- });
- $q.all(promises).then(function success() {
- callback();
- }, function error(err) {
- callback(err);
- });
- };
- jsLoader.ocLazyLoadLoader = true;
- }
- if(angular.isUndefined(cssLoader)) {
- /**
- * cssLoader function
- * @type Function
- * @param paths array list of css files to load
- * @param callback to call when everything is loaded. We use a callback and not a promise
- * @param params object config parameters
- * because the user can overwrite cssLoader and it will probably not use promises :(
- */
- cssLoader = function(paths, callback, params) {
- var promises = [];
- angular.forEach(paths, function loading(path) {
- promises.push(buildElement('css', path, params));
- });
- $q.all(promises).then(function success() {
- callback();
- }, function error(err) {
- callback(err);
- });
- };
- cssLoader.ocLazyLoadLoader = true;
- }
- if(angular.isUndefined(templatesLoader)) {
- /**
- * templatesLoader function
- * @type Function
- * @param paths array list of css files to load
- * @param callback to call when everything is loaded. We use a callback and not a promise
- * @param params object config parameters for $http
- * because the user can overwrite templatesLoader and it will probably not use promises :(
- */
- templatesLoader = function(paths, callback, params) {
- var promises = [];
- angular.forEach(paths, function(url) {
- var deferred = $q.defer();
- promises.push(deferred.promise);
- $http.get(url, params).success(function(data) {
- if(angular.isString(data) && data.length > 0) {
- angular.forEach(angular.element(data), function(node) {
- if(node.nodeName === 'SCRIPT' && node.type === 'text/ng-template') {
- $templateCache.put(node.id, node.innerHTML);
- }
- });
- }
- if(angular.isUndefined(filesCache.get(url))) {
- filesCache.put(url, true);
- }
- deferred.resolve();
- }).error(function(err) {
- deferred.reject(new Error('Unable to load template file "' + url + '": ' + err));
- });
- });
- return $q.all(promises).then(function success() {
- callback();
- }, function error(err) {
- callback(err);
- });
- };
- templatesLoader.ocLazyLoadLoader = true;
- }
- var filesLoader = function filesLoader(config, params) {
- var cssFiles = [],
- templatesFiles = [],
- jsFiles = [],
- promises = [],
- cachePromise = null;
- recordDeclarations.push(true); // start watching angular.module calls
- angular.extend(params || {}, config);
- var pushFile = function(path) {
- var file_type = null, m;
- if (typeof path === 'object') {
- file_type = path.type;
- path = path.path;
- }
- cachePromise = filesCache.get(path);
- if(angular.isUndefined(cachePromise) || params.cache === false) {
- // always check for requirejs syntax just in case
- if ((m = /^(css|less|html|htm|js)?(?=!)/.exec(path)) !== null) { // Detect file type using preceding type declaration (ala requireJS)
- file_type = m[1];
- path = path.substr(m[1].length + 1, path.length); // Strip the type from the path
- }
- if (!file_type) {
- if ((m = /[.](css|less|html|htm|js)?(\?.*)?$/.exec(path)) !== null) { // Detect file type via file extension
- file_type = m[1];
- } else if(!jsLoader.hasOwnProperty('ocLazyLoadLoader') && jsLoader.hasOwnProperty('load')) { // requirejs
- file_type = 'js';
- } else {
- $log.error('File type could not be determined. ' + path);
- return;
- }
- }
- if((file_type === 'css' || file_type === 'less') && cssFiles.indexOf(path) === -1) {
- cssFiles.push(path);
- } else if ((file_type === 'html' || file_type === 'htm') && templatesFiles.indexOf(path) === -1) {
- templatesFiles.push(path);
- } else if((file_type === 'js') || jsFiles.indexOf(path) === -1) {
- jsFiles.push(path);
- } else {
- $log.error('File type is not valid. ' + path);
- }
- } else if(cachePromise) {
- promises.push(cachePromise);
- }
- };
- if(params.serie) {
- pushFile(params.files.shift());
- } else {
- angular.forEach(params.files, function(path) {
- pushFile(path);
- });
- }
- if(cssFiles.length > 0) {
- var cssDeferred = $q.defer();
- cssLoader(cssFiles, function(err) {
- if(angular.isDefined(err) && cssLoader.hasOwnProperty('ocLazyLoadLoader')) {
- $log.error(err);
- cssDeferred.reject(err);
- } else {
- cssDeferred.resolve();
- }
- }, params);
- promises.push(cssDeferred.promise);
- }
- if(templatesFiles.length > 0) {
- var templatesDeferred = $q.defer();
- templatesLoader(templatesFiles, function(err) {
- if(angular.isDefined(err) && templatesLoader.hasOwnProperty('ocLazyLoadLoader')) {
- $log.error(err);
- templatesDeferred.reject(err);
- } else {
- templatesDeferred.resolve();
- }
- }, params);
- promises.push(templatesDeferred.promise);
- }
- if(jsFiles.length > 0) {
- var jsDeferred = $q.defer();
- jsLoader(jsFiles, function(err) {
- if(angular.isDefined(err) && jsLoader.hasOwnProperty('ocLazyLoadLoader')) {
- $log.error(err);
- jsDeferred.reject(err);
- } else {
- jsDeferred.resolve();
- }
- }, params);
- promises.push(jsDeferred.promise);
- }
- if(params.serie && params.files.length > 0) {
- return $q.all(promises).then(function success() {
- return filesLoader(config, params);
- });
- } else {
- return $q.all(promises).finally(function(res) {
- recordDeclarations.pop(); // stop watching angular.module calls
- return res;
- });
- }
- };
- return {
- /**
- * Let you get a module config object
- * @param moduleName String the name of the module
- * @returns {*}
- */
- getModuleConfig: function(moduleName) {
- if(!angular.isString(moduleName)) {
- throw new Error('You need to give the name of the module to get');
- }
- if(!modules[moduleName]) {
- return null;
- }
- return modules[moduleName];
- },
- /**
- * Let you define a module config object
- * @param moduleConfig Object the module config object
- * @returns {*}
- */
- setModuleConfig: function(moduleConfig) {
- if(!angular.isObject(moduleConfig)) {
- throw new Error('You need to give the module config object to set');
- }
- modules[moduleConfig.name] = moduleConfig;
- return moduleConfig;
- },
- /**
- * Returns the list of loaded modules
- * @returns {string[]}
- */
- getModules: function() {
- return regModules;
- },
- /**
- * Let you check if a module has been loaded into Angular or not
- * @param modulesNames String/Object a module name, or a list of module names
- * @returns {boolean}
- */
- isLoaded: function(modulesNames) {
- var moduleLoaded = function(module) {
- var isLoaded = regModules.indexOf(module) > -1;
- if(!isLoaded) {
- isLoaded = !!moduleExists(module);
- }
- return isLoaded;
- };
- if(angular.isString(modulesNames)) {
- modulesNames = [modulesNames];
- }
- if(angular.isArray(modulesNames)) {
- var i, len;
- for(i = 0, len = modulesNames.length; i < len; i++) {
- if(!moduleLoaded(modulesNames[i])) {
- return false;
- }
- }
- return true;
- } else {
- throw new Error('You need to define the module(s) name(s)');
- }
- },
- /**
- * Load a module or a list of modules into Angular
- * @param module Mixed the name of a predefined module config object, or a module config object, or an array of either
- * @param params Object optional parameters
- * @returns promise
- */
- load: function(module, params) {
- var self = this,
- config = null,
- moduleCache = [],
- deferredList = [],
- deferred = $q.defer(),
- errText;
- if(angular.isUndefined(params)) {
- params = {};
- }
- // If module is an array, break it down
- if(angular.isArray(module)) {
- // Resubmit each entry as a single module
- angular.forEach(module, function(m) {
- if(m) {
- deferredList.push(self.load(m, params));
- }
- });
- // Resolve the promise once everything has loaded
- $q.all(deferredList).then(function success() {
- deferred.resolve(module);
- }, function error(err) {
- deferred.reject(err);
- });
- return deferred.promise;
- }
- // Get or Set a configuration depending on what was passed in
- if(typeof module === 'string') {
- config = self.getModuleConfig(module);
- if(!config) {
- config = {
- files: [module]
- };
- }
- } else if(typeof module === 'object') {
- if(angular.isDefined(module.path) && angular.isDefined(module.type)) { // case {type: 'js', path: lazyLoadUrl + 'testModule.fakejs'}
- config = {
- files: [module]
- };
- } else {
- config = self.setModuleConfig(module);
- }
- }
- if(config === null) {
- var moduleName = getModuleName(module);
- errText = 'Module "' + (moduleName || 'unknown') + '" is not configured, cannot load.';
- $log.error(errText);
- deferred.reject(new Error(errText));
- return deferred.promise;
- } else {
- // deprecated
- if(angular.isDefined(config.template)) {
- if(angular.isUndefined(config.files)) {
- config.files = [];
- }
- if(angular.isString(config.template)) {
- config.files.push(config.template);
- } else if(angular.isArray(config.template)) {
- config.files.concat(config.template);
- }
- }
- }
- moduleCache.push = function(value) {
- if(this.indexOf(value) === -1) {
- Array.prototype.push.apply(this, arguments);
- }
- };
- var localParams = {};
- angular.extend(localParams, params, config);
- var loadDependencies = function loadDependencies(module) {
- var moduleName,
- loadedModule,
- requires,
- diff,
- promisesList = [];
- moduleName = getModuleName(module);
- if(moduleName === null) {
- return $q.when();
- } else {
- try {
- loadedModule = getModule(moduleName);
- } catch(e) {
- var deferred = $q.defer();
- $log.error(e.message);
- deferred.reject(e);
- return deferred.promise;
- }
- requires = getRequires(loadedModule);
- }
- angular.forEach(requires, function(requireEntry) {
- // If no configuration is provided, try and find one from a previous load.
- // If there isn't one, bail and let the normal flow run
- if(typeof requireEntry === 'string') {
- var config = self.getModuleConfig(requireEntry);
- if(config === null) {
- moduleCache.push(requireEntry); // We don't know about this module, but something else might, so push it anyway.
- return;
- }
- requireEntry = config;
- }
- // Check if this dependency has been loaded previously
- if(moduleExists(requireEntry.name)) {
- if(typeof module !== 'string') {
- // compare against the already loaded module to see if the new definition adds any new files
- diff = requireEntry.files.filter(function(n) {
- return self.getModuleConfig(requireEntry.name).files.indexOf(n) < 0;
- });
- // If the module was redefined, advise via the console
- if(diff.length !== 0) {
- $log.warn('Module "', moduleName, '" attempted to redefine configuration for dependency. "', requireEntry.name, '"\n Additional Files Loaded:', diff);
- }
- // Push everything to the file loader, it will weed out the duplicates.
- promisesList.push(filesLoader(requireEntry.files, localParams).then(function() {
- return loadDependencies(requireEntry);
- }));
- }
- return;
- } else if(angular.isArray(requireEntry)) {
- requireEntry = {
- files: requireEntry
- };
- } else if(typeof requireEntry === 'object') {
- if(requireEntry.hasOwnProperty('name') && requireEntry['name']) {
- // The dependency doesn't exist in the module cache and is a new configuration, so store and push it.
- self.setModuleConfig(requireEntry);
- moduleCache.push(requireEntry['name']);
- }
- }
- // 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, localParams).then(function() {
- return loadDependencies(requireEntry);
- }));
- }
- }
- });
- // Create a wrapper promise to watch the promise list and resolve it once everything is done.
- return $q.all(promisesList);
- };
- // if someone loaded the module file with something else and called the load function with just the module name
- if(angular.isUndefined(config.files) && angular.isDefined(config.name) && moduleExists(config.name)) {
- recordDeclarations.push(true); // start watching angular.module calls
- addToLoadList(config.name);
- recordDeclarations.pop();
- }
- filesLoader(config, localParams).then(function success() {
- if(modulesToLoad.length === 0) {
- deferred.resolve(module);
- } else {
- var loadNext = function loadNext(moduleName) {
- moduleCache.push(moduleName);
- loadDependencies(moduleName).then(function success() {
- try {
- justLoaded = [];
- register(providers, moduleCache, localParams);
- } catch(e) {
- $log.error(e.message);
- deferred.reject(e);
- return;
- }
- if(modulesToLoad.length > 0) {
- loadNext(modulesToLoad.shift()); // load the next in list
- } else {
- deferred.resolve(module); // everything has been loaded, resolve
- }
- }, function error(err) {
- deferred.reject(err);
- });
- };
- // load the first in list
- loadNext(modulesToLoad.shift());
- }
- }, function error(err) {
- deferred.reject(err);
- });
- return deferred.promise;
- }
- };
- }];
- this.config = function(config) {
- if(angular.isDefined(config.jsLoader) || angular.isDefined(config.asyncLoader)) {
- if(!angular.isFunction(config.jsLoader || config.asyncLoader)) {
- throw('The js loader needs to be a function');
- }
- jsLoader = config.jsLoader || config.asyncLoader;
- }
- if(angular.isDefined(config.cssLoader)) {
- if(!angular.isFunction(config.cssLoader)) {
- throw('The css loader needs to be a function');
- }
- cssLoader = config.cssLoader;
- }
- if(angular.isDefined(config.templatesLoader)) {
- if(!angular.isFunction(config.templatesLoader)) {
- throw('The template loader needs to be a function');
- }
- templatesLoader = config.templatesLoader;
- }
- // If we want to define modules configs
- if(angular.isDefined(config.modules)) {
- if(angular.isArray(config.modules)) {
- angular.forEach(config.modules, function(moduleConfig) {
- modules[moduleConfig.name] = moduleConfig;
- });
- } else {
- modules[config.modules.name] = config.modules;
- }
- }
- if(angular.isDefined(config.debug)) {
- debug = config.debug;
- }
- if(angular.isDefined(config.events)) {
- events = config.events;
- }
- };
- }]);
- ocLazyLoad.directive('ocLazyLoad', ['$ocLazyLoad', '$compile', '$animate', '$parse',
- function($ocLazyLoad, $compile, $animate, $parse) {
- return {
- restrict: 'A',
- terminal: true,
- priority: 1000,
- compile: function(element, attrs) {
- // we store the content and remove it before compilation
- var content = element[0].innerHTML;
- element.html('');
- return function($scope, $element, $attr) {
- var model = $parse($attr.ocLazyLoad);
- $scope.$watch(function() {
- // it can be a module name (string), an object, an array, or a scope reference to any of this
- return model($scope) || $attr.ocLazyLoad;
- }, function(moduleName) {
- if(angular.isDefined(moduleName)) {
- $ocLazyLoad.load(moduleName).then(function success(moduleConfig) {
- $animate.enter($compile(content)($scope), null, $element);
- });
- }
- }, true);
- };
- }
- };
- }]);
- /**
- * Get the list of required modules/services/... for this module
- * @param module
- * @returns {Array}
- */
- function getRequires(module) {
- var requires = [];
- angular.forEach(module.requires, function(requireModule) {
- if(regModules.indexOf(requireModule) === -1) {
- requires.push(requireModule);
- }
- });
- return requires;
- }
- /**
- * Check if a module exists and returns it if it does
- * @param moduleName
- * @returns {boolean}
- */
- function moduleExists(moduleName) {
- if(!angular.isString(moduleName)) {
- return false;
- }
- try {
- return ngModuleFct(moduleName);
- } catch(e) {
- if(/No module/.test(e) || (e.message.indexOf('$injector:nomod') > -1)) {
- return false;
- }
- }
- }
- function getModule(moduleName) {
- try {
- return ngModuleFct(moduleName);
- } catch(e) {
- // this error message really suxx
- if(/No module/.test(e) || (e.message.indexOf('$injector:nomod') > -1)) {
- e.message = 'The module "' + moduleName + '" that you are trying to load does not exist. ' + e.message
- }
- throw e;
- }
- }
- function invokeQueue(providers, queue, moduleName, reconfig) {
- if(!queue) {
- return;
- }
- var i, len, args, provider;
- for(i = 0, len = queue.length; i < len; i++) {
- args = queue[i];
- if(angular.isArray(args)) {
- if(providers !== null) {
- if(providers.hasOwnProperty(args[0])) {
- provider = providers[args[0]];
- } else {
- throw new Error('unsupported provider ' + args[0]);
- }
- }
- var isNew = registerInvokeList(args, moduleName);
- if(args[1] !== 'invoke') {
- if(isNew && angular.isDefined(provider)) {
- provider[args[1]].apply(provider, args[2]);
- }
- } else { // config block
- var callInvoke = function(fct) {
- var invoked = regConfigs.indexOf(moduleName + '-' + fct);
- if(invoked === -1 || reconfig) {
- if(invoked === -1) {
- regConfigs.push(moduleName + '-' + fct);
- }
- if(angular.isDefined(provider)) {
- provider[args[1]].apply(provider, args[2]);
- }
- }
- };
- if(angular.isFunction(args[2][0])) {
- callInvoke(args[2][0]);
- } else if(angular.isArray(args[2][0])) {
- for(var j = 0, jlen = args[2][0].length; j < jlen; j++) {
- if(angular.isFunction(args[2][0][j])) {
- callInvoke(args[2][0][j]);
- }
- }
- }
- }
- }
- }
- }
- /**
- * Register a new module and load it
- * @param providers
- * @param registerModules
- * @returns {*}
- */
- function register(providers, registerModules, params) {
- if(registerModules) {
- var k, r, moduleName, moduleFn, tempRunBlocks = [];
- for(k = registerModules.length - 1; k >= 0; k--) {
- moduleName = registerModules[k];
- if(typeof moduleName !== 'string') {
- moduleName = getModuleName(moduleName);
- }
- if(!moduleName || justLoaded.indexOf(moduleName) !== -1) {
- continue;
- }
- var newModule = regModules.indexOf(moduleName) === -1;
- moduleFn = ngModuleFct(moduleName);
- if(newModule) { // new module
- regModules.push(moduleName);
- register(providers, moduleFn.requires, params);
- }
- if(moduleFn._runBlocks.length > 0) {
- // new run blocks detected! Replace the old ones (if existing)
- runBlocks[moduleName] = [];
- while(moduleFn._runBlocks.length > 0) {
- runBlocks[moduleName].push(moduleFn._runBlocks.shift());
- }
- }
- if(angular.isDefined(runBlocks[moduleName]) && (newModule || params.rerun)) {
- tempRunBlocks = tempRunBlocks.concat(runBlocks[moduleName]);
- }
- invokeQueue(providers, moduleFn._invokeQueue, moduleName, params.reconfig);
- invokeQueue(providers, moduleFn._configBlocks, moduleName, params.reconfig); // angular 1.3+
- broadcast(newModule ? 'ocLazyLoad.moduleLoaded' : 'ocLazyLoad.moduleReloaded', moduleName);
- registerModules.pop();
- justLoaded.push(moduleName);
- }
- // execute the run blocks at the end
- var instanceInjector = providers.getInstanceInjector();
- angular.forEach(tempRunBlocks, function(fn) {
- instanceInjector.invoke(fn);
- });
- }
- }
- /**
- * Register an invoke
- * @param args
- * @param moduleName
- * @returns {boolean}
- */
- function registerInvokeList(args, moduleName) {
- var invokeList = args[2][0],
- type = args[1],
- newInvoke = false;
- if(angular.isUndefined(regInvokes[moduleName])) {
- regInvokes[moduleName] = {};
- }
- if(angular.isUndefined(regInvokes[moduleName][type])) {
- regInvokes[moduleName][type] = {};
- }
- var onInvoke = function(invokeName, signature) {
- newInvoke = true;
- regInvokes[moduleName][type][invokeName].push(signature);
- broadcast('ocLazyLoad.componentLoaded', [moduleName, type, invokeName]);
- };
- var signature = function signature(data) {
- if(angular.isArray(data)) { // arrays are objects, we need to test for it first
- return hashCode(data.toString());
- } else if(angular.isObject(data)) { // constants & values for example
- return hashCode(stringify(data));
- } else {
- if(angular.isDefined(data) && data !== null) {
- return hashCode(data.toString());
- } else { // null & undefined constants
- return data;
- }
- }
- };
- if(angular.isString(invokeList)) {
- if(angular.isUndefined(regInvokes[moduleName][type][invokeList])) {
- regInvokes[moduleName][type][invokeList] = [];
- }
- if(regInvokes[moduleName][type][invokeList].indexOf(signature(args[2][1])) === -1) {
- onInvoke(invokeList, signature(args[2][1]));
- }
- } else if(angular.isObject(invokeList)) { // decorators for example
- angular.forEach(invokeList, function(invoke) {
- if(angular.isString(invoke)) {
- if(angular.isUndefined(regInvokes[moduleName][type][invoke])) {
- regInvokes[moduleName][type][invoke] = [];
- }
- if(regInvokes[moduleName][type][invoke].indexOf(signature(invokeList[1])) === -1) {
- onInvoke(invoke, signature(invokeList[1]));
- }
- }
- });
- } else {
- return false;
- }
- return newInvoke;
- }
- function getModuleName(module) {
- var moduleName = null;
- if(angular.isString(module)) {
- moduleName = module;
- } else if(angular.isObject(module) && module.hasOwnProperty('name') && angular.isString(module.name)) {
- moduleName = module.name;
- }
- return moduleName;
- }
- /**
- * Get the list of existing registered modules
- * @param element
- */
- function init(element) {
- if(modulesToLoad.length === 0) {
- var elements = [element],
- names = ['ng:app', 'ng-app', 'x-ng-app', 'data-ng-app'],
- NG_APP_CLASS_REGEXP = /\sng[:\-]app(:\s*([\w\d_]+);?)?\s/,
- append = function append(elm) {
- return (elm && elements.push(elm));
- };
- angular.forEach(names, function(name) {
- names[name] = true;
- append(document.getElementById(name));
- name = name.replace(':', '\\:');
- if(element[0].querySelectorAll) {
- angular.forEach(element[0].querySelectorAll('.' + name), append);
- angular.forEach(element[0].querySelectorAll('.' + name + '\\:'), append);
- angular.forEach(element[0].querySelectorAll('[' + name + ']'), append);
- }
- });
- angular.forEach(elements, function(elm) {
- if(modulesToLoad.length === 0) {
- var className = ' ' + element.className + ' ';
- var match = NG_APP_CLASS_REGEXP.exec(className);
- if(match) {
- modulesToLoad.push((match[2] || '').replace(/\s+/g, ','));
- } else {
- angular.forEach(elm.attributes, function(attr) {
- if(modulesToLoad.length === 0 && names[attr.name]) {
- modulesToLoad.push(attr.value);
- }
- });
- }
- }
- });
- }
- if(modulesToLoad.length === 0 && !((window.jasmine || window.mocha) && angular.isDefined(angular.mock))) {
- 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.');
- }
- var addReg = function addReg(moduleName) {
- if(regModules.indexOf(moduleName) === -1) {
- // register existing modules
- regModules.push(moduleName);
- var mainModule = angular.module(moduleName);
- // register existing components (directives, services, ...)
- invokeQueue(null, mainModule._invokeQueue, moduleName);
- invokeQueue(null, mainModule._configBlocks, moduleName); // angular 1.3+
- angular.forEach(mainModule.requires, addReg);
- }
- };
- angular.forEach(modulesToLoad, function(moduleName) {
- addReg(moduleName);
- });
- modulesToLoad = []; // reset for next bootstrap
- recordDeclarations.pop(); // wait for the next lazy load
- }
- var bootstrapFct = angular.bootstrap;
- angular.bootstrap = function(element, modules, config) {
- // we use slice to make a clean copy
- angular.forEach(modules.slice(), function(module) {
- addToLoadList(module);
- });
- return bootstrapFct(element, modules, config);
- };
- var addToLoadList = function addToLoadList(name) {
- if(recordDeclarations.length > 0 && angular.isString(name) && modulesToLoad.indexOf(name) === -1) {
- modulesToLoad.push(name);
- }
- };
- var ngModuleFct = angular.module;
- angular.module = function(name, requires, configFn) {
- addToLoadList(name);
- return ngModuleFct(name, requires, configFn);
- };
- var hashCode = function hashCode(str) {
- var hash = 0, i, chr, len;
- if (str.length == 0) return hash;
- for (i = 0, len = str.length; i < len; i++) {
- chr = str.charCodeAt(i);
- hash = ((hash << 5) - hash) + chr;
- hash |= 0; // Convert to 32bit integer
- }
- return hash;
- };
- var stringify = function stringify(obj) {
- var cache = [];
- return JSON.stringify(obj, function(key, value) {
- if (typeof value === 'object' && value !== null) {
- if (cache.indexOf(value) !== -1) {
- // Circular reference found, discard key
- return;
- }
- // Store value in our collection
- cache.push(value);
- }
- return value;
- });
- };
- // Array.indexOf polyfill for IE8
- if(!Array.prototype.indexOf) {
- Array.prototype.indexOf = function(searchElement, fromIndex) {
- var k;
- // 1. Let O be the result of calling ToObject passing
- // the this value as the argument.
- if(this == null) {
- throw new TypeError('"this" is null or not defined');
- }
- var O = Object(this);
- // 2. Let lenValue be the result of calling the Get
- // internal method of O with the argument "length".
- // 3. Let len be ToUint32(lenValue).
- var len = O.length >>> 0;
- // 4. If len is 0, return -1.
- if(len === 0) {
- return -1;
- }
- // 5. If argument fromIndex was passed let n be
- // ToInteger(fromIndex); else let n be 0.
- var n = +fromIndex || 0;
- if(Math.abs(n) === Infinity) {
- n = 0;
- }
- // 6. If n >= len, return -1.
- if(n >= len) {
- return -1;
- }
- // 7. If n >= 0, then Let k be n.
- // 8. Else, n<0, Let k be len - abs(n).
- // If k is less than 0, then let k be 0.
- k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
- // 9. Repeat, while k < len
- while(k < len) {
- // a. Let Pk be ToString(k).
- // This is implicit for LHS operands of the in operator
- // b. Let kPresent be the result of calling the
- // HasProperty internal method of O with argument Pk.
- // This step can be combined with c
- // c. If kPresent is true, then
- // i. Let elementK be the result of calling the Get
- // internal method of O with the argument ToString(k).
- // ii. Let same be the result of applying the
- // Strict Equality Comparison Algorithm to
- // searchElement and elementK.
- // iii. If same is true, return k.
- if(k in O && O[k] === searchElement) {
- return k;
- }
- k++;
- }
- return -1;
- };
- }
- })();
|