6656205a7aea006cd32de70b2fd93bd8024d6e4fb91a618c9fc1b42690e1118cfc77bbb5e6751e03169103c0a0678f15d3aefc883e0759d7d330d789609f83 2.5 KB

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