1export class DependencyLoader { 2 loadModules(modules) { 3 return this.__load(modules, '__loadModule'); 4 } 5 6 loadScripts(scripts) { 7 return this.__load(scripts, '__loadScript'); 8 } 9 10 loadModularScripts(scripts) { 11 return this.__load(scripts, '__loadModularScript'); 12 } 13 14 loadStyles(styles) { 15 return this.__load(styles, '__loadStyle'); 16 } 17 18 __load(items, loaderFunctionName) { 19 if (typeof items == 'string') items = [items]; 20 21 let promises = []; 22 23 for (let item of items) { 24 if (typeof item == 'string') { 25 // Chargement de script simple 26 promises.push(this[loaderFunctionName](item)); 27 } else { 28 // Chargement de script(s) sur plusieurs niveaux 29 let toLoadPromise = new Promise((resolve, reject) => { 30 this['__load'](item.load, loaderFunctionName).then((mainOutput) => { 31 if ('then' in item) { 32 this['__load'](item.then, loaderFunctionName).then((thenOutputs) => { 33 resolve(this.__mergeOutputs([].concat(mainOutput, thenOutputs), loaderFunctionName)); 34 }); 35 } else { 36 resolve(mainOutput); 37 } 38 }) 39 }); 40 41 promises.push(toLoadPromise); 42 } 43 } 44 45 return new Promise((resolve, reject) => { 46 Promise.all(promises) 47 .then((outputs) => { 48 resolve(this.__mergeOutputs(outputs, loaderFunctionName)); 49 }) 50 .catch((e) => reject(e)); 51 }); 52 } 53 54 __mergeOutputs(outputs, loaderFunctionName) { 55 if (loaderFunctionName == '__loadModularScript') { 56 let outputData = {}; 57 for (let output of outputs) { 58 if (typeof output != 'object') continue; 59 60 for (let [className, exportClass] of Object.entries(output)) { 61 outputData[className] = exportClass; 62 } 63 } 64 return outputData; 65 } else { 66 return outputs; 67 } 68 } 69 70 __loadScript(url) { 71 let head = document.getElementsByTagName('head')[0]; 72 if (!head) throw "Document must have a HEAD tag to dynamically load scripts"; 73 74 let script = this.__getDocumentScriptFromUrl(url); 75 if (script) { 76 // Script already added 77 if ('__loader' in script) { 78 // Added from DependencyLoader 79 return script.__loader.promise; 80 } else { 81 return Promise.resolve(script);// TODO: Found a solution to know if an error occurred 82 } 83 } else { 84 let loader = {promise: null}; 85 86 loader.promise = new Promise((resolve, reject) => { 87 script = document.createElement('script'); 88 script.__loader = loader; 89 script.src = url; 90 script.async = true; 91 script.type = 'text/javascript'; 92 93 script.onload = function () { 94 resolve(script); 95 }; 96 script.onerror = function () { 97 reject(); 98 }; 99 100 head.appendChild(script); 101 }); 102 103 return loader.promise; 104 } 105 } 106 107 108 __loadModularScript(url) { 109 return new Promise((resolve, reject) => { 110 import(url).then((module) => { 111 resolve(module); 112 }); 113 }); 114 } 115 116 __loadModule(moduleId) { 117 return window.appModules.loadModule(moduleId); 118 } 119 120 __loadStyle(url) { 121 let head = document.getElementsByTagName('head')[0]; 122 if (!head) throw "Document must have a HEAD tag to dynamically load stylesheets"; 123 124 let styleSheet = this.__getDocumentStyleSheetFromUrl(url); 125 if (styleSheet) { 126 // Script already added 127 if ('__loader' in styleSheet) { 128 // Added from DependencyLoader 129 return styleSheet.__loader.promise; 130 } else { 131 return Promise.resolve(styleSheet); // TODO: Found a solution to know if an error occurred 132 } 133 } else { 134 let loader = {promise: null}; 135 loader.promise = new Promise((resolve, reject) => { 136 styleSheet = document.createElement('link'); 137 styleSheet.__loader = loader; 138 styleSheet.href = url; 139 styleSheet.rel = 'stylesheet'; 140 styleSheet.type = 'text/css'; 141 142 styleSheet.onload = function () { 143 resolve(styleSheet); 144 }; 145 styleSheet.onerror = function () { 146 reject(); 147 }; 148 149 head.appendChild(styleSheet); 150 }); 151 152 return loader.promise; 153 } 154 } 155 156 __getDocumentScriptFromUrl(url) { 157 if (!url.match(/^https?:/)) { 158 if (!url.match(/^\//)) url = document.location.pathname.replace(/\/$/)+'/'+url; 159 160 url = document.location.protocol+'//'+document.location.host+url; 161 } 162 163 for (let script of document.scripts) { 164 if (script.src == url) return script; 165 } 166 167 return null; 168 } 169 170 __getDocumentStyleSheetFromUrl(url) { 171 if (!url.match(/^https?:/)) { 172 if (!url.match(/^\//)) url = document.location.pathname.replace(/\/$/)+'/'+url; 173 174 url = document.location.protocol+'//'+document.location.host+url; 175 } 176 177 for (let styleSheet of document.styleSheets) { 178 if (styleSheet.href == url) return styleSheet; 179 } 180 181 return null; 182 } 183} 184 185window.dependencyLoader = new DependencyLoader();
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.