UNPKG

mutants-appfx

Version:

appfx module

1,558 lines (1,258 loc) 53.3 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _map = require('babel-runtime/core-js/map'); var _map2 = _interopRequireDefault(_map); var _getIterator2 = require('babel-runtime/core-js/get-iterator'); var _getIterator3 = _interopRequireDefault(_getIterator2); var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties'); var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2); var _defineProperty2 = require('babel-runtime/core-js/object/define-property'); var _defineProperty3 = _interopRequireDefault(_defineProperty2); var _getOwnPropertyDescriptor = require('babel-runtime/core-js/object/get-own-property-descriptor'); var _getOwnPropertyDescriptor2 = _interopRequireDefault(_getOwnPropertyDescriptor); var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); var _regenerator = require('babel-runtime/regenerator'); var _regenerator2 = _interopRequireDefault(_regenerator); var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); var _set = require('babel-runtime/core-js/set'); var _set2 = _interopRequireDefault(_set); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _defineProperty4 = require('babel-runtime/helpers/defineProperty'); var _defineProperty5 = _interopRequireDefault(_defineProperty4); var _keys = require('babel-runtime/core-js/object/keys'); var _keys2 = _interopRequireDefault(_keys); var _extends3 = require('babel-runtime/helpers/extends'); var _extends4 = _interopRequireDefault(_extends3); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _desc, _value, _class, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5, _descriptor6, _descriptor7, _descriptor8, _desc2, _value2, _class3, _descriptor9; var _mobx = require('mobx'); var _viewmodels = require('./viewmodels'); var _viewmodels2 = _interopRequireDefault(_viewmodels); var _datamodels = require('./datamodels'); var _controllers = require('./controllers'); var _controllers2 = _interopRequireDefault(_controllers); var _ruleset = require('./ruleset'); var _ruleset2 = _interopRequireDefault(_ruleset); var _DataProviderManager = require('./DataProviderManager'); var _DataProviderManager2 = _interopRequireDefault(_DataProviderManager); var _AppFlowManager = require('./flow/AppFlowManager'); var _AppFlowManager2 = _interopRequireDefault(_AppFlowManager); var _UIFunction = require('./UIFunction'); var _UIFunction2 = _interopRequireDefault(_UIFunction); var _querystring = require('querystring'); var _querystring2 = _interopRequireDefault(_querystring); var _matchPath = require('react-router/matchPath'); var _matchPath2 = _interopRequireDefault(_matchPath); var _mutantsMicrofx = require('mutants-microfx'); var _UIController = require('./viewmodels/UIController'); var _UIController2 = _interopRequireDefault(_UIController); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _initDefineProp(target, property, descriptor, context) { if (!descriptor) return; (0, _defineProperty3.default)(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); } function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object['ke' + 'ys'](descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object['define' + 'Property'](target, property, desc); desc = null; } return desc; } function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that transform-class-properties is enabled.'); } var t = _mutantsMicrofx.i18n.getFixedT(null, 'appfx'); var Event = function Event(name, args) { (0, _classCallCheck3.default)(this, Event); this.name = name; this.args = args; }; var EventStamp = function EventStamp(name) { var timestamp = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date().getTime(); (0, _classCallCheck3.default)(this, EventStamp); this.name = name; this.timestamp = timestamp; }; function formatJson(json) { return (0, _extends4.default)({}, json, { view: typeof json.view === 'string' ? { name: json.view, mode: 'single' } : json.view, views: (0, _keys2.default)(json.views || {}).reduce(function (ret, key) { return (0, _extends4.default)({}, ret, (0, _defineProperty5.default)({}, key, typeof json.views[key] === 'string' ? { name: json.views[key], mode: 'single' } : json.views[key])); }, {}) }); } var AppInstance = (_class = function () { (0, _createClass3.default)(AppInstance, [{ key: 'eventHanding', // 是否有事件正在执行 // vm的单例集合 // 页面实例ID // 提供模块间数据共享,用map结构添加值可以被观察 // 当前行的数据模型 get: function get() { return this.eventStack.length > 0; } // 缓存所有加载的datamodel.id // 所有数据的集合,集合里只是数据不是DM }, { key: 'view', get: function get() { var _this = this; var vo = this.views.find(function (it) { return it.iid === _this.iid; }); if (!vo) { return null; } return vo.view; } }, { key: 'viewModel', get: function get() { var _this2 = this; var vmo = this.viewModels.find(function (it) { return it.iid === _this2.iid; }); if (!vmo) { return null; } return vmo.viewModel; } }, { key: 'loaded', get: function get() { return this.viewModel.loaded; } }]); function AppInstance(manager, dataList, dataModel, viewController, controller, ruleset, module, state, cache) { var _this3 = this; (0, _classCallCheck3.default)(this, AppInstance); _initDefineProp(this, 'dataList', _descriptor, this); _initDefineProp(this, 'dataModel', _descriptor2, this); _initDefineProp(this, 'views', _descriptor3, this); _initDefineProp(this, 'viewModels', _descriptor4, this); _initDefineProp(this, 'pid', _descriptor5, this); _initDefineProp(this, 'iid', _descriptor6, this); _initDefineProp(this, 'state', _descriptor7, this); this.ids = new _set2.default(); this.singles = {}; _initDefineProp(this, 'eventStack', _descriptor8, this); // MD和DL没有直接关系,DM靠DL数据行改变刷新数据 this.dataList = dataList; this.dataModel = dataModel; this.ruleset = ruleset; this.manager = manager; this.cache = cache; this.viewController = viewController; this.controller = controller; this.module = module; this.updateState(state); this.viewController._init(this.viewModels); this.controller._init(this.viewModels); // 当前模型改变需要重置controller this.listListener = (0, _mobx.reaction)(function () { return _this3.dataList.current; }, function () { var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(data) { return _regenerator2.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!data) { _context.next = 6; break; } _context.next = 3; return _this3.dataModel.restore(data); case 3: if (!data.ID) { _context.next = 6; break; } _context.next = 6; return _this3.loadData(data.ID); case 6: case 'end': return _context.stop(); } } }, _callee, _this3); })); return function (_x2) { return _ref.apply(this, arguments); }; }()); this.pageListener = (0, _mobx.reaction)(function () { return _this3.iid; }, function () { var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(iid) { return _regenerator2.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return _this3.viewController._curView(iid, _this3.pid); case 2: _context2.next = 4; return _this3.controller._curView(iid, _this3.pid); case 4: _context2.next = 6; return _this3.controller.ui._init(_this3.viewModel); case 6: case 'end': return _context2.stop(); } } }, _callee2, _this3); })); return function (_x3) { return _ref2.apply(this, arguments); }; }()); this.dataListener = (0, _mobx.reaction)(function () { return _this3.dataModel.toJS(); }, function (data) { if (!_this3.dataModel.getId()) { // 没有保存不缓存,清空后无法恢复 return; } var cacheKey = _this3.module.sysid + '_' + _this3.module.mid + ':' + _this3.dataModel.getId(); _this3.cache.set(cacheKey, data); }, { delay: 500 }); } (0, _createClass3.default)(AppInstance, [{ key: 'updateState', value: function updateState(state) { var _this4 = this; (0, _keys2.default)(state).forEach(function (key) { _this4.state.set(key, state[key]); }); } }, { key: 'pushEvent', value: function () { var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(name) { var et; return _regenerator2.default.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: et = new EventStamp(name); // TODO 提供事件的依赖控制 this.eventStack.push(et); console.log('events===>', this.eventStack.map(function (it) { return it.name; })); return _context3.abrupt('return', et.timestamp); case 4: case 'end': return _context3.stop(); } } }, _callee3, this); })); function pushEvent(_x4) { return _ref3.apply(this, arguments); } return pushEvent; }() }, { key: 'popEvent', value: function () { var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4(timestamp) { var et; return _regenerator2.default.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: et = this.eventStack.find(function (et) { return et.timestamp === timestamp; }); if (et) { _context4.next = 4; break; } console.error('event out!'); return _context4.abrupt('return'); case 4: this.eventStack.splice(this.eventStack.indexOf(et), 1); console.log('events===>', this.eventStack.map(function (it) { return it.name; })); case 6: case 'end': return _context4.stop(); } } }, _callee4, this); })); function popEvent(_x5) { return _ref4.apply(this, arguments); } return popEvent; }() }, { key: 'close', value: function () { var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5() { return _regenerator2.default.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: _context5.next = 2; return this.manager.closeApp(this); case 2: case 'end': return _context5.stop(); } } }, _callee5, this); })); function close() { return _ref5.apply(this, arguments); } return close; }() }, { key: 'dispose', value: function dispose() { this.ruleset && this.ruleset.dispose(); this.listListener(); this.pageListener(); this.dataListener(); } }, { key: 'executeAction', value: function () { var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6(name, args) { var dataModel, snapshot; return _regenerator2.default.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: dataModel = this.dataModel; // 执行业务前需要生成数据快照,要是发生业务错误需要回滚 // 快照放在viewcontroller之前创建,有可能vc调用bc _context6.next = 3; return dataModel.toJS(); case 3: snapshot = _context6.sent; _context6.prev = 4; if (!(typeof this.viewController[name] === 'function')) { _context6.next = 10; break; } _context6.next = 8; return this.viewController[name].bind(this.viewController)(args); case 8: _context6.next = 13; break; case 10: if (!(typeof this.controller[name] === 'function')) { _context6.next = 13; break; } _context6.next = 13; return this.controller[name].bind(this.controller)(args); case 13: _context6.next = 22; break; case 15: _context6.prev = 15; _context6.t0 = _context6['catch'](4); if (_context6.t0.name === 'BizError') { // message.error(err.message); this.controller.ui.feedback(_context6.t0.message, 'error'); } console.error('action failed!', _context6.t0); if (!snapshot) { _context6.next = 22; break; } _context6.next = 22; return dataModel.restore(snapshot); case 22: case 'end': return _context6.stop(); } } }, _callee6, this, [[4, 15]]); })); function executeAction(_x6, _x7) { return _ref6.apply(this, arguments); } return executeAction; }() }, { key: 'triggerEvent', value: function () { var _ref7 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee7(name, args) { return _regenerator2.default.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: _context7.next = 2; return this.ruleset; case 2: _context7.t0 = _context7.sent; if (!_context7.t0) { _context7.next = 5; break; } this.ruleset.execute([this.viewModel, this.dataModel, new Event(name, args)]); case 5: case 'end': return _context7.stop(); } } }, _callee7, this); })); function triggerEvent(_x8, _x9) { return _ref7.apply(this, arguments); } return triggerEvent; }() }, { key: 'clearData', value: function () { var _ref8 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee8() { return _regenerator2.default.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: _context8.t0 = this.viewModel; if (!_context8.t0) { _context8.next = 4; break; } _context8.next = 4; return this.viewModel.refresh((0, _extends4.default)({}, this.state.toJS(), this.module, { pid: this.pid })); case 4: this.dataModel && this.dataModel.restore(); _context8.t1 = this.viewController && this.viewController.init; if (!_context8.t1) { _context8.next = 9; break; } _context8.next = 9; return this.viewController._initData(this.dataModel); case 9: _context8.t2 = this.controller && this.controller.init; if (!_context8.t2) { _context8.next = 13; break; } _context8.next = 13; return this.controller._initData(this.dataModel); case 13: this.loadedId = null; case 14: case 'end': return _context8.stop(); } } }, _callee8, this); })); function clearData() { return _ref8.apply(this, arguments); } return clearData; }() }, { key: 'loadData', value: function () { var _ref9 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee9(id) { var cacheKey; return _regenerator2.default.wrap(function _callee9$(_context9) { while (1) { switch (_context9.prev = _context9.next) { case 0: if (!(this.loadedId === id || !id)) { _context9.next = 2; break; } return _context9.abrupt('return'); case 2: console.log('load data...', this.state.toJS()); //console.log('app.dataModel========',dataModel); // 有缓存 cacheKey = this.module.sysid + '_' + this.module.mid + ':' + id; if (!this.cache.has(cacheKey)) { _context9.next = 9; break; } _context9.next = 7; return this.dataModel.restore(this.cache.get(cacheKey)); case 7: _context9.next = 11; break; case 9: _context9.next = 11; return this.dataModel.load((0, _extends4.default)({}, this.state.toJS(), this.module, { id: id })); case 11: this.ids.add(id); this.loadedId = id; case 13: case 'end': return _context9.stop(); } } }, _callee9, this); })); function loadData(_x10) { return _ref9.apply(this, arguments); } return loadData; }() }, { key: '_close', value: function () { var _ref10 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee10(i, backIndex) { var _this5 = this; var backView, removeVm; return _regenerator2.default.wrap(function _callee10$(_context10) { while (1) { switch (_context10.prev = _context10.next) { case 0: console.log('close view...'); // 返回距这个页面最近的view backView = this.views[backIndex ? backIndex : i - 1] || { pid: null, iid: null }; removeVm = this.viewModels[i]; // 这里好像不太好,open是靠数据驱动,但是close可能会null一下 _context10.next = 5; return this.viewController.closeView(removeVm.viewModel); case 5: (0, _mobx.runInAction)(function () { _this5.views.remove(_this5.views[i]); _this5.viewModels.remove(removeVm); _this5.pid = backView.pid; _this5.iid = backView.iid; }); case 6: case 'end': return _context10.stop(); } } }, _callee10, this); })); function _close(_x11, _x12) { return _ref10.apply(this, arguments); } return _close; }() }, { key: 'popView', value: function () { var _ref11 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee11() { var i; return _regenerator2.default.wrap(function _callee11$(_context11) { while (1) { switch (_context11.prev = _context11.next) { case 0: i = this.views.length - 1; if (!(i <= 0)) { _context11.next = 3; break; } return _context11.abrupt('return'); case 3: _context11.next = 5; return this._close(i); case 5: case 'end': return _context11.stop(); } } }, _callee11, this); })); function popView() { return _ref11.apply(this, arguments); } return popView; }() }, { key: 'closeView', value: function () { var _ref12 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee12(pid) { var lastOpened, i; return _regenerator2.default.wrap(function _callee12$(_context12) { while (1) { switch (_context12.prev = _context12.next) { case 0: pid = pid || 'index'; lastOpened = [].concat((0, _toConsumableArray3.default)(this.views)).reverse().find(function (it) { return it.pid === pid; }); if (lastOpened) { _context12.next = 4; break; } return _context12.abrupt('return'); case 4: i = this.views.indexOf(lastOpened); // if (i <= 0) { // return; // } _context12.next = 7; return this._close(i); case 7: if (!(i <= 0)) { _context12.next = 10; break; } _context12.next = 10; return this.manager.close(this.module); case 10: case 'end': return _context12.stop(); } } }, _callee12, this); })); function closeView(_x13) { return _ref12.apply(this, arguments); } return closeView; }() }, { key: 'openView', value: function () { var _ref13 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee13(pid) { var _this6 = this; var action = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'PUSH'; var opened, vi, iid, view, vm, newView; return _regenerator2.default.wrap(function _callee13$(_context13) { while (1) { switch (_context13.prev = _context13.next) { case 0: if (!(action === 'REPLACE')) { _context13.next = 2; break; } return _context13.abrupt('return'); case 2: pid = pid || 'index'; // view支持多实例 opened = this.views.find(function (it) { return it.pid === pid; }); vi = _viewmodels2.default.getView((0, _extends4.default)({}, this.state.toJS(), this.module, { pid: pid })); iid = new Date().getTime(); if (!opened) { _context13.next = 17; break; } if (!(action === 'POP')) { _context13.next = 11; break; } this.pid = pid; this.iid = opened.iid; return _context13.abrupt('return', opened); case 11: if (!(vi.mode !== 'multi')) { _context13.next = 17; break; } this.views.push((0, _extends4.default)({}, opened, { iid: iid })); this.viewModels.push((0, _extends4.default)({}, this.viewModels.find(function (it) { return it.pid === pid; }), { iid: iid })); this.pid = pid; this.iid = iid; // // 采用改变栈顺序代替压栈 // const i = this.views.indexOf(opened); // const removeViews = this.views.splice(i, 1); // const removeViewModels = this.viewModels.splice(i, 1); // this.views.push(...removeViews); // this.viewModels.push(...removeViewModels); // this.pid = pid; // this.iid = opened.iid; return _context13.abrupt('return', (0, _extends4.default)({}, opened, { iid: iid })); case 17: console.log('open view...'); view = vi.type; //console.log('app.view========',view); _context13.next = 21; return _viewmodels2.default.load((0, _extends4.default)({}, this.state.toJS(), this.module, { pid: pid }), this.dataModel, this.dataList, this.singles); case 21: vm = _context13.sent; //console.log('app.viewModel========',viewModel); newView = { iid: iid, pid: pid, view: view, mode: vi.mode }; (0, _mobx.runInAction)(function () { _this6.views.push(newView); _this6.viewModels.push({ iid: iid, pid: pid, viewModel: vm, mode: vi.mode }); _this6.pid = pid; _this6.iid = iid; }); return _context13.abrupt('return', newView); case 25: case 'end': return _context13.stop(); } } }, _callee13, this); })); function openView(_x14) { return _ref13.apply(this, arguments); } return openView; }() }]); return AppInstance; }(), (_descriptor = _applyDecoratedDescriptor(_class.prototype, 'dataList', [_mobx.observable], { enumerable: true, initializer: null }), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, 'dataModel', [_mobx.observable], { enumerable: true, initializer: null }), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, 'views', [_mobx.observable], { enumerable: true, initializer: function initializer() { return []; } }), _descriptor4 = _applyDecoratedDescriptor(_class.prototype, 'viewModels', [_mobx.observable], { enumerable: true, initializer: function initializer() { return []; } }), _descriptor5 = _applyDecoratedDescriptor(_class.prototype, 'pid', [_mobx.observable], { enumerable: true, initializer: null }), _descriptor6 = _applyDecoratedDescriptor(_class.prototype, 'iid', [_mobx.observable], { enumerable: true, initializer: null }), _descriptor7 = _applyDecoratedDescriptor(_class.prototype, 'state', [_mobx.observable], { enumerable: true, initializer: function initializer() { return _mobx.observable.map({}); } }), _descriptor8 = _applyDecoratedDescriptor(_class.prototype, 'eventStack', [_mobx.observable], { enumerable: true, initializer: function initializer() { return []; } }), _applyDecoratedDescriptor(_class.prototype, 'eventHanding', [_mobx.computed], (0, _getOwnPropertyDescriptor2.default)(_class.prototype, 'eventHanding'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'view', [_mobx.computed], (0, _getOwnPropertyDescriptor2.default)(_class.prototype, 'view'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'viewModel', [_mobx.computed], (0, _getOwnPropertyDescriptor2.default)(_class.prototype, 'viewModel'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'loaded', [_mobx.computed], (0, _getOwnPropertyDescriptor2.default)(_class.prototype, 'loaded'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'updateState', [_mobx.action], (0, _getOwnPropertyDescriptor2.default)(_class.prototype, 'updateState'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'pushEvent', [_mobx.action], (0, _getOwnPropertyDescriptor2.default)(_class.prototype, 'pushEvent'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'popEvent', [_mobx.action], (0, _getOwnPropertyDescriptor2.default)(_class.prototype, 'popEvent'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'triggerEvent', [_mobx.action], (0, _getOwnPropertyDescriptor2.default)(_class.prototype, 'triggerEvent'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'clearData', [_mobx.action], (0, _getOwnPropertyDescriptor2.default)(_class.prototype, 'clearData'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'loadData', [_mobx.action], (0, _getOwnPropertyDescriptor2.default)(_class.prototype, 'loadData'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_close', [_mobx.action], (0, _getOwnPropertyDescriptor2.default)(_class.prototype, '_close'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'popView', [_mobx.action], (0, _getOwnPropertyDescriptor2.default)(_class.prototype, 'popView'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'closeView', [_mobx.action], (0, _getOwnPropertyDescriptor2.default)(_class.prototype, 'closeView'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'openView', [_mobx.action], (0, _getOwnPropertyDescriptor2.default)(_class.prototype, 'openView'), _class.prototype)), _class); var AppManager = (_class3 = function () { (0, _createClass3.default)(AppManager, [{ key: 'open', value: function () { var _ref14 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee14(_ref15) { var _this7 = this; var pid = _ref15.pid, sysid = _ref15.sysid, mid = _ref15.mid, id = _ref15.id, action = _ref15.action, state = (0, _objectWithoutProperties3.default)(_ref15, ['pid', 'sysid', 'mid', 'id', 'action']); var appKey, _app, module, appFlow, dataList, dataModel, uicontroller, controller, viewController, ruleset, app; return _regenerator2.default.wrap(function _callee14$(_context14) { while (1) { switch (_context14.prev = _context14.next) { case 0: appKey = sysid + '_' + mid; if (!this.apps.has(appKey)) { _context14.next = 12; break; } _app = this.apps.get(appKey); _app.updateState(state); _context14.next = 6; return _app.openView(pid, action); case 6: if (!id) { _context14.next = 11; break; } _context14.next = 9; return _app.loadData(id); case 9: _context14.next = 11; break; case 11: return _context14.abrupt('return', _app); case 12: console.log('open app...'); _context14.t0 = formatJson; _context14.next = 16; return _DataProviderManager2.default.getModule({ sysid: sysid, mid: mid }); case 16: _context14.t1 = _context14.sent; module = (0, _context14.t0)(_context14.t1); appFlow = _AppFlowManager2.default.getAppFlow({ sysid: sysid, mid: mid }); if (!appFlow.useUIFunction) { _context14.next = 24; break; } _context14.next = 22; return _UIFunction2.default.loadUIFunction(module); case 22: if (_UIFunction2.default.get(module.mid).has('Read')) { _context14.next = 24; break; } throw new Error(t('你没有当前模块的权限,无法打开此模块')); case 24: console.log('app.module========', module); // todo register task // DL暂时只加载一个简单的数据列表 dataList = new _datamodels.DataList(module); // 调整下加载dm和vm顺序 适应T+的后台服务 _context14.next = 28; return _datamodels.DataModel.create(module); case 28: dataModel = _context14.sent; // 加载一个业务控制器,由view发送event触发其action执行 uicontroller = new _UIController2.default(); _context14.next = 32; return _controllers2.default.createBizController(module, null, uicontroller); case 32: controller = _context14.sent; _context14.next = 35; return _controllers2.default.createViewController(module, controller, uicontroller); case 35: viewController = _context14.sent; // nools规则引擎,编写事件、数据规则,调用vm、dm和controller ruleset = void 0; if (!appFlow.useRuleEngine) { _context14.next = 41; break; } _context14.next = 40; return _ruleset2.default.load(module, { ViewModel: _viewmodels2.default, DataModel: _datamodels.DataModel, Event: Event }, { controller: controller, // 业务控制器 uicontroller: uicontroller, // 抽象 UI 控制器 viewController: viewController, // 视图控制器 uifunction: _UIFunction2.default.get(module.mid) // 功能权限 }); case 40: ruleset = _context14.sent; case 41: app = new AppInstance(this, dataList, dataModel, viewController, controller, ruleset, module, state, this.cache); // 这里不能放在构造函数中,保证业务逻辑的发生在所有app对象构造完毕之后 _context14.next = 44; return app.openView(pid); case 44: _context14.next = 46; return app.loadData(id); case 46: _context14.next = 48; return viewController._initData(dataModel); case 48: _context14.next = 50; return controller._initData(dataModel); case 50: if (appFlow.useRuleEngine) { // 执行规则计算 app.whenDisposer = (0, _mobx.reaction)(function () { return [app.viewModel, app.dataModel]; }, function (models) { return ruleset.execute(models); }); } // 需要报set集合中放到最后,有可能加载viewinfo或者datainfo时出错 if (appFlow.noCache) { console.warn('app [' + appKey + '] is forbidden cache'); // for(const it of this.apps){ // await this.close(it.module); // } } else { (0, _mobx.runInAction)(function () { _this7.apps.set(appKey, app); }); } return _context14.abrupt('return', app); case 53: case 'end': return _context14.stop(); } } }, _callee14, this); })); function open(_x16) { return _ref14.apply(this, arguments); } return open; }() // 缓存数据实例 }, { key: 'close', value: function () { var _ref16 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee15(_ref17) { var sysid = _ref17.sysid, mid = _ref17.mid; var appKey, inst, ids, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, id; return _regenerator2.default.wrap(function _callee15$(_context15) { while (1) { switch (_context15.prev = _context15.next) { case 0: appKey = sysid + '_' + mid; if (!this.apps.has(appKey)) { _context15.next = 28; break; } console.log('close app...'); inst = this.apps.get(appKey); inst.dispose(); ids = inst.ids; _iteratorNormalCompletion = true; _didIteratorError = false; _iteratorError = undefined; _context15.prev = 9; for (_iterator = (0, _getIterator3.default)(ids); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { id = _step.value; this.cache.delete(appKey + ':' + id); } _context15.next = 17; break; case 13: _context15.prev = 13; _context15.t0 = _context15['catch'](9); _didIteratorError = true; _iteratorError = _context15.t0; case 17: _context15.prev = 17; _context15.prev = 18; if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } case 20: _context15.prev = 20; if (!_didIteratorError) { _context15.next = 23; break; } throw _iteratorError; case 23: return _context15.finish(20); case 24: return _context15.finish(17); case 25: ids.clear(); this.apps.delete(appKey); //if (this.apps.length<=0){ (0, _mutantsMicrofx.dropByCacheKey)('templatePage'); //} case 28: case 'end': return _context15.stop(); } } }, _callee15, this, [[9, 13, 17, 25], [18,, 20, 24]]); })); function close(_x17) { return _ref16.apply(this, arguments); } return close; }() // TODO 废弃改用close }, { key: 'closeApp', value: function () { var _ref18 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee16() { var _args16 = arguments; return _regenerator2.default.wrap(function _callee16$(_context16) { while (1) { switch (_context16.prev = _context16.next) { case 0: this.close.apply(this, _args16); case 1: case 'end': return _context16.stop(); } } }, _callee16, this); })); function closeApp() { return _ref18.apply(this, arguments); } return closeApp; }() }, { key: 'closeView', value: function () { var _ref19 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee17(_ref20) { var pid = _ref20.pid, sysid = _ref20.sysid, mid = _ref20.mid; var appKey, app; return _regenerator2.default.wrap(function _callee17$(_context17) { while (1) { switch (_context17.prev = _context17.next) { case 0: appKey = sysid + '_' + mid; if (!this.apps.has(appKey)) { _context17.next = 11; break; } app = this.apps.get(appKey); if (!pid) { _context17.next = 8; break; } _context17.next = 6; return app.closeView(pid); case 6: _context17.next = 10; break; case 8: _context17.next = 10; return app.popView(); case 10: return _context17.abrupt('return', app); case 11: case 'end': return _context17.stop(); } } }, _callee17, this); })); function closeView(_x18) { return _ref19.apply(this, arguments); } return closeView; }() }]); function AppManager() { var _this8 = this; (0, _classCallCheck3.default)(this, AppManager); _initDefineProp(this, 'apps', _descriptor9, this); this.cache = new _map2.default(); var lastMatch = void 0; var tasks = []; var isActiving = false; var executeTask = function () { var _ref21 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee18() { var it; return _regenerator2.default.wrap(function _callee18$(_context18) { while (1) { switch (_context18.prev = _context18.next) { case 0: it = tasks[0]; if (it) { _context18.next = 4; break; } isActiving = false; return _context18.abrupt('return'); case 4: isActiving = true; _context18.next = 7; return it(); case 7: tasks.shift(); _context18.next = 10; return executeTask(); case 10: case 'end': return _context18.stop(); } } }, _callee18, _this8); })); return function executeTask() { return _ref21.apply(this, arguments); }; }(); var addTask = function addTask() { tasks.push.apply(tasks, arguments); if (!isActiving) { executeTask().catch(console.error); } }; _mutantsMicrofx.history.listen(function (location, action) { var _querystring$parse = _querystring2.default.parse(location.search.substr(1)), pid = _querystring$parse.pid; var match = (0, _matchPath2.default)(location.pathname, { path: '/:sysid/:mid/:id?' }); if (!match) { lastMatch = match; return; } var app = _this8.apps.get(match.params.sysid + '_' + match.params.mid); if (app) { // for (const v of app.views) { // // 多实例视图切换页面自动关闭 // if (v.mode === 'multi' && v.pid !== pid) { // app.closeView(v.pid).catch(console.error); // } // } if (action === 'POP') { addTask((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee19() { var curIndex, removes, i, l, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, _pid; return _regenerator2.default.wrap(function _callee19$(_context19) { while (1) { switch (_context19.prev = _context19.next) { case 0: curIndex = app.views.indexOf([].concat((0, _toConsumableArray3.default)(app.views)).reverse().find(function (it) { return it.pid === (pid || 'index'); })); removes = []; for (i = curIndex + 1, l = app.views.length; i < l; i++) { if (app.views[i].mode !== 'single') { removes.push(app.views[i].pid); } } _iteratorNormalCompletion2 = true; _didIteratorError2 = false; _iteratorError2 = undefined; _context19.prev = 6; _iterator2 = (0, _getIterator3.default)(removes); case 8: if (_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done) { _context19.next = 15; break; } _pid = _step2.value; _context19.next = 12; return app.closeView(_pid); case 12: _iteratorNormalCompletion2 = true; _context19.next = 8; break; case 15: _context19.next = 21; break; case 17: _context19.prev = 17; _context19.t0 = _context19['catch'](6); _didIteratorError2 = true; _iteratorError2 = _context19.t0; case 21: _context19.prev = 21; _context19.prev = 22; if (!_iteratorNormalCompletion2 && _iterator2.return) { _iterator2.return(); } case 24: _context19.prev = 24; if (!_didIteratorError2) { _context19.next = 27; break; } throw _iteratorError2; case 27: return _context19.finish(24); case 28: return _context19.finish(21); case 29: case 'end': return _context19.stop(); } } }, _callee19, _this8, [[6, 17, 21, 29], [22,, 24, 28]]); }))); } else if (action === 'REPLACE' && lastMatch) { // 需要先开后关,保证不会出现白屏 addTask((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee20() { var curIndex, removeIndexs, i, l, view, _iteratorNormalCompletion3, _didIteratorError3, _iteratorError3, _iterator3, _step3, _i; return _regenerator2.default.wrap(function _callee20$(_context20) { while (1) { switch (_context20.prev = _context20.next) { case 0: curIndex = app.views.indexOf([].concat((0, _toConsumableArray3.default)(app.views)).reverse().find(function (it) { return it.pid === (app.pid || 'index'); })); removeIndexs = []; for (i = curIndex, l = app.views.length; i < l; i++) { if (app.views[i].mode !== 'single') { removeIndexs.push(i); } } app.updateState(location.state); _context20.next = 6; return app.openView(pid); case 6: view = _context20.sent; _iteratorNormalCompletion3 = true; _didIteratorError3 = false; _iteratorError3 = undefined; _context20.prev = 10; _iterator3 = (0, _getIterator3.default)(removeIndexs); case 12: if (_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done) { _context20.next = 19; break; } _i = _step3.value; _context20.next = 16; return app._close(_i, app.views.findIndex(function (v) { return v.iid === view.iid; })); case 16: _iteratorNormalCompletion3 = true; _context20.next = 12; break