omnomirc.js 22 KB

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