UNPKG

@difizen/mana-core

Version:

301 lines (294 loc) 14.5 kB
"use strict"; 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); } Object.defineProperty(exports, "__esModule", { value: true }); exports.MenuRegistry = exports.MenuContribution = void 0; var _manaCommon = require("@difizen/mana-common"); var _manaSyringe = require("@difizen/mana-syringe"); var _application = require("../application/application"); var _commandRegistry = require("../command/command-registry"); var _menuProtocol = require("./menu-protocol"); var _dec, _class; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } 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 _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 _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); } var MenuContribution = exports.MenuContribution = _manaSyringe.Syringe.defineToken('MenuContribution'); /** * Register and unregister menus, submenus and actions */ var MenuRegistry = exports.MenuRegistry = (_dec = (0, _manaSyringe.singleton)({ contrib: _application.ApplicationContribution }), _dec(_class = /*#__PURE__*/function () { function MenuRegistry(contributions, generalItemFactory, actionItemFactory, commands) { _classCallCheck(this, MenuRegistry); this.contributions = contributions; this.generalItemFactory = generalItemFactory; this.actionItemFactory = actionItemFactory; this.commands = commands; this.root = generalItemFactory({ id: '' }, []); } MenuRegistry = (0, _manaSyringe.inject)(_commandRegistry.CommandRegistry)(MenuRegistry, undefined, 3) || MenuRegistry; MenuRegistry = (0, _manaSyringe.inject)(_menuProtocol.ActionMenuItemFactory)(MenuRegistry, undefined, 2) || MenuRegistry; MenuRegistry = (0, _manaSyringe.inject)(_menuProtocol.GeneralMenuItemFactory)(MenuRegistry, undefined, 1) || MenuRegistry; MenuRegistry = (0, _manaSyringe.contrib)(MenuContribution)(MenuRegistry, undefined, 0) || MenuRegistry; _createClass(MenuRegistry, [{ key: "onStart", value: function onStart() { var _iterator = _createForOfIteratorHelper(this.contributions.getContributions()), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var contribution = _step.value; contribution.registerMenus(this); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } /** * Adds the given menu action to the menu denoted by the given path. * * @returns a disposable which, when called, will remove the menu action again. */ }, { key: "registerMenuAction", value: function registerMenuAction(menuPath, item) { var menuNode = this.actionItemFactory(item, menuPath); var parent = this.getOrCreateGroup(menuPath); return parent.addNode(menuNode); } /** * Adds the given menu node to the menu denoted by the given path. * * @returns a disposable which, when called, will remove the menu node again. */ }, { key: "registerMenuNode", value: function registerMenuNode(menuPath, item) { var menuNode = this.generalItemFactory(item, menuPath); var parent = this.getOrCreateGroup(menuPath); return parent.addNode(menuNode); } }, { key: "registerGroupMenu", value: function registerGroupMenu(menuPath) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (menuPath.length === 0) { throw new Error('The group menu path cannot be empty.'); } var index = menuPath.length - 1; var menuId = menuPath[index]; var groupPath = index === 0 ? [] : menuPath.slice(0, index); var parent = this.getOrCreateGroup(groupPath, options); var groupNode = this.getOrCreateSub(parent, menuId, options); if (!groupNode) { groupNode = this.generalItemFactory(_objectSpread({ id: menuId }, options), parent.path); return parent.addNode(groupNode); } if (options.label) { groupNode.label = options.label; } if (options.icon) { groupNode.icon = options.icon; } if (options.order) { groupNode.order = options.order; } return _manaCommon.Disposable.NONE; } /** * Register a new menu at the given path with the given label. * (If the menu already exists without a label, iconClass or order this method can be used to set them.) * * @param menuPath the path for which a new submenu shall be registered. * @param label the label to be used for the new submenu. * @param options optionally allows to set an icon class and specify the order of the new menu. * * @returns if the menu was successfully created a disposable will be returned which, * when called, will remove the menu again. If the menu already existed a no-op disposable * will be returned. * * Note that if the menu already existed and was registered with a different label an error * will be thrown. */ }, { key: "registerSubmenu", value: function registerSubmenu(menuPath) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (menuPath.length === 0) { throw new Error('The sub menu path cannot be empty.'); } var index = menuPath.length - 1; var menuId = menuPath[index]; var groupPath = index === 0 ? [] : menuPath.slice(0, index); var parent = this.getOrCreateGroup(groupPath, options); var groupNode = this.getOrCreateSub(parent, menuId, options); if (!groupNode) { groupNode = this.generalItemFactory(_objectSpread({ id: menuId }, options), parent.path); return parent.addNode(groupNode); } if (options.label) { groupNode.label = options.label; } if (options.icon) { groupNode.icon = options.icon; } if (options.order) { groupNode.order = options.order; } return _manaCommon.Disposable.NONE; } /** * Unregister all menu nodes with the same id as the given menu action. * * @param item the item whose id will be used. * @param menuPath if specified only nodes within the path will be unregistered. */ }, { key: "unregisterMenuAction", value: function unregisterMenuAction(itemOrCommandOrId, menuPath) { var id = _menuProtocol.MenuNode.is(itemOrCommandOrId) ? itemOrCommandOrId.id : itemOrCommandOrId; if (menuPath) { var parent = this.getOrCreateGroup(menuPath); parent.removeNode(id); return; } this.unregisterMenuNode(id); } /** * Recurse all menus, removing any menus matching the `id`. * * @param id technical identifier of the `MenuNode`. */ }, { key: "unregisterMenuNode", value: function unregisterMenuNode(id) { var recurse = function recurse(root) { root.children.forEach(function (node) { if (_menuProtocol.MenuItem.isGeneralMenuItem(node)) { node.removeNode(id); recurse(node); } }); }; recurse(this.root); } }, { key: "getOrCreateGroup", value: function getOrCreateGroup(menuPath, options) { var currentMenu = this.root; var _iterator2 = _createForOfIteratorHelper(menuPath), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var segment = _step2.value; currentMenu = this.getOrCreateSub(currentMenu, segment, options); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } return currentMenu; } }, { key: "getOrCreateSub", value: function getOrCreateSub(current, menuId) { var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; var sub = current.children.find(function (e) { return e.id === menuId; }); if (_menuProtocol.MenuItem.isGeneralMenuItem(sub)) { return sub; } if (sub) { throw new Error("'".concat(menuId, "' is not a menu group.")); } var newSub = this.generalItemFactory(_objectSpread({ id: menuId }, options), current.path); current.addNode(newSub); return newSub; } /** * Returns the menu at the given path. * * @param menuPath the path specifying the menu to return. If not given the empty path will be used. * * @returns the root menu when `menuPath` is empty. If `menuPath` is not empty the specified menu is * returned if it exists, otherwise an error is thrown. */ }, { key: "getMenu", value: function getMenu() { var menuPath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; return this.getOrCreateGroup(menuPath); } }, { key: "isVisible", value: function isVisible(item) { for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } if (_menuProtocol.ExecutableMenuNode.is(item)) { if (item.isVisible) { return item.isVisible.apply(item, args); } } if (_menuProtocol.CommandMenuNode.is(item)) { var _this$commands; return (_this$commands = this.commands).isVisible.apply(_this$commands, [item.command].concat(args)); } return true; } }, { key: "isEnabled", value: function isEnabled(item) { for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { args[_key2 - 1] = arguments[_key2]; } if (_menuProtocol.ExecutableMenuNode.is(item)) { if (item.isEnabled) { return item.isEnabled.apply(item, args); } } if (_menuProtocol.CommandMenuNode.is(item)) { var _this$commands2; return (_this$commands2 = this.commands).isVisible.apply(_this$commands2, [item.command].concat(args)); } return true; } }, { key: "execute", value: function execute(item) { for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { args[_key3 - 1] = arguments[_key3]; } if (_menuProtocol.ExecutableMenuNode.is(item)) { return item.execute.apply(item, args); } if (_menuProtocol.CommandMenuNode.is(item)) { var _this$commands3; return (_this$commands3 = this.commands).executeCommand.apply(_this$commands3, [item.command].concat(args)); } } }]); return MenuRegistry; }()) || _class);