0){
selectedTab--;
}
break;
}
}
$o.ui.render.tablist();
$cl.html($o.ui.tabs.current().body);
$o.ui.render.users();
},
selected: function(){
return selectedTab;
},
idForName: function(name){
for(var i in tabs){
if(tabs[i].name == name){
return i;
}
}
return false;
},
tab: function(id){
if(typeof id == 'string' && !id.isNumber()){
id = $o.ui.tabs.idForName(id);
if(!id) return false;
}
return exists(tabs[id])?tabs[id]:false;
},
dom: function(id){
if(typeof id == 'string' && !id.isNumber()){
id = $o.ui.tabs.idForName(id);
if(!id) return false;
}
return typeof tabs[id] == 'undefined'?false:tabs[id].body;
},
obj: function(id){
if(exists(id)){
if(typeof id == 'string' && !id.isNumber()){
id = $o.ui.tabs.idForName(id);
if(!id) return;
}
return $('')
.addClass('tab')
.text($o.ui.tabs.tab(id).name)
.mouseup(function(e){
switch(e.which){
case 1: // RMB
if($(this).data('id')!=selectedTab){
$o.ui.tabs.select($(this).data('id'));
return prevent(e);
}
break;
case 2: // MMB
$(this).children('span.close-button').click();
return prevent(e);
break;
case 3: // LMB
return prevent(e);
break;
default:
return prevent(e);
}
})
.append(
$('')
.addClass('close-button')
.click(function(){
$o.ui.tabs.remove(id);
return false;
})
.css({
'position': 'absolute',
'background-color': 'inherit',
'top': 0,
'right': 0
})
.html('×')
)
.data('id',id);
}
},
select: function(id){
if(typeof id == 'string' && !id.isNumber()){
id = $o.ui.tabs.idForName(id);
if(!id){
return false;
}
}
event(id+' '+$o.ui.tabs.tab(id).name,'tab_select');
if(id=0){
runHook('tabswitch',[$o.ui.tabs.tab(id),$o.ui.tabs.current()]);
selectedTab=id;
}
$tl.children('.clicked').removeClass('clicked');
$($tl.children().get(id)).addClass('clicked');
$('#title').text($o.ui.tabs.tab(id).name);
$('#topic').text($o.ui.tabs.tab(id).topic);
$cl.html($($o.ui.tabs.tab(id).body).clone());
abbrDate('abbr.date');
$o.ui.render.users();
setTimeout(function scrollContent(){
if($c.scrollTop()+$c.height() < $c.prop('scrollHeight')){
$c.scrollTop($c.scrollTop()+1);
setTimeout(scrollContent,settings.scrollspeed);
}else{
event('scrolling stopped');
}
},settings.scrollspeed);
},
current: function(){
if(tabs.length > 0 && tabs.length > selectedTab){
return tabs[selectedTab];
}else{
return {
name: '',
body: document.createDocumentFragment(),
date: new Date(),
send: noop,
close: noop,
users: [],
names: noop,
select: noop,
clear: noop
}
}
}
},
},
chat: {
connect: function(server){
if($o.chat.connected()){
$o.disconnect();
}
event('Connecting');
if(!exists(server)){
server = settings.server;
}
socket = window.socket = io.connect(server);
for(var i in handles){
socket.on(handles[i].on,handles[i].fn);
}
$o.chat.auth();
},
disconnect: function(){
if($o.chat.connected()){
event('Disconnecting');
socket.disconnect();
}
},
connected: function(){
return exists(socket)?properties.connected:false;
},
send: function(msg,room){
if(!exists(room)){
room = $o.ui.tabs.current().name;
}
if(msg !== ''){
if(msg[0] == '/' && msg[1] != '/'){
var args = msg.split(' '),
cmd = args[0].substr(1),
i;
event(msg,'command');
for(i in commands){
if(commands[i].cmd == cmd){
commands[i].fn(args);
return;
}
}
$o.msg(cmd+' is not a valid command.');
}else{
if(msg[0]+msg[1] == '//'){
msg = msg.substr(1);
}
event(msg,'send');
if(runHook('send',[msg,room])){
emit('message',{
message: msg,
room: room,
from: properties.nick,
origin: origin('OmnomIRC')
});
}
}
}
},
auth: function(){
if(settings.nick == ''){
$o.set('nick','User');
return;
}
emit('auth',{
nick: settings.nick
// TODO - send authorization info
});
}
},
get: function(name,formatted){
if(!exists(formatted)){
return exists(settings[name])?settings[name]:false;
}else{
if(exists(settingsConf[name]) && exists(settings[name])){
var r = $.extend({},settingsConf[name]);
r.val = settings[name];
r.validate = undefined;
r.callback = undefined;
delete r['validate'];
delete r['callback'];
return r;
}else{
return false;
}
}
},
set: function(name,value,render){
if(exists(settings[name])){
var setting;
if(exists(settingsConf[name])){
setting = $.extend({},settingsConf[name]);
}else{
setting = {
val: setting[name],
callback: function(){},
validate: function(){},
values: undefined,
type: typeof setting[name]
}
}
if(
(exists(setting.values) && $.inArray(value,setting.values) == -1) ||
setting.validate(settings[name],value,setting.values,name) == false ||
!runHook('setting',[
name,
settings[name],
value,
$o.get(name,true).values
])
){
if(exists(render)){
$o.ui.render.settings();
}
return false;
}
settings[name] = value;
$.localStorage('settings',JSON.stringify(settings));
setting.callback(value,name,exists(render));
if(exists(render)){
$o.ui.render.settings();
}
return true;
}else{
if(exists(render)){
$o.ui.render.settings();
}
return false;
}
},
prop: function(name){
return exists(properties[name])?properties[name]:null;
},
send: function(msg){
$o.chat.send(msg);
},
msg: function(msg,tabName){
var frag;
if(!exists(tabName) || tabName == $o.ui.tabs.current().name || !exists($o.ui.tabs.tab(tabName).body)){
frag = document.createDocumentFragment();
}else{
frag = $o.ui.tabs.tab(tabName).body;
}
try{
switch(typeof msg){
case 'string':
$(frag).append($('').html(msg.htmlentities()));
break;
case 'object':
if(!exists(msg.html)){
$(frag).append($('').html('<'+msg.user+'> '+msg.text.htmlentities()));
}else{
$(frag).append(msg.html);
}
break;
}
}catch(e){event('Failed to add message','error')}
if(tabs.length > 0){
$($o.ui.tabs.tab(tabName).body || $o.ui.tabs.current().body).append(frag);
}
var scroll = [],i,html;
for(i in tabs){
html = '';
$(tabs[i].body).children().each(function(){
html += this.outerHTML;
});
scroll.push({
name: tabs[i].name,
body: html,
date: new Date().toString()
});
}
$.localStorage('tabs',scroll);
if(!exists(tabName) || tabName == $o.ui.tabs.current().name){
$o.ui.tabs.select(selectedTab);
}
},
event: function(event_name,message){
event(message,event_name);
}
});
(function(settings){
var i,s;
for(i in settings){
s = settings[i];
$o.register.setting(i,s.type,s.val,s['validate'],s['values'],s['callback']);
}
})({
colour: {
type: 'boolean',
val: false
},
debug: {
type: 'boolean',
val: false,
callback: function(v,s,r){
if(r){
$o.ui.render.settings();
}
}
},
timestamp: {
type: 'string',
val: 'exact',
callback: function(v,s){
abbrDate('abbr.date');
if(v == ''){
$('.date_cell').css('visibility','hidden');
}else{
$('.date_cell').css('visibility','visible');
}
}
},
server: {
type: 'string',
val: location.origin
},
autoconnect: {
type: 'boolean',
val: true
},
autojoin: {
type: 'array',
val: [
'#omnimaga',
'#omnimaga-fr',
'#irp'
]
},
scrollspeed: {
type: 'number',
val: 10
},
theme: {
type: 'select',
val: 'default',
values: properties.themes,
validate: function(o,n,v,s){
runHook('untheme',[o,n]);
},
callback: function(v,s,r){
if($('link[id="theme-style"]').attr('href') != 'data/themes/'+v+'/style.css'){
event('Loading theme '+v);
runHook('theme',[v]);
$('link[id="theme-style"]').attr({
id: 'theme-style',
rel: 'stylesheet',
href: 'data/themes/'+v+'/style.css'
});
var i,h;
for(i in hooks){
h = hooks[i];
if(h.type == 'style'){
hooks.splice(i,1);
}
}
$.ajax('data/themes/'+v+'/script.js',{
dataType: 'text',
success: function(data){
var sandbox = {
load: function(fn){
fn();
},
unload: function(fn){
$o.register.hook('untheme',"function(o,n){if(o == '"+v+"'){("+(fn+'').replace(/\/\/.+?(?=\n|\r|$)|\/\*[\s\S]+?\*\//g,'').replace(/\"/g,'\\"').replace(/\n/g,'').replace(/\r/g,'')+")();}}",'style');
},
$: window.jQuery,
$o: window.OmnomIRC,
OmnomIRC: window.OmnomIRC,
};
runInSandbox(data,sandbox);
runHook('themechange',[v]);
}
});
}
}
},
nick: {
type: 'string',
val: 'User',
callback: function(){
$o.chat.auth();
}
}
});
$.extend(pluginSandbox,$o);
String.prototype.htmlentities = function(){
return this
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/\n/g,'
')
.replace(/\t/g, ' ')
.replace(/\s/g, ' ')
.replace(/"/g, '"');
};
$(document).ready(function(){
$.ajax('api/origins.js',{
dataType: 'json',
success:function(data){
properties.origins = data;
}
});
$.extend(settings,$.parseJSON($.localStorage('settings')));
$.localStorage('settings',JSON.stringify(settings));
settingsConf['theme'].callback(settings['theme'],'theme',true);
$i = $('#input');
$s = $('#send');
$cl = $('#content-list');
$c = $('#content');
$tl = $('#tabs-list');
$h = $('#head');
$s.click(function(){
if(!$s.hasClass('clicked')){
$s.addClass('clicked');
setTimeout(function(){
$s.removeClass('clicked');
},500);
}
$o.send($i.val());
$i.val('');
});
$i.keypress(function(e){
if(e.keyCode == 13){
if(!$s.hasClass('clicked')){
$s.addClass('clicked');
setTimeout(function(){
$s.removeClass('clicked');
},500);
}
$o.send($i.val());
$i.val('');
}
});
$('#settings, #users').click(function(){
$(this).addClass('open');
$(this).children('.close-button').show();
}).hover(function(){
$(this).addClass('hovered');
},function(){
$(this).removeClass('hovered');
}).children('.close-button').click(function(){
$(this).parent().removeClass('open');
$(this).hide();
return false;
}).hide();
$('#users').hoverIntent({
out: function(){
$(this).removeClass('open');
$(this).children('.close-button').hide();
},
timeout: 1000
});
$('#content').click(function(){
$('#settings, #users, #head').removeClass('hovered').removeClass('open');
$('#settings, #users').children('.close-button').hide()
});
$('.unselectable').attr('unselectable','on');
$.contextMenu({
selector: 'div.tab',
items: {
add: {
name: 'New Tab',
icon: 'add',
callback: function(){
$(this).contextMenu('hide');
$o.ui.tabs.add(prompt('Channel'));
}
},
s1: '',
close: {
name: 'Close',
icon: 'delete',
callback: function(){
$(this).contextMenu('hide');
$o.ui.tabs.remove($(this).data('id'));
}
}
},
zIndex: 99999,
trigger: 'right'
});
$.contextMenu({
selector: '#tabs-list',
items: {
add: {
name: 'New Tab',
icon: 'add',
callback: function(){
$(this).contextMenu('hide');
$o.ui.tabs.add(prompt('channel'));
}
}
},
zIndex: 99999,
trigger: 'right'
});
$('#tabs-scroll-right').click(function(){
event('scroll right');
$tl.scrollTop(($tl.scrollTop()||0)+20);
if($tl.get(0).scrollHeight-20 == $tl.scrollTop()){
$('#tabs-scroll-right').addClass('disabled');
}
$('#tabs-scroll-left').removeClass('disabled');
});
$('#tabs-scroll-left').click(function(){
event('scroll left');
$tl.scrollTop(($tl.scrollTop()||0)-20);
if($tl.scrollTop() == 0){
$('#tabs-scroll-left').addClass('disabled');
}
$('#tabs-scroll-right').removeClass('disabled');
});
(function scrollup(){
$('#tabs-scroll-left').click();
if($tl.scrollTop() != 0){
setTimeout(scrollup,10);
}
})();
event('Date '+new Date,'ready');
$h.addClass('hovered');
setTimeout(function(){
$h.removeClass('hovered');
},1000);
$o.ui.render.settings();
if(settings.autoconnect){
$o.chat.connect();
}
// Check for script updates and update if required
(function checkScripts(){
for(var i in document.scripts){
(function(el,src){
if(exists(src) && el.innerHTML == ''){
$.ajax(src,{
success: function(source){
if(exists($(el).data('source')) && $(el).data('source') != source){
event('Reloading','update');
location.reload();
}
$(el).data('source',source);
},
dataType: 'text'
});
}
})(document.scripts[i],document.scripts[i].src);
}
var s = $('link');
for(i in s){
(function(el,href){
if(exists(href) && el.innerHTML == ''){
$.ajax(href,{
success: function(source){
if(exists($(el).data('source')) && $(el).data('source') != source){
event('Updating CSS','update');
if(!exists($(el).data('href'))){
$(el).data('href',href);
}
if($(el).data('href') != href){
href = $(el).data('href');
}
el.href += '?reload='+new Date().getTime();
}
$(el).data('source',source);
},
dataType: 'text'
});
}
})(s[i],s[i].href);
}
setTimeout(checkScripts,1000*30);
})();
runHook('load');
});
window.io = null;
})(window,jQuery,io);
if (!Date.prototype.toISOString) {
Date.prototype.toISOString = function() {
function pad(n) { return n < 10 ? '0' + n : n }
return this.getUTCFullYear() + '-'
+ pad(this.getUTCMonth() + 1) + '-'
+ pad(this.getUTCDate()) + 'T'
+ pad(this.getUTCHours()) + ':'
+ pad(this.getUTCMinutes()) + ':'
+ pad(this.getUTCSeconds()) + 'Z';
};
}
if(!String.prototype.isNumber){
String.prototype.isNumber = function(){return /^\d+$/.test(this);}
}