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. (function(window,undefined){
  16. var message = document.getElementById("message"),
  17. isInTab = false,
  18. tabWord = "",
  19. tabCount = 0,
  20. startPos = 0,
  21. endPos = 0,
  22. endPosO = 0;
  23. if(message.addEventListener ){
  24. message.addEventListener("keydown",keyHandler,false);
  25. } else if(message.attachEvent ){
  26. message.attachEvent("onkeydown",keyHandler);
  27. }
  28. function keyHandler(e){
  29. if (getCurrentWord() === "")
  30. return true;
  31. var TABKEY = 9;
  32. if(e.keyCode == TABKEY){
  33. if(e.preventDefault) {
  34. e.preventDefault();
  35. }
  36. tabWord = getCurrentWord();
  37. getTabComplete();
  38. tabCount++;
  39. isInTab = true;
  40. //setTimeout(1,1); //Who woulda thought that a bogus call makes it not parse it in FF4?
  41. return false;
  42. }else{
  43. tabWord = "";
  44. tabCount = 0;
  45. isInTab = false;
  46. }
  47. }
  48. function getCurrentWord(){
  49. if (isInTab){
  50. return tabWord;
  51. }
  52. startPos = message.selectionStart;
  53. endPos = message.selectionStart;
  54. var startChar = message.value.charAt(startPos);
  55. while (startChar != " " && --startPos > 0){
  56. startChar = message.value.charAt(startPos);
  57. }
  58. if (startChar == " ") startPos++;
  59. var endChar = message.value.charAt(endPos);
  60. while (endChar != " " && ++endPos <= message.value.length){
  61. endChar = message.value.charAt(endPos);
  62. }
  63. endPosO = endPos;
  64. return message.value.substr(startPos,endPos - startPos).trim();
  65. }
  66. function getTabComplete(){
  67. if (!isInTab){
  68. startPos = message.selectionStart;
  69. var startChar = message.value.charAt(startPos);
  70. while (startChar != " " && --startPos > 0){
  71. startChar = message.value.charAt(startPos);
  72. }
  73. if (startChar == " "){
  74. startChar+=2;
  75. }
  76. endPos = message.selectionStart;
  77. var endChar = message.value.charAt(endPos);
  78. while (endChar != " " && ++endPos <= message.value.length){
  79. endChar = message.value.charAt(endPos);
  80. }
  81. if (endChar == " "){
  82. endChar-=2;
  83. }
  84. }
  85. var name = searchUser(getCurrentWord(),tabCount);
  86. if (name == getCurrentWord()){
  87. tabCount = 0;
  88. name = searchUser(getCurrentWord(),tabCount);
  89. }
  90. message.value = message.value.substr(0,startPos) + name + message.value.substr(endPos + 1);
  91. endPos = endPosO + name.length;
  92. }
  93. })(window);