commandExecutor.js 3.8 KB

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