1'use strict'; 2 3/** 4 * 0.1.1 5 * Deferred load js/css file, used for ui-jq.js and Lazy Loading. 6 * 7 * @ flatfull.com All Rights Reserved. 8 * Author url: http://themeforest.net/user/flatfull 9 */ 10 11angular.module('ui.load', []) 12 .service('uiLoad', ['$document', '$q', '$timeout', function ($document, $q, $timeout) { 13 14 var loaded = []; 15 var loadedByKey = []; 16 var promise = false; 17 var deferred = $q.defer(); 18 function cacheKey(url) { 19 if (!url) return ''; 20 return String(url) 21 .replace(/([?&])_=\d+/g, '$1') 22 .replace(/[?&]$/, '') 23 .replace(/#.*$/, ''); 24 } 25 26 /** 27 * Chain loads the given sources 28 * @param srcs array, script or css 29 * @returns {*} Promise that will be resolved once the sources has been loaded. 30 */ 31 this.load = function (srcs) { 32 srcs = angular.isArray(srcs) ? srcs : srcs.split(/\s+/); 33 var self = this; 34 if(!promise){ 35 promise = deferred.promise; 36 } 37 angular.forEach(srcs, function(src) { 38 promise = promise.then( function(){ 39 return src.indexOf('.css') >=0 ? self.loadCSS(src) : self.loadScript(src); 40 } ); 41 }); 42 deferred.resolve(); 43 return promise; 44 } 45 46 /** 47 * Dynamically loads the given script 48 * @param src The url of the script to load dynamically 49 * @returns {*} Promise that will be resolved once the script has been loaded. 50 */ 51 this.loadScript = function (src) { 52 var key = cacheKey(src); 53 if(loaded[src]) return loaded[src].promise; 54 if(key && loadedByKey[key]) return loadedByKey[key].promise; 55 // Evitar recarregar o core do Angular: quando isto acontece, 56 // o browser mostra "Tried to load angular more than once". 57 if (/(^|\/)angular(\.min)?\.js$/i.test(key) && window.angular && window.angular.module) { 58 var skipDeferred = $q.defer(); 59 $timeout(function () { skipDeferred.resolve(); }); 60 loaded[src] = skipDeferred; 61 if (key) loadedByKey[key] = skipDeferred; 62 return skipDeferred.promise; 63 } 64 65 var deferred = $q.defer(); 66 var script = $document[0].createElement('script'); 67 script.src = src; 68 script.onload = function (e) { 69 $timeout(function () { 70 deferred.resolve(e); 71 }); 72 }; 73 script.onerror = function (e) { 74 $timeout(function () { 75 deferred.reject(e); 76 }); 77 }; 78 $document[0].body.appendChild(script); 79 loaded[src] = deferred; 80 if (key) loadedByKey[key] = deferred; 81 82 return deferred.promise; 83 }; 84 85 /** 86 * Dynamically loads the given CSS file 87 * @param href The url of the CSS to load dynamically 88 * @returns {*} Promise that will be resolved once the CSS file has been loaded. 89 */ 90 this.loadCSS = function (href) { 91 var key = cacheKey(href); 92 if(loaded[href]) return loaded[href].promise; 93 if(key && loadedByKey[key]) return loadedByKey[key].promise; 94 95 var deferred = $q.defer(); 96 var style = $document[0].createElement('link'); 97 style.rel = 'stylesheet'; 98 style.type = 'text/css'; 99 style.href = href; 100 style.onload = function (e) { 101 $timeout(function () { 102 deferred.resolve(e); 103 }); 104 }; 105 style.onerror = function (e) { 106 $timeout(function () { 107 deferred.reject(e); 108 }); 109 }; 110 $document[0].head.appendChild(style); 111 loaded[href] = deferred; 112 if (key) loadedByKey[key] = deferred; 113 114 return deferred.promise; 115 }; 116}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.