omnomirc.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. OmnomIRC COPYRIGHT 2010,2011 Netham45
  3. OmnomIRC3 rewrite COPYRIGHT 2013 Nathaniel 'Eeems' van Diepen
  4. This file is part of OmnomIRC.
  5. OmnomIRC is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. OmnomIRC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with OmnomIRC. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. (function(window,$,undefined){
  17. var $o = window.OmnomIRC = window.$o = function(){
  18. return 'Version: '+$o.version
  19. },
  20. event = function(msg,type){
  21. type=type==undefined?'event':type;
  22. switch(type){
  23. case 'ready':type='document_ready';break;
  24. }
  25. log('['+type.toUpperCase()+'] '+msg);
  26. },
  27. log=console.log,
  28. tabs=[],
  29. selectedTab=0,
  30. commands = [
  31. {
  32. cmd: 'help',
  33. fn: function(args){
  34. var m = 'Commands:',i;
  35. for(i in commands){
  36. m += ' '+commands[i].cmd;
  37. }
  38. $o.msg(m);
  39. }
  40. },
  41. {
  42. cmd: 'open',
  43. fn: function(args){
  44. tabs.push({
  45. name: args[1],
  46. title: args[2],
  47. topic: 'Topic for '+args[2]
  48. });
  49. $o.refreshTabs();
  50. }
  51. },
  52. {
  53. cmd: 'clear',
  54. fn: function(args){
  55. $cl.html('');
  56. }
  57. },
  58. {
  59. cmd: 'close',
  60. fn: function(args){
  61. if(args.length > 1){
  62. $o.removeTab(args[1]);
  63. }else{
  64. $o.removeTab(selectedTab);
  65. }
  66. }
  67. },
  68. {
  69. cmd: 'tabs',
  70. fn: function(args){
  71. $o.msg('Tabs:');
  72. for(var i in tabs){
  73. $o.msg(' ['+i+'] '+tabs[i].name);
  74. }
  75. }
  76. }
  77. ],
  78. $i,$s,$h,$cl,$tl,hht;
  79. $.extend($o,{
  80. version: '3.0',
  81. send: function(msg){
  82. if(msg !== ''){
  83. if(msg[0] == '/'){
  84. var args = msg.split(' '),
  85. cmd = args[0].substr(1),
  86. i;
  87. event(msg,'command');
  88. for(i in commands){
  89. if(commands[i].cmd == cmd){
  90. commands[i].fn(args);
  91. return;
  92. }
  93. }
  94. $o.msg(cmd+' is not a valid command.');
  95. }else{
  96. event(msg,'send');
  97. $o.msg({
  98. text: msg,
  99. user: 'User'
  100. });
  101. }
  102. }
  103. },
  104. msg: function(msg){
  105. switch(typeof msg){
  106. case 'string':
  107. $cl.append($('<li>').html(msg.htmlentities()));
  108. break;
  109. case 'object':
  110. $cl.append($('<li>').html('&lt;'+msg.user+'&gt;&nbsp;'+msg.text.htmlentities()));
  111. break;
  112. }
  113. },
  114. event: function(event_name,message){
  115. event(message,event_name);
  116. },
  117. selectTab: function(id){
  118. event(id,'tab_select');
  119. if(id<tabs.length-1&&id>=0){
  120. selectedTab=id;
  121. }
  122. $tl.children('.clicked').removeClass('clicked');
  123. $($tl.children().get(id)).addClass('clicked');
  124. $('#title').text(tabs[id].title);
  125. $('#topic').text(tabs[id].topic);
  126. },
  127. tabDOM: function(id){
  128. },
  129. addTab: function(name,title){
  130. event('Tab added: '+name);
  131. tabs.push({
  132. name: name,
  133. title: title
  134. });
  135. $tl.append($o.tabObj(tabs.length-1));
  136. },
  137. removeTab: function(id){
  138. event('Tab removed: '+tabs[id].name);
  139. tabs.splice(id,1);
  140. if(selectedTab==id&&selectedTab>0){
  141. selectedTab--;
  142. }
  143. $o.refreshTabs();
  144. },
  145. tabObj: function(id){
  146. if(typeof id !== 'undefined'){
  147. return $('<span>')
  148. .addClass('tab')
  149. .text(tabs[id].title)
  150. .click(function(){
  151. if($(this).data('id')!=selectedTab){
  152. $o.selectTab($(this).data('id'));
  153. return false;
  154. }
  155. })
  156. .append(
  157. $('<span>')
  158. .addClass('close-button')
  159. .click(function(){
  160. $o.removeTab(id);
  161. return false;
  162. })
  163. .html('&times;')
  164. )
  165. .data('id',id);
  166. }
  167. },
  168. refreshTabs: function(){
  169. $tl.html('');
  170. var i,tab;
  171. for(i in tabs){
  172. tab = $o.tabObj(i);
  173. if(i==selectedTab){
  174. tab.addClass('clicked');
  175. $('#title').text(tabs[i].title);
  176. $('#topic').text(tabs[i].topic);
  177. }
  178. $tl.append(tab);
  179. }
  180. }
  181. });
  182. String.prototype.htmlentities = function(){
  183. return this.replace(/&/g, '&amp;').replace(/\s/g, '&nbsp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
  184. };
  185. $(document).ready(function(){
  186. $i = $('#input');
  187. $s = $('#send');
  188. $cl = $('#content-list');
  189. $tl = $('#tabs-list');
  190. $h = $('#head');
  191. $s.click(function(){
  192. if(!$s.hasClass('clicked')){
  193. $s.addClass('clicked');
  194. setTimeout(function(){
  195. $s.removeClass('clicked');
  196. },500);
  197. }
  198. $o.send($i.val());
  199. $i.val('');
  200. });
  201. $i.keypress(function(e){
  202. if(e.keyCode == 13){
  203. if(!$s.hasClass('clicked')){
  204. $s.addClass('clicked');
  205. setTimeout(function(){
  206. $s.removeClass('clicked');
  207. },500);
  208. }
  209. $o.send($i.val());
  210. $i.val('');
  211. }
  212. });
  213. $('#settings, #users').click(function(){
  214. $(this).addClass('open');
  215. $(this).children('.close-button').show();
  216. }).hover(function(){
  217. $(this).addClass('hovered');
  218. },function(){
  219. $(this).removeClass('hovered');
  220. }).children('.close-button').click(function(){
  221. $(this).parent().removeClass('open');
  222. $(this).hide();
  223. return false;
  224. }).hide();
  225. $('#users').hoverIntent({
  226. out: function(){
  227. $(this).removeClass('open');
  228. $(this).children('.close-button').hide();
  229. },
  230. timeout: 1000
  231. });
  232. $('#content').click(function(){
  233. $('#settings, #users, #head').removeClass('hovered').removeClass('open');
  234. $('#settings, #users').children('.close-button').hide()
  235. });
  236. $h.hoverIntent({
  237. out: function(){
  238. $h.removeClass('hovered');
  239. },
  240. over: function(){},
  241. timeout: 1000
  242. }).hover(function(){
  243. hht = setTimeout(function(){
  244. event('Head HoverIntent timeout','timeout');
  245. $('#head:hover').addClass('hovered');
  246. },1000);
  247. },function(){
  248. clearInterval(hht);
  249. }).click(function(){
  250. $(this).addClass('hovered');
  251. });
  252. $('.unselectable').attr('unselectable','on');
  253. //DEBUG
  254. for(var i=0;i<10;i++){
  255. tabs.push({
  256. name: '#Tab'+i,
  257. title: 'Tab '+i,
  258. topic: 'Topic for tab '+i
  259. });
  260. }
  261. //END DEBUG
  262. $o.refreshTabs();
  263. event('Date '+new Date,'ready');
  264. $h.addClass('hovered');
  265. setTimeout(function(){
  266. $h.removeClass('hovered');
  267. },1000);
  268. });
  269. })(window,jQuery);