/* dcwebui.js */
//;(function() {
"use strict";
var $ = (document.querySelectorAll ?
function(s) {
var r = document.querySelectorAll(s);
return (s[0] === '#' && r.length === 1) ? r[0] : r;
} :
function(s) {
// i'm not writing a selector engine...
if (! s.length) return [];
if (s[0] === '#') {
return document.getElementById(s.slice(1));
} else if (s[0] === '.') {
return document.getElementsByClassName(s.slice(1));
} else {
return document.getElementsByTagName(s);
}
}
);
var nmdc_escape = function(str) {
return (''+str).length ? (''+str).
replace(/&/g,'&').replace(/\|/g,'|').replace(/\$/g,'$') :
' ';
};
var hesc = function(s) {
var filter = {
'&': '&', '<': '<', '>': '>', '"': '"', '\'': '''
};
return s.toString().replace(/[&<>'"]/g, function(s) { return filter[s]; });
};
var nl2br = function(str) { // thanks php.js!
return (str+'').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1
$2');
};
var linkify = function(str) {
return str.replace(
/(https?:\/\/[^\s<]+)/g, "$1"
);
};
var sanitise = function(s) {
return linkify(hesc(s));
};
var toggle = function($el) {
$el.style.display = ($el.style.display === "block") ? "none" : "block";
};
var textContent = function($el) {
if ($el.textContent) return $el.textContent;
if ($el.innerText) return $el.innerText;
return "";
};
// https://gist.github.com/eligrey/1276030
var appendInnerHTML = function($el, html) {
var child = document.createElement("span");
child.innerHTML = html;
var node;
while ((node = child.firstChild)) {
$el.appendChild(node);
}
};
// http://stackoverflow.com/a/5598797
function getOffsetLeft( elem ) {
var offsetLeft = 0;
do {
if (!isNaN(elem.offsetLeft)) {
offsetLeft += elem.offsetLeft;
}
} while (elem = elem.offsetParent);
return offsetLeft;
}
function getOffsetTop( elem ) {
var offsetTop = 0;
do {
if (!isNaN(elem.offsetTop)) {
offsetTop += elem.offsetTop;
}
} while (elem = elem.offsetParent);
return offsetTop;
}
/* Tab writers */
var write = function(tab) {
var $tab = $('#inner-'+tab);
return {
'cls': function() {
$tab.innerHTML = '';
return this;
},
'scroll': function() {
$tab.scrollTop = $tab.scrollHeight;
return this;
},
'raw': function(s) {
appendInnerHTML($tab, s);
return this.scroll();
},
'c': function(c, s) {
return this.raw(''+s+'');
},
'time': function() {
var d = new Date();
var pad = function(s) {
return (s < 10) ? '0'+s : ''+s ;
};
return this.raw(
'['+
pad(d.getHours()) + ":" + pad(d.getMinutes())+
"] "
);
},
'system': function(s) {
return this.time().c('tx-sys', sanitise(s)).raw('
');
},
'pubnick': function(u) {
return this.raw('<'+hesc(u)+'>');
},
'pub': function(u, s) {
return this.time().
pubnick(u).raw(' ').
c('tx-chat', sanitise(s)).raw('
');
}
};
};
/* Userlist */
var switchToPM = function(u) {
writerFor(u); // create
tab_set(pm_tabs[u]); // switch
writerFor(u).scroll(); // scroll
};
var userMenu = function(u, ev) {
usermenu.hide();
usermenu = new MenuList(ev.target);
usermenu.add("Send private message...", function() {
switchToPM(u);
});
// Usercommands
for (var i = 0; i < user_usercommands.length; i++) (function(i) {
var raw = user_usercommands[i].raw;
usermenu.add(user_usercommands[i].title, function() {
var message = usercommand_process( raw .replace(/%\[nick\]/g, u) );
sock.emit('raw', {message: message});
});
})(i);
// Show
usermenu.show();
//
ev.preventDefault();
return false;
};
var userlist = {
'add': function(u) {
if (this.has(u)) return;
var userlists = $(".userlist");
for (var l = 0, e = userlists.length; l !== e; ++l) {
var userlist = userlists[l];
var to_add = document.createElement('li');
to_add.innerHTML = hesc(u);
to_add.onclick = function() { switchToPM(u); };
to_add.oncontextmenu = function(ev) { return userMenu(u, ev); };
var users = userlist.children;
var cmp = hesc(u).toUpperCase();
var found = false;
for (var i = 0; i < users.length; i++) {
if ((''+users[i].innerHTML).toUpperCase() > cmp) {
userlist.insertBefore(to_add, users[i]);
found = true;
break;
}
}
if (! found) {
userlist.appendChild(to_add);
}
}
return this;
},
'del': function(u) {
var userlists = $(".userlist");
for (var l = 0, e = userlists.length; l !== e; ++l) {
if (! userlists[l].children) continue;
var userlist = userlists[l];
var users = userlist.children;
var cmp = hesc(u).toUpperCase();
for (var i = 0; i < users.length; i++) {
if ((''+users[i].innerHTML).toUpperCase() === cmp) {
userlist.removeChild(users[i]);
break;
}
}
}
return this;
},
'clear': function() {
var userlists = $(".userlist");
for (var i in userlists) {
if (! userlists[i].children) continue;
var userlist = userlists[i];
var users = userlist.children;
for (var j = users.length; j --> 0;) {
userlist.removeChild(users[j]);
}
}
return this;
},
'names': function() {
var userlist = $(".userlist")[0].children;
var ret = [];
for (var i = 0, e = userlist.length; i < e; ++i) {
ret.push( textContent(userlist[i]) );
}
return ret;
},
'has': function(u) {
var userlist = $(".userlist")[0].children;
for (var i = 0, e = userlist.length; i < e; ++i) {
if (textContent(userlist[i]) === u) {
return true;
}
}
return false;
},
'count': function() {
return $(".userlist")[0].children.length;
}
};
var submit = function() {
var str = $("#chatbox").value;
if (! str.length) return;
if (hub_state === 0) {
hub_state = 1;
var name_parts = str.split(":", 2);
hub_last_nick = name_parts[0];
sock.emit('hello', {'nick' : hub_last_nick, 'pass' : name_parts.length >= 2 ? name_parts[1] : ''});
$("#chatbox").disabled = true;
write("tab-main").system("Connecting...");
} else if (hub_state === 2) {
if (pm_target !== false) {
sock.emit('priv', {'user': pm_target, 'message': str});
writerFor(pm_target).pub(hub_last_nick, str );
} else {
sock.emit('pub', {'message' : str});
}
} else {
write("tab-main").system("Invalid internal state.");
}
$("#chatbox").value = '';
};
/* tabs */
/**
* Switch active tab
*
* @param {String} tab Full tab ID (e.g. tab-main, tab-users, tab-ext-???)
*/
var tab_set = function(tab) {
var tabs = $(".tabpane");
for (var i in tabs) {
try {
tabs[i].style.display = (tabs[i].id === tab ? 'block' : 'none');
} catch (e) {};
}
var tabitems = $(".tabitem");
for (var i in tabitems) {
try {
tabitems[i].className = "tabitem" + (tabitems[i].getAttribute('data-tab') === tab ? ' selected' : '');
} catch (e) {};
}
pm_target = false;
for (var i in pm_tabs) {
if (pm_tabs[i] === tab) {
pm_target = i;
break;
}
}
updateTitle();
write(tab).scroll();
$("#chatbox").focus();
last_tab = tab;
};
var tab_new = function(id, name) {
appendInnerHTML($("#bar"),
'