omnomirc.js 8.7 KB

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