Omnom_Tab.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. OmnomIRC COPYRIGHT 2010,2011 Netham45
  3. This file is part of OmnomIRC.
  4. OmnomIRC is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. OmnomIRC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with OmnomIRC. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. message = document.getElementById("message");
  16. if(message.addEventListener ) {
  17. message.addEventListener("keydown",keyHandler,false);
  18. } else if(message.attachEvent ) {
  19. message.attachEvent("onkeydown",keyHandler);
  20. }
  21. isInTab = false;
  22. tabWord = "";
  23. tabCount = 0;
  24. startPos = 0;
  25. endPos = 0;
  26. endPosO = 0;
  27. function keyHandler(e) {
  28. if (getCurrentWord() == "")
  29. return true;
  30. var TABKEY = 9;
  31. if(e.keyCode == TABKEY) {
  32. if(e.preventDefault) {
  33. e.preventDefault();
  34. }
  35. tabWord = getCurrentWord();
  36. getTabComplete();
  37. tabCount++;
  38. isInTab = true;
  39. setTimeout(1,1); //Who woulda thought that a bogus call makes it not parse it in FF4?
  40. return false;
  41. }
  42. else
  43. {
  44. tabWord = "";
  45. tabCount = 0;
  46. isInTab = false;
  47. }
  48. }
  49. function getCurrentWord()
  50. {
  51. if (isInTab)
  52. return tabWord;
  53. startPos = message.selectionStart;
  54. endPos = message.selectionStart;
  55. startChar = message.value.charAt(startPos);
  56. while (startChar != " " && --startPos > 0)
  57. startChar = message.value.charAt(startPos);
  58. if (startChar == " ") startPos++;
  59. endChar = message.value.charAt(endPos);
  60. while (endChar != " " && ++endPos <= message.value.length)
  61. endChar = message.value.charAt(endPos);
  62. endPosO = endPos;
  63. return message.value.substr(startPos,endPos - startPos).trim();
  64. }
  65. function getTabComplete()
  66. {
  67. if (!isInTab)
  68. {
  69. startPos = message.selectionStart;
  70. startChar = message.value.charAt(startPos);
  71. while (startChar != " " && --startPos > 0)
  72. startChar = message.value.charAt(startPos);
  73. if (startChar == " ") startChar+=2;
  74. endPos = message.selectionStart;
  75. endChar = message.value.charAt(endPos);
  76. while (endChar != " " && ++endPos <= message.value.length)
  77. endChar = message.value.charAt(endPos);
  78. if (endChar == " ") endChar-=2;
  79. }
  80. name = searchUser(getCurrentWord(),tabCount);
  81. if (name == getCurrentWord())
  82. {
  83. tabCount = 0;
  84. name = searchUser(getCurrentWord(),tabCount);
  85. }
  86. message.value = message.value.substr(0,startPos) + name + message.value.substr(endPos + 1);
  87. endPos = endPosO + name.length;
  88. }