Skip to content

Commit

Permalink
Updated the gulp build task to also copy the new lib to the example f…
Browse files Browse the repository at this point in the history
…olders
  • Loading branch information
ocombe committed May 20, 2014
1 parent 22beecf commit 3e19a0d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 37 deletions.
23 changes: 15 additions & 8 deletions examples/example1/js/ocLazyLoad.js
Original file line number Diff line number Diff line change
@@ -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 <olivier.combe@gmail.com>
*/
/**
* original copyright: Andy Grom (https://github.com/AndyGrom/loadOnDemand)
* rewrite by: Olivier Combe (https://github.com/ocombe/ocLazyLoad)
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down
128 changes: 99 additions & 29 deletions examples/example2/js/ocLazyLoad.js
Original file line number Diff line number Diff line change
@@ -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 <olivier.combe@gmail.com>
*/
/**
* original copyright: Andy Grom (https://github.com/AndyGrom/loadOnDemand)
* rewrite by: Olivier Combe (https://github.com/ocombe/ocLazyLoad)
Expand All @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -196,7 +257,7 @@
});

return deferred.promise;
}
}
};
}];

Expand Down Expand Up @@ -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
Expand All @@ -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') {
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}))
Expand Down

0 comments on commit 3e19a0d

Please sign in to comment.