bootstrap.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Input History
  2. //
  3. // Douglas Thrift
  4. //
  5. // bootstrap.js
  6. /* ***** BEGIN LICENSE BLOCK *****
  7. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  8. *
  9. * The contents of this file are subject to the Mozilla Public License Version
  10. * 1.1 (the "License"); you may not use this file except in compliance with
  11. * the License. You may obtain a copy of the License at
  12. * http://www.mozilla.org/MPL/
  13. *
  14. * Software distributed under the License is distributed on an "AS IS" basis,
  15. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. * for the specific language governing rights and limitations under the
  17. * License.
  18. *
  19. * The Original Code is Input History.
  20. *
  21. * The Initial Developer of the Original Code is
  22. * Douglas Thrift <douglas@douglasthrift.net>.
  23. * Portions created by the Initial Developer are Copyright (C) 2011
  24. * the Initial Developer. All Rights Reserved.
  25. *
  26. * Contributor(s):
  27. *
  28. * Alternatively, the contents of this file may be used under the terms of
  29. * either the GNU General Public License Version 2 or later (the "GPL"), or
  30. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31. * in which case the provisions of the GPL or the LGPL are applicable instead
  32. * of those above. If you wish to allow use of your version of this file only
  33. * under the terms of either the GPL or the LGPL, and not to allow others to
  34. * use your version of this file under the terms of the MPL, indicate your
  35. * decision by deleting the provisions above and replace them with the notice
  36. * and other provisions required by the GPL or the LGPL. If you do not delete
  37. * the provisions above, a recipient may use your version of this file under
  38. * the terms of any one of the MPL, the GPL or the LGPL.
  39. *
  40. * ***** END LICENSE BLOCK ***** */
  41. Components.utils.import("resource:///modules/imServices.jsm");
  42. function generatorFromArray(array)
  43. {
  44. for (var index = 0; index != array.length; ++index)
  45. yield array[index];
  46. }
  47. function generatorFromEnumerator(enumerator)
  48. {
  49. while (enumerator.hasMoreElements())
  50. yield enumerator.getNext();
  51. }
  52. let observer = {
  53. observe: function(subject, topic, data)
  54. {
  55. if (topic != "conversation-loaded")
  56. return;
  57. this.addArrowListener(subject);
  58. },
  59. addArrowListener: function(browser)
  60. {
  61. var binding = browser.ownerDocument.getBindingParent(browser);
  62. if (!binding || !("editor" in binding) || !binding.editor)
  63. return;
  64. binding._inputHistory = [];
  65. binding._inputHistoryIndex = 0;
  66. binding.editor.addEventListener("keypress", this.onKeyPress, true);
  67. },
  68. removeArrowListener: function(browser)
  69. {
  70. var binding = browser.ownerDocument.getBindingParent(browser);
  71. if (!binding || !("editor" in binding) || !binding.editor)
  72. return;
  73. binding.editor.removeEventListener("keypress", this.onKeyPress, true);
  74. delete binding._inputHistoryIndex;
  75. delete binding._inputHistory;
  76. },
  77. onKeyPress: function(event)
  78. {
  79. var editor = event.target;
  80. var binding = editor.ownerDocument.getBindingParent(editor);
  81. switch (event.keyCode)
  82. {
  83. case event.DOM_VK_RETURN:
  84. case event.DOM_VK_ENTER:
  85. if (!event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey && editor.value.length != 0 && editor.value != binding._inputHistory[binding._inputHistory.length - 1])
  86. {
  87. binding._inputHistory.push(editor.value);
  88. binding._inputHistoryIndex = binding._inputHistory.length;
  89. }
  90. return;
  91. case event.DOM_VK_UP:
  92. case event.DOM_VK_DOWN:
  93. if (event.ctrlKey && !event.altKey && !event.metaKey && !event.shiftKey && binding._inputHistory.length != 0)
  94. break;
  95. default:
  96. return;
  97. }
  98. switch (event.keyCode)
  99. {
  100. case event.DOM_VK_UP:
  101. if (binding._inputHistoryIndex == 0)
  102. return;
  103. --binding._inputHistoryIndex;
  104. break;
  105. case event.DOM_VK_DOWN:
  106. if (binding._inputHistoryIndex == binding._inputHistory.length)
  107. return;
  108. ++binding._inputHistoryIndex;
  109. }
  110. editor.value = binding._inputHistoryIndex != binding._inputHistory.length ? binding._inputHistory[binding._inputHistoryIndex] : "";
  111. editor.setSelectionRange(editor.value.length, editor.value.length);
  112. },
  113. };
  114. function startup(data, reason)
  115. {
  116. for each (let window in generatorFromEnumerator(Services.wm.getEnumerator("Messenger:convs")))
  117. for each (let tabconversation in generatorFromArray(window.document.getElementsByTagName("tabconversation")))
  118. for each (let browser in tabconversation.browsers)
  119. observer.addArrowListener(browser);
  120. Services.obs.addObserver(observer, "conversation-loaded", false);
  121. }
  122. function shutdown(data, reason)
  123. {
  124. Services.obs.removeObserver(observer, "conversation-loaded");
  125. for each (let window in generatorFromEnumerator(Services.wm.getEnumerator("Messenger:convs")))
  126. for each (let tabconversation in generatorFromArray(window.document.getElementsByTagName("tabconversation")))
  127. for each (let browser in tabconversation.browsers)
  128. observer.removeArrowListener(browser);
  129. }
  130. function install(data, reason) {}
  131. function uninstall(data, reason) {}
  132. // vim: expandtab