@turbox3d/command-manager
Version:
Large-scale productivity application command management library
118 lines (117 loc) • 5.06 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Command = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _Interaction2 = require("./Interaction");
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(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; })(); }
var Command = exports.Command = /*#__PURE__*/function (_Interaction) {
function Command(holder, parent) {
var _this;
(0, _classCallCheck2["default"])(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);
}
// 便利激活的子节点,置为注销状态
(0, _toConsumableArray2["default"])(_this.activeChildren).forEach(function (child) {
return child.dispose.apply(child, args);
});
// 处理自定义工作
disposeFunc.apply(void 0, args);
};
return _this;
}
/**
* 应用当前 Command 及其子级,该方法不仅会强制激活当前 Command 及其子级,还会使它们的交互事件生效
* 同时只能有一个 Command 节点处于应用状态(兄弟节点的交互事件是互斥的)
* 永远不用重写该方法,相关初始化逻辑请在 active 接口中编码
*/
(0, _inherits2["default"])(Command, _Interaction);
return (0, _createClass2["default"])(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);
}
}
}]);
}(_Interaction2.Interaction);