command_manager.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. define(function(require, exports, module) {
  2. "use strict";
  3. var oop = require("../lib/oop");
  4. var HashHandler = require("../keyboard/hash_handler").HashHandler;
  5. var EventEmitter = require("../lib/event_emitter").EventEmitter;
  6. /**
  7. * @class CommandManager
  8. *
  9. *
  10. **/
  11. /**
  12. * new CommandManager(platform, commands)
  13. * @param {String} platform Identifier for the platform; must be either `'mac'` or `'win'`
  14. * @param {Array} commands A list of commands
  15. *
  16. **/
  17. var CommandManager = function(platform, commands) {
  18. HashHandler.call(this, commands, platform);
  19. this.byName = this.commands;
  20. this.setDefaultHandler("exec", function(e) {
  21. return e.command.exec(e.editor, e.args || {});
  22. });
  23. };
  24. oop.inherits(CommandManager, HashHandler);
  25. (function() {
  26. oop.implement(this, EventEmitter);
  27. this.exec = function(command, editor, args) {
  28. if (typeof command === 'string')
  29. command = this.commands[command];
  30. if (!command)
  31. return false;
  32. if (editor && editor.$readOnly && !command.readOnly)
  33. return false;
  34. var e = {editor: editor, command: command, args: args};
  35. var retvalue = this._emit("exec", e);
  36. this._signal("afterExec", e);
  37. return retvalue === false ? false : true;
  38. };
  39. this.toggleRecording = function(editor) {
  40. if (this.$inReplay)
  41. return;
  42. editor && editor._emit("changeStatus");
  43. if (this.recording) {
  44. this.macro.pop();
  45. this.removeEventListener("exec", this.$addCommandToMacro);
  46. if (!this.macro.length)
  47. this.macro = this.oldMacro;
  48. return this.recording = false;
  49. }
  50. if (!this.$addCommandToMacro) {
  51. this.$addCommandToMacro = function(e) {
  52. this.macro.push([e.command, e.args]);
  53. }.bind(this);
  54. }
  55. this.oldMacro = this.macro;
  56. this.macro = [];
  57. this.on("exec", this.$addCommandToMacro);
  58. return this.recording = true;
  59. };
  60. this.replay = function(editor) {
  61. if (this.$inReplay || !this.macro)
  62. return;
  63. if (this.recording)
  64. return this.toggleRecording(editor);
  65. try {
  66. this.$inReplay = true;
  67. this.macro.forEach(function(x) {
  68. if (typeof x == "string")
  69. this.exec(x, editor);
  70. else
  71. this.exec(x[0], editor, x[1]);
  72. }, this);
  73. } finally {
  74. this.$inReplay = false;
  75. }
  76. };
  77. this.trimMacro = function(m) {
  78. return m.map(function(x){
  79. if (typeof x[0] != "string")
  80. x[0] = x[0].name;
  81. if (!x[1])
  82. x = x[0];
  83. return x;
  84. });
  85. };
  86. }).call(CommandManager.prototype);
  87. exports.CommandManager = CommandManager;
  88. });