UNPKG

zmp-core

Version:

Full featured mobile HTML framework for building iOS & Android apps

144 lines (117 loc) 4.16 kB
import { getWindow, getDocument } from 'ssr-window'; import { id } from '../../shared/utils'; import $ from '../../shared/zmp-dom'; var fetchedModules = []; function loadModule(moduleToLoad) { var ZMP = this; var window = getWindow(); var document = getDocument(); return new Promise(function (resolve, reject) { var app = ZMP.instance; var modulePath; var moduleObj; var moduleFunc; if (!moduleToLoad) { reject(new Error('ZMP: Lazy module must be specified')); return; } function install(module) { ZMP.use(module); if (app) { app.useModuleParams(module, app.params); app.useModule(module); } } if (typeof moduleToLoad === 'string') { var matchNamePattern = moduleToLoad.match(/([a-z0-9-]*)/i); if (moduleToLoad.indexOf('.') < 0 && matchNamePattern && matchNamePattern[0].length === moduleToLoad.length) { if (!app || app && !app.params.lazyModulesPath) { reject(new Error('ZMP: "lazyModulesPath" app parameter must be specified to fetch module by name')); return; } modulePath = app.params.lazyModulesPath + "/" + moduleToLoad + "/" + moduleToLoad + ".js"; } else { modulePath = moduleToLoad; } } else if (typeof moduleToLoad === 'function') { moduleFunc = moduleToLoad; } else { // considering ZMP-Plugin object moduleObj = moduleToLoad; } if (moduleFunc) { var module = moduleFunc(ZMP, false); if (!module) { reject(new Error("ZMP: Can't find ZMP component in specified component function")); return; } // Check if it was added if (ZMP.prototype.modules && ZMP.prototype.modules[module.name]) { resolve(); return; } // Install It install(module); resolve(); } if (moduleObj) { var _module = moduleObj; if (!_module) { reject(new Error("ZMP: Can't find ZMP component in specified component")); return; } // Check if it was added if (ZMP.prototype.modules && ZMP.prototype.modules[_module.name]) { resolve(); return; } // Install It install(_module); resolve(); } if (modulePath) { if (fetchedModules.indexOf(modulePath) >= 0) { resolve(); return; } fetchedModules.push(modulePath); var scriptLoad = new Promise(function (resolveScript, rejectScript) { ZMP.request.get(modulePath, function (scriptContent) { var callbackId = id(); var callbackLoadName = "zmp_component_loader_callback_" + callbackId; var scriptEl = document.createElement('script'); scriptEl.innerHTML = "window." + callbackLoadName + " = function (ZMP, ZMPAutoInstallComponent) {return " + scriptContent.trim() + "}"; $('head').append(scriptEl); var componentLoader = window[callbackLoadName]; delete window[callbackLoadName]; $(scriptEl).remove(); var module = componentLoader(ZMP, false); if (!module) { rejectScript(new Error("ZMP: Can't find ZMP component in " + modulePath + " file")); return; } // Check if it was added if (ZMP.prototype.modules && ZMP.prototype.modules[module.name]) { resolveScript(); return; } // Install It install(module); resolveScript(); }, function (xhr, status) { rejectScript(xhr, status); }); }); var styleLoad = new Promise(function (resolveStyle) { ZMP.request.get(modulePath.replace('.js', app.rtl ? '.rtl.css' : '.css'), function (styleContent) { var styleEl = document.createElement('style'); styleEl.innerHTML = styleContent; $('head').append(styleEl); resolveStyle(); }, function () { resolveStyle(); }); }); Promise.all([scriptLoad, styleLoad]).then(function () { resolve(); }).catch(function (err) { reject(err); }); } }); } export default loadModule;