omnomirc.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. OmnomIRC COPYRIGHT 2010,2011 Netham45
  3. OmnomIRC JavaScript Client rewrite COPYRIGHT 2013
  4. Nathaniel 'Eeems' van Diepen
  5. This file is part of OmnomIRC.
  6. OmnomIRC is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. OmnomIRC is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with OmnomIRC. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. (function(window,undefined){
  18. document.domain=HOSTNAME;
  19. var OmnomIRC = window.OmnomIRC = (function(){
  20. var ret = {
  21. options: "----------------------------------------|", //40 for future expansion!(and 40 bytes isn't much.) Pipe is a terminator.
  22. cookieLoad: proto('cookieLoad'),
  23. getOption: proto('getOption'),
  24. setOption: proto('setOption'),
  25. clearCookies: proto('clearCookies'),
  26. getHTMLToggle: proto('getHTMLToggle'),
  27. setAllowNotification: proto('setAllowNotification'),
  28. startIndicator: proto('startIndicator'),
  29. stopIndicator: proto('stopIndicator')
  30. };
  31. if(message.addEventListener ){
  32. message.addEventListener("keydown",proto('keyHandler').call(ret),false);
  33. }else if(message.attachEvent ){
  34. message.attachEvent("onkeydown",proto('keyHandler').call(ret));
  35. }
  36. window.onLoad = this.cookieLoad();
  37. return ret;
  38. })(),
  39. proto = function(fn){
  40. return function(){
  41. try{
  42. return _proto[fn].apply(OmnomIRC,arguments);
  43. }catch(e){
  44. return null;
  45. }
  46. };
  47. },
  48. _proto = {
  49. cookieLoad: function() {
  50. if (document.cookie.indexOf("OmnomIRC") >= 0) {
  51. this.options = document.cookie.replace(/^.*OmnomIRC=(.+?)|.*/, "\$1");
  52. }else{
  53. document.cookie = "OmnomIRC=" + this.options + ";expires=Sat, 20 Nov 2286 17:46:39 GMT;";
  54. }
  55. },
  56. getOption: function(Option,def) { //Returns what 'Option' is. Option must be a number 1-40. def is what to return if it is not set(equal to -)
  57. if (Option < 1 || Option > 40){
  58. return 0;
  59. }
  60. var result = this.options.charAt(Option - 1);
  61. if (result == '-'){
  62. return def;
  63. }
  64. return result;
  65. },
  66. setOption: function(Option, value,noRefresh) { //Sets 'Option' to 'value'. Value must be a single char. Option must be a number 1-40.
  67. if (Option < 1 || Option > 40){
  68. return;
  69. }
  70. this.options = this.options.substring(0, Option - 1) + value + this.options.substring(Option);
  71. document.cookie = "OmnomIRC=" + this.options + ";expires=Sat, 20 Nov 2286 17:46:39 GMT;";
  72. if (!noRefresh){
  73. document.location.reload();
  74. }
  75. },
  76. clearCookies: function(){
  77. document.cookie = "OmnomIRC=a;expires=Thu, 01-Jan-1970 00:00:01 GMT;";
  78. document.cookie = "OmnomChannels=a;expires=Thu, 01-Jan-1970 00:00:01 GMT;";
  79. document.location.reload();
  80. },
  81. permissionGranted: function(){
  82. if (window.webkitNotifications.checkPermission() === 0){
  83. proto('showNotification').call(this,"Notifications Enabled!");
  84. this.setOption(7,'T');
  85. window.location.refresh(true);
  86. }
  87. },
  88. getHTMLToggle: function(State, StateOn, StateOff,StateOnFunc,StateOffFunc){
  89. var result = "";
  90. if (State){
  91. result += "<b>";
  92. result += StateOn;
  93. result += "</b>";
  94. }else{
  95. result += '<a href="#" onclick="'+StateOnFunc+'">';
  96. result += StateOn;
  97. result += '</a>';
  98. }
  99. result += "</td><td>";
  100. if(!State){
  101. result += "<b>";
  102. result += StateOff;
  103. result += "</b>";
  104. }else{
  105. result += '<a href="#" onclick="'+StateOffFunc+'">';
  106. result += StateOff;
  107. result += '</a>';
  108. }
  109. return result;
  110. },
  111. setAllowNotification: function(){
  112. if (window.webkitNotifications === undefined || window.webkitNotifications === null || !window.webkitNotifications){
  113. alert("This feature only works in chrome.");
  114. return;
  115. }
  116. window.webkitNotifications.requestPermission(proto('permissionGranted').call(this));
  117. },
  118. showNotification: function(message){
  119. if (window.webkitNotifications === undefined || window.webkitNotifications === null || !window.webkitNotifications){
  120. return 0;
  121. }
  122. if (window.webkitNotifications.checkPermission() !== 0){
  123. return 0;
  124. }
  125. var n;
  126. n = window.webkitNotifications.createNotification('http://www.omnimaga.org/favicon.ico', 'OmnomIRC Highlight', message);
  127. n.show();
  128. },
  129. keyHandler: function(e){
  130. var getCurrentWord = proto('getCurrentWord').call(this),
  131. TABKEY = 9;
  132. if (getCurrentWord() === ""){
  133. return true;
  134. }
  135. if(e.keyCode == TABKEY){
  136. if(e.preventDefault) {
  137. e.preventDefault();
  138. }
  139. tabWord = getCurrentWord();
  140. getTabComplete();
  141. tabCount++;
  142. isInTab = true;
  143. //setTimeout(1,1); //Who woulda thought that a bogus call makes it not parse it in FF4?
  144. return false;
  145. }else{
  146. tabWord = "";
  147. tabCount = 0;
  148. isInTab = false;
  149. }
  150. },
  151. getCurrentWord: function(){
  152. if (isInTab){
  153. return tabWord;
  154. }
  155. startPos = message.selectionStart;
  156. endPos = message.selectionStart;
  157. var startChar = message.value.charAt(startPos);
  158. while (startChar != " " && --startPos > 0){
  159. startChar = message.value.charAt(startPos);
  160. }
  161. if (startChar == " "){
  162. startPos++;
  163. }
  164. var endChar = message.value.charAt(endPos);
  165. while (endChar != " " && ++endPos <= message.value.length){
  166. endChar = message.value.charAt(endPos);
  167. }
  168. endPosO = endPos;
  169. return message.value.substr(startPos,endPos - startPos).trim();
  170. },
  171. getTabComplete: function(){
  172. var getCurrentWord = proto('getCurrentWord').call(this),
  173. name = searchUser(getCurrentWord(),tabCount);
  174. if (!isInTab){
  175. startPos = message.selectionStart;
  176. var startChar = message.value.charAt(startPos);
  177. while (startChar != " " && --startPos > 0){
  178. startChar = message.value.charAt(startPos);
  179. }
  180. if (startChar == " "){
  181. startChar+=2;
  182. }
  183. endPos = message.selectionStart;
  184. var endChar = message.value.charAt(endPos);
  185. while (endChar != " " && ++endPos <= message.value.length){
  186. endChar = message.value.charAt(endPos);
  187. }
  188. if (endChar == " "){
  189. endChar-=2;
  190. }
  191. }
  192. if (name == getCurrentWord()){
  193. tabCount = 0;
  194. name = searchUser(getCurrentWord(),tabCount);
  195. }
  196. message.value = message.value.substr(0,startPos) + name + message.value.substr(endPos + 1);
  197. endPos = endPosO + name.length;
  198. },
  199. startIndicator: function(){
  200. if(!indicatorTimer){
  201. indicatorTimer = setInterval(proto('updateIndicator').call(this),50);
  202. indicatorPixels = Array(true,true,true,true,true,false,false,false);
  203. }
  204. },
  205. stopIndicator: function() {
  206. clearInterval(indicatorTimer);
  207. document.getElementById('indicator').innerHTML = '';
  208. indicatorTimer = false;
  209. },
  210. updateIndicator: function() {
  211. var indicator = document.getElementById('indicator'),
  212. div,
  213. temp = indicatorPixels[7];
  214. indicator.innerHTML = "";
  215. for (var i=0;i<8;i++){
  216. div = document.createElement('div');
  217. div.style.padding = 0;
  218. div.style.margin = 0;
  219. div.style.width = '3px';
  220. div.style.height = '3px';
  221. if (indicatorPixels[i]){
  222. div.style.backgroundColor = 'black';
  223. }
  224. indicator.appendChild(div);
  225. }
  226. for(i=6;i>=0;i--){
  227. indicatorPixels[(i+1)] = indicatorPixels[i];
  228. }
  229. indicatorPixels[0] = temp;
  230. },
  231. readOldMessagesCookies: function() {
  232. var oldMessages = [],
  233. temp = getCookie("oldMessages-"+getChannelEn());
  234. if (temp!==null){
  235. oldMessages = temp.split("\n");
  236. }
  237. messageCounter = oldMessages.length;
  238. }
  239. },
  240. message = document.getElementById("message"),
  241. isInTab = false,
  242. tabWord = "",
  243. tabCount = 0,
  244. startPos = 0,
  245. endPos = 0,
  246. endPosO = 0,
  247. indicatorTimer = false,
  248. oldMessages = [],
  249. messageCounter = 1,
  250. currentMessage,
  251. messageList = [],
  252. UserListArr = [],
  253. curLine = 0,
  254. messageBox = window.messageBox = document.createElement("table"),
  255. mBoxCont = window.mBoxCont = document.getElementById("mboxCont"),
  256. Userlist = [],
  257. scrolledDown = true,
  258. statusTxt = "",
  259. statusStarted = false,
  260. focusHandlerRegistered = false,
  261. userListContainer = document.getElementById("UserListArrContainer"),
  262. userListDiv = document.getElementById("UserList"),
  263. xmlhttp,
  264. inRequest = false,
  265. errorCount = 0;
  266. messageBox.style.width="100%";
  267. messageBox.style.height="100%";
  268. messageBox.className='MessageBox';
  269. window.addEventListener('keydown',function(e){
  270. if(document.activeElement.id=="message"){
  271. var messageBoxElement = document.getElementById("message");
  272. if(messageCounter==oldMessages.length){
  273. currentMessage=messageBoxElement.value;
  274. }
  275. if(oldMessages.length!==0) {
  276. if (e.keyCode==38) { //up
  277. if(messageCounter!==0){
  278. messageCounter--;
  279. }
  280. messageBoxElement.value = oldMessages[messageCounter];
  281. }else if(e.keyCode==40){ //down
  282. if (messageCounter!=oldMessages.length){
  283. messageCounter++;
  284. }
  285. if (messageCounter==oldMessages.length){
  286. messageBoxElement.value = currentMessage;
  287. }else{
  288. messageBoxElement.value = oldMessages[messageCounter];
  289. }
  290. }
  291. }
  292. }
  293. }, false);
  294. })(window);