Improved the event/render flow

This commit is contained in:
Mattias Erming 2014-03-07 04:18:53 +01:00
parent acfeac27f4
commit efa3fcd7ab
4 changed files with 191 additions and 150 deletions

View File

@ -36,10 +36,6 @@ h2 {
cursor: pointer; cursor: pointer;
padding: 0 12px; padding: 0 12px;
} }
#sidebar .channel[data-id='0'] {
background: #eaeaea;
border-bottom-color: #e5e5e5;
}
#sidebar .channel:first-child { #sidebar .channel:first-child {
color: #333; color: #333;
overflow: hidden; overflow: hidden;
@ -47,6 +43,10 @@ h2 {
#sidebar .channel:hover { #sidebar .channel:hover {
text-decoration: underline; text-decoration: underline;
} }
#sidebar .network:first-child .channel:first-child {
background: #eaeaea;
border-bottom-color: #e5e5e5;
}
#chat { #chat {
bottom: 0; bottom: 0;
left: 200px; left: 200px;

View File

@ -1,12 +1,8 @@
$(function() { $(function() {
var socket = io.connect(""); var socket = io.connect("");
socket.on( socket.on("event", function(event) {
"event", render(event);
function(event) { });
console.log(event);
View[event.action](event);
}
);
var chat = $("#chat"); var chat = $("#chat");
var sidebar = $("#sidebar"); var sidebar = $("#sidebar");
@ -17,11 +13,77 @@ $(function() {
var messages = $("#messages").html(); var messages = $("#messages").html();
var users = $("#users").html() var users = $("#users").html()
function render(event) {
var type = event.type;
var data = event.data;
var action = event.action;
var target = event.target;
if (action == "REMOVE") {
remove(target);
return;
}
if (target != "") {
target = $("[data-id='" + target + "']");
}
switch (type) {
case "NETWORK":
case "CHANNEL":
refresh(data);
break;
case "USER":
target = target.find(".users");
target.html(Mustache.render(users, {users: event.data}));
break;
case "MESSAGE":
var keepAtBottom = target.isScrollBottom();
target = target.find(".messages");
target.append(Mustache.render(messages, {messages: event.data}));
if (keepAtBottom) {
target.scrollToBottom();
}
break;
}
}
function remove(id) {
$("[data-id='" + id + "']").remove();
}
function refresh(data) {
chat.html("");
var partials = {
users: users,
messages: messages
};
data.forEach(function(network) {
chat.append(Mustache.render(channels, network, partials));
});
sidebar.html(
Mustache.render(networks, {
networks: data
})
);
chat.find(".messages").scrollToBottom();
chat.find(".window")
// Sort windows by `data-id` value.
.sort(function(a, b) { return ($(a).data("id") - $(b).data("id")); })
.last()
.bringToTop()
.find(".input")
.focus();
}
var View = {}; var View = {};
View.refresh = function(event) { View.refresh = function(event) {
var data = event.data; var data = event.data;
sidebar.html( sidebar.html(
Mustache.render(networks, { Mustache.render(networks, {
networks: data networks: data
@ -48,32 +110,23 @@ $(function() {
}; };
View.add = function(event) { View.add = function(event) {
var target = ""; var target = $("[data-id='" + event.target + "'] ");
var render = "";
switch (event.type) { switch (event.type) {
case "user":
target = ".users"; case "users":
render = Mustache.render( target = target.find(".users");
users, {users: event.data} target.html(Mustache.render(users, {users: event.data}));
);
break; break;
case "message": case "messages":
target = ".messages";
render = Mustache.render(
messages, {messages: event.data}
);
break;
}
if (target != "") {
target = $("[data-id='" + event.target + "'] " + target);
var keepAtBottom = target.isScrollBottom(); var keepAtBottom = target.isScrollBottom();
target.append(render); target = target.find(".messages");
target.append(Mustache.render(messages, {messages: event.data}));
if (keepAtBottom) { if (keepAtBottom) {
target.scrollToBottom(); target.scrollToBottom();
} }
break;
} }
}; };
@ -81,10 +134,6 @@ $(function() {
$("[data-id='" + event.target + "']").remove(); $("[data-id='" + event.target + "']").remove();
}; };
View.change = function(event) {
// ..
};
chat.on("submit", "form", function() { chat.on("submit", "form", function() {
var input = $(this).find(".input"); var input = $(this).find(".input");
var text = input.val(); var text = input.val();
@ -107,7 +156,10 @@ $(function() {
(function() { (function() {
var highest = 1; var highest = 1;
$.fn.bringToTop = function() { $.fn.bringToTop = function() {
return this.css('z-index', highest++); return this
.css('z-index', highest++)
.find("input")
.focus();
}; };
$.fn.scrollToBottom = function() { $.fn.scrollToBottom = function() {

View File

@ -2,7 +2,7 @@ var _ = require("lodash");
var moment = require("moment"); var moment = require("moment");
var models = exports; var models = exports;
var id = 0; var id = 1;
models.Network = function(attr) { models.Network = function(attr) {
attr = attr || {}; attr = attr || {};
@ -56,3 +56,11 @@ models.Event = function(attr) {
type: "" type: ""
})); }));
}; };
models.Target = function(attr) {
attr = attr || {};
_.extend(this, _.defaults(attr, {
network: "",
channel: ""
}));
};

View File

@ -1,149 +1,130 @@
var _ = require("lodash");
var connect = require("connect"); var connect = require("connect");
var models = require("./models.js");
var _ = require("lodash");
var irc = require("irc"); var irc = require("irc");
var io = require("socket.io"); var io = require("socket.io");
var models = require("./models.js");
exports.listen = listen; exports.listen = listen;
var sockets; var sockets = false;;
var networks = []; var networks = [];
addNetwork("Lobby", false); addToServer(
"NETWORK",
new models.Network({address: "Start"})
);
function listen(port) { function listen(port) {
var http = connect() var http = connect()
.use(connect.static("client")) .use(connect.static("client"))
.listen(port); .listen(port);
sockets = io.listen(http).sockets; sockets = io
sockets.on("connection", function(socket) { .listen(http)
init(socket); .on("connection", initSocket)
}); .sockets;
} }
function init(socket) { function initSocket(socket) {
socket.on("input", handleUserInput);
refresh(); refresh();
socket.on( }
"input",
function(input) { function sendEvent(params) {
handleUserInput(input) if (sockets) {
} sockets.emit("event", new models.Event(params));
); }
} }
function refresh() { function refresh() {
if (typeof sockets === "undefined") { sendEvent({action: "RENDER", type: "NETWORK", data: networks});
return; }
function addToServer(type, model, target) {
switch (type) {
case "NETWORK":
var channel = new models.Channel({
name: model.address,
type: "network"
});
model.channels.push(channel);
networks.push(model);
refresh();
break;
case "CHANNEL":
target.network.channels.push(model);
refresh();
break;
case "MESSAGE":
target.channel.messages
.push(model);
sendEvent({
action: "RENDER",
type: "MESSAGE",
target: target.channel.id,
data: model
});
break;
} }
sockets.emit("event", new models.Event({
action: "refresh",
data: networks
}));
} }
function handleUserInput(input) { function handleUserInput(input) {
var id = input.id;
var text = input.text; var text = input.text;
var target = getChannel(input.id);
if (text.charAt(0) != "/") { var args = text.substr(1).split(' ');
return addMessage(target, text); var cmd = text.charAt(0) == "/" ? args[0].toUpperCase()
} : "MESSAGE";
var args = text.substr(1).split(" "); var target = getTarget(id);
var cmd = args[0].toUpperCase();
switch (cmd) { switch (cmd) {
case "SERVER": case "SERVER":
case "CONNECT": case "CONNECT":
if (args[1]) { addToServer(
addNetwork(args[1], true); "NETWORK",
} new models.Network({address: args[1]})
);
break; break;
case "JOIN": case "JOIN":
if (args[1]) { addToServer(
target.network.channels.push( "CHANNEL",
new models.Channel({ new models.Channel({name: args[1]}),
name: args[1] target
}) );
);
refresh();
}
break; break;
case "PART": case "PART":
target.network.channels = target.network.channels = _.reject(target.network.channels, {id: id});
_.without(target.network.channels, target.channel);
refresh(); refresh();
break; break;
default: case "MESSAGE":
addMessage( addToServer(
target, "MESSAGE",
"Command '/" + args[0] + "' does not exist." new models.Message({text: input.text}),
getTarget(id)
); );
break; break;
} }
} }
function addNetwork(addr, bool) { function getTarget(id) {
bool = bool || false; var find;
_.each(networks, function(n) {
var chan = new models.Channel({ find = {network: n, channel: _.findWhere(n.channels, {id: id})};
name: addr, if (find.channel)
type: "network" return;
}); });
var network = new models.Network({ if (find.channel) {
channels: [chan] return new models.Target(find);
});
networks.push(network);
refresh();
if (addr == "Lobby") {
return;
}
network.client = new irc.Client(addr, "default_user");
network.client.addListener("raw", function() {
handleEvent(
network, arguments
);
});
}
function handleEvent(network) {
var args = arguments;
var target = {
network: network,
channel: network.channels[0]
};
console.log(args[1]);
addMessage(target, args[1][0].args);
}
function addMessage(target, text) {
var message = _.extend(new models.Message, {text: text});
target.channel.messages.push(message);
sockets.emit("event", new models.Event({
action: "add",
type: "message",
target: target.channel.id,
data: message
}));
}
function getChannel(id) {
for (var i = 0; i < networks.length; i++) {
var find = {
network: networks[i],
channel: _.findWhere(networks[i].channels, {id: id})
};
if (typeof find.channel !== "undefined") {
return find;
}
} }
} }