hash_handler.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 useragent = require("../lib/useragent");
  34. function HashHandler(config, platform) {
  35. this.platform = platform || (useragent.isMac ? "mac" : "win");
  36. this.commands = {};
  37. this.commandKeyBinding = {};
  38. // todo remove this after a while
  39. if (this.__defineGetter__ && this.__defineSetter__ && typeof console != "undefined" && console.error) {
  40. var warned = false;
  41. var warn = function() {
  42. if (!warned) {
  43. warned = true;
  44. console.error("commmandKeyBinding has too many m's. use commandKeyBinding");
  45. }
  46. };
  47. this.__defineGetter__("commmandKeyBinding", function() {
  48. warn();
  49. return this.commandKeyBinding;
  50. });
  51. this.__defineSetter__("commmandKeyBinding", function(val) {
  52. warn();
  53. return this.commandKeyBinding = val;
  54. });
  55. } else {
  56. this.commmandKeyBinding = this.commandKeyBinding;
  57. }
  58. this.addCommands(config);
  59. };
  60. (function() {
  61. this.addCommand = function(command) {
  62. if (this.commands[command.name])
  63. this.removeCommand(command);
  64. this.commands[command.name] = command;
  65. if (command.bindKey)
  66. this._buildKeyHash(command);
  67. };
  68. this.removeCommand = function(command) {
  69. var name = (typeof command === 'string' ? command : command.name);
  70. command = this.commands[name];
  71. delete this.commands[name];
  72. // exhaustive search is brute force but since removeCommand is
  73. // not a performance critical operation this should be OK
  74. var ckb = this.commandKeyBinding;
  75. for (var hashId in ckb) {
  76. for (var key in ckb[hashId]) {
  77. if (ckb[hashId][key] == command)
  78. delete ckb[hashId][key];
  79. }
  80. }
  81. };
  82. this.bindKey = function(key, command) {
  83. if(!key)
  84. return;
  85. if (typeof command == "function") {
  86. this.addCommand({exec: command, bindKey: key, name: command.name || key});
  87. return;
  88. }
  89. var ckb = this.commandKeyBinding;
  90. key.split("|").forEach(function(keyPart) {
  91. var binding = this.parseKeys(keyPart, command);
  92. var hashId = binding.hashId;
  93. (ckb[hashId] || (ckb[hashId] = {}))[binding.key] = command;
  94. }, this);
  95. };
  96. this.addCommands = function(commands) {
  97. commands && Object.keys(commands).forEach(function(name) {
  98. var command = commands[name];
  99. if (!command)
  100. return;
  101. if (typeof command === "string")
  102. return this.bindKey(command, name);
  103. if (typeof command === "function")
  104. command = { exec: command };
  105. if (typeof command !== "object")
  106. return;
  107. if (!command.name)
  108. command.name = name;
  109. this.addCommand(command);
  110. }, this);
  111. };
  112. this.removeCommands = function(commands) {
  113. Object.keys(commands).forEach(function(name) {
  114. this.removeCommand(commands[name]);
  115. }, this);
  116. };
  117. this.bindKeys = function(keyList) {
  118. Object.keys(keyList).forEach(function(key) {
  119. this.bindKey(key, keyList[key]);
  120. }, this);
  121. };
  122. this._buildKeyHash = function(command) {
  123. var binding = command.bindKey;
  124. if (!binding)
  125. return;
  126. var key = typeof binding == "string" ? binding: binding[this.platform];
  127. this.bindKey(key, command);
  128. };
  129. // accepts keys in the form ctrl+Enter or ctrl-Enter
  130. // keys without modifiers or shift only
  131. this.parseKeys = function(keys) {
  132. // todo support keychains
  133. if (keys.indexOf(" ") != -1)
  134. keys = keys.split(/\s+/).pop();
  135. var parts = keys.toLowerCase().split(/[\-\+]([\-\+])?/).filter(function(x){return x});
  136. var key = parts.pop();
  137. var keyCode = keyUtil[key];
  138. if (keyUtil.FUNCTION_KEYS[keyCode])
  139. key = keyUtil.FUNCTION_KEYS[keyCode].toLowerCase();
  140. else if (!parts.length)
  141. return {key: key, hashId: -1};
  142. else if (parts.length == 1 && parts[0] == "shift")
  143. return {key: key.toUpperCase(), hashId: -1};
  144. var hashId = 0;
  145. for (var i = parts.length; i--;) {
  146. var modifier = keyUtil.KEY_MODS[parts[i]];
  147. if (modifier == null) {
  148. if (typeof console != "undefined")
  149. console.error("invalid modifier " + parts[i] + " in " + keys);
  150. return false;
  151. }
  152. hashId |= modifier;
  153. }
  154. return {key: key, hashId: hashId};
  155. };
  156. this.findKeyCommand = function findKeyCommand(hashId, keyString) {
  157. var ckbr = this.commandKeyBinding;
  158. return ckbr[hashId] && ckbr[hashId][keyString];
  159. };
  160. this.handleKeyboard = function(data, hashId, keyString, keyCode) {
  161. return {
  162. command: this.findKeyCommand(hashId, keyString)
  163. };
  164. };
  165. }).call(HashHandler.prototype)
  166. exports.HashHandler = HashHandler;
  167. });