1 2/** 3 * 0.1.1 4 * Deferred load js/css file, used for ui-jq.js and Lazy Loading. 5 * 6 * @ flatfull.com All Rights Reserved. 7 * Author url: http://themeforest.net/user/flatfull 8 */ 9var uiLoad = uiLoad || {}; 10 11(function($, $document, uiLoad) { 12 "use strict"; 13 14 var loaded = [], 15 promise = false, 16 deferred = $.Deferred(); 17 18 /** 19 * Chain loads the given sources 20 * @param srcs array, script or css 21 * @returns {*} Promise that will be resolved once the sources has been loaded. 22 */ 23 uiLoad.load = function (srcs) { 24 srcs = $.isArray(srcs) ? srcs : srcs.split(/\s+/); 25 if(!promise){ 26 promise = deferred.promise(); 27 } 28 29 $.each(srcs, function(index, src) { 30 promise = promise.then( function(){ 31 return loaded[src] ? loaded[src].promise() : (src.indexOf('.css') >=0 ? loadCSS(src) : loadScript(src)); 32 } ); 33 }); 34 deferred.resolve(); 35 return promise; 36 }; 37 38 uiLoad.remove = function(srcs){ 39 srcs = $.isArray(srcs) ? srcs : srcs.split(/\s+/); 40 $.each(srcs, function(index, src) { 41 src.indexOf('.css') >=0 ? $('link[href="'+src+'"]').remove() : $('script[src="'+src+'"]').remove(); 42 delete loaded[src]; 43 }); 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 var loadScript = function (src) { 52 var deferred = $.Deferred(); 53 var script = $document.createElement('script'); 54 script.src = src; 55 script.onload = function (e) { 56 deferred.resolve(e); 57 }; 58 script.onerror = function (e) { 59 deferred.reject(e); 60 }; 61 $document.body.appendChild(script); 62 loaded[src] = deferred; 63 64 return deferred.promise(); 65 }; 66 67 /** 68 * Dynamically loads the given CSS file 69 * @param href The url of the CSS to load dynamically 70 * @returns {*} Promise that will be resolved once the CSS file has been loaded. 71 */ 72 var loadCSS = function (href) { 73 var deferred = $.Deferred(); 74 var style = $document.createElement('link'); 75 style.rel = 'stylesheet'; 76 style.type = 'text/css'; 77 style.href = href; 78 style.onload = function (e) { 79 deferred.resolve(e); 80 }; 81 style.onerror = function (e) { 82 deferred.reject(e); 83 }; 84 $document.head.appendChild(style); 85 loaded[href] = deferred; 86 87 return deferred.promise(); 88 }; 89 90})(jQuery, document, uiLoad);
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.