command.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. define(function(require, exports, module) {
  2. var kity = require('./kity');
  3. var utils = require('./utils');
  4. var Minder = require('./minder');
  5. var MinderNode = require('./node');
  6. var MinderEvent = require('./event');
  7. var COMMAND_STATE_NORMAL = 0;
  8. var COMMAND_STATE_DISABLED = -1;
  9. var COMMAND_STATE_ACTIVED = 1;
  10. /**
  11. * 表示一个命令,包含命令的查询及执行
  12. */
  13. var Command = kity.createClass('Command', {
  14. constructor: function() {
  15. this._isContentChange = true;
  16. this._isSelectionChange = false;
  17. },
  18. execute: function(minder, args) {
  19. throw new Error('Not Implement: Command.execute()');
  20. },
  21. setContentChanged: function(val) {
  22. this._isContentChange = !!val;
  23. },
  24. isContentChanged: function() {
  25. return this._isContentChange;
  26. },
  27. setSelectionChanged: function(val) {
  28. this._isSelectionChange = !!val;
  29. },
  30. isSelectionChanged: function() {
  31. return this._isContentChange;
  32. },
  33. queryState: function(km) {
  34. return COMMAND_STATE_NORMAL;
  35. },
  36. queryValue: function(km) {
  37. return 0;
  38. },
  39. isNeedUndo: function() {
  40. return true;
  41. }
  42. });
  43. Command.STATE_NORMAL = COMMAND_STATE_NORMAL;
  44. Command.STATE_ACTIVE = COMMAND_STATE_ACTIVED;
  45. Command.STATE_DISABLED = COMMAND_STATE_DISABLED;
  46. kity.extendClass(Minder, {
  47. _getCommand: function(name) {
  48. return this._commands[name.toLowerCase()];
  49. },
  50. _queryCommand: function(name, type, args) {
  51. var cmd = this._getCommand(name);
  52. if (cmd) {
  53. var queryCmd = cmd['query' + type];
  54. if (queryCmd)
  55. return queryCmd.apply(cmd, [this].concat(args));
  56. }
  57. return 0;
  58. },
  59. /**
  60. * @method queryCommandState()
  61. * @for Minder
  62. * @description 查询指定命令的状态
  63. *
  64. * @grammar queryCommandName(name) => {number}
  65. *
  66. * @param {string} name 要查询的命令名称
  67. *
  68. * @return {number}
  69. * -1: 命令不存在或命令当前不可用
  70. * 0: 命令可用
  71. * 1: 命令当前可用并且已经执行过
  72. */
  73. queryCommandState: function(name) {
  74. return this._queryCommand(name, 'State', [].slice.call(arguments, 1));
  75. },
  76. /**
  77. * @method queryCommandValue()
  78. * @for Minder
  79. * @description 查询指定命令当前的执行值
  80. *
  81. * @grammar queryCommandValue(name) => {any}
  82. *
  83. * @param {string} name 要查询的命令名称
  84. *
  85. * @return {any}
  86. * 如果命令不存在,返回 undefined
  87. * 不同命令具有不同返回值,具体请查看 [Command](command) 章节
  88. */
  89. queryCommandValue: function(name) {
  90. return this._queryCommand(name, 'Value', [].slice.call(arguments, 1));
  91. },
  92. /**
  93. * @method execCommand()
  94. * @for Minder
  95. * @description 执行指定的命令。
  96. *
  97. * @grammar execCommand(name, args...)
  98. *
  99. * @param {string} name 要执行的命令名称
  100. * @param {argument} args 要传递给命令的其它参数
  101. */
  102. execCommand: function(name) {
  103. if (!name) return null;
  104. name = name.toLowerCase();
  105. var cmdArgs = [].slice.call(arguments, 1),
  106. cmd, stoped, result, eventParams;
  107. var me = this;
  108. cmd = this._getCommand(name);
  109. eventParams = {
  110. command: cmd,
  111. commandName: name.toLowerCase(),
  112. commandArgs: cmdArgs
  113. };
  114. if (!cmd || !~this.queryCommandState(name)) {
  115. return false;
  116. }
  117. if (!this._hasEnterExecCommand) {
  118. this._hasEnterExecCommand = true;
  119. stoped = this._fire(new MinderEvent('beforeExecCommand', eventParams, true));
  120. if (!stoped) {
  121. this._fire(new MinderEvent('preExecCommand', eventParams, false));
  122. result = cmd.execute.apply(cmd, [me].concat(cmdArgs));
  123. this._fire(new MinderEvent('execCommand', eventParams, false));
  124. if (cmd.isContentChanged()) {
  125. this._firePharse(new MinderEvent('contentchange'));
  126. }
  127. this._interactChange();
  128. }
  129. this._hasEnterExecCommand = false;
  130. } else {
  131. result = cmd.execute.apply(cmd, [me].concat(cmdArgs));
  132. if (!this._hasEnterExecCommand) {
  133. this._interactChange();
  134. }
  135. }
  136. return result === undefined ? null : result;
  137. }
  138. });
  139. module.exports = Command;
  140. });