keybinding.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var keyUtil = require("../lib/keys");
  33. var event = require("../lib/event");
  34. var KeyBinding = function(editor) {
  35. this.$editor = editor;
  36. this.$data = { };
  37. this.$handlers = [];
  38. this.setDefaultHandler(editor.commands);
  39. };
  40. (function() {
  41. this.setDefaultHandler = function(kb) {
  42. this.removeKeyboardHandler(this.$defaultHandler);
  43. this.$defaultHandler = kb;
  44. this.addKeyboardHandler(kb, 0);
  45. this.$data = {editor: this.$editor};
  46. };
  47. this.setKeyboardHandler = function(kb) {
  48. var h = this.$handlers;
  49. if (h[h.length - 1] == kb)
  50. return;
  51. while (h[h.length - 1] && h[h.length - 1] != this.$defaultHandler)
  52. this.removeKeyboardHandler(h[h.length - 1]);
  53. this.addKeyboardHandler(kb, 1);
  54. };
  55. this.addKeyboardHandler = function(kb, pos) {
  56. if (!kb)
  57. return;
  58. var i = this.$handlers.indexOf(kb);
  59. if (i != -1)
  60. this.$handlers.splice(i, 1);
  61. if (pos == undefined)
  62. this.$handlers.push(kb);
  63. else
  64. this.$handlers.splice(pos, 0, kb);
  65. if (i == -1 && kb.attach)
  66. kb.attach(this.$editor);
  67. };
  68. this.removeKeyboardHandler = function(kb) {
  69. var i = this.$handlers.indexOf(kb);
  70. if (i == -1)
  71. return false;
  72. this.$handlers.splice(i, 1);
  73. kb.detach && kb.detach(this.$editor);
  74. return true;
  75. };
  76. this.getKeyboardHandler = function() {
  77. return this.$handlers[this.$handlers.length - 1];
  78. };
  79. this.$callKeyboardHandlers = function (hashId, keyString, keyCode, e) {
  80. var toExecute;
  81. var success = false;
  82. var commands = this.$editor.commands;
  83. for (var i = this.$handlers.length; i--;) {
  84. toExecute = this.$handlers[i].handleKeyboard(
  85. this.$data, hashId, keyString, keyCode, e
  86. );
  87. if (!toExecute || !toExecute.command)
  88. continue;
  89. // allow keyboardHandler to consume keys
  90. if (toExecute.command == "null") {
  91. success = true;
  92. } else {
  93. success = commands.exec(toExecute.command, this.$editor, toExecute.args, e);
  94. }
  95. // do not stop input events to not break repeating
  96. if (success && e && hashId != -1 &&
  97. toExecute.passEvent != true && toExecute.command.passEvent != true
  98. ) {
  99. event.stopEvent(e);
  100. }
  101. if (success)
  102. break;
  103. }
  104. return success;
  105. };
  106. this.onCommandKey = function(e, hashId, keyCode) {
  107. var keyString = keyUtil.keyCodeToString(keyCode);
  108. this.$callKeyboardHandlers(hashId, keyString, keyCode, e);
  109. };
  110. this.onTextInput = function(text) {
  111. var success = this.$callKeyboardHandlers(-1, text);
  112. if (!success)
  113. this.$editor.commands.exec("insertstring", this.$editor, text);
  114. };
  115. }).call(KeyBinding.prototype);
  116. exports.KeyBinding = KeyBinding;
  117. });