omnomirc.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 $('<div>')
  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. .css({
  208. 'position': 'absolute',
  209. 'background-color': 'inherit',
  210. 'top': 0,
  211. 'right': 0
  212. })
  213. .html('&times;')
  214. )
  215. .data('id',id);
  216. }
  217. },
  218. refreshTabs: function(){
  219. $tl.html('');
  220. var i,tab;
  221. for(i in tabs){
  222. tab = $o.tabObj(i);
  223. if(i==selectedTab){
  224. tab.addClass('clicked');
  225. $('#title').text(tabs[i].title);
  226. $('#topic').text(tabs[i].topic);
  227. }
  228. $tl.append(tab);
  229. }
  230. if($tl.get(0).scrollHeight-20 != $tl.scrollTop()){
  231. $('#tabs-scroll-right').removeClass('disabled');
  232. }
  233. if($tl.scrollTop() != 0){
  234. $('#tabs-scroll-left').removeClass('disabled');
  235. }
  236. }
  237. });
  238. String.prototype.htmlentities = function(){
  239. return this.replace(/&/g, '&amp;').replace(/\s/g, '&nbsp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
  240. };
  241. $(document).ready(function(){
  242. $i = $('#input');
  243. $s = $('#send');
  244. $cl = $('#content-list');
  245. $tl = $('#tabs-list');
  246. $h = $('#head');
  247. $s.click(function(){
  248. if(!$s.hasClass('clicked')){
  249. $s.addClass('clicked');
  250. setTimeout(function(){
  251. $s.removeClass('clicked');
  252. },500);
  253. }
  254. $o.send($i.val());
  255. $i.val('');
  256. });
  257. $i.keypress(function(e){
  258. if(e.keyCode == 13){
  259. if(!$s.hasClass('clicked')){
  260. $s.addClass('clicked');
  261. setTimeout(function(){
  262. $s.removeClass('clicked');
  263. },500);
  264. }
  265. $o.send($i.val());
  266. $i.val('');
  267. }
  268. });
  269. $('#settings, #users').click(function(){
  270. $(this).addClass('open');
  271. $(this).children('.close-button').show();
  272. }).hover(function(){
  273. $(this).addClass('hovered');
  274. },function(){
  275. $(this).removeClass('hovered');
  276. }).children('.close-button').click(function(){
  277. $(this).parent().removeClass('open');
  278. $(this).hide();
  279. return false;
  280. }).hide();
  281. $('#users').hoverIntent({
  282. out: function(){
  283. $(this).removeClass('open');
  284. $(this).children('.close-button').hide();
  285. },
  286. timeout: 1000
  287. });
  288. $('#content').click(function(){
  289. $('#settings, #users, #head').removeClass('hovered').removeClass('open');
  290. $('#settings, #users').children('.close-button').hide()
  291. });
  292. $('.unselectable').attr('unselectable','on');
  293. $.contextMenu({
  294. selector: 'span.tab',
  295. items: {
  296. add: {
  297. name: 'New Tab',
  298. icon: 'add',
  299. callback: function(){
  300. $(this).contextMenu('hide');
  301. var title = prompt('Title');
  302. tabs.push({
  303. name: prompt('channel'),
  304. title: title,
  305. topic: 'Topic for '+title
  306. });
  307. $o.refreshTabs();
  308. }
  309. },
  310. s1: '',
  311. close: {
  312. name: 'Close',
  313. icon: 'delete',
  314. callback: function(){
  315. $(this).contextMenu('hide');
  316. $o.removeTab($(this).data('id'));
  317. }
  318. }
  319. },
  320. zIndex: 99999,
  321. trigger: 'right'
  322. });
  323. $('#tabs-scroll-right').click(function(){
  324. event('scroll right');
  325. $tl.scrollTop(($tl.scrollTop()||0)+20);
  326. if($tl.get(0).scrollHeight-20 == $tl.scrollTop()){
  327. $('#tabs-scroll-right').addClass('disabled');
  328. }
  329. $('#tabs-scroll-left').removeClass('disabled');
  330. });
  331. $('#tabs-scroll-left').click(function(){
  332. event('scroll left');
  333. $tl.scrollTop(($tl.scrollTop()||0)-20);
  334. if($tl.scrollTop() == 0){
  335. $('#tabs-scroll-left').addClass('disabled');
  336. }
  337. $('#tabs-scroll-right').removeClass('disabled');
  338. });
  339. (function scrollup(){
  340. if($tl.scrollTop() != 0){
  341. $('#tabs-scroll-left').click();
  342. setTimeout(scrollup,10);
  343. }
  344. })();
  345. //DEBUG
  346. for(var i=0;i<20;i++){
  347. tabs.push({
  348. name: '#Tab'+i,
  349. title: 'Tab '+i,
  350. topic: 'Topic for tab '+i
  351. });
  352. }
  353. //END DEBUG
  354. $o.refreshTabs();
  355. event('Date '+new Date,'ready');
  356. $h.addClass('hovered');
  357. setTimeout(function(){
  358. $h.removeClass('hovered');
  359. },1000);
  360. });
  361. })(window,jQuery);