commandExecutor.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _createClass = 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, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  4. var _array = require('./../../helpers/array');
  5. var _object = require('./../../helpers/object');
  6. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  7. /**
  8. * Command executor for ContextMenu.
  9. *
  10. * @class CommandExecutor
  11. * @plugin ContextMenu
  12. */
  13. var CommandExecutor = function () {
  14. function CommandExecutor(hotInstance) {
  15. _classCallCheck(this, CommandExecutor);
  16. this.hot = hotInstance;
  17. this.commands = {};
  18. this.commonCallback = null;
  19. }
  20. /**
  21. * Register command.
  22. *
  23. * @param {String} name Command name.
  24. * @param {Object} commandDescriptor Command descriptor object with properties like `key` (command id),
  25. * `callback` (task to execute), `name` (command name), `disabled` (command availability).
  26. */
  27. _createClass(CommandExecutor, [{
  28. key: 'registerCommand',
  29. value: function registerCommand(name, commandDescriptor) {
  30. this.commands[name] = commandDescriptor;
  31. }
  32. /**
  33. * Set common callback which will be trigger on every executed command.
  34. *
  35. * @param {Function} callback Function which will be fired on every command execute.
  36. */
  37. }, {
  38. key: 'setCommonCallback',
  39. value: function setCommonCallback(callback) {
  40. this.commonCallback = callback;
  41. }
  42. /**
  43. * Execute command by its name.
  44. *
  45. * @param {String} commandName Command id.
  46. * @param {*} params Arguments passed to command task.
  47. */
  48. }, {
  49. key: 'execute',
  50. value: function execute(commandName) {
  51. var _this = this;
  52. for (var _len = arguments.length, params = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  53. params[_key - 1] = arguments[_key];
  54. }
  55. var commandSplit = commandName.split(':');
  56. commandName = commandSplit[0];
  57. var subCommandName = commandSplit.length === 2 ? commandSplit[1] : null;
  58. var command = this.commands[commandName];
  59. if (!command) {
  60. throw new Error('Menu command \'' + commandName + '\' not exists.');
  61. }
  62. if (subCommandName && command.submenu) {
  63. command = findSubCommand(subCommandName, command.submenu.items);
  64. }
  65. if (command.disabled === true) {
  66. return;
  67. }
  68. if (typeof command.disabled == 'function' && command.disabled.call(this.hot) === true) {
  69. return;
  70. }
  71. if ((0, _object.hasOwnProperty)(command, 'submenu')) {
  72. return;
  73. }
  74. var callbacks = [];
  75. if (typeof command.callback === 'function') {
  76. callbacks.push(command.callback);
  77. }
  78. if (typeof this.commonCallback === 'function') {
  79. callbacks.push(this.commonCallback);
  80. }
  81. params.unshift(commandSplit.join(':'));
  82. (0, _array.arrayEach)(callbacks, function (callback) {
  83. return callback.apply(_this.hot, params);
  84. });
  85. }
  86. }]);
  87. return CommandExecutor;
  88. }();
  89. function findSubCommand(subCommandName, subCommands) {
  90. var command = void 0;
  91. (0, _array.arrayEach)(subCommands, function (cmd) {
  92. var cmds = cmd.key ? cmd.key.split(':') : null;
  93. if (Array.isArray(cmds) && cmds[1] === subCommandName) {
  94. command = cmd;
  95. return false;
  96. }
  97. });
  98. return command;
  99. }
  100. exports.default = CommandExecutor;