UNPKG

eapp

Version:
403 lines (381 loc) 11 kB
/** * 擎天模块引擎,ibox内核,by xuww * date 20190905 * v1.0.0 */ var Ibox; var iboxLoad; (function () { // 模块集合 var iboxList = {}; // 引用栈,递归一条线的引用栈 var loadStack = []; // 模块在引用栈中的下标 var loadIndexMap = {}; var SEPARATOR = '.'; /** * 模块构建 * @param ibox * @param father 父类 */ function build(ibox, father) { var factory = ibox.factory; var localRequire = function (id) { var resultantId = id; // 嵌套模块引用,相对路径 if (id.charAt(0) === '.') { var last = ibox.id.lastIndexOf(SEPARATOR); resultantId = ibox.id.slice(0, last); if(id != './'){ resultantId += SEPARATOR + id.slice(2); } } return iboxLoad(resultantId); }; ibox.exports = { _id_: ibox.id, getId: function () { return ibox.id; } }; ibox.super = father; // console.log("build--->", ibox); // console.log("build-super--->", father); // delete ibox.factory; ibox.build = true; factory(localRequire, ibox.exports, ibox); // 重载预处理 ibox.exports.overrideBy = function (id) { return overrideBy(id, ibox); }; return ibox.exports; } /** * 模块交集重载 * @param newId * @param newId * @returns {*} */ overrideBy = function (newId, oldM) { // console.log("new Id-->" + id + "; override oldM-->", oldM); // console.log("iboxList--->", iboxList); var oldId = oldM.exports._id_; // A@A.C动态继承 = A.C if (newId.indexOf(oldId + SEPARATOR) == 0) { return iboxLoad(newId); } // A.B@C.D动态继承; A.B@A.C动态继承 var oldAndNewId = oldId + "@" + newId; // 如果存在 if (iboxList[oldAndNewId]) { // return iboxList[oldAndNewId].exports; return iboxLoad(oldAndNewId, "@"); } iboxList[oldAndNewId] = { id: oldAndNewId, factory: iboxList[newId].factory, build: false }; // var newExp = iboxLoad(newId); var oldAndNewExp = iboxLoad(oldAndNewId, "@"); return oldAndNewExp; }; /** * 模块加载 * @param id * @returns {*} */ iboxLoad = function (id, separator) { separator = separator || SEPARATOR; // 模块不存在 if (!iboxList[id]) { throw '模块:' + id + '不存在!'; } var fatherExp = null; if(id.indexOf(separator) > 0){ var last = id.lastIndexOf(separator); var fatherId = id.slice(0, last); fatherExp = iboxLoad(fatherId, separator); } // 屏蔽掉循环引用 else if (id in loadIndexMap) { var cycle = loadStack.slice(loadIndexMap[id]).join('->') + '->' + id; // var cycle = loadStack.slice(loadIndexMap[id]); throw '不能循环引用:' + cycle; } // 加载模块到内存 if (iboxList[id].build == false) { try { loadIndexMap[id] = loadStack.length; loadStack.push(id); var sonExp = build(iboxList[id], fatherExp); if(fatherExp != null && fatherExp != undefined && fatherExp != ''){ // 克隆fatherExp var fatherExp2 = Ibox.utils.clone(fatherExp); //console.log("fatherExp2--->", fatherExp2); sonExp = Ibox.utils.extend(fatherExp2, sonExp); // 子类功能集合更新 iboxList[id].exports = sonExp; } return sonExp; } finally { delete loadIndexMap[id]; loadStack.pop(); } } return iboxList[id].exports; }; /** * 模块定义 * @param id * @param factory * @constructor */ Ibox = function (id, factory) { if (iboxList[id]) { throw '模块:' + id + '已经定义了!'; } iboxList[id] = { id: id, factory: factory, build: false }; }; /** * 删除模块 * @param id */ Ibox.remove = function (id) { // var m = iboxList[id]; // m.exports = {}; delete iboxList[id]; }; /** * 对外提供map数据 */ Ibox.map = iboxList; })(); Ibox("Ibox-utils", function (iboxLoad, exports, ibox) { /** * 调试模式,用于日志开关 */ var DEBUG = true; /** * 1为console;2为alert */ var LOG_TYPE = 1; /** * 设置调试模式 * @param b * @param logType 0为console;1为alert */ exports.setDebug = function (b, logType) { DEBUG = b; LOG_TYPE = logType || LOG_TYPE; }; /** * 扩展 * @returns {*} */ exports.extend = function () { var args = [], len$1 = arguments.length; while (len$1--) args[len$1] = arguments[len$1]; var deep = true; var to; var from; if (typeof args[0] === 'boolean') { deep = args[0]; to = args[1]; args.splice(0, 2); from = args; } else { to = args[0]; args.splice(0, 1); from = args; } for (var i = 0; i < from.length; i += 1) { var nextSource = args[i]; if (nextSource !== undefined && nextSource !== null) { var keysArray = Object.keys(Object(nextSource)); for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex += 1) { var nextKey = keysArray[nextIndex]; var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); if (desc !== undefined && desc.enumerable) { if (!deep) { to[nextKey] = nextSource[nextKey]; } else if (this.isObject(to[nextKey]) && this.isObject(nextSource[nextKey])) { this.extend(to[nextKey], nextSource[nextKey]); } else if (!this.isObject(to[nextKey]) && this.isObject(nextSource[nextKey])) { to[nextKey] = {}; this.extend(to[nextKey], nextSource[nextKey]); } else { to[nextKey] = nextSource[nextKey]; } } } } } return to; }; /** * 判断是否是对象 * @param o * @returns {boolean|Object} */ exports.isObject = function (o) { return typeof o === 'object' && o !== null && o.constructor && o.constructor === Object; }; /** * 类型名字 * @param val * @returns {string} */ exports.typeName = function (val) { return Object.prototype.toString.call(val).slice(8, -1); }; /** * 是否是数组 * @type {((arg: any) => arg is Array<any>) | (function(*=): boolean)} */ exports.isArray = Array.isArray || function (a) { return exports.typeName(a) === 'Array'; }; /** * 是否是日期 * @param d * @returns {boolean} */ exports.isDate = function (d) { return (d instanceof Date); }; /** * 克隆 * @param obj * @param hash * @returns {*} */ exports.clone = function (obj) { if (!obj || typeof obj === 'function' || exports.isDate(obj) || typeof obj !== 'object') { return obj; } var retVal, i; if (exports.isArray(obj)) { retVal = []; for (i = 0; i < obj.length; ++i) { retVal.push(exports.clone(obj[i])); } return retVal; } retVal = {}; for (i in obj) { if ((!(i in retVal) || retVal[i] !== obj[i]) && typeof obj[i] !== 'undefined' && typeof obj[i] !== 'unknown') { // eslint-disable-line valid-typeof retVal[i] = exports.clone(obj[i]); } } return retVal; }; /** * 非空判断 * @param value */ exports.isEmpty = function (value) { return (value == null || value == undefined || value === ''); }; /** * 日志 * @param tag * @param msg */ exports.showLog = function (tag, msg) { showLogByType(1, tag, msg); }; /** * 日志 * @param tag * @param msg */ exports.showWarnLog = function (tag, msg) { showLogByType(2, tag, msg); }; /** * 日志 * @param tag * @param msg */ exports.showErrorLog = function (tag, msg) { showLogByType(3, tag, msg); }; function showLogByType(type, tag, msg){ if (DEBUG) { msg = msg || ''; tag = tag || ''; // 1为console;2为alert if (LOG_TYPE == 1) { if (type == 2) { console.warn(tag, msg); } else if (type == 3) { console.error(tag, msg); } else { console.log(tag, msg); } } else { alert(tag + JSON.stringify(msg)); } } } /** * 缓存数据 * @param key * @param value */ exports.saveItem = function (key, value) { window.localStorage.setItem(key, value); }; /** * 删除缓存 * @param key */ exports.removeItem = function (key) { window.localStorage.removeItem(key); }; /** * 获取缓存 * @param key * @returns {string} */ exports.getItem = function (key) { return window.localStorage.getItem(key); }; /** * 获取json格式缓存 * @param key * @returns {any} */ exports.getItemJO = function (key) { try { var value = exports.getItem(key); return JSON.parse(value); }catch (e) { exports.showErrorLog("getItemJO-->", e); return null; } }; /** * 动态加载js * @param url * @param onload * @param onerror */ exports.addJavaScript = function (url, onload, onerror) { var script = document.createElement('script'); script.onload = onload; script.onerror = onerror; script.src = url; document.head.appendChild(script); }; }); Ibox("Ibox", function () { }); // 加载工具类 Ibox.utils = iboxLoad("Ibox-utils");