|
| 1 | +import $ from 'dom7'; |
| 2 | +import Utils from '../../utils/utils'; |
| 3 | + |
| 4 | +const fetchedModules = []; |
| 5 | +function loadModule(moduleToLoad) { |
| 6 | + const Framework7 = this; |
| 7 | + return new Promise((resolve, reject) => { |
| 8 | + const app = Framework7.instance; |
| 9 | + let modulePath; |
| 10 | + let moduleObj; |
| 11 | + let moduleFunc; |
| 12 | + if (!moduleToLoad) { |
| 13 | + reject(new Error('Framework7: Lazy module must be specified')); |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + function install(module) { |
| 18 | + Framework7.use(module); |
| 19 | + |
| 20 | + if (app) { |
| 21 | + app.useModuleParams(module, app.params); |
| 22 | + app.useModule(module); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + if (typeof moduleToLoad === 'string') { |
| 27 | + const matchNamePattern = moduleToLoad.match(/([a-z0-9-]*)/i); |
| 28 | + if (moduleToLoad.indexOf('.') < 0 && matchNamePattern && matchNamePattern[0].length === moduleToLoad.length) { |
| 29 | + if (!app || (app && !app.params.lazyModulesPath)) { |
| 30 | + reject(new Error('Framework7: "lazyModulesPath" app parameter must be specified to fetch module by name')); |
| 31 | + return; |
| 32 | + } |
| 33 | + modulePath = `${app.params.lazyModulesPath}/${moduleToLoad}.js`; |
| 34 | + } else { |
| 35 | + modulePath = moduleToLoad; |
| 36 | + } |
| 37 | + } else if (typeof moduleToLoad === 'function') { |
| 38 | + moduleFunc = moduleToLoad; |
| 39 | + } else { |
| 40 | + // considering F7-Plugin object |
| 41 | + moduleObj = moduleToLoad; |
| 42 | + } |
| 43 | + |
| 44 | + if (moduleFunc) { |
| 45 | + const module = moduleFunc(Framework7, false); |
| 46 | + if (!module) { |
| 47 | + reject(new Error('Framework7: Can\'t find Framework7 component in specified component function')); |
| 48 | + return; |
| 49 | + } |
| 50 | + // Check if it was added |
| 51 | + if (Framework7.prototype.modules && Framework7.prototype.modules[module.name]) { |
| 52 | + resolve(); |
| 53 | + return; |
| 54 | + } |
| 55 | + // Install It |
| 56 | + install(module); |
| 57 | + |
| 58 | + resolve(); |
| 59 | + } |
| 60 | + if (moduleObj) { |
| 61 | + const module = moduleObj; |
| 62 | + if (!module) { |
| 63 | + reject(new Error('Framework7: Can\'t find Framework7 component in specified component')); |
| 64 | + return; |
| 65 | + } |
| 66 | + // Check if it was added |
| 67 | + if (Framework7.prototype.modules && Framework7.prototype.modules[module.name]) { |
| 68 | + resolve(); |
| 69 | + return; |
| 70 | + } |
| 71 | + // Install It |
| 72 | + install(module); |
| 73 | + |
| 74 | + resolve(); |
| 75 | + } |
| 76 | + if (modulePath) { |
| 77 | + if (fetchedModules.indexOf(modulePath) >= 0) { |
| 78 | + resolve(); |
| 79 | + return; |
| 80 | + } |
| 81 | + fetchedModules.push(modulePath); |
| 82 | + const scriptLoad = new Promise((resolveScript, rejectScript) => { |
| 83 | + Framework7.request.get( |
| 84 | + modulePath, |
| 85 | + (scriptContent) => { |
| 86 | + const id = Utils.id(); |
| 87 | + const callbackLoadName = `f7_component_loader_callback_${id}`; |
| 88 | + |
| 89 | + const scriptEl = document.createElement('script'); |
| 90 | + scriptEl.innerHTML = `window.${callbackLoadName} = function (Framework7, Framework7AutoInstallComponent) {return ${scriptContent.trim()}}`; |
| 91 | + $('head').append(scriptEl); |
| 92 | + |
| 93 | + const componentLoader = window[callbackLoadName]; |
| 94 | + delete window[callbackLoadName]; |
| 95 | + $(scriptEl).remove(); |
| 96 | + |
| 97 | + const module = componentLoader(Framework7, false); |
| 98 | + |
| 99 | + if (!module) { |
| 100 | + rejectScript(new Error(`Framework7: Can't find Framework7 component in ${modulePath} file`)); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + // Check if it was added |
| 105 | + if (Framework7.prototype.modules && Framework7.prototype.modules[module.name]) { |
| 106 | + resolveScript(); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + // Install It |
| 111 | + install(module); |
| 112 | + |
| 113 | + resolveScript(); |
| 114 | + }, |
| 115 | + (xhr, status) => { |
| 116 | + rejectScript(xhr, status); |
| 117 | + } |
| 118 | + ); |
| 119 | + }); |
| 120 | + const styleLoad = new Promise((resolveStyle) => { |
| 121 | + Framework7.request.get( |
| 122 | + modulePath.replace('.js', app.rtl ? '.rtl.css' : '.css'), |
| 123 | + (styleContent) => { |
| 124 | + const styleEl = document.createElement('style'); |
| 125 | + styleEl.innerHTML = styleContent; |
| 126 | + $('head').append(styleEl); |
| 127 | + |
| 128 | + resolveStyle(); |
| 129 | + }, |
| 130 | + () => { |
| 131 | + resolveStyle(); |
| 132 | + } |
| 133 | + ); |
| 134 | + }); |
| 135 | + |
| 136 | + Promise.all([scriptLoad, styleLoad]).then(() => { |
| 137 | + resolve(); |
| 138 | + }).catch((err) => { |
| 139 | + reject(err); |
| 140 | + }); |
| 141 | + } |
| 142 | + }); |
| 143 | +} |
| 144 | + |
| 145 | +export default loadModule; |
0 commit comments