UNPKG

mk-meta-engine

Version:

元数据化引擎,在mk-app-loader实现的应用隔离基础上,实现可以用json元数据描述界面模型,并提供了action、reducer的基础函数和monkeyKing组件.

487 lines (373 loc) 15.5 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); var _typeof2 = require('babel-runtime/helpers/typeof'); var _typeof3 = _interopRequireDefault(_typeof2); var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); var _keys = require('babel-runtime/core-js/object/keys'); var _keys2 = _interopRequireDefault(_keys); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _class, _temp, _initialiseProps; exports.default = creator; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _mkAppLoader = require('mk-app-loader'); var _common = require('./common'); var common = _interopRequireWildcard(_common); var _mkUtils = require('mk-utils'); var _mkUtils2 = _interopRequireDefault(_mkUtils); var _immutable = require('immutable'); var _context = require('./context'); var _context2 = _interopRequireDefault(_context); var _config = require('./config'); var _config2 = _interopRequireDefault(_config); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var appInstances = {}; var action = (_temp = _class = function action(option) { (0, _classCallCheck3.default)(this, action); _initialiseProps.call(this); this.appInfo = option.appInfo; this.meta = (0, _immutable.fromJS)(option.appInfo.meta); this.cache = {}; common.setMeta(option.appInfo); }, _initialiseProps = function _initialiseProps() { var _this = this; this.config = function (_ref) { var metaHandlers = _ref.metaHandlers; _this.metaHandlers = metaHandlers; }; this.initView = function (component, injections) { _this.component = component; _this.injections = injections; appInstances[component.props.appFullName] = { appName: component.props.appName, appQuery: component.props.appQuery, //app: config.getApps()[component.props.appName], instance: component }; _this.metaHandlers && _this.metaHandlers.onInit && _this.metaHandlers.onInit({ component: component, injections: injections }); }; this.unmount = function () { delete appInstances[_this.component.appFullName]; }; this.componentWillMount = function () { _this.metaHandlers && _this.metaHandlers['componentWillMount'] && _this.metaHandlers['componentWillMount'] != _this.componentWillMount && _this.metaHandlers['componentWillMount'](); }; this.componentDidMount = function () { _this.metaHandlers && _this.metaHandlers['componentDidMount'] && _this.metaHandlers['componentDidMount'] != _this.componentDidMount && _this.metaHandlers['componentDidMount'](); }; this.shouldComponentUpdate = function (nextProps, nextState) { _this.metaHandlers && _this.metaHandlers['shouldComponentUpdate'] && _this.metaHandlers['shouldComponentUpdate'] != _this.shouldComponentUpdate && _this.metaHandlers['shouldComponentUpdate'](nextProps, nextState); }; this.componentWillReceiveProps = function (nextProps) { _this.metaHandlers && _this.metaHandlers['componentWillReceiveProps'] && _this.metaHandlers['componentWillReceiveProps'] != _this.componentWillReceiveProps && _this.metaHandlers['componentWillReceiveProps'](nextProps); }; this.componentWillUpdate = function (nextProps, nextState) { _this.metaHandlers && _this.metaHandlers['componentWillUpdate'] && _this.metaHandlers['componentWillUpdate'] != _this.componentWillUpdate && _this.metaHandlers['componentWillUpdate'](nextProps, nextState); }; this.componentDidCatch = function (error, info) { _this.metaHandlers && _this.metaHandlers['componentDidCatch'] && _this.metaHandlers['componentDidCatch'] != _this.componentDidCatch && _this.metaHandlers['componentDidCatch'](error, info); }; this.componentWillUnmount = function () { _this.metaHandlers && _this.metaHandlers['componentWillUnmount'] && _this.metaHandlers['componentWillUnmount'] != _this.componentWillUnmount && _this.metaHandlers['componentWillUnmount'](); }; this.componentDidUpdate = function () { _this.metaHandlers && _this.metaHandlers['componentDidUpdate'] && _this.metaHandlers['componentDidUpdate'] != _this.componentDidUpdate && _this.metaHandlers['componentDidUpdate'](); }; this.getAppInstances = function () { return appInstances; }; this.getField = function (fieldPath) { return common.getField(_this.injections.getState(), fieldPath); }; this.getFields = function (fieldPaths) { return common.getFields(_this.injections.getState(), fieldPaths); }; this.setField = function (fieldPath, value) { return _this.injections.reduce('setField', fieldPath, value); }; this.setFields = function (values) { return _this.injections.reduce('setFields', values); }; this.parseExpreesion = function (v) { if (!_this.cache.expression) _this.cache.expression = {}; if (_this.cache.expression[v]) { return _this.cache.expression[v]; } if (!_this.cache.expressionParams) { _this.cache.expressionParams = ['data'].concat((0, _keys2.default)(_this.metaHandlers).map(function (k) { return "$" + k; })).concat(['_path', '_rowIndex', '_vars', '_ctrlPath', '_lastIndex']); } var params = _this.cache.expressionParams; var body = _mkUtils2.default.expression.getExpressionBody(v); _this.cache.expression[v] = new (Function.prototype.bind.apply(Function, [null].concat((0, _toConsumableArray3.default)(params), [body])))(); return _this.cache.expression[v]; }; this.execExpression = function (expressContent, data, path, rowIndex, vars, ctrlPath) { var values = [data]; var metaHandlerKeys = (0, _keys2.default)(_this.metaHandlers), i, key; var fun = function fun(n, option) { return function () { var _metaHandlers; for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return (_metaHandlers = _this.metaHandlers)[n].apply(_metaHandlers, args.concat([option])); }; }; for (i = 0; key = metaHandlerKeys[i++];) { values.push(fun(key, { currentPath: path, rowIndex: rowIndex, vars: vars, lastIndex: vars && vars[vars.length - 1] })); } /* Object.keys(this.metaHandlers).forEach(k => { values.push((...args) => this.metaHandlers[k](...args, { currentPath: path, rowIndex, vars, lastIndex: vars && vars[vars.length - 1] })) }) */ values.push(path); values.push(rowIndex); values.push(vars); values.push(ctrlPath); values.push(vars && vars[vars.length - 1]); //values = values.concat([path, rowIndex, vars, ctrlPath, vars && vars[vars.length-1] ]) try { return _this.parseExpreesion(expressContent).apply(_this, values); } catch (e) { _this.metaHandlers && _this.metaHandlers.componentDidCatch && _this.metaHandlers.componentDidCatch != _this.componentDidCatch && _this.metaHandlers.componentDidCatch(e); setTimeout(function () { console.error('\u8868\u8FBE\u5F0F\u89E3\u6790\u9519\u8BEF\uFF1A' + expressContent); _mkUtils2.default.exception.error(e); }, 500); } }; this.needUpdate = function (meta) { if (!meta) return false; var t = typeof meta === 'undefined' ? 'undefined' : (0, _typeof3.default)(meta); if (t == 'string' && _mkUtils2.default.expression.isExpression(meta)) return true; if (t != 'object') return false; if (meta._notParse === true) { return false; } return !(t != 'object' || !!meta['$$typeof'] || !!meta._isAMomentObject || !!meta._power || meta._visible === false); }; this.updateMeta = function (meta, path, rowIndex, vars, data, ctrlPath) { if (!_this.needUpdate(meta)) return; if (meta instanceof Array) { for (var _i = 0; _i < meta.length; _i++) { var sub = meta[_i]; var currentPath = path; if (!sub) continue; if (sub._power) { currentPath = path + '.' + sub.name; sub.path = vars ? currentPath + ', ' + vars.join(',') : currentPath; continue; } var subType = typeof sub === 'undefined' ? 'undefined' : (0, _typeof3.default)(sub), isExpression = false, isMeta = false; if (subType == 'string' && _mkUtils2.default.expression.isExpression(sub)) { sub = _this.execExpression(sub, data, path, rowIndex, vars, ctrlPath); isExpression = true; if (sub && sub._isMeta === true) isMeta = true; if (sub && sub._isMeta === true) { isMeta = true; meta[_i] = sub.value; } else { meta[_i] = sub; } } if (!_this.needUpdate(sub)) continue; if (isExpression && !isMeta) { continue; } subType = typeof sub === 'undefined' ? 'undefined' : (0, _typeof3.default)(sub); if (sub instanceof Array) { currentPath = path + '.' + _i; sub.path = vars ? currentPath + ', ' + vars.join(',') : currentPath; _this.updateMeta(sub, currentPath, rowIndex, vars, data, ctrlPath); continue; } if (sub.name && sub.component) { currentPath = path + '.' + sub.name; sub.path = vars ? currentPath + ', ' + vars.join(',') : currentPath; _this.updateMeta(sub, currentPath, rowIndex, vars, data, sub.path); } else { currentPath = path + '.' + _i; sub.path = vars ? currentPath + ', ' + vars.join(',') : currentPath; _this.updateMeta(sub, currentPath, rowIndex, vars, data, ctrlPath); } } return; } var excludeProps = meta._excludeProps; if (excludeProps && _mkUtils2.default.expression.isExpression(excludeProps)) { excludeProps = _this.execExpression(excludeProps, data, path, rowIndex, vars, ctrlPath); } //去除meta的排除属性 if (excludeProps && excludeProps instanceof Array) { var excludePropsKeys = (0, _keys2.default)(excludeProps), i, excludePropsKey; for (i = 0; excludePropsKey = excludePropsKeys[i++];) { if (meta[excludePropsKey]) delete meta[excludePropsKey]; } /* excludeProps.forEach(k => { if (meta[k]) delete meta[k] })*/ } var keys = (0, _keys2.default)(meta), j, key; var _loop = function _loop() { var v = meta[key], t = typeof v === 'undefined' ? 'undefined' : (0, _typeof3.default)(v), currentPath = path; if (!v) return 'continue'; if (v._power) { currentPath = path + '.' + key + '.' + v.name; v.path = vars ? currentPath + ', ' + vars.join(',') : currentPath; return 'continue'; } var isExpression = false, isMeta = false; if (t == 'string' && _mkUtils2.default.expression.isExpression(v)) { v = _this.execExpression(v, data, path + '.' + key, rowIndex, vars, ctrlPath); isExpression = true; if (key == '...' && v && (typeof v === 'undefined' ? 'undefined' : (0, _typeof3.default)(v)) == 'object') { (0, _keys2.default)(v).forEach(function (kk) { meta[kk] = v[kk]; }); delete meta['...']; } else { if (v && v._isMeta === true) { isMeta = true; meta[key] = v.value; } else { meta[key] = v; } } } t = typeof t === 'undefined' ? 'undefined' : (0, _typeof3.default)(t); if (!_this.needUpdate(v)) return 'continue'; if (isExpression && !isMeta) { return 'continue'; } if (v instanceof Array) { _this.updateMeta(v, path + '.' + key, rowIndex, vars, data, ctrlPath); return 'continue'; } if (v.name && v.component) { currentPath = path + '.' + key + '.' + v.name; v.path = vars ? currentPath + ', ' + vars.join(',') : currentPath; _this.updateMeta(v, currentPath, rowIndex, vars, data, v.path); } else { currentPath = path + '.' + key; v.path = vars ? currentPath + ', ' + vars.join(',') : currentPath; _this.updateMeta(v, currentPath, rowIndex, vars, data, ctrlPath); } }; for (j = 0; key = keys[j++];) { var _ret = _loop(); if (_ret === 'continue') continue; } }; this.getMeta = function (fullPath, propertys, data) { var meta = common.getMeta(_this.appInfo, fullPath, propertys), parsedPath = _mkUtils2.default.path.parsePath(fullPath), path = parsedPath.path, rowIndex = parsedPath.vars ? parsedPath.vars[0] : undefined, vars = parsedPath.vars; if (!data) data = common.getField(_this.injections.getState()).toJS(); meta._power = undefined; meta.path = fullPath; _this.updateMeta(meta, path, rowIndex, vars, data, fullPath); return meta; }; this.setMetaForce = function (appName, meta) { common.setMetaForce(appName, meta); }; this.focus = function (path) { if (_this.isFocus(path)) return false; _this.setField('data.other.focusFieldPath', path); return true; }; this.focusByEvent = function (e) { var path = _mkUtils2.default.path.findPathByEvent(e); return _this.focus(path); }; this.isFocus = function (path) { if (!path) return false; var focusFieldPath = _this.getField('data.other.focusFieldPath'); if (!focusFieldPath) return false; return path.replace(/\s/g, '') == focusFieldPath.replace(/\s/g, ''); }; this.getDirectFuns = function () { return { getMeta: function getMeta() { return _this.getMeta.apply(_this, arguments); }, getField: function getField(fieldPath) { return _this.getField(fieldPath); }, gm: function gm() { return _this.getMeta.apply(_this, arguments); }, gf: function gf(fieldPath) { return _this.getField(fieldPath); } }; }; this.toast = function () { for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } var Toast = _config2.default.getToast(); if (!Toast || args.length == 0 || !Toast[args[0]]) return; Toast[args[0]].apply(Toast, (0, _toConsumableArray3.default)(args.slice(1))); }; this.notification = function () { for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { args[_key3] = arguments[_key3]; } var Notification = _config2.default.getNotification(); if (!Notification || args.length == 0 || !Notification[args[0]]) return; Notification[args[0]].apply(Notification, (0, _toConsumableArray3.default)(args.slice(1))); }; this.modal = function () { for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { args[_key4] = arguments[_key4]; } var Modal = _config2.default.getModal(); if (!Modal || args.length == 0 || !Modal[args[0]]) return; return Modal[args[0]].apply(Modal, (0, _toConsumableArray3.default)(args.slice(1))); }; this.loadApp = function (name, props) { return _react2.default.createElement(_mkAppLoader.AppLoader, (0, _extends3.default)({}, props, { name: name })); }; this.gm = this.getMeta; this.gf = this.getField; this.gfs = this.getFields; this.sf = this.setField; this.sfs = this.setFields; this.findPathByEvent = _mkUtils2.default.path.findPathByEvent; this.stringToMoment = _mkUtils2.default.moment.stringToMoment; this.momentToString = _mkUtils2.default.moment.momentToString; this.fromJS = _immutable.fromJS; this.context = _context2.default; }, _temp); function creator(option) { return new action(option); } module.exports = exports['default'];