omnomirc.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. date: new Date(),
  506. send: function(msg){
  507. $o.chat.send(msg,tabs[id].name);
  508. },
  509. close: function(){
  510. $o.ui.tabs.remove(id);
  511. },
  512. users: [],
  513. names: function(){
  514. socket.emit('names',{
  515. name: tabs[id].name
  516. });
  517. },
  518. select: function(){
  519. $o.ui.tabs.select(id);
  520. },
  521. clear: function(){
  522. $cl.html('');
  523. tabs[id].body = document.createDocumentFragment();
  524. socket.emit('echo',{
  525. room: tabs[id].name,
  526. message: 'messages cleared',
  527. from: 0
  528. });
  529. }
  530. });
  531. $tl.append($o.ui.tabs.obj(id));
  532. $o.ui.render.tablist();
  533. $o.ui.render.users();
  534. }else{
  535. event('Attempted to add an existing tab');
  536. }
  537. },
  538. remove: function(name){
  539. if(typeof name == 'number'){
  540. name = tabs[name].name;
  541. }
  542. for(var id=0;id<tabs.length;id++){
  543. if(tabs[id].name == name){
  544. event('Tab removed: '+tabs[id].name);
  545. socket.emit('part',{
  546. name: tabs[id].name
  547. });
  548. tabs.splice(id,1);
  549. if(selectedTab==id&&selectedTab>0){
  550. selectedTab--;
  551. }
  552. break;
  553. }
  554. }
  555. $o.ui.render.tablist();
  556. $cl.html(tabs[selectedTab].body);
  557. $o.ui.render.users();
  558. },
  559. selected: function(){
  560. return selectedTab;
  561. },
  562. idForName: function(name){
  563. for(var i in tabs){
  564. if(tabs[i].name == name){
  565. return i;
  566. }
  567. }
  568. return false;
  569. },
  570. tab: function(id){
  571. return typeof tabs[id] == 'undefined'?false:tabs[id];
  572. },
  573. dom: function(id){
  574. return typeof tabs[id] == 'undefined'?false:tabs[id].body;
  575. },
  576. obj: function(id){
  577. if(typeof id !== 'undefined'){
  578. return $('<div>')
  579. .addClass('tab')
  580. .text(tabs[id].name)
  581. .mouseup(function(e){
  582. switch(e.which){
  583. case 1: // RMB
  584. if($(this).data('id')!=selectedTab){
  585. $o.ui.tabs.select($(this).data('id'));
  586. return prevent(e);
  587. }
  588. break;
  589. case 2: // MMB
  590. $(this).children('span.close-button').click();
  591. return prevent(e);
  592. break;
  593. case 3: // LMB
  594. return prevent(e);
  595. break;
  596. default:
  597. return prevent(e);
  598. }
  599. })
  600. .append(
  601. $('<span>')
  602. .addClass('close-button')
  603. .click(function(){
  604. $o.ui.tabs.remove(id);
  605. return false;
  606. })
  607. .css({
  608. 'position': 'absolute',
  609. 'background-color': 'inherit',
  610. 'top': 0,
  611. 'right': 0
  612. })
  613. .html('&times;')
  614. )
  615. .data('id',id);
  616. }
  617. },
  618. select: function(id){
  619. event(id+' '+tabs[id].name,'tab_select');
  620. if(id<tabs.length&&id>=0){
  621. selectedTab=id;
  622. }
  623. $tl.children('.clicked').removeClass('clicked');
  624. $($tl.children().get(id)).addClass('clicked');
  625. $('#title').text(tabs[id].name);
  626. $('#topic').text(tabs[id].topic);
  627. $cl.html($(tabs[id].body).clone());
  628. abbrDate('abbr.date');
  629. $o.ui.render.users();
  630. setTimeout(function scrollContent(){
  631. if($c.scrollTop() < $c[0].scrollHeight){
  632. $c.scrollTop($c.scrollTop()+1);
  633. setTimeout(scrollContent,settings.scrollspeed);
  634. }else{
  635. event('scrolling stopped');
  636. }
  637. },settings.scrollspeed);
  638. }
  639. }
  640. },
  641. chat: {
  642. connect: function(server){
  643. if($o.chat.connected()){
  644. $o.disconnect();
  645. }
  646. if(typeof server == 'undefined'){
  647. server = settings.server;
  648. }
  649. socket = io.connect(server);
  650. for(var i in handles){
  651. socket.on(handles[i].on,handles[i].fn);
  652. }
  653. $o.chat.auth();
  654. },
  655. disconnect: function(){
  656. if($o.chat.connected()){
  657. socket.disconnect();
  658. socket = undefined;
  659. }
  660. },
  661. connected: function(){
  662. return typeof socket != 'undefined';
  663. },
  664. send: function(msg,room){
  665. if(typeof room == 'undefined'){
  666. room = tabs[selectedTab].name;
  667. }
  668. if(msg !== ''){
  669. if(msg[0] == '/' && msg[1] != '/'){
  670. var args = msg.split(' '),
  671. cmd = args[0].substr(1),
  672. i;
  673. event(msg,'command');
  674. for(i in commands){
  675. if(commands[i].cmd == cmd){
  676. commands[i].fn(args);
  677. return;
  678. }
  679. }
  680. $o.msg(cmd+' is not a valid command.');
  681. }else{
  682. event(msg,'send');
  683. socket.emit('message',{
  684. message: msg,
  685. room: room,
  686. from: properties.nick
  687. });
  688. }
  689. }
  690. },
  691. auth: function(){
  692. if(settings.nick == ''){
  693. $o.set('nick','User');
  694. return;
  695. }
  696. socket.emit('auth',{
  697. nick: settings.nick
  698. // TODO - send authorization info
  699. });
  700. }
  701. },
  702. get: function(name,formatted){
  703. if(typeof formatted == 'undefined'){
  704. return exists(settings[name])?settings[name]:false;
  705. }else{
  706. var val = $o.get(name),
  707. type,
  708. values = false;
  709. switch(name){
  710. case 'theme':
  711. type = 'select';
  712. values = properties.themes;
  713. break;
  714. case 'autojoin':type = 'array';break;
  715. case 'timestamp':type = 'string';break;
  716. default:
  717. type = typeof val;
  718. }
  719. return {
  720. type: type,
  721. val: val,
  722. values: values,
  723. name: name
  724. };
  725. }
  726. },
  727. set: function(name,value,render){
  728. if(exists(settings[name])){
  729. settings[name] = value;
  730. $.localStorage('settings',JSON.stringify(settings));
  731. switch(name){
  732. case 'timestamp':
  733. abbrDate('abbr.date');
  734. if(settings.timestamp == ''){
  735. $('.date_cell').css('visibility','hidden');
  736. }else{
  737. $('.date_cell').css('visibility','visible');
  738. }
  739. break;
  740. case 'nick':
  741. $o.chat.auth();
  742. break;
  743. }
  744. if(typeof render == 'undefined'){
  745. $o.ui.render.settings();
  746. }
  747. return true;
  748. }else{
  749. return false;
  750. }
  751. },
  752. prop: function(name){
  753. return exists(properties[name])?properties[name]:null;
  754. },
  755. send: function(msg){
  756. $o.chat.send(msg);
  757. },
  758. msg: function(msg,tabName){
  759. var frag;
  760. if(typeof tabName == 'undefined' || tabName == tabs[selectedTab].name){
  761. frag = document.createDocumentFragment();
  762. }else{
  763. frag = tabs[$o.ui.tabs.idForName(tabName)].body;
  764. }
  765. switch(typeof msg){
  766. case 'string':
  767. $(frag).append($('<li>').html(msg.htmlentities()));
  768. break;
  769. case 'object':
  770. if(typeof msg.html == 'undefined'){
  771. $(frag).append($('<li>').html('&lt;'+msg.user+'&gt;&nbsp;'+msg.text.htmlentities()));
  772. }else{
  773. $(frag).append(msg.html);
  774. }
  775. break;
  776. }
  777. $(tabs[$o.ui.tabs.idForName(tabName) || selectedTab].body).append(frag);
  778. var scroll = [],i,html;
  779. for(i in tabs){
  780. html = '';
  781. $(tabs[i].body).children().each(function(){
  782. html += this.outerHTML;
  783. });
  784. scroll.push({
  785. name: tabs[i].name,
  786. body: html,
  787. date: new Date().toString()
  788. });
  789. }
  790. $.localStorage('tabs',scroll);
  791. if(typeof tabName == 'undefined' || tabName == tabs[selectedTab].name){
  792. $o.ui.tabs.select(selectedTab);
  793. }
  794. },
  795. event: function(event_name,message){
  796. event(message,event_name);
  797. }
  798. });
  799. String.prototype.htmlentities = function(){
  800. return this
  801. .replace(/&/g, '&amp;')
  802. .replace(/</g, '&lt;')
  803. .replace(/>/g, '&gt;')
  804. .replace(/\n/g,'<br/>')
  805. .replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
  806. .replace(/\s/g, '&nbsp;')
  807. .replace(/"/g, '&quot;');
  808. };
  809. $(document).ready(function(){
  810. $.extend(settings,$.parseJSON($.localStorage('settings')));
  811. $.localStorage('settings',JSON.stringify(settings));
  812. $i = $('#input');
  813. $s = $('#send');
  814. $cl = $('#content-list');
  815. $c = $('#content');
  816. $tl = $('#tabs-list');
  817. $h = $('#head');
  818. $s.click(function(){
  819. if(!$s.hasClass('clicked')){
  820. $s.addClass('clicked');
  821. setTimeout(function(){
  822. $s.removeClass('clicked');
  823. },500);
  824. }
  825. $o.send($i.val());
  826. $i.val('');
  827. });
  828. $i.keypress(function(e){
  829. if(e.keyCode == 13){
  830. if(!$s.hasClass('clicked')){
  831. $s.addClass('clicked');
  832. setTimeout(function(){
  833. $s.removeClass('clicked');
  834. },500);
  835. }
  836. $o.send($i.val());
  837. $i.val('');
  838. }
  839. });
  840. $('#settings, #users').click(function(){
  841. $(this).addClass('open');
  842. $(this).children('.close-button').show();
  843. }).hover(function(){
  844. $(this).addClass('hovered');
  845. },function(){
  846. $(this).removeClass('hovered');
  847. }).children('.close-button').click(function(){
  848. $(this).parent().removeClass('open');
  849. $(this).hide();
  850. return false;
  851. }).hide();
  852. $('#users').hoverIntent({
  853. out: function(){
  854. $(this).removeClass('open');
  855. $(this).children('.close-button').hide();
  856. },
  857. timeout: 1000
  858. });
  859. $('#content').click(function(){
  860. $('#settings, #users, #head').removeClass('hovered').removeClass('open');
  861. $('#settings, #users').children('.close-button').hide()
  862. });
  863. $('.unselectable').attr('unselectable','on');
  864. $.contextMenu({
  865. selector: 'div.tab',
  866. items: {
  867. add: {
  868. name: 'New Tab',
  869. icon: 'add',
  870. callback: function(){
  871. $(this).contextMenu('hide');
  872. $o.ui.tabs.add(prompt('Channel'));
  873. }
  874. },
  875. s1: '',
  876. close: {
  877. name: 'Close',
  878. icon: 'delete',
  879. callback: function(){
  880. $(this).contextMenu('hide');
  881. $o.ui.tabs.remove($(this).data('id'));
  882. }
  883. }
  884. },
  885. zIndex: 99999,
  886. trigger: 'right'
  887. });
  888. $.contextMenu({
  889. selector: '#tabs-list',
  890. items: {
  891. add: {
  892. name: 'New Tab',
  893. icon: 'add',
  894. callback: function(){
  895. $(this).contextMenu('hide');
  896. $o.ui.tabs.add(prompt('channel'));
  897. }
  898. }
  899. },
  900. zIndex: 99999,
  901. trigger: 'right'
  902. });
  903. $('#tabs-scroll-right').click(function(){
  904. event('scroll right');
  905. $tl.scrollTop(($tl.scrollTop()||0)+20);
  906. if($tl.get(0).scrollHeight-20 == $tl.scrollTop()){
  907. $('#tabs-scroll-right').addClass('disabled');
  908. }
  909. $('#tabs-scroll-left').removeClass('disabled');
  910. });
  911. $('#tabs-scroll-left').click(function(){
  912. event('scroll left');
  913. $tl.scrollTop(($tl.scrollTop()||0)-20);
  914. if($tl.scrollTop() == 0){
  915. $('#tabs-scroll-left').addClass('disabled');
  916. }
  917. $('#tabs-scroll-right').removeClass('disabled');
  918. });
  919. (function scrollup(){
  920. $('#tabs-scroll-left').click();
  921. if($tl.scrollTop() != 0){
  922. setTimeout(scrollup,10);
  923. }
  924. })();
  925. event('Date '+new Date,'ready');
  926. $h.addClass('hovered');
  927. setTimeout(function(){
  928. $h.removeClass('hovered');
  929. },1000);
  930. $o.ui.render.settings();
  931. if(settings.autoconnect){
  932. $o.chat.connect();
  933. }
  934. });
  935. delete window.io;
  936. })(window,jQuery,io);
  937. if (!Date.prototype.toISOString) {
  938. Date.prototype.toISOString = function() {
  939. function pad(n) { return n < 10 ? '0' + n : n }
  940. return this.getUTCFullYear() + '-'
  941. + pad(this.getUTCMonth() + 1) + '-'
  942. + pad(this.getUTCDate()) + 'T'
  943. + pad(this.getUTCHours()) + ':'
  944. + pad(this.getUTCMinutes()) + ':'
  945. + pad(this.getUTCSeconds()) + 'Z';
  946. };
  947. }