UNPKG

@difizen/mana-core

Version:

205 lines (203 loc) 11.1 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } var _dec, _dec2, _class, _class2, _descriptor, _dec3, _dec4, _dec5, _dec6, _dec7, _class3, _class4, _descriptor2, _descriptor3, _descriptor4, _descriptor5; function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(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.defineProperty(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 and runs after the decorators transform.'); } /* eslint-disable @typescript-eslint/no-unused-vars */ import { Emitter } from '@difizen/mana-common'; import { getOrigin, prop } from '@difizen/mana-observable'; import { inject, transient } from '@difizen/mana-syringe'; import { CommandRegistry } from "../command/command-registry"; import { renderNode } from "../view/utils"; import { CommandMenuNode, ExecutableMenuNode, MenuSymbol } from "./menu-protocol"; /** * Node representing a (sub)menu in the menu tree structure. */ export var DefaultActionMenuItem = (_dec = transient(), _dec2 = prop(), _dec(_class = (_class2 = /*#__PURE__*/function () { function DefaultActionMenuItem(commands, node, parentPath) { var _this = this; _classCallCheck(this, DefaultActionMenuItem); _initializerDefineProperty(this, "order", _descriptor, this); this.disposedEventEmitter = new Emitter(); this.onDisposed = this.disposedEventEmitter.event; this.doCommandExecute = function () { if (_this.command) { var _this$commands; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return (_this$commands = _this.commands).executeCommand.apply(_this$commands, [_this.command].concat(args)); } }; this.renderTitle = function () { var label = _this.label; if (!label && _this.command) { var _this$commands$getCom; label = (_this$commands$getCom = _this.commands.getCommand(_this.command)) === null || _this$commands$getCom === void 0 ? void 0 : _this$commands$getCom.label; } return renderNode(label); }; this.renderIcon = function () { var icon = _this.icon; if (!icon && _this.command) { var _this$commands$getCom2; icon = (_this$commands$getCom2 = _this.commands.getCommand(_this.command)) === null || _this$commands$getCom2 === void 0 ? void 0 : _this$commands$getCom2.icon; } return renderNode(icon); }; this.commands = commands; this.node = node; this.parentPath = parentPath; this.order = node.order; this.label = node.label; this.icon = node.icon; this.id = node.id; this.key = parentPath.join('/') + '/' + this.id; if (CommandMenuNode.is(node)) { this.command = node.command; this.execute = this.doCommandExecute; } if (ExecutableMenuNode.is(node)) { this.execute = node.execute; this.isEnabled = node.isEnabled; this.isVisible = node.isVisible; this.isActive = node.isActive; } } DefaultActionMenuItem = inject(MenuSymbol.ParentPathSymbol)(DefaultActionMenuItem, undefined, 2) || DefaultActionMenuItem; DefaultActionMenuItem = inject(MenuSymbol.ActionMenuNodeSymbol)(DefaultActionMenuItem, undefined, 1) || DefaultActionMenuItem; DefaultActionMenuItem = inject(CommandRegistry)(DefaultActionMenuItem, undefined, 0) || DefaultActionMenuItem; _createClass(DefaultActionMenuItem, [{ key: "dispose", value: function dispose() { this.disposedEventEmitter.fire(); this.disposed = true; } }]); return DefaultActionMenuItem; }(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "order", [_dec2], { configurable: true, enumerable: true, writable: true, initializer: null })), _class2)) || _class); /** * Node representing a (sub)menu in the menu tree structure. */ export var DefaultGeneralMenuItem = (_dec3 = transient(), _dec4 = prop(), _dec5 = prop(), _dec6 = prop(), _dec7 = prop(), _dec3(_class3 = (_class4 = /*#__PURE__*/function () { function DefaultGeneralMenuItem(commands, node, parentPath) { var _this2 = this; _classCallCheck(this, DefaultGeneralMenuItem); _initializerDefineProperty(this, "children", _descriptor2, this); _initializerDefineProperty(this, "order", _descriptor3, this); /** * Optional label for the item. */ _initializerDefineProperty(this, "label", _descriptor4, this); /** * Optional icon for the item. */ _initializerDefineProperty(this, "icon", _descriptor5, this); this.disposedEventEmitter = new Emitter(); this.onDisposed = this.disposedEventEmitter.event; /** * Inserts the given node at the position indicated by `sortString`. * * @returns a disposable which, when called, will remove the given node again. */ this.addNode = function (item) { _this2.children.push(item); var remove = function remove() { var idx = _this2.children.indexOf(getOrigin(item)); if (idx >= 0) { _this2.children.splice(idx, 1); } }; item.onDisposed(remove); return item; }; this.renderTitle = function () { return renderNode(_this2.label); }; this.renderIcon = function () { return renderNode(_this2.icon); }; this.commands = commands; this.node = node; this.parentPath = parentPath; this.order = node.order; this.label = node.label; this.icon = node.icon; this.id = node.id; this.key = parentPath.join('/') + '/' + this.id; this.path = [].concat(_toConsumableArray(parentPath), [this.id]); } DefaultGeneralMenuItem = inject(MenuSymbol.ParentPathSymbol)(DefaultGeneralMenuItem, undefined, 2) || DefaultGeneralMenuItem; DefaultGeneralMenuItem = inject(MenuSymbol.MenuNodeSymbol)(DefaultGeneralMenuItem, undefined, 1) || DefaultGeneralMenuItem; DefaultGeneralMenuItem = inject(CommandRegistry)(DefaultGeneralMenuItem, undefined, 0) || DefaultGeneralMenuItem; _createClass(DefaultGeneralMenuItem, [{ key: "isSubmenu", get: function get() { return !!this.label; } }, { key: "removeNode", value: /** * Removes the first node with the given id. * * @param id node id. */ function removeNode(id) { var node = this.children.find(function (n) { return n.id === id; }); if (node) { var idx = this.children.indexOf(node); if (idx >= 0) { this.children.splice(idx, 1); } } } }, { key: "dispose", value: function dispose() { this.disposedEventEmitter.fire(); this.disposed = true; } }]); return DefaultGeneralMenuItem; }(), (_descriptor2 = _applyDecoratedDescriptor(_class4.prototype, "children", [_dec4], { configurable: true, enumerable: true, writable: true, initializer: function initializer() { return []; } }), _descriptor3 = _applyDecoratedDescriptor(_class4.prototype, "order", [_dec5], { configurable: true, enumerable: true, writable: true, initializer: null }), _descriptor4 = _applyDecoratedDescriptor(_class4.prototype, "label", [_dec6], { configurable: true, enumerable: true, writable: true, initializer: null }), _descriptor5 = _applyDecoratedDescriptor(_class4.prototype, "icon", [_dec7], { configurable: true, enumerable: true, writable: true, initializer: null })), _class4)) || _class3);