UNPKG

@turbox3d/command-manager

Version:

Large-scale productivity application command management library

111 lines 4.58 kB
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/esm/inherits"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } import { Interaction } from './Interaction'; export var Command = /*#__PURE__*/function (_Interaction) { function Command(holder, parent) { var _this; _classCallCheck(this, Command); _this = _callSuper(this, Command); _this.holder = holder; _this.parent = parent; _this.$children = {}; _this.activeChildren = []; var activeFunc = _this.active.bind(_this); var disposeFunc = _this.dispose.bind(_this); _this.active = function () { if (_this.parent && !_this.holder.isCommandActive(_this)) { // 仅作为激活的子 Command 记录在父 Command 上 _this.parent.onChildActive(_this); } // 先执行自定义的 active 的相关工作 activeFunc.apply(void 0, arguments); }; _this.dispose = function () { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var _a; // 如果自身是应用在 Mgr 上的 Command,则要清理掉 if (_this.holder.isCommandActive(_this)) { _this.holder.clearCommand(); } else { // 将自己改为注销状态 (_a = _this.parent) === null || _a === void 0 ? void 0 : _a.onChildDispose(_this); } // 便利激活的子节点,置为注销状态 _toConsumableArray(_this.activeChildren).forEach(function (child) { return child.dispose.apply(child, args); }); // 处理自定义工作 disposeFunc.apply(void 0, args); }; return _this; } /** * 应用当前 Command 及其子级,该方法不仅会强制激活当前 Command 及其子级,还会使它们的交互事件生效 * 同时只能有一个 Command 节点处于应用状态(兄弟节点的交互事件是互斥的) * 永远不用重写该方法,相关初始化逻辑请在 active 接口中编码 */ _inherits(Command, _Interaction); return _createClass(Command, [{ key: "apply", value: function apply() { this.holder.toggleCommand(this); this.active.apply(this, arguments); } /** * 激活当前 Command 及其子级,为即将到来的交互事件做好准备 */ }, { key: "active", value: function active() { for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } // 将子 command 也激活 Object.values(this.$children).forEach(function (command) { return command.active.apply(command, args); }); } /** * 注销当前 Command 及其子级,同时将停止响应该指令及子级的交互事件 */ }, { key: "dispose", value: function dispose() { // } }, { key: "distributeEvent", value: function distributeEvent(eventType, ev, event, tools) { // step 1 先让子 Commands 处理事件 // Object.values(this.$children).forEach(command => command.distributeEvent(eventType, ev, event)); this.activeChildren.forEach(function (command) { return command.distributeEvent(eventType, ev, event, tools); }); // step2 自身处理事件 this.distributeToSelf(eventType, ev, event, tools); } }, { key: "onChildActive", value: function onChildActive(command) { if (this.activeChildren.indexOf(command) === -1) { this.activeChildren.push(command); } } }, { key: "onChildDispose", value: function onChildDispose(command) { var index = this.activeChildren.indexOf(command); if (index !== -1) { this.activeChildren.splice(index, 1); } } }]); }(Interaction);