omnomirc.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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,$,io,undefined){
  17. var $o = window.OmnomIRC = window.$o = function(){
  18. return 'Version: '+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. timestamp: 'exact',
  43. server: location.origin,
  44. autoconnect: true,
  45. autojoin: [
  46. '#omnimaga',
  47. '#omnimaga-fr',
  48. '#irp'
  49. ],
  50. scrollspeed: 100,
  51. theme: 'default',
  52. nick: 'User'
  53. },
  54. tabs = [],
  55. properties = {
  56. nick: 'User',
  57. sig: '',
  58. tabs: tabs,
  59. themes: [
  60. 'default'
  61. ]
  62. },
  63. commands = [
  64. { // names
  65. cmd: 'names',
  66. fn: function(args){
  67. socket.emit('names',{
  68. name: tabs[selectedTab].name
  69. });
  70. }
  71. },
  72. { // me
  73. cmd: 'me',
  74. help: 'Say something in third person',
  75. fn: function(args){
  76. var i,ret='';
  77. for(i=1;i<args.length;i++){
  78. ret += ' '+args[i];
  79. }
  80. socket.emit('message',{
  81. from: 0,
  82. message: properties.nick+' '+ret,
  83. room: tabs[selectedTab].name
  84. });
  85. }
  86. },
  87. { // connect
  88. cmd: 'connect',
  89. fn: function(){
  90. if(!$o.chat.connected()){
  91. $o.chat.connect();
  92. }
  93. }
  94. },
  95. { // disconnect
  96. cmd: 'disconnect',
  97. fn: function(){
  98. $o.disconnect();
  99. }
  100. },
  101. { // nick
  102. cmd: 'nick',
  103. fn: function(args){
  104. $o.set('nick',args[1]);
  105. }
  106. },
  107. { // help
  108. cmd: 'help',
  109. fn: function(args){
  110. if(typeof args[1] == 'undefined'){
  111. var m = 'Commands:',i;
  112. for(i in commands){
  113. m += ' '+commands[i].cmd;
  114. }
  115. $o.msg(m);
  116. }else{
  117. var i,cmd;
  118. for(i in commands){
  119. cmd = commands[i];
  120. if(cmd.cmd == args[1] && typeof cmd.help != 'undefined'){
  121. $o.msg('Command /'+cmd.cmd+': '+cmd.help);
  122. return;
  123. }
  124. }
  125. $o.send('/help');
  126. }
  127. }
  128. },
  129. { // open
  130. cmd: 'open',
  131. fn: function(args){
  132. $o.ui.tabs.add(args[1]);
  133. }
  134. },
  135. { // clear
  136. cmd: 'clear',
  137. fn: function(args){
  138. $cl.html('');
  139. tabs[selectedTab].body = document.createDocumentFragment();
  140. socket.emit('echo',{
  141. room: tabs[selectedTab].name,
  142. message: 'messages cleared',
  143. from: 0
  144. });
  145. }
  146. },
  147. { // close
  148. cmd: 'close',
  149. fn: function(args){
  150. if(args.length > 1){
  151. $o.ui.tabs.remove(args[1]);
  152. }else{
  153. $o.ui.tabs.remove(selectedTab);
  154. }
  155. }
  156. },
  157. { // tabs
  158. cmd: 'tabs',
  159. fn: function(args){
  160. $o.msg('tabs:');
  161. for(var i in tabs){
  162. $o.msg(' ['+i+'] '+tabs[i].name);
  163. }
  164. }
  165. }
  166. ],
  167. handles = [
  168. { // names
  169. on: 'names',
  170. fn: function(data){
  171. var tab = tabs[$o.ui.tabs.idForName(data.room)],
  172. users = tab.users,
  173. i;
  174. tab.users = data.names;
  175. if($o.ui.tabs.idForName(data.room) == selectedTab){
  176. $o.ui.render.users();
  177. }
  178. $(users).each(function(i,v){
  179. if(-1 == $.inArray(tab.users,v) && v != null){
  180. socket.emit('echo',{
  181. room: tabs[selectedTab].name,
  182. message: v+' left the room',
  183. from: 0
  184. });
  185. }
  186. });
  187. }
  188. },
  189. { // authorized
  190. on: 'authorized',
  191. fn: function(data){
  192. properties.nick = data.nick;
  193. for(var i in settings.autojoin){
  194. socket.emit('join',{
  195. name: settings.autojoin[i]
  196. });
  197. }
  198. }
  199. },
  200. { // join
  201. on: 'join',
  202. fn: function(data){
  203. event('joined '+data.name);
  204. var flag = tabs.length == 0;
  205. $o.ui.tabs.add(data.name);
  206. if(flag){
  207. $o.ui.tabs.select(0);
  208. }
  209. }
  210. },
  211. { // reconnect
  212. on: 'reconnect',
  213. fn: function(data){
  214. event('reconnected');
  215. $o.chat.auth();
  216. }
  217. },
  218. { // message
  219. on: 'message',
  220. fn: function(data){
  221. event('recieved message');
  222. var date = new Date(),
  223. string,
  224. time = date.getTime(),
  225. child,
  226. i,
  227. msg = function(msg){
  228. string = '<span class="cell date_cell">[<abbr class="date date_'+time+'" title="'+date.toISOString()+'"></abbr>]</span>';
  229. child = $('<li>').html(string+'<span class="cell">'+
  230. msg
  231. .htmlentities()
  232. .replace(
  233. /(https?:\/\/(([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?))/g,
  234. "<a href=\"$1\" title=\"\">$1</a>"
  235. )
  236. +'</span>');
  237. $o.msg({html:child},data.room);
  238. };
  239. if(data.from != 0){
  240. msg(' <'+data.from+'> '+data.message);
  241. }else{
  242. msg(' * '+data.message);
  243. }
  244. abbrDate('abbr.date_'+time);
  245. if(settings.timestamp == ''){
  246. $('.date_cell').css('visibility','hidden');
  247. }else{
  248. $('.date_cell').css('visibility','visible');
  249. }
  250. }
  251. }
  252. ],
  253. hooks = [
  254. {
  255. type: 'style',
  256. hook: 'load',
  257. fn: function(){
  258. }
  259. }
  260. ],
  261. version = '3.0',
  262. abbrDate = function(selector){
  263. if(settings.timestamp == 'fuzzy'){
  264. $(selector).timeago();
  265. }else{
  266. $(selector).each(function(){
  267. var timestamp = settings.timestamp,
  268. i,
  269. text='',
  270. date = new Date($(this).attr('title'));
  271. if(timestamp == 'exact'){
  272. timestamp = 'H:m:s t';
  273. }
  274. for(i=0;i<timestamp.length;i++){
  275. switch(timestamp[i]){
  276. case 'H':text+=((date.getHours()+11)%12)+1;break;
  277. case 'h':text+=date.getHours();break;
  278. case 'm':text+=(date.getMinutes()>9?'':'0')+date.getMinutes();break;
  279. case 's':text+=(date.getSeconds()>9?'':'0')+date.getSeconds();break;
  280. case 't':text+=(date.getHours()>11)?'pm':'am';break;
  281. default:text+=timestamp[i];
  282. }
  283. }
  284. $(this).text(text);
  285. }).timeago('dispose');
  286. }
  287. },
  288. socket,$i,$s,$h,$cl,$tl,hht;
  289. $.extend($o,{
  290. version: function(){
  291. return version;
  292. },
  293. register: {
  294. theme: function(name){
  295. if(-1==$.inArray(properties.themes,name)){
  296. properties.themes.push(name);
  297. return true;
  298. }
  299. return false;
  300. },
  301. command: function(name,fn,help){
  302. if(-1==$.inArray(commands,name)){
  303. var o = {
  304. cmd: name,
  305. fn: fn
  306. };
  307. if(typeof help != 'undefined'){
  308. o.help = help;
  309. }
  310. commands.push(o);
  311. return true;
  312. }
  313. return false;
  314. },
  315. plugin: function(){
  316. // STUB
  317. },
  318. setting: function(name,defaultVal,validate,values){
  319. // STUB
  320. },
  321. hook: function(event,fn){
  322. }
  323. },
  324. hook: function(event,fn){
  325. $o.register.hook(event,fn);
  326. },
  327. ui: {
  328. render: {
  329. settings: function(){
  330. var name,setting,frag = document.createDocumentFragment(),item;
  331. for(name in settings){
  332. setting = $o.get(name,true);
  333. switch(setting.type){
  334. case 'select':
  335. item = $('<select>')
  336. .attr('id','setting_'+name)
  337. .change(function(){
  338. $o.set(this.id.substr(8),$(this).find(':selected').text(),false);
  339. });
  340. for(var i in setting.values){
  341. item.append(
  342. $('<option>')
  343. .text(setting.values[i])
  344. );
  345. }
  346. item.find(':contains('+setting.val+')').attr('selected','selected');
  347. break;
  348. case 'array':
  349. item = $('<input>')
  350. .attr({
  351. type: 'text',
  352. id: 'setting_'+name
  353. })
  354. .val(setting.val)
  355. .change(function(){
  356. $o.set(this.id.substr(8),$(this).val().split(','),false);
  357. });
  358. break;
  359. case 'boolean':
  360. item = $('<input>')
  361. .attr({
  362. type: 'checkbox',
  363. id: 'setting_'+name
  364. })
  365. .change(function(){
  366. $o.set(this.id.substr(8),$(this).is(':checked'),false);
  367. });
  368. if(setting.val){
  369. item.attr('checked','checked');
  370. }
  371. break;
  372. case 'number':
  373. case 'string':default:
  374. item = $('<input>')
  375. .attr({
  376. type: 'text',
  377. id: 'setting_'+name
  378. })
  379. .val(setting.val)
  380. .change(function(){
  381. $o.set(this.id.substr(8),$(this).val(),false);
  382. });
  383. }
  384. $(frag).append(
  385. $('<li>')
  386. .addClass('row')
  387. .append(
  388. $('<span>')
  389. .text(name)
  390. .addClass('cell')
  391. )
  392. .append(
  393. $('<span>')
  394. .append(item)
  395. .addClass('cell')
  396. )
  397. );
  398. }
  399. $('#settings-list').html(frag);
  400. },
  401. users: function(){
  402. event('Rendering userlist');
  403. var $ul = $('#user-list').html(''),
  404. i,
  405. names = tabs[selectedTab].users;
  406. for(i in names){
  407. $ul.append(
  408. $('<li>').text(names[i])
  409. );
  410. }
  411. },
  412. tab: function(){
  413. $cl.html($(tabs[selectedTab].body).clone());
  414. },
  415. tablist: function(){
  416. $tl.html('');
  417. var i,tab;
  418. for(i in tabs){
  419. tab = $o.ui.tabs.obj(i);
  420. if(i==selectedTab){
  421. tab.addClass('clicked');
  422. $('#title').text(tabs[i].name);
  423. $('#topic').text(tabs[i].topic);
  424. }
  425. $tl.append(tab);
  426. }
  427. if($tl.get(0).scrollHeight-20 != $tl.scrollTop()){
  428. $('#tabs-scroll-right').removeClass('disabled');
  429. }
  430. if($tl.scrollTop() != 0){
  431. $('#tabs-scroll-left').removeClass('disabled');
  432. }
  433. }
  434. },
  435. tabs: {
  436. add: function(name){
  437. event('Tab added: '+name);
  438. if(!(function(){
  439. for(var i in tabs){
  440. if(name==tabs[i].name){
  441. return true;
  442. }
  443. }
  444. return false;
  445. })()){
  446. var scroll = $.localStorage('tabs'),
  447. i,
  448. frag = document.createDocumentFragment(),
  449. id = tabs.length;
  450. for(i in scroll){
  451. if(scroll[i].name == name){
  452. scroll[i].body = $(scroll[i].body).slice(-10);
  453. $(frag)
  454. .append(scroll[i].body)
  455. .append(
  456. $('<li>').html('<span class="to_remove">-- loaded old scrollback for '+scroll[i].date+' --</span>')
  457. )
  458. .children()
  459. .children('.remove')
  460. .remove();
  461. $(frag)
  462. .children()
  463. .children('.to_remove')
  464. .removeClass('to_remove')
  465. .addClass('remove');
  466. event('loading old tab scrollback for '+name+' last saved +'+scroll[i].date);
  467. }
  468. }
  469. tabs.push({
  470. name: name,
  471. body: frag,
  472. users: [],
  473. close: function(){
  474. $o.ui.tabs.remove(id);
  475. },
  476. select: function(){
  477. $o.ui.tabs.select(id);
  478. }
  479. });
  480. $tl.append($o.ui.tabs.obj(id));
  481. $o.ui.render.tablist();
  482. $o.ui.render.users();
  483. }else{
  484. event('Attempted to add an existing tab');
  485. }
  486. },
  487. remove: function(name){
  488. if(typeof name == 'number'){
  489. name = tabs[name].name;
  490. }
  491. for(var id=0;id<tabs.length;id++){
  492. if(tabs[id].name == name){
  493. event('Tab removed: '+tabs[id].name);
  494. socket.emit('part',{
  495. name: tabs[id].name
  496. });
  497. tabs.splice(id,1);
  498. if(selectedTab==id&&selectedTab>0){
  499. selectedTab--;
  500. }
  501. break;
  502. }
  503. }
  504. $o.ui.render.tablist();
  505. $cl.html(tabs[selectedTab].body);
  506. $o.ui.render.users();
  507. },
  508. selected: function(){
  509. return selectedTab;
  510. },
  511. idForName: function(name){
  512. for(var i in tabs){
  513. if(tabs[i].name == name){
  514. return i;
  515. }
  516. }
  517. return false;
  518. },
  519. tab: function(id){
  520. return typeof tabs[id] == 'undefined'?false:tabs[id];
  521. },
  522. dom: function(id){
  523. return typeof tabs[id] == 'undefined'?false:tabs[id].body;
  524. },
  525. obj: function(id){
  526. if(typeof id !== 'undefined'){
  527. return $('<div>')
  528. .addClass('tab')
  529. .text(tabs[id].name)
  530. .mouseup(function(e){
  531. switch(e.which){
  532. case 1: // RMB
  533. if($(this).data('id')!=selectedTab){
  534. $o.ui.tabs.select($(this).data('id'));
  535. return prevent(e);
  536. }
  537. break;
  538. case 2: // MMB
  539. $(this).children('span.close-button').click();
  540. return prevent(e);
  541. break;
  542. case 3: // LMB
  543. return prevent(e);
  544. break;
  545. default:
  546. return prevent(e);
  547. }
  548. })
  549. .append(
  550. $('<span>')
  551. .addClass('close-button')
  552. .click(function(){
  553. $o.ui.tabs.remove(id);
  554. return false;
  555. })
  556. .css({
  557. 'position': 'absolute',
  558. 'background-color': 'inherit',
  559. 'top': 0,
  560. 'right': 0
  561. })
  562. .html('&times;')
  563. )
  564. .data('id',id);
  565. }
  566. },
  567. select: function(id){
  568. event(id+' '+tabs[id].name,'tab_select');
  569. if(id<tabs.length&&id>=0){
  570. selectedTab=id;
  571. }
  572. $tl.children('.clicked').removeClass('clicked');
  573. $($tl.children().get(id)).addClass('clicked');
  574. $('#title').text(tabs[id].name);
  575. $('#topic').text(tabs[id].topic);
  576. $cl.html($(tabs[id].body).clone());
  577. abbrDate('abbr.date');
  578. $o.ui.render.users();
  579. setTimeout(function scrollContent(){
  580. if($c.scrollTop() < $c[0].scrollHeight){
  581. $c.scrollTop($c.scrollTop()+1);
  582. setTimeout(scrollContent,settings.scrollspeed);
  583. }else{
  584. event('scrolling stopped');
  585. }
  586. },settings.scrollspeed);
  587. }
  588. }
  589. },
  590. chat: {
  591. connect: function(server){
  592. if($o.chat.connected()){
  593. $o.disconnect();
  594. }
  595. if(typeof server == 'undefined'){
  596. server = settings.server;
  597. }
  598. socket = io.connect(server);
  599. for(var i in handles){
  600. socket.on(handles[i].on,handles[i].fn);
  601. }
  602. $o.chat.auth();
  603. },
  604. disconnect: function(){
  605. if($o.chat.connected()){
  606. socket.disconnect();
  607. socket = undefined;
  608. }
  609. },
  610. connected: function(){
  611. return typeof socket != 'undefined';
  612. },
  613. send: function(msg,room){
  614. if(typeof room == 'undefined'){
  615. room = tabs[selectedTab].name;
  616. }
  617. if(msg !== ''){
  618. if(msg[0] == '/' && msg[1] != '/'){
  619. var args = msg.split(' '),
  620. cmd = args[0].substr(1),
  621. i;
  622. event(msg,'command');
  623. for(i in commands){
  624. if(commands[i].cmd == cmd){
  625. commands[i].fn(args);
  626. return;
  627. }
  628. }
  629. $o.msg(cmd+' is not a valid command.');
  630. }else{
  631. event(msg,'send');
  632. socket.emit('message',{
  633. message: msg,
  634. room: room,
  635. from: properties.nick
  636. });
  637. }
  638. }
  639. },
  640. auth: function(){
  641. if(settings.nick == ''){
  642. $o.set('nick','User');
  643. return;
  644. }
  645. socket.emit('auth',{
  646. nick: settings.nick
  647. // TODO - send authorization info
  648. });
  649. }
  650. },
  651. get: function(name,formatted){
  652. if(typeof formatted == 'undefined'){
  653. return exists(settings[name])?settings[name]:false;
  654. }else{
  655. var val = $o.get(name),
  656. type,
  657. values = false;
  658. switch(name){
  659. case 'theme':
  660. type = 'select';
  661. values = properties.themes;
  662. break;
  663. case 'autojoin':type = 'array';break;
  664. case 'timestamp':type = 'string';break;
  665. default:
  666. type = typeof val;
  667. }
  668. return {
  669. type: type,
  670. val: val,
  671. values: values,
  672. name: name
  673. };
  674. }
  675. },
  676. set: function(name,value,render){
  677. if(exists(settings[name])){
  678. settings[name] = value;
  679. $.localStorage('settings',JSON.stringify(settings));
  680. switch(name){
  681. case 'timestamp':
  682. abbrDate('abbr.date');
  683. if(settings.timestamp == ''){
  684. $('.date_cell').css('visibility','hidden');
  685. }else{
  686. $('.date_cell').css('visibility','visible');
  687. }
  688. break;
  689. case 'nick':
  690. $o.chat.auth();
  691. break;
  692. }
  693. if(typeof render == 'undefined'){
  694. $o.ui.render.settings();
  695. }
  696. return true;
  697. }else{
  698. return false;
  699. }
  700. },
  701. prop: function(name){
  702. return exists(properties[name])?properties[name]:null;
  703. },
  704. send: function(msg){
  705. $o.chat.send(msg);
  706. },
  707. msg: function(msg,tabName){
  708. var frag;
  709. if(typeof tabName == 'undefined' || tabName == tabs[selectedTab].name){
  710. frag = document.createDocumentFragment();
  711. }else{
  712. frag = tabs[$o.ui.tabs.idForName(tabName)].body;
  713. }
  714. switch(typeof msg){
  715. case 'string':
  716. $(frag).append($('<li>').html(msg.htmlentities()));
  717. break;
  718. case 'object':
  719. if(typeof msg.html == 'undefined'){
  720. $(frag).append($('<li>').html('&lt;'+msg.user+'&gt;&nbsp;'+msg.text.htmlentities()));
  721. }else{
  722. $(frag).append(msg.html);
  723. }
  724. break;
  725. }
  726. $(tabs[$o.ui.tabs.idForName(tabName) || selectedTab].body).append(frag);
  727. var scroll = [],i,html;
  728. for(i in tabs){
  729. html = '';
  730. $(tabs[i].body).children().each(function(){
  731. html += this.outerHTML;
  732. });
  733. scroll.push({
  734. name: tabs[i].name,
  735. body: html,
  736. date: new Date().toString()
  737. });
  738. }
  739. $.localStorage('tabs',scroll);
  740. if(typeof tabName == 'undefined' || tabName == tabs[selectedTab].name){
  741. $o.ui.tabs.select(selectedTab);
  742. }
  743. },
  744. event: function(event_name,message){
  745. event(message,event_name);
  746. }
  747. });
  748. String.prototype.htmlentities = function(){
  749. return this
  750. .replace(/&/g, '&amp;')
  751. .replace(/</g, '&lt;')
  752. .replace(/>/g, '&gt;')
  753. .replace(/\n/g,'<br/>')
  754. .replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
  755. .replace(/\s/g, '&nbsp;')
  756. .replace(/"/g, '&quot;');
  757. };
  758. $(document).ready(function(){
  759. $.extend(settings,$.parseJSON($.localStorage('settings')));
  760. $.localStorage('settings',JSON.stringify(settings));
  761. $i = $('#input');
  762. $s = $('#send');
  763. $cl = $('#content-list');
  764. $c = $('#content');
  765. $tl = $('#tabs-list');
  766. $h = $('#head');
  767. $s.click(function(){
  768. if(!$s.hasClass('clicked')){
  769. $s.addClass('clicked');
  770. setTimeout(function(){
  771. $s.removeClass('clicked');
  772. },500);
  773. }
  774. $o.send($i.val());
  775. $i.val('');
  776. });
  777. $i.keypress(function(e){
  778. if(e.keyCode == 13){
  779. if(!$s.hasClass('clicked')){
  780. $s.addClass('clicked');
  781. setTimeout(function(){
  782. $s.removeClass('clicked');
  783. },500);
  784. }
  785. $o.send($i.val());
  786. $i.val('');
  787. }
  788. });
  789. $('#settings, #users').click(function(){
  790. $(this).addClass('open');
  791. $(this).children('.close-button').show();
  792. }).hover(function(){
  793. $(this).addClass('hovered');
  794. },function(){
  795. $(this).removeClass('hovered');
  796. }).children('.close-button').click(function(){
  797. $(this).parent().removeClass('open');
  798. $(this).hide();
  799. return false;
  800. }).hide();
  801. $('#users').hoverIntent({
  802. out: function(){
  803. $(this).removeClass('open');
  804. $(this).children('.close-button').hide();
  805. },
  806. timeout: 1000
  807. });
  808. $('#content').click(function(){
  809. $('#settings, #users, #head').removeClass('hovered').removeClass('open');
  810. $('#settings, #users').children('.close-button').hide()
  811. });
  812. $('.unselectable').attr('unselectable','on');
  813. $.contextMenu({
  814. selector: 'div.tab',
  815. items: {
  816. add: {
  817. name: 'New Tab',
  818. icon: 'add',
  819. callback: function(){
  820. $(this).contextMenu('hide');
  821. $o.ui.tabs.add(prompt('Channel'));
  822. }
  823. },
  824. s1: '',
  825. close: {
  826. name: 'Close',
  827. icon: 'delete',
  828. callback: function(){
  829. $(this).contextMenu('hide');
  830. $o.ui.tabs.remove($(this).data('id'));
  831. }
  832. }
  833. },
  834. zIndex: 99999,
  835. trigger: 'right'
  836. });
  837. $.contextMenu({
  838. selector: '#tabs-list',
  839. items: {
  840. add: {
  841. name: 'New Tab',
  842. icon: 'add',
  843. callback: function(){
  844. $(this).contextMenu('hide');
  845. $o.ui.tabs.add(prompt('channel'));
  846. }
  847. }
  848. },
  849. zIndex: 99999,
  850. trigger: 'right'
  851. });
  852. $('#tabs-scroll-right').click(function(){
  853. event('scroll right');
  854. $tl.scrollTop(($tl.scrollTop()||0)+20);
  855. if($tl.get(0).scrollHeight-20 == $tl.scrollTop()){
  856. $('#tabs-scroll-right').addClass('disabled');
  857. }
  858. $('#tabs-scroll-left').removeClass('disabled');
  859. });
  860. $('#tabs-scroll-left').click(function(){
  861. event('scroll left');
  862. $tl.scrollTop(($tl.scrollTop()||0)-20);
  863. if($tl.scrollTop() == 0){
  864. $('#tabs-scroll-left').addClass('disabled');
  865. }
  866. $('#tabs-scroll-right').removeClass('disabled');
  867. });
  868. (function scrollup(){
  869. $('#tabs-scroll-left').click();
  870. if($tl.scrollTop() != 0){
  871. setTimeout(scrollup,10);
  872. }
  873. })();
  874. //DEBUG
  875. /* for(var i=0;i<20;i++){
  876. tabs.push({
  877. name: '#Tab'+i,
  878. title: 'Tab '+i,
  879. topic: 'Topic for tab '+i
  880. });
  881. } */
  882. //END DEBUG
  883. event('Date '+new Date,'ready');
  884. $h.addClass('hovered');
  885. setTimeout(function(){
  886. $h.removeClass('hovered');
  887. },1000);
  888. $o.ui.render.settings();
  889. if(settings.autoconnect){
  890. $o.chat.connect();
  891. }
  892. });
  893. delete window.io;
  894. })(window,jQuery,io);
  895. if (!Date.prototype.toISOString) {
  896. Date.prototype.toISOString = function() {
  897. function pad(n) { return n < 10 ? '0' + n : n }
  898. return this.getUTCFullYear() + '-'
  899. + pad(this.getUTCMonth() + 1) + '-'
  900. + pad(this.getUTCDate()) + 'T'
  901. + pad(this.getUTCHours()) + ':'
  902. + pad(this.getUTCMinutes()) + ':'
  903. + pad(this.getUTCSeconds()) + 'Z';
  904. };
  905. }