locale.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. var KeyCombo = require('./key-combo');
  2. function Locale(name) {
  3. this.localeName = name;
  4. this.pressedKeys = [];
  5. this._appliedMacros = [];
  6. this._keyMap = {};
  7. this._killKeyCodes = [];
  8. this._macros = [];
  9. }
  10. Locale.prototype.bindKeyCode = function(keyCode, keyNames) {
  11. if (typeof keyNames === 'string') {
  12. keyNames = [keyNames];
  13. }
  14. this._keyMap[keyCode] = keyNames;
  15. };
  16. Locale.prototype.bindMacro = function(keyComboStr, keyNames) {
  17. if (typeof keyNames === 'string') {
  18. keyNames = [ keyNames ];
  19. }
  20. var handler = null;
  21. if (typeof keyNames === 'function') {
  22. handler = keyNames;
  23. keyNames = null;
  24. }
  25. var macro = {
  26. keyCombo : new KeyCombo(keyComboStr),
  27. keyNames : keyNames,
  28. handler : handler
  29. };
  30. this._macros.push(macro);
  31. };
  32. Locale.prototype.getKeyCodes = function(keyName) {
  33. var keyCodes = [];
  34. for (var keyCode in this._keyMap) {
  35. var index = this._keyMap[keyCode].indexOf(keyName);
  36. if (index > -1) { keyCodes.push(keyCode|0); }
  37. }
  38. return keyCodes;
  39. };
  40. Locale.prototype.getKeyNames = function(keyCode) {
  41. return this._keyMap[keyCode] || [];
  42. };
  43. Locale.prototype.setKillKey = function(keyCode) {
  44. if (typeof keyCode === 'string') {
  45. var keyCodes = this.getKeyCodes(keyCode);
  46. for (var i = 0; i < keyCodes.length; i += 1) {
  47. this.setKillKey(keyCodes[i]);
  48. }
  49. return;
  50. }
  51. this._killKeyCodes.push(keyCode);
  52. };
  53. Locale.prototype.pressKey = function(keyCode) {
  54. if (typeof keyCode === 'string') {
  55. var keyCodes = this.getKeyCodes(keyCode);
  56. for (var i = 0; i < keyCodes.length; i += 1) {
  57. this.pressKey(keyCodes[i]);
  58. }
  59. return;
  60. }
  61. var keyNames = this.getKeyNames(keyCode);
  62. for (var i = 0; i < keyNames.length; i += 1) {
  63. if (this.pressedKeys.indexOf(keyNames[i]) === -1) {
  64. this.pressedKeys.push(keyNames[i]);
  65. }
  66. }
  67. this._applyMacros();
  68. };
  69. Locale.prototype.releaseKey = function(keyCode) {
  70. if (typeof keyCode === 'string') {
  71. var keyCodes = this.getKeyCodes(keyCode);
  72. for (var i = 0; i < keyCodes.length; i += 1) {
  73. this.releaseKey(keyCodes[i]);
  74. }
  75. }
  76. else {
  77. var keyNames = this.getKeyNames(keyCode);
  78. var killKeyCodeIndex = this._killKeyCodes.indexOf(keyCode);
  79. if (killKeyCodeIndex > -1) {
  80. this.pressedKeys.length = 0;
  81. } else {
  82. for (var i = 0; i < keyNames.length; i += 1) {
  83. var index = this.pressedKeys.indexOf(keyNames[i]);
  84. if (index > -1) {
  85. this.pressedKeys.splice(index, 1);
  86. }
  87. }
  88. }
  89. this._clearMacros();
  90. }
  91. };
  92. Locale.prototype._applyMacros = function() {
  93. var macros = this._macros.slice(0);
  94. for (var i = 0; i < macros.length; i += 1) {
  95. var macro = macros[i];
  96. if (macro.keyCombo.check(this.pressedKeys)) {
  97. if (macro.handler) {
  98. macro.keyNames = macro.handler(this.pressedKeys);
  99. }
  100. for (var j = 0; j < macro.keyNames.length; j += 1) {
  101. if (this.pressedKeys.indexOf(macro.keyNames[j]) === -1) {
  102. this.pressedKeys.push(macro.keyNames[j]);
  103. }
  104. }
  105. this._appliedMacros.push(macro);
  106. }
  107. }
  108. };
  109. Locale.prototype._clearMacros = function() {
  110. for (var i = 0; i < this._appliedMacros.length; i += 1) {
  111. var macro = this._appliedMacros[i];
  112. if (!macro.keyCombo.check(this.pressedKeys)) {
  113. for (var j = 0; j < macro.keyNames.length; j += 1) {
  114. var index = this.pressedKeys.indexOf(macro.keyNames[j]);
  115. if (index > -1) {
  116. this.pressedKeys.splice(index, 1);
  117. }
  118. }
  119. if (macro.handler) {
  120. macro.keyNames = null;
  121. }
  122. this._appliedMacros.splice(i, 1);
  123. i -= 1;
  124. }
  125. }
  126. };
  127. module.exports = Locale;