From 3e19a0d1e1f8d9fb169e76c869b44c14968f9086 Mon Sep 17 00:00:00 2001 From: Olivier Combe Date: Tue, 20 May 2014 18:03:30 +0200 Subject: [PATCH] Updated the gulp build task to also copy the new lib to the example folders --- examples/example1/js/ocLazyLoad.js | 23 ++++-- examples/example2/js/ocLazyLoad.js | 128 ++++++++++++++++++++++------- gulpfile.js | 2 + 3 files changed, 116 insertions(+), 37 deletions(-) diff --git a/examples/example1/js/ocLazyLoad.js b/examples/example1/js/ocLazyLoad.js index ee17f53..f647fe9 100644 --- a/examples/example1/js/ocLazyLoad.js +++ b/examples/example1/js/ocLazyLoad.js @@ -1,3 +1,10 @@ +/** + * ocLazyLoad - Load modules on demand (lazy load) with angularJS + * @version v0.2.0 + * @link https://github.com/ocombe/ocLazyLoad + * @license MIT + * @author Olivier Combe + */ /** * original copyright: Andy Grom (https://github.com/AndyGrom/loadOnDemand) * rewrite by: Olivier Combe (https://github.com/ocombe/ocLazyLoad) @@ -114,13 +121,13 @@ }, load: function(module) { - var self = this, - config = null, - moduleCache = [], + var self = this, + config = null, + moduleCache = [], deferred_list = [], - deferred = $q.defer(), - moduleName, - errText; + deferred = $q.defer(), + moduleName, + errText; // If module is an array, break it down if (angular.isArray(module)) { @@ -202,9 +209,9 @@ } 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); + self.setModuleConfig(requireEntry); moduleCache.push(requireEntry['name']); - } + } // CSS Loading Handler if (requireEntry.hasOwnProperty('css') && requireEntry['css'].length !== 0) { diff --git a/examples/example2/js/ocLazyLoad.js b/examples/example2/js/ocLazyLoad.js index e54b8b5..f647fe9 100644 --- a/examples/example2/js/ocLazyLoad.js +++ b/examples/example2/js/ocLazyLoad.js @@ -1,3 +1,10 @@ +/** + * ocLazyLoad - Load modules on demand (lazy load) with angularJS + * @version v0.2.0 + * @link https://github.com/ocombe/ocLazyLoad + * @license MIT + * @author Olivier Combe + */ /** * original copyright: Andy Grom (https://github.com/AndyGrom/loadOnDemand) * rewrite by: Olivier Combe (https://github.com/ocombe/ocLazyLoad) @@ -9,8 +16,8 @@ var ocLazyLoad = angular.module('oc.lazyLoad', ['ng']); - ocLazyLoad.provider('$ocLazyLoad', ['$controllerProvider', '$provide', '$compileProvider', '$filterProvider', '$injector', - function($controllerProvider, $provide, $compileProvider, $filterProvider, $injector) { + ocLazyLoad.provider('$ocLazyLoad', ['$controllerProvider', '$provide', '$compileProvider', '$filterProvider', '$injector', '$animateProvider', + function($controllerProvider, $provide, $compileProvider, $filterProvider, $injector, $animateProvider) { var modules = {}, asyncLoader, @@ -20,8 +27,35 @@ $compileProvider: $compileProvider, $filterProvider: $filterProvider, $provide: $provide, // other things - $injector: $injector - }; + $injector: $injector, + $animateProvider: $animateProvider + }, + anchor = document.getElementsByTagName('head')[0]; + + if (anchor === null) { + anchor = document.getElementsByTagName('body')[0]; + } + + function buildElement(type, path) { + if (filesLoaded.indexOf(path) === -1) { + var el; + // Switch in case more content types are added later (for example a native JS loader!) + switch (type) { + case 'css': + el = document.createElement('link'); + el.type = 'text/css'; + el.rel = 'stylesheet'; + el.href = path; + break; + default: + throw ('Requested type "' + type + '" is not known. Could not inject "' + path + '"'); + break; + } + + filesLoaded.push(path); + anchor.insertBefore(el, anchor.lastChild); + } + } this.$get = ['$timeout', '$log', '$q', '$templateCache', '$http', '$rootElement', function($timeout, $log, $q, $templateCache, $http, $rootElement) { var instanceInjector; @@ -90,10 +124,26 @@ var self = this, config = null, moduleCache = [], + deferred_list = [], deferred = $q.defer(), moduleName, errText; + // If module is an array, break it down + if (angular.isArray(module)) { + // Resubmit each entry as a single module + angular.forEach(module, function (m) { + deferred_list.push(self.load(m)); + }); + + // Resolve the promise once everything has loaded + $q.all(deferred_list).then(function() { + deferred.resolve(); + }); + + return deferred.promise; + } + moduleName = self.getModuleName(module); // If this module has been loaded before, re-use it. @@ -157,10 +207,21 @@ } return; } else if (typeof requireEntry === 'object') { - // The dependency doesn't exist in the module cache and is a new conifguration, so store and push it. - self.setModuleConfig(requireEntry); - moduleCache.push(requireEntry.name); - } + 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']); + } + + // CSS Loading Handler + if (requireEntry.hasOwnProperty('css') && requireEntry['css'].length !== 0) { + // Locate the document insertion point + angular.forEach(requireEntry['css'], function (path) { + buildElement('css', path); + }); + } + // CSS End. + } // 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) { @@ -196,7 +257,7 @@ }); return deferred.promise; - } + } }; }]; @@ -326,6 +387,30 @@ return true; } + function invokeQueue (providers, queue, $log) { + if (!queue) { + return; + } + + var i, ii, args, provider; + try { + for(i = 0, ii = queue.length; i < ii; i++) { + args = queue[i]; + if (angular.isArray(args)) { + if(providers.hasOwnProperty(args[0])) { + provider = providers[args[0]]; + } else { + return $log.error('unsupported provider ' + args[0]); + } + provider[args[1]].apply(provider, args[2]); + } + } + } catch(e) { + $log.error(e.message); + throw e; + } + } + /** * Register a new module and load it * @param providers @@ -335,7 +420,7 @@ */ function register(providers, registerModules, $log) { if(registerModules) { - var i, ii, k, invokeQueue, moduleName, moduleFn, invokeArgs, provider, runBlocks = []; + var k, moduleName, moduleFn, runBlocks = []; for(k = registerModules.length - 1; k >= 0; k--) { moduleName = registerModules[k]; if (typeof moduleName !== 'string') { @@ -348,25 +433,10 @@ moduleFn = angular.module(moduleName); register(providers, moduleFn.requires, $log); runBlocks = runBlocks.concat(moduleFn._runBlocks); - try { - for(invokeQueue = moduleFn._invokeQueue, i = 0, ii = invokeQueue.length; i < ii; i++) { - invokeArgs = invokeQueue[i]; - if (angular.isArray(invokeArgs)) { - if(providers.hasOwnProperty(invokeArgs[0])) { - provider = providers[invokeArgs[0]]; - } else { - return $log.error('unsupported provider ' + invokeArgs[0]); - } - provider[invokeArgs[1]].apply(provider, invokeArgs[2]); - } - } - } catch(e) { - if(e.message) { - e.message += ' from ' + moduleName; - } - $log.error(e.message); - throw e; - } + + invokeQueue(providers, moduleFn._invokeQueue, $log); + invokeQueue(providers, moduleFn._configBlocks, $log); + registerModules.pop(); } var instanceInjector = providers.getInstanceInjector(); diff --git a/gulpfile.js b/gulpfile.js index cfcd971..d52c75f 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -17,6 +17,8 @@ gulp.task('build', function() { return gulp.src('src/ocLazyLoad.js') .pipe(header(banner, { pkg : pkg } )) .pipe(gulp.dest('dist')) + .pipe(gulp.dest('examples/example1/js/')) + .pipe(gulp.dest('examples/example2/js/')) .pipe(uglify()) .pipe(header(banner, { pkg : pkg } )) .pipe(rename({suffix: '.min'}))