omnomirc.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. var OmnomIRC = window.OmnomIRC = (function(){
  19. var ret = {
  20. options: "----------------------------------------|", //40 for future expansion!(and 40 bytes isn't much.) Pipe is a terminator.
  21. cookieLoad: proto('cookieLoad'),
  22. getOption: proto('getOption'),
  23. setOption: proto('setOption'),
  24. clearCookies: proto('clearCookies'),
  25. getHTMLToggle: proto('getHTMLToggle'),
  26. setAllowNotification: proto('setAllowNotification'),
  27. startIndicator: proto('startIndicator'),
  28. stopIndicator: proto('stopIndicator')
  29. };
  30. if(message.addEventListener ){
  31. message.addEventListener("keydown",proto('keyHandler').call(ret),false);
  32. }else if(message.attachEvent ){
  33. message.attachEvent("onkeydown",proto('keyHandler').call(ret));
  34. }
  35. window.onLoad = this.cookieLoad();
  36. return ret;
  37. })(),
  38. proto = function(fn){
  39. return function(){
  40. try{
  41. return _proto[fn].apply(OmnomIRC,arguments);
  42. }catch(e){
  43. return null;
  44. }
  45. };
  46. },
  47. _proto = {
  48. cookieLoad: function() {
  49. if (document.cookie.indexOf("OmnomIRC") >= 0) {
  50. this.options = document.cookie.replace(/^.*OmnomIRC=(.+?)|.*/, "\$1");
  51. }else{
  52. document.cookie = "OmnomIRC=" + this.options + ";expires=Sat, 20 Nov 2286 17:46:39 GMT;";
  53. }
  54. },
  55. 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 -)
  56. if (Option < 1 || Option > 40){
  57. return 0;
  58. }
  59. var result = this.options.charAt(Option - 1);
  60. if (result == '-'){
  61. return def;
  62. }
  63. return result;
  64. },
  65. setOption: function(Option, value,noRefresh) { //Sets 'Option' to 'value'. Value must be a single char. Option must be a number 1-40.
  66. if (Option < 1 || Option > 40){
  67. return;
  68. }
  69. this.options = this.options.substring(0, Option - 1) + value + this.options.substring(Option);
  70. document.cookie = "OmnomIRC=" + this.options + ";expires=Sat, 20 Nov 2286 17:46:39 GMT;";
  71. if (!noRefresh){
  72. document.location.reload();
  73. }
  74. },
  75. clearCookies: function(){
  76. document.cookie = "OmnomIRC=a;expires=Thu, 01-Jan-1970 00:00:01 GMT;";
  77. document.cookie = "OmnomChannels=a;expires=Thu, 01-Jan-1970 00:00:01 GMT;";
  78. document.location.reload();
  79. },
  80. permissionGranted: function(){
  81. if (window.webkitNotifications.checkPermission() === 0){
  82. proto('showNotification').call(this,"Notifications Enabled!");
  83. this.setOption(7,'T');
  84. window.location.refresh(true);
  85. }
  86. },
  87. getHTMLToggle: function(State, StateOn, StateOff,StateOnFunc,StateOffFunc){
  88. var result = "";
  89. if (State){
  90. result += "<b>";
  91. result += StateOn;
  92. result += "</b>";
  93. }else{
  94. result += '<a href="#" onclick="'+StateOnFunc+'">';
  95. result += StateOn;
  96. result += '</a>';
  97. }
  98. result += "</td><td>";
  99. if(!State){
  100. result += "<b>";
  101. result += StateOff;
  102. result += "</b>";
  103. }else{
  104. result += '<a href="#" onclick="'+StateOffFunc+'">';
  105. result += StateOff;
  106. result += '</a>';
  107. }
  108. return result;
  109. },
  110. setAllowNotification: function(){
  111. if (window.webkitNotifications === undefined || window.webkitNotifications === null || !window.webkitNotifications){
  112. alert("This feature only works in chrome.");
  113. return;
  114. }
  115. window.webkitNotifications.requestPermission(proto('permissionGranted').call(this));
  116. },
  117. showNotification: function(message){
  118. if (window.webkitNotifications === undefined || window.webkitNotifications === null || !window.webkitNotifications){
  119. return 0;
  120. }
  121. if (window.webkitNotifications.checkPermission() !== 0){
  122. return 0;
  123. }
  124. var n;
  125. n = window.webkitNotifications.createNotification('http://www.omnimaga.org/favicon.ico', 'OmnomIRC Highlight', message);
  126. n.show();
  127. },
  128. keyHandler: function(e){
  129. var getCurrentWord = proto('getCurrentWord').call(this),
  130. TABKEY = 9;
  131. if (getCurrentWord() === ""){
  132. return true;
  133. }
  134. if(e.keyCode == TABKEY){
  135. if(e.preventDefault) {
  136. e.preventDefault();
  137. }
  138. tabWord = getCurrentWord();
  139. getTabComplete();
  140. tabCount++;
  141. isInTab = true;
  142. //setTimeout(1,1); //Who woulda thought that a bogus call makes it not parse it in FF4?
  143. return false;
  144. }else{
  145. tabWord = "";
  146. tabCount = 0;
  147. isInTab = false;
  148. }
  149. },
  150. getCurrentWord: function(){
  151. if (isInTab){
  152. return tabWord;
  153. }
  154. startPos = message.selectionStart;
  155. endPos = message.selectionStart;
  156. var startChar = message.value.charAt(startPos);
  157. while (startChar != " " && --startPos > 0){
  158. startChar = message.value.charAt(startPos);
  159. }
  160. if (startChar == " "){
  161. startPos++;
  162. }
  163. var endChar = message.value.charAt(endPos);
  164. while (endChar != " " && ++endPos <= message.value.length){
  165. endChar = message.value.charAt(endPos);
  166. }
  167. endPosO = endPos;
  168. return message.value.substr(startPos,endPos - startPos).trim();
  169. },
  170. getTabComplete: function(){
  171. var getCurrentWord = proto('getCurrentWord').call(this),
  172. name = searchUser(getCurrentWord(),tabCount);
  173. if (!isInTab){
  174. startPos = message.selectionStart;
  175. var startChar = message.value.charAt(startPos);
  176. while (startChar != " " && --startPos > 0){
  177. startChar = message.value.charAt(startPos);
  178. }
  179. if (startChar == " "){
  180. startChar+=2;
  181. }
  182. endPos = message.selectionStart;
  183. var endChar = message.value.charAt(endPos);
  184. while (endChar != " " && ++endPos <= message.value.length){
  185. endChar = message.value.charAt(endPos);
  186. }
  187. if (endChar == " "){
  188. endChar-=2;
  189. }
  190. }
  191. if (name == getCurrentWord()){
  192. tabCount = 0;
  193. name = searchUser(getCurrentWord(),tabCount);
  194. }
  195. message.value = message.value.substr(0,startPos) + name + message.value.substr(endPos + 1);
  196. endPos = endPosO + name.length;
  197. },
  198. startIndicator: function(){
  199. if(!indicatorTimer){
  200. indicatorTimer = setInterval(proto('updateIndicator').call(this),50);
  201. indicatorPixels = Array(true,true,true,true,true,false,false,false);
  202. }
  203. },
  204. stopIndicator: function() {
  205. clearInterval(indicatorTimer);
  206. document.getElementById('indicator').innerHTML = '';
  207. indicatorTimer = false;
  208. },
  209. updateIndicator: function() {
  210. var indicator = document.getElementById('indicator'),
  211. div,
  212. temp = indicatorPixels[7];
  213. indicator.innerHTML = "";
  214. for (var i=0;i<8;i++){
  215. div = document.createElement('div');
  216. div.style.padding = 0;
  217. div.style.margin = 0;
  218. div.style.width = '3px';
  219. div.style.height = '3px';
  220. if (indicatorPixels[i]){
  221. div.style.backgroundColor = 'black';
  222. }
  223. indicator.appendChild(div);
  224. }
  225. for(i=6;i>=0;i--){
  226. indicatorPixels[(i+1)] = indicatorPixels[i];
  227. }
  228. indicatorPixels[0] = temp;
  229. },
  230. readOldMessagesCookies: function() {
  231. var oldMessages = [],
  232. temp = getCookie("oldMessages-"+getChannelEn());
  233. if (temp!==null){
  234. oldMessages = temp.split("\n");
  235. }
  236. messageCounter = oldMessages.length;
  237. }
  238. },
  239. message = document.getElementById("message"),
  240. isInTab = false,
  241. tabWord = "",
  242. tabCount = 0,
  243. startPos = 0,
  244. endPos = 0,
  245. endPosO = 0,
  246. indicatorTimer = false,
  247. oldMessages = [],
  248. messageCounter = 1,
  249. currentMessage;
  250. window.addEventListener('keydown',function(e){
  251. if(document.activeElement.id=="message"){
  252. var messageBoxElement = document.getElementById("message");
  253. if(messageCounter==oldMessages.length){
  254. currentMessage=messageBoxElement.value;
  255. }
  256. if(oldMessages.length!==0) {
  257. if (e.keyCode==38) { //up
  258. if(messageCounter!==0){
  259. messageCounter--;
  260. }
  261. messageBoxElement.value = oldMessages[messageCounter];
  262. }else if(e.keyCode==40){ //down
  263. if (messageCounter!=oldMessages.length){
  264. messageCounter++;
  265. }
  266. if (messageCounter==oldMessages.length){
  267. messageBoxElement.value = currentMessage;
  268. }else{
  269. messageBoxElement.value = oldMessages[messageCounter];
  270. }
  271. }
  272. }
  273. }
  274. }, false);
  275. })(window);