UNPKG

@difizen/mana-app

Version:

234 lines (233 loc) 12.2 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, _dec3, _dec4, _class, _class2, _descriptor, _descriptor2, _descriptor3; 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.'); } import { DisposableCollection, Emitter } from '@difizen/mana-common'; import { MenuItem } from '@difizen/mana-core'; import { CommandRegistry, MenuRegistry } from '@difizen/mana-core'; import { prop } from '@difizen/mana-observable'; import { Menu as MenuComponent } from '@difizen/mana-react'; import { inject, transient } from '@difizen/mana-syringe'; import { MenuItemRender } from "./menu-item-render"; import { jsx as _jsx } from "react/jsx-runtime"; export var MenuPathSymbol = Symbol('MenuPathSymbol'); /** * Factory for instantiating toolbars. */ export var MenuFactory = Symbol('ToolbarFactory'); /** * The toolbar manager, a toolbar instance should be created with view. */ export var Menu = (_dec = transient(), _dec2 = prop(), _dec3 = prop(), _dec4 = prop(), _dec(_class = (_class2 = /*#__PURE__*/function () { function Menu(commands, menus, menuPath) { var _this = this; _classCallCheck(this, Menu); this.toDispose = new DisposableCollection(); this._disposed = false; this.disposedEventEmitter = new Emitter(); this.onDisposed = this.disposedEventEmitter.event; _initializerDefineProperty(this, "current", _descriptor, this); _initializerDefineProperty(this, "states", _descriptor2, this); _initializerDefineProperty(this, "root", _descriptor3, this); this.sort = function (left, right) { var _left$order, _right$order; var leftOrder = (_left$order = left.order) !== null && _left$order !== void 0 ? _left$order : left.id; var rightOrder = (_right$order = right.order) !== null && _right$order !== void 0 ? _right$order : right.id; return leftOrder.localeCompare(rightOrder); }; this.setState = function (item) { _this.states.set(item.id, item); }; this.getState = function (item) { var currentState = _this.states.get(item.id); return currentState; }; this.renderMenuList = function (list) { var inMenu = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var childNodes = _this.doRenderList(list); if (inMenu) { return childNodes; } return /*#__PURE__*/_jsx(MenuComponent, { children: childNodes }); }; this.commands = commands; this.menus = menus; this.menuPath = menuPath; this.menuPath = menuPath; this.root = this.menus.getMenu(this.menuPath); } Menu = inject(MenuPathSymbol)(Menu, undefined, 2) || Menu; Menu = inject(MenuRegistry)(Menu, undefined, 1) || Menu; Menu = inject(CommandRegistry)(Menu, undefined, 0) || Menu; _createClass(Menu, [{ key: "disposed", get: function get() { return this._disposed; } }, { key: "setCurrent", value: function setCurrent(current) { this.current = current; } }, { key: "commandIsEnabled", value: function commandIsEnabled(command) { return this.commands.isEnabled(command, this.current); } }, { key: "execute", value: function execute(item) { var _this$states$get; var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.current; if ((_this$states$get = this.states.get(item.id)) !== null && _this$states$get !== void 0 && _this$states$get.enable) { var _this$commands; var args = data; if (!Array.isArray(data)) { args = [data]; } return (_this$commands = this.commands).executeCommandByHandler.apply(_this$commands, [item, item.command].concat(_toConsumableArray(args))); } return undefined; } }, { key: "removeItemState", value: function removeItemState(key) { this.states.delete(key); } }, { key: "isGroup", value: function isGroup(item) { return MenuItem.isGeneralMenuItem(item) && !item.isSubmenu; } }, { key: "doRenderList", value: function doRenderList(list) { var _this2 = this; var childNodes = []; var lastItemIsDivider = false; for (var index = 0; index < list.length; index++) { var child = list[index]; if (MenuItem.isGeneralMenuItem(child)) { if (this.isGroup(child)) { if (index > 0 && !lastItemIsDivider) { childNodes.push( /*#__PURE__*/_jsx(MenuComponent.Divider, {}, "".concat(child.id, "-divider-before"))); lastItemIsDivider = true; } var visibleChildren = child.children.filter(function (item) { return _this2.isVisible(item); }); if (visibleChildren.length === 0) { continue; } var children = this.renderMenuList(_toConsumableArray(visibleChildren).sort(this.sort), true); if (children instanceof Array) { childNodes.push.apply(childNodes, _toConsumableArray(children)); } else { childNodes.push(children); } lastItemIsDivider = false; if (index < list.length - 1 && !this.isGroup(list[index + 1]) && !lastItemIsDivider) { childNodes.push( /*#__PURE__*/_jsx(MenuComponent.Divider, {}, "".concat(child.id, "-divider-after"))); lastItemIsDivider = true; } continue; } } childNodes.push( /*#__PURE__*/_jsx(MenuItemRender, { item: child, root: false }, child.id)); lastItemIsDivider = false; } return childNodes; } }, { key: "renderMenuItem", value: function renderMenuItem(item) { var _this3 = this; var root = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var content = null; if (MenuItem.isGeneralMenuItem(item)) { var sorted = _toConsumableArray(item.children).sort(this.sort); if (item.isSubmenu) { content = /*#__PURE__*/_jsx(MenuComponent.SubMenu, { text: item.renderTitle(), children: this.renderMenuList(sorted, true) }, item.id); } else { content = this.renderMenuList(sorted, true); } } if (MenuItem.isActionMenuItem(item)) { if (!this.isVisible(item)) { return null; } return /*#__PURE__*/_jsx(MenuComponent.Item, { disabled: !this.isEnable(item), icon: item.renderIcon(), onClick: function onClick() { _this3.execute(item); }, children: item.renderTitle() }, item.id); } if (root) { return /*#__PURE__*/_jsx(MenuComponent, { children: content }); } return content; } }, { key: "dispose", value: function dispose() { this.states.clear(); this.toDispose.dispose(); this._disposed = true; } }, { key: "isVisible", value: function isVisible(item) { var _this$states$get$visi, _this$states$get2; return (_this$states$get$visi = (_this$states$get2 = this.states.get(item.id)) === null || _this$states$get2 === void 0 ? void 0 : _this$states$get2.visible) !== null && _this$states$get$visi !== void 0 ? _this$states$get$visi : false; } }, { key: "isEnable", value: function isEnable(item) { var _this$states$get$enab, _this$states$get3; return (_this$states$get$enab = (_this$states$get3 = this.states.get(item.id)) === null || _this$states$get3 === void 0 ? void 0 : _this$states$get3.enable) !== null && _this$states$get$enab !== void 0 ? _this$states$get$enab : false; } }]); return Menu; }(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "current", [_dec2], { configurable: true, enumerable: true, writable: true, initializer: null }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "states", [_dec3], { configurable: true, enumerable: true, writable: true, initializer: function initializer() { return new Map(); } }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "root", [_dec4], { configurable: true, enumerable: true, writable: true, initializer: null })), _class2)) || _class);