omnomirc.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. exists = function(object){
  29. return typeof object != 'undefined';
  30. },
  31. prevent = function(e){
  32. e.stopImmediatePropagation();
  33. e.stopPropagation();
  34. e.preventDefault();
  35. return false;
  36. },
  37. selectedTab=0,
  38. settings = {
  39. colour: false
  40. },
  41. tabs = [],
  42. properties = {
  43. nick: 'User',
  44. sig: '',
  45. tabs: tabs
  46. },
  47. commands = [
  48. {
  49. cmd: 'help',
  50. fn: function(args){
  51. var m = 'Commands:',i;
  52. for(i in commands){
  53. m += ' '+commands[i].cmd;
  54. }
  55. $o.msg(m);
  56. }
  57. },
  58. {
  59. cmd: 'open',
  60. fn: function(args){
  61. tabs.push({
  62. name: args[1],
  63. title: args[2],
  64. topic: 'Topic for '+args[2]
  65. });
  66. $o.refreshTabs();
  67. }
  68. },
  69. {
  70. cmd: 'clear',
  71. fn: function(args){
  72. $cl.html('');
  73. }
  74. },
  75. {
  76. cmd: 'close',
  77. fn: function(args){
  78. if(args.length > 1){
  79. $o.removeTab(args[1]);
  80. }else{
  81. $o.removeTab(selectedTab);
  82. }
  83. }
  84. },
  85. {
  86. cmd: 'tabs',
  87. fn: function(args){
  88. $o.msg('tabs:');
  89. for(var i in tabs){
  90. $o.msg(' ['+i+'] '+tabs[i].name);
  91. }
  92. }
  93. }
  94. ],
  95. $i,$s,$h,$cl,$tl,hht;
  96. $.extend($o,{
  97. version: '3.0',
  98. get: function(name){
  99. return exists(settings[name])?settings[name]:false;
  100. },
  101. set: function(name,value){
  102. if(exists(settings[name])){
  103. settings[name] = value;
  104. return true;
  105. }else{
  106. return false;
  107. }
  108. },
  109. prop: function(name){
  110. return exists(properties[name])?properties[name]:null;
  111. },
  112. send: function(msg){
  113. if(msg !== ''){
  114. if(msg[0] == '/'){
  115. var args = msg.split(' '),
  116. cmd = args[0].substr(1),
  117. i;
  118. event(msg,'command');
  119. for(i in commands){
  120. if(commands[i].cmd == cmd){
  121. commands[i].fn(args);
  122. return;
  123. }
  124. }
  125. $o.msg(cmd+' is not a valid command.');
  126. }else{
  127. event(msg,'send');
  128. $o.msg({
  129. text: msg,
  130. user: properties.nick
  131. });
  132. }
  133. }
  134. },
  135. msg: function(msg){
  136. switch(typeof msg){
  137. case 'string':
  138. $cl.append($('<li>').html(msg.htmlentities()));
  139. break;
  140. case 'object':
  141. $cl.append($('<li>').html('&lt;'+msg.user+'&gt;&nbsp;'+msg.text.htmlentities()));
  142. break;
  143. }
  144. },
  145. event: function(event_name,message){
  146. event(message,event_name);
  147. },
  148. selectTab: function(id){
  149. event(id,'tab_select');
  150. if(id<tabs.length-1&&id>=0){
  151. selectedTab=id;
  152. }
  153. $tl.children('.clicked').removeClass('clicked');
  154. $($tl.children().get(id)).addClass('clicked');
  155. $('#title').text(tabs[id].title);
  156. $('#topic').text(tabs[id].topic);
  157. },
  158. tabDOM: function(id){
  159. },
  160. addTab: function(name,title){
  161. event('Tab added: '+name);
  162. tabs.push({
  163. name: name,
  164. title: title
  165. });
  166. $tl.append($o.tabObj(tabs.length-1));
  167. },
  168. removeTab: function(id){
  169. event('Tab removed: '+tabs[id].name);
  170. tabs.splice(id,1);
  171. if(selectedTab==id&&selectedTab>0){
  172. selectedTab--;
  173. }
  174. $o.refreshTabs();
  175. },
  176. tabObj: function(id){
  177. if(typeof id !== 'undefined'){
  178. return $('<span>')
  179. .addClass('tab')
  180. .text(tabs[id].title)
  181. .mouseup(function(e){
  182. switch(e.which){
  183. case 1: // RMB
  184. if($(this).data('id')!=selectedTab){
  185. $o.selectTab($(this).data('id'));
  186. return prevent(e);
  187. }
  188. break;
  189. case 2: // MMB
  190. $(this).children('span.close-button').click();
  191. return prevent(e);
  192. break;
  193. case 3: // LMB
  194. return prevent(e);
  195. break;
  196. default:
  197. return prevent(e);
  198. }
  199. })
  200. .append(
  201. $('<span>')
  202. .addClass('close-button')
  203. .click(function(){
  204. $o.removeTab(id);
  205. return false;
  206. })
  207. .html('&times;')
  208. )
  209. .data('id',id);
  210. }
  211. },
  212. refreshTabs: function(){
  213. $tl.html('');
  214. var i,tab;
  215. for(i in tabs){
  216. tab = $o.tabObj(i);
  217. if(i==selectedTab){
  218. tab.addClass('clicked');
  219. $('#title').text(tabs[i].title);
  220. $('#topic').text(tabs[i].topic);
  221. }
  222. $tl.append(tab);
  223. }
  224. }
  225. });
  226. String.prototype.htmlentities = function(){
  227. return this.replace(/&/g, '&amp;').replace(/\s/g, '&nbsp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
  228. };
  229. $(document).ready(function(){
  230. $i = $('#input');
  231. $s = $('#send');
  232. $cl = $('#content-list');
  233. $tl = $('#tabs-list');
  234. $h = $('#head');
  235. $s.click(function(){
  236. if(!$s.hasClass('clicked')){
  237. $s.addClass('clicked');
  238. setTimeout(function(){
  239. $s.removeClass('clicked');
  240. },500);
  241. }
  242. $o.send($i.val());
  243. $i.val('');
  244. });
  245. $i.keypress(function(e){
  246. if(e.keyCode == 13){
  247. if(!$s.hasClass('clicked')){
  248. $s.addClass('clicked');
  249. setTimeout(function(){
  250. $s.removeClass('clicked');
  251. },500);
  252. }
  253. $o.send($i.val());
  254. $i.val('');
  255. }
  256. });
  257. $('#settings, #users').click(function(){
  258. $(this).addClass('open');
  259. $(this).children('.close-button').show();
  260. }).hover(function(){
  261. $(this).addClass('hovered');
  262. },function(){
  263. $(this).removeClass('hovered');
  264. }).children('.close-button').click(function(){
  265. $(this).parent().removeClass('open');
  266. $(this).hide();
  267. return false;
  268. }).hide();
  269. $('#users').hoverIntent({
  270. out: function(){
  271. $(this).removeClass('open');
  272. $(this).children('.close-button').hide();
  273. },
  274. timeout: 1000
  275. });
  276. $('#content').click(function(){
  277. $('#settings, #users, #head').removeClass('hovered').removeClass('open');
  278. $('#settings, #users').children('.close-button').hide()
  279. });
  280. $h.hoverIntent({
  281. out: function(){
  282. $h.removeClass('hovered');
  283. },
  284. over: function(){},
  285. timeout: 1000
  286. }).hover(function(){
  287. hht = setTimeout(function(){
  288. event('Head HoverIntent timeout','timeout');
  289. $('#head:hover').addClass('hovered');
  290. },1000);
  291. },function(){
  292. clearInterval(hht);
  293. }).click(function(){
  294. $(this).addClass('hovered');
  295. });
  296. $('.unselectable').attr('unselectable','on');
  297. $.contextMenu({
  298. selector: 'span.tab',
  299. items: {
  300. add: {
  301. name: 'New Tab',
  302. icon: 'add',
  303. callback: function(){
  304. $(this).contextMenu('hide');
  305. var title = prompt('Title');
  306. tabs.push({
  307. name: prompt('channel'),
  308. title: title,
  309. topic: 'Topic for '+title
  310. });
  311. $o.refreshTabs();
  312. }
  313. },
  314. s1: '',
  315. close: {
  316. name: 'Close',
  317. icon: 'delete',
  318. callback: function(){
  319. $(this).contextMenu('hide');
  320. $o.removeTab($(this).data('id'));
  321. }
  322. }
  323. },
  324. zIndex: 99999,
  325. trigger: 'right'
  326. });
  327. //DEBUG
  328. for(var i=0;i<10;i++){
  329. tabs.push({
  330. name: '#Tab'+i,
  331. title: 'Tab '+i,
  332. topic: 'Topic for tab '+i
  333. });
  334. }
  335. //END DEBUG
  336. $o.refreshTabs();
  337. event('Date '+new Date,'ready');
  338. $h.addClass('hovered');
  339. setTimeout(function(){
  340. $h.removeClass('hovered');
  341. },1000);
  342. });
  343. })(window,jQuery);