1/** 2 * Created by user on 29-May-19. 3 */ 4/** 5 * Copyright (c) 2017 Boris Seang 6 * Load JS CSS 7 * ISC License 8 */ 9// Adapted from Rollup 10(function (global, factory) { 11 typeof exports === 'object' && typeof module !== 'undefined' 12 ? factory(exports) 13 : typeof define === 'function' && define.amd 14 ? define(['exports'], factory) 15 : (factory((global.loadJsCss = {}))); 16}(this, (function (exports) { 17 'use strict'; 18 19 /** 20 * Load the list of resources. 21 * ResourceSpec is a JsAssetSpec or a CssAssetSpec with expected following extra key(s): 22 * - type <'script'|'stylesheet'> string specifying whether the asset is a (CSS) stylesheet or a (JS) script. 23 * OptionsDict is a Dictionary with possible following key(s): 24 * - delayScripts <Number|false> duration after which scripts should be loaded (after stylesheets), typically when 25 * script execution depends on stylesheet being already applied. 26 * @param resources <ResourceSpec[]> 27 * @param options <OptionsDict> 28 */ 29 function loadList(resources, options) { 30 options = options || {}; 31 32 var i = 0, 33 delayScripts = options.delayScripts || false, 34 scripts = [], 35 resource; 36 37 for (; i < resources.length; i += 1) { 38 resource = resources[i]; 39 40 switch (resource.type && resource.type.toLowerCase()) { 41 case 'js': 42 case 'javascript': 43 case 'script': 44 if (delayScripts === false) { 45 loadJs(resource); 46 } else { 47 scripts.push(resource); 48 } 49 break; 50 case 'css': 51 case 'stylesheet': 52 loadCss(resource); 53 break; 54 default: 55 console.error('Could not determine type of resource: ' + JSON.stringify(resource)); 56 } 57 } 58 59 if (scripts.length) { 60 setTimeout(function () { 61 options.delayScripts = false; 62 loadList(scripts, options); 63 }, delayScripts); 64 } 65 } 66 67 /** 68 * Load a (JavaScript) script asset. 69 * options is a JsAssetSpec. 70 * JsAssetSpec is an Object with expected following keys: 71 * - src <String> URL where to fetch the asset from. 72 * - async? <Boolean> if true, the script will be executed in any order; if false, it will be executed in insertion order. 73 * - attrs? <AttributesDict> Dictionary of attributes (e.g. to specify SRI - integrity and crossorigin). 74 * Adapted from load-script (https://github.com/eldargab/load-script, MIT License). 75 * @param options <JsAssetSpec> 76 */ 77 function loadJs(options) { 78 var script = document.createElement('script'), 79 src = options.src || options.path || options.href, 80 cb = options.callback; 81 82 // Use async=false by default to emulate defer and get a more predictable behaviour (i.e. scripts executed in order). 83 script.async = !!options.async || false; 84 script.src = src; 85 86 // options.attrs can be used for SRI for example (integrity, crossorigin). 87 if (options.attrs) { 88 _setAttributes(script, options.attrs) 89 } 90 91 if (typeof cb === 'function') { 92 var onEnd = 'onload' in script ? _stdOnEnd : _ieOnEnd; 93 94 onEnd(script, cb); 95 96 // some good legacy browsers (firefox) fail the 'in' detection above 97 // so as a fallback we always set onload 98 // old IE will ignore this and new IE will set onload 99 if (!script.onload) { 100 _stdOnEnd(script, cb); 101 } 102 } 103 104 _getHead().appendChild(script); 105 } 106 107 /** 108 * Load a (CSS) stylesheet asset. 109 * options is a CssAssetSpec. 110 * CssAssetSpec is an Object with expected following keys: 111 * - href <String> URL where to fetch the asset from. 112 * - attrs? <AttributesDict> Dictionary of attributes (e.g. to specify SRI - integrity and crossorigin). 113 * @param options <CssAssetSpec> 114 */ 115 function loadCss(options) { 116 var link = document.createElement('link'), 117 href = options.href || options.path || options.src, 118 cb = options.callback; 119 120 link.rel = 'stylesheet'; 121 link.href = href; 122 123 // options.attrs can be used for SRI for example (integrity, crossorigin). 124 if (options.attrs) { 125 _setAttributes(link, options.attrs) 126 } 127 128 _getHead().appendChild(link); 129 130 // Workaround the absence of error event on <link> resources. 131 if (typeof cb === 'function') { 132 var img = document.createElement('img'); 133 134 img.onerror = function (e) { 135 cb(e, link); 136 }; 137 img.onload = function () { 138 cb(null, link); 139 }; 140 img.src = href; 141 } 142 } 143 144 function _setAttributes(element, attributes) { 145 for (var attributeName in attributes) { 146 if (attributes.hasOwnProperty(attributeName)) { 147 element.setAttribute(attributeName, attributes[attributeName]); 148 } 149 } 150 } 151 152 153 // Adapted from load-script 154 function _stdOnEnd (script, cb) { 155 script.onload = function () { 156 this.onerror = this.onload = null; 157 cb(null, script) 158 }; 159 script.onerror = function () { 160 // this.onload = null here is necessary 161 // because even IE9 works not like others 162 this.onerror = this.onload = null;
163 cb(new Error('Failed to load ' + this.src), script) 164 } 165 } 166 167 // Adapted from load-script 168 function _ieOnEnd (script, cb) { 169 script.onreadystatechange = function () { 170 if (this.readyState !== 'complete' && this.readyState !== 'loaded') { 171 return; 172 } 173 this.onreadystatechange = null; 174 cb(null, script) // there is no way to catch loading errors in IE8 175 } 176 } 177 178 function _getHead() { 179 // This library assumes it is loaded in a browser, hence `document` is defined. 180 return document.head || document.getElementsByTagName('head')[0]; 181 } 182 183 184 // Exports 185 exports.js = loadJs; 186 exports.css = loadCss; 187 exports.list = loadList; 188})));
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.