spongy-client-chrome/app/room.js

162 lines
4.0 KiB
JavaScript
Raw Normal View History

function djbhash(a) {
var r = 5381;
for (var i = 0; i < a.length; i += 1) {
r = (((r << 5) + r) + a.charCodeAt(i)) & 0xffff;
}
return r;
}
2014-10-27 23:12:05 -06:00
function purtify(text) {
// Look for a URL
var txtElement = document.createElement("span");
txtElement.className = "text";
var rhs = text;
var match;
while ((match = urlRe.exec(rhs)) != null) {
var before = rhs.substr(0, match.index);
var a = document.createElement("a");
var href = match[0];
if (href.indexOf("hxx") == 0) {
href = "htt" + href.substr(3);
}
a.href = href
a.target = "_blank";
a.appendChild(document.createTextNode(match[0]));
txtElement.appendChild(document.createTextNode(before));
txtElement.appendChild(a);
rhs = rhs.substr(match.index + match[0].length);
}
txtElement.appendChild(document.createTextNode(rhs));
2014-10-27 23:12:05 -06:00
return txtElement;
}
2014-10-29 17:47:04 -06:00
function kiboze(this_is_currently_busted) {
if ((kiboze) || (-1 != text.search(kibozeRe))) {
var k = document.getElementById("kiboze");
var p2 = p.cloneNode(true);
if (k) {
k.insertBefore(p2, k.firstChild);
p2.onclick = function() { focus(p); }
// Setting title makes the tab flash sorta
document.title = document.title;
}
}
}
2014-10-29 17:47:04 -06:00
var visibleRoom;
function newRoom(element, network, name, maxSize) {
var messages = getTemplate("messages");
2014-10-27 22:57:07 -06:00
var lastmsg;
if (! maxSize) {
maxSize = 500;
}
2014-10-29 17:47:04 -06:00
function purtify(fullSender, command, sender, args, txt) {
var txtElement = document.createElement("span");
var msg = chrome.i18n.getMessage(command + "Command", [fullSender, command, sender, name, args, txt]);
if (! msg) {
msg = chrome.i18n.getMessage("unknownCommand", [fullSender, command, sender, name, String(args), txt]);
}
var rhs = msg;
var match;
while ((match = urlRe.exec(rhs)) != null) {
var before = rhs.substr(0, match.index);
var a = document.createElement("a");
var href = match[0];
if (href.indexOf("hxx") == 0) {
href = "htt" + href.substr(3);
}
a.href = href
a.target = "_blank";
a.appendChild(document.createTextNode(match[0]));
txtElement.appendChild(document.createTextNode(before));
txtElement.appendChild(a);
rhs = rhs.substr(match.index + match[0].length);
}
txtElement.appendChild(document.createTextNode(rhs));
return txtElement;
}
2014-10-27 22:57:07 -06:00
2014-10-29 17:47:04 -06:00
element.addMessage = function(timestamp, fullSender, command, sender, args, txt) {
var message = getTemplate("message");
var eTimestamp = message.getElementsByClassName("timestamp")[0];
var eSource = message.getElementsByClassName("source")[0];
var eContent = message.getElementsByClassName("content")[0];
2014-10-27 22:57:07 -06:00
message.classList.add(command);
2014-10-29 17:47:04 -06:00
if (sender == ".") {
2014-10-27 22:57:07 -06:00
message.classList.add("self");
}
eTimestamp.textContent = timestamp.toLocaleTimeString();
2014-10-29 17:47:04 -06:00
eSource.textContent = sender;
eSource.setAttribute("colornumber", djbhash(sender) % 31);
eContent.appendChild(purtify(fullSender, command, sender, args, txt));
messages.appendChild(message);
while (messages.childNodes.length > maxSize) {
2014-10-29 17:47:04 -06:00
messages.removeChild(messages.firstChild);
}
2014-10-27 22:57:07 -06:00
lastmsg = message;
if (visibleRoom == element) {
2014-10-27 22:57:07 -06:00
lastmsg.scrollIntoView(false);
}
}
element.hide = function() {
element.classList.remove("selected");
2014-10-27 22:57:07 -06:00
messages.style.display = "none";
}
element.show = function() {
if (visibleRoom) {
visibleRoom.hide()
}
element.classList.add("selected");
2014-10-27 22:57:07 -06:00
messages.style.display = null;
lastmsg.scrollIntoView(false);
visibleRoom = element;
}
element.send = function(text) {
network.send(name, text);
}
2014-11-07 13:53:50 -07:00
element.close = function() {
console.log(messages);
console.log(messages.parent);
messages.parent.removeChild(messages);
element.parent.removeChild(element);
}
function clicked() {
element.show();
}
// start hidden
element.hide();
element.addEventListener("click", clicked);
element.getElementsByClassName("content-item")[0].textContent = name;
2014-10-27 22:57:07 -06:00
document.getElementById("messages-container").appendChild(messages);
return element;
}