Skip to content

Instantly share code, notes, and snippets.

@toutpt
Last active March 21, 2016 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toutpt/fae0a65dc41260114048 to your computer and use it in GitHub Desktop.
Save toutpt/fae0a65dc41260114048 to your computer and use it in GitHub Desktop.
angular require extra libs
(function () {
'use strict';
/*global _:false */
/*global angular:false */
/*global SWIF_RESOURCES_VERSION:false */
/*jshint latedef:false */
/* config.json file available throw settings.app:
"resources": {
"scripts": {
"fullcalendar": ["libs-fullcalendar.min.js"],
"tinymce": [
"tinymce/tinymce.js",
"tinymce/tinymce.js",
"tinymce/langs/fr_FR.js",
"tinymce/themes/modern/theme.js",
"tinymce/plugins/textcolor/plugin.js",
"tinymce/plugins/code/plugin.js"
],
"leaflet": ["libs-leaflet.min.js"],
"d3": ["libs-d3.min.js"],
"ical": ["libs-ical.min.js"]
}
}
*/
angular.module('swif.framework')
.factory('swifResourcesService', swifResourcesService);
/*@ngInject*/
function swifResourcesService ($q, settings) {
var service = {};
service.scripts = [];
service.styles = [];
service.resources = [];
service.add = function (resources) {
var promises = [];
_.forEach(resources, function (resource) {
if (!_.contains(service.resources, resource)) {
service.resources.push(resource);
promises.push(service._add(resource));
}
});
return $q.all(promises);
};
service._add = function (resource, deferred) {
if (!service._scripts) {
service._scripts = settings.app.resources.scripts;
}
if (!_.isEmpty(service._scripts[resource])) {
//include next only as browser behavior
if (!deferred) {
deferred = $q.defer();
}
var script = document.createElement('script');
var src = service._scripts[resource].shift();
src += '?' + SWIF_RESOURCES_VERSION;
script.onload = function() {
if (!_.isEmpty(service._scripts[resource])) {
service._add(resource, deferred);
} else {
deferred.resolve(true);
}
};
script.src = src;
document.getElementById('resources').appendChild(script);
return deferred.promise;
}
return $q.when(true);
};
return service;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment