Sort of filling in rooms
This commit is contained in:
parent
332df850cd
commit
6bf90a1e17
|
@ -1,15 +1,14 @@
|
||||||
{
|
{
|
||||||
"appName": {
|
"appName": {
|
||||||
"message": "wIRC Chat Client",
|
"message": "Spongy Chat Client",
|
||||||
"description": "Application name"
|
"description": "Application name"
|
||||||
},
|
},
|
||||||
"appShortName": {
|
"appShortName": {
|
||||||
"message": "wIRC",
|
"message": "Spongy Chat",
|
||||||
"description": "Short application name"
|
"description": "Short application name"
|
||||||
},
|
},
|
||||||
"appDesc": {
|
"appDesc": {
|
||||||
"message": "Chat client for the wIRC bouncer thingamajiggy",
|
"message": "Chat client for the Spongy bouncer thingamajiggy",
|
||||||
"description": "Application description for app store listing"
|
"description": "Application description for app store listing"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
var maxScrollback = 500;
|
var maxScrollback = 500;
|
||||||
|
|
||||||
function Server(baseURL, network, authtok, messageHandler) {
|
function Server(network, baseURL, authtok, messageHandler) {
|
||||||
function handleEventSourceLine(line) {
|
function handleEventSourceLine(line) {
|
||||||
var lhs = line.split(" :", 1)[0]
|
var lhs = line.split(" :", 1)[0]
|
||||||
var parts = lhs.split(' ')
|
var parts = lhs.split(' ')
|
||||||
|
@ -20,9 +20,9 @@ function Server(baseURL, network, authtok, messageHandler) {
|
||||||
function handleEventSourceMessage(oEvent) {
|
function handleEventSourceMessage(oEvent) {
|
||||||
msgs = oEvent.data.split("\n");
|
msgs = oEvent.data.split("\n");
|
||||||
|
|
||||||
var first = Math.min(0, msgs.length - maxScrollback);
|
var first = Math.max(0, msgs.length - maxScrollback);
|
||||||
for (var i = first; i < msgs.length; i += 1) {
|
for (var i = first; i < msgs.length; i += 1) {
|
||||||
handleEeventSourceLine(msgs[i]);
|
handleEventSourceLine(msgs[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,8 +31,8 @@ function Server(baseURL, network, authtok, messageHandler) {
|
||||||
messageHandler(timestamp, null, "ERROR", null, null, [], null);
|
messageHandler(timestamp, null, "ERROR", null, null, [], null);
|
||||||
}
|
}
|
||||||
|
|
||||||
var pullURL = baseURL + "?network=" + encodeURIComponent(network) + "&authtok=" + encodeURIComponent(authtok);
|
var pullURL = baseURL + "?network=" + encodeURIComponent(network) + "&auth=" + encodeURIComponent(authtok);
|
||||||
this.eventSource = new EventSource(baseURL);
|
this.eventSource = new EventSource(pullURL);
|
||||||
this.eventSource.addEventListener("message", handleEventSourceMessage);
|
this.eventSource.addEventListener("message", handleEventSourceMessage);
|
||||||
this.eventSource.addEventListener("error", handleEventSourceError);
|
this.eventSource.addEventListener("error", handleEventSourceError);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
<link rel="stylesheet" href="style.css">
|
<link rel="stylesheet" href="style.css">
|
||||||
<link rel="stylesheet" href="message_style.css">
|
<link rel="stylesheet" href="message_style.css">
|
||||||
<link rel="stylesheet" href="topbar.css">
|
<link rel="stylesheet" href="topbar.css">
|
||||||
|
|
||||||
|
<script src="server.js"></script>
|
||||||
<script src="wirc.js"></script>
|
<script src="wirc.js"></script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
|
|
112
app/wirc.js
112
app/wirc.js
|
@ -4,7 +4,9 @@ var urlRe = /[a-z]+:\/\/[^ ]*/;
|
||||||
|
|
||||||
var nick = "Mme. M";
|
var nick = "Mme. M";
|
||||||
|
|
||||||
|
// XXX: get rid of this
|
||||||
var scrollbackLength = 500;
|
var scrollbackLength = 500;
|
||||||
|
var current;
|
||||||
|
|
||||||
if (String.prototype.startsWith == null) {
|
if (String.prototype.startsWith == null) {
|
||||||
String.prototype.startsWith = function(needle) {
|
String.prototype.startsWith = function(needle) {
|
||||||
|
@ -13,7 +15,7 @@ if (String.prototype.startsWith == null) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTemplate(className) {
|
function getTemplate(className) {
|
||||||
return document.templates.getElementsByClassName(className)[0].cloneNode(true);
|
return templates.getElementsByClassName(className)[0].cloneNode(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isinView(oObject) {
|
function isinView(oObject) {
|
||||||
|
@ -21,21 +23,19 @@ function isinView(oObject) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function selectForum(room) {
|
function selectForum(room) {
|
||||||
var kids = document.rooms_list.childNodes;
|
if (current) {
|
||||||
|
current.classList.remove("selected");
|
||||||
|
// XXX: do this with a class, too
|
||||||
|
current.messages.display = "none";
|
||||||
|
}
|
||||||
|
|
||||||
for (i = 0; i < kids.length; i += 1) {
|
|
||||||
e = kids[i];
|
|
||||||
if (e == room) {
|
|
||||||
e.className = "room selected";
|
|
||||||
e.messages.display = "block";
|
|
||||||
} else {
|
|
||||||
e.className = "room";
|
|
||||||
e.messages.display = "none";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (room.lastChild) {
|
current = room;
|
||||||
room.lastChild.scrollIntoView(false);
|
room.classList.add("selected");
|
||||||
|
room.messages.display = "block";
|
||||||
|
|
||||||
|
if (room.messages.lastChild) {
|
||||||
|
room.messages.lastChild.scrollIntoView(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,15 +45,17 @@ function getForumElement(forum) {
|
||||||
|
|
||||||
if (! fe) {
|
if (! fe) {
|
||||||
var room = getTemplate("channel room");
|
var room = getTemplate("channel room");
|
||||||
room.textContent = forum;
|
var content = room.getElementsByClassName("content-item");
|
||||||
document.rooms_list.appendChild(room);
|
|
||||||
|
content.textContent = forum;
|
||||||
|
rooms.appendChild(room);
|
||||||
|
|
||||||
fe = getTemplate("messages");
|
fe = getTemplate("messages");
|
||||||
fe.room = room;
|
fe.room = room;
|
||||||
|
|
||||||
room.messages = fe;
|
room.messages = fe;
|
||||||
// XXX: split out into non-anon function
|
// XXX: split out into non-anon function
|
||||||
room.addEventListener("click", function() {selectForum(fe)});
|
room.addEventListener("click", function() {selectForum(room)});
|
||||||
|
|
||||||
fora[forum] = fe;
|
fora[forum] = fe;
|
||||||
document.getElementById("messages-container").appendChild(fe);
|
document.getElementById("messages-container").appendChild(fe);
|
||||||
|
@ -127,21 +129,13 @@ function focus(e) {
|
||||||
}, 50)
|
}, 50)
|
||||||
}
|
}
|
||||||
|
|
||||||
function addMessage(txt) {
|
function addMessage(timestamp, fullSender, command, sender, forum, args, msg) {
|
||||||
var lhs = txt.split(" :", 1)[0]
|
|
||||||
var parts = lhs.split(' ')
|
|
||||||
var ts = new Date(parts[0] * 1000);
|
|
||||||
var fullSender = parts[1];
|
|
||||||
var command = parts[2];
|
|
||||||
var sender = parts[3];
|
|
||||||
var forum = parts[4];
|
|
||||||
var args = parts.slice(5);
|
|
||||||
var msg = txt.substr(lhs.length + 2)
|
|
||||||
|
|
||||||
var forumElement = getForumElement(forum);
|
var forumElement = getForumElement(forum);
|
||||||
var p = getTemplate("message");
|
var p = getTemplate("message");
|
||||||
|
|
||||||
addMessagePart(p, "timestamp", ts.toLocaleTimeString());
|
console.log(timestamp, msg);
|
||||||
|
|
||||||
|
addMessagePart(p, "timestamp", timestamp.toLocaleTimeString());
|
||||||
|
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "PING":
|
case "PING":
|
||||||
|
@ -171,15 +165,6 @@ function addMessage(txt) {
|
||||||
p.scrollIntoView(false);
|
p.scrollIntoView(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
function newmsg(oEvent) {
|
|
||||||
msgs = oEvent.data.split("\n");
|
|
||||||
|
|
||||||
var first = Math.max(0, msgs.length - scrollbackLength);
|
|
||||||
for (var i = first; i < msgs.length; i += 1) {
|
|
||||||
addMessage(msgs[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleInput(oEvent) {
|
function handleInput(oEvent) {
|
||||||
console.log(oEvent);
|
console.log(oEvent);
|
||||||
var oReq = new XMLHttpRequest();
|
var oReq = new XMLHttpRequest();
|
||||||
|
@ -190,8 +175,13 @@ function handleInput(oEvent) {
|
||||||
if (txt.startsWith("/connect ")) {
|
if (txt.startsWith("/connect ")) {
|
||||||
// XXX: should allow tokens with spaces
|
// XXX: should allow tokens with spaces
|
||||||
var parts = txt.split(" ");
|
var parts = txt.split(" ");
|
||||||
|
var network = parts[1];
|
||||||
|
var url = parts[2];
|
||||||
|
var authtok = parts[3];
|
||||||
|
|
||||||
connect(parts[1], parts[2], parts[3]);
|
connect(network, url, authtok);
|
||||||
|
storedConnections[network] = [url, authtok];
|
||||||
|
chrome.storage.sync.set({"connections": storedConnections});
|
||||||
} else {
|
} else {
|
||||||
oReq.onload = reqListener;
|
oReq.onload = reqListener;
|
||||||
oReq.open("POST", window.postURL, true);
|
oReq.open("POST", window.postURL, true);
|
||||||
|
@ -203,35 +193,47 @@ function handleInput(oEvent) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function connect(url, server, authtok) {
|
var activeNetworks = {};
|
||||||
document.postURL = url;
|
var storedConnections = {};
|
||||||
var pullURL = url + "?server=" + server + "&auth=" + authtok
|
|
||||||
|
|
||||||
if (document.source != null) {
|
function connect(network, url, authtok) {
|
||||||
document.source.close();
|
var newServer = new Server(network, url, authtok, addMessage);
|
||||||
}
|
var element;
|
||||||
document.source = new EventSource(pullURL);
|
|
||||||
document.source.onmessage = newmsg;
|
|
||||||
|
|
||||||
chrome.storage.sync.set({"connections": [[url, server, authtok]]});
|
if (activeNetworks[network]) {
|
||||||
|
activeNetworks[network].close();
|
||||||
|
element = activeNetworks[network].element;
|
||||||
|
} else {
|
||||||
|
newServer.element = getTemplate("server-channels");
|
||||||
|
rooms.appendChild(newServer.element);
|
||||||
|
}
|
||||||
|
|
||||||
|
newServer.room = newServer.element.getElementsByClassName("server room")[0];
|
||||||
|
newServer.content = newServer.element.getElementsByClassName("content-item")[0];
|
||||||
|
|
||||||
|
newServer.content.textContent = network;
|
||||||
|
|
||||||
|
activeNetworks[network] = newServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function restore(items) {
|
function restore(items) {
|
||||||
var connections = items["connections"];
|
storedConnections = items["connections"];
|
||||||
|
|
||||||
for (var k = 0; k < connections.length; k += 1) {
|
for (var network in storedConnections) {
|
||||||
var conn = connections[k];
|
var conn = storedConnections[network];
|
||||||
|
|
||||||
connect(conn[0], conn[1], conn[2]);
|
connect(network, conn[0], conn[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
chrome.storage.sync.get("connections", restore);
|
chrome.storage.sync.get(["connections"], restore);
|
||||||
document.getElementById("input").addEventListener("change", handleInput);
|
document.getElementById("input").addEventListener("change", handleInput);
|
||||||
|
|
||||||
document.templates = document.getElementById("templates");
|
templates = document.getElementById("templates");
|
||||||
document.rooms_list = document.getElementById("rooms-container").getElementsByClassName("rooms")[0];
|
rooms = document.getElementById("rooms-container").getElementsByClassName("rooms")[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener("load", init);
|
window.addEventListener("load", init);
|
||||||
|
|
Loading…
Reference in New Issue