From 5f59f937ee724b9ce2fa355b24b696ebf37cd4d5 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sat, 19 Apr 2014 23:40:36 +0200 Subject: [PATCH] Refactoring --- .gitmodules | 12 ++--- client/css/style.css | 1 - client/index.html | 29 +++++------- client/js/{lib => }/bootstrap.js | 0 client/js/chat.js | 46 +++++++++--------- client/js/inputHistory | 1 + client/js/lib/inputHistory | 1 - client/js/lib/scrollGlue | 1 - client/js/lib/tabComplete | 1 - client/js/scrollGlue | 1 + client/js/tabComplete | 1 + lib/models.js | 81 ++++++++++++-------------------- lib/server.js | 43 ++++++++--------- 13 files changed, 96 insertions(+), 122 deletions(-) rename client/js/{lib => }/bootstrap.js (100%) create mode 160000 client/js/inputHistory delete mode 160000 client/js/lib/inputHistory delete mode 160000 client/js/lib/scrollGlue delete mode 160000 client/js/lib/tabComplete create mode 160000 client/js/scrollGlue create mode 160000 client/js/tabComplete diff --git a/.gitmodules b/.gitmodules index e01e7083..ae4b2056 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,12 +1,12 @@ [submodule "node_modules/slate-irc"] path = node_modules/slate-irc url = http://github.com/erming/slate-irc -[submodule "client/js/lib/scrollGlue"] - path = client/js/lib/scrollGlue +[submodule "client/js/scrollGlue"] + path = client/js/scrollGlue url = http://github.com/erming/scrollGlue -[submodule "client/js/lib/inputHistory"] - path = client/js/lib/inputHistory +[submodule "client/js/inputHistory"] + path = client/js/inputHistory url = http://github.com/erming/inputHistory -[submodule "client/js/lib/tabComplete"] - path = client/js/lib/tabComplete +[submodule "client/js/tabComplete"] + path = client/js/tabComplete url = http://github.com/erming/tabComplete diff --git a/client/css/style.css b/client/css/style.css index 7b6298b7..6c2fb681 100644 --- a/client/css/style.css +++ b/client/css/style.css @@ -131,7 +131,6 @@ h2 { } #chat .window { background: #fff; - display: none; height: 100%; position: absolute; width: 100%; diff --git a/client/index.html b/client/index.html index 6e2539ac..217d51d1 100644 --- a/client/index.html +++ b/client/index.html @@ -46,24 +46,21 @@ +
-
-
-
- - - - - - - - - - + + + + -
- diff --git a/client/js/lib/bootstrap.js b/client/js/bootstrap.js similarity index 100% rename from client/js/lib/bootstrap.js rename to client/js/bootstrap.js diff --git a/client/js/chat.js b/client/js/chat.js index ac8cb35f..bb278435 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -1,4 +1,7 @@ $(function() { + var chat = $("#chat"); + var sidebar = $("#sidebar"); + var commands = [ "/connect", "/deop", @@ -20,15 +23,12 @@ $(function() { ]; var socket = io.connect(""); - $.each(["network", "channel", "message", "user"], function(i, type) { - socket.on(type, function(json) { - event(type, json); + $.each(["network", "channel", "user", "message"], function(i, event) { + socket.on(event, function(json) { + handleEvent(event, json); }); }); - var chat = $("#chat"); - var sidebar = $("#sidebar"); - var tpl = []; function render(id, json, partials) { tpl[id] = tpl[id] || $(id).html(); @@ -44,24 +44,25 @@ $(function() { ); } - function event(type, json) { - switch (type) { + function handleEvent(event, json) { + var data = json.data; + switch (event) { case "network": var html = ""; var partials = { - users: render("#user"), - messages: render("#message"), + users: render("#users"), + messages: render("#messages"), }; - json.forEach(function(n) { + data.forEach(function(n) { html += render( - "#window", n, partials + "#windows", {windows: n.channels}, partials ); }); - $("#windows")[0].innerHTML = html; + chat[0].innerHTML = html; sidebar.find("#list").html( - render("#network", {networks: json}, {channels: render("#channel")}) + render("#networks", {networks: data}, {channels: render("#channels")}) ).find(".channel") .first() .addClass("active") @@ -82,7 +83,7 @@ $(function() { break; case "channel": - var id = json.data.id; + var id = data.id; if (json.action == "remove") { $("#channel-" + id + ", #window-" + id).remove(); var highest = 0; @@ -103,13 +104,13 @@ $(function() { sidebar.find(".channel").removeClass("active"); $("#network-" + json.target).append( - render("#channel", {channels: json.data}) + render("#channels", {channels: data}) ).find(".channel") .last() .addClass("active"); - $("#windows").append( - render("#window", {channels: json.data}) + chat.append( + render("#windows", {windows: data}) ).find(".window") .last() .find("input") @@ -124,7 +125,7 @@ $(function() { case "user": var target = chat.find("#window-" + json.target).find(".users"); - target.html(render("#user", {users: json.data})); + target.html(render("#users", {users: data})); break; case "message": @@ -133,12 +134,11 @@ $(function() { return; } - var message = json.data; - if (message.type == "error") { + if (data.type == "error") { target = target.parent().find(".active"); } - var msg = $(render("#message", {messages: message})) + var msg = $(render("#messages", {messages: data})) target = target.find(".messages"); target.append(msg); @@ -181,7 +181,7 @@ $(function() { } else { hidden += value; } - $.cookie("hidden", hidden); // Save the cookie with the new values. + $.cookie("hidden", hidden); chat.toggleClass( "show-" + value, input.prop("checked") diff --git a/client/js/inputHistory b/client/js/inputHistory new file mode 160000 index 00000000..9c0e3378 --- /dev/null +++ b/client/js/inputHistory @@ -0,0 +1 @@ +Subproject commit 9c0e33780e5317404d10d817a10127a2a218852c diff --git a/client/js/lib/inputHistory b/client/js/lib/inputHistory deleted file mode 160000 index b547cb6c..00000000 --- a/client/js/lib/inputHistory +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b547cb6c9f4db8114dbd6fb0d00b69faf7c4ddac diff --git a/client/js/lib/scrollGlue b/client/js/lib/scrollGlue deleted file mode 160000 index ce2447d3..00000000 --- a/client/js/lib/scrollGlue +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ce2447d3ad45a4208536ccea8fb0735e3bedf65f diff --git a/client/js/lib/tabComplete b/client/js/lib/tabComplete deleted file mode 160000 index b2e909a1..00000000 --- a/client/js/lib/tabComplete +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b2e909a1f2bbf671ba825ae69b212765e87d2c96 diff --git a/client/js/scrollGlue b/client/js/scrollGlue new file mode 160000 index 00000000..8fcc5e8f --- /dev/null +++ b/client/js/scrollGlue @@ -0,0 +1 @@ +Subproject commit 8fcc5e8f7e8f465f9cfce5b59f7a89abf9bf1593 diff --git a/client/js/tabComplete b/client/js/tabComplete new file mode 160000 index 00000000..41caddc1 --- /dev/null +++ b/client/js/tabComplete @@ -0,0 +1 @@ +Subproject commit 41caddc1723b8eb0dfe3b5614f87c7d1744a8996 diff --git a/lib/models.js b/lib/models.js index dad962dc..b6bb874a 100644 --- a/lib/models.js +++ b/lib/models.js @@ -1,6 +1,5 @@ -var _ = require("lodash"); +var _ = require("lodash"); var Backbone = require("backbone"); -var moment = require("moment"); var id = 1; var models = @@ -23,28 +22,19 @@ models.Users = Backbone.Collection.extend({ return user.get("name").toLowerCase(); } ); - - // Move users with these modes to the top. - var modes = ["+", "@"]; - for (var i in modes) { - this.models = _.remove(this.models, function(user) { - if (user.get("mode") == modes[i]) { - return true; - } - }).concat(this.models); - } + this.trigger("sort", {}, this); } }); models.Message = Backbone.Model.extend({ defaults: { - type: "", - time: "", - from: "-!-", + type: "", + time: "", + from: "", message: "", }, initialize: function() { - this.set("time", moment().format("HH:mm")); + this.set("time", require("moment")().format("HH:mm")); } }); @@ -57,37 +47,30 @@ models.Channel = Backbone.Model.extend({ type: "channel", name: "", }, - addUser: function(models) { - return this.get("users").add(models); - }, - addMessage: function(models) { - return this.get("messages").add(models); - }, initialize: function() { - this.set({ - id: id++, - messages: new models.Messages, - users: new models.Users, - }); - - this.get("messages").on("all", function(action, data) { - this.trigger("message", { - target: this.get("id"), - type: "message", - data: data, - action: action, - }); - }, this); - - var users = this.get("users"); + var users = new models.Users; users.on("all", function(action, data) { this.trigger("user", { - target: this.get("id"), - type: "user", - data: users, action: action, + target: this.get("id"), + data: users, }); }, this); + + var messages = new models.Messages; + messages.on("all", function(action, data) { + this.trigger("message", { + action: action, + target: this.get("id"), + data: data, + }); + }, this); + + this.set({ + id: id++, + users: users, + messages: messages, + }); } }); @@ -97,32 +80,28 @@ models.Channels = Backbone.Collection.extend({ models.Network = Backbone.Model.extend({ defaults: { - host: "", + host: "", client: null, }, - addChannel: function(models) { - return this.get("channels").add(models); - }, initialize: function() { this.set({ id: id++, channels: new models.Channels, }); - + this.get("channels").on("all", function(action, data) { if (action == "message" || action == "user") { return this.trigger(action, data); } this.trigger("channel", { - target: this.get("id"), - type: "channel", - data: data, action: action, + target: this.get("id"), + data: data, }); }, this); - - this.addChannel({ + + this.get("channels").add({ type: "network", name: this.get("host") }); diff --git a/lib/server.js b/lib/server.js index bf4c8269..c1e907dd 100644 --- a/lib/server.js +++ b/lib/server.js @@ -32,7 +32,9 @@ module.exports = function listen() { sockets = io.listen(app, {log: 0}).sockets.on("connection", function(s) { s.on("input", input); - sockets.emit("network", networks); + sockets.emit("network", { + data: networks, + }); }); networks.on("all", function() { @@ -117,7 +119,7 @@ function input(json) { user = client.me; client.send(args[1], message); } - channel.addMessage({ + channel.get("messages").add({ from: user, message: message, }); @@ -220,9 +222,11 @@ function event(event, data) { case "join": var chan = channels.findWhere({name: data[0].channel}) || channels.add({name: data[0].channel}); var users = chan.get("users"); - users.add({name: data[0].nick}); - users.sort(); - chan.addMessage({ + users.add({name: data[0].nick}, {silent: true}); + if (data[0].nick != this.get("client").me) { + users.sort(); + } + chan.get("messages").add({ from: data[0].nick, type: "join", }); @@ -238,7 +242,7 @@ function event(event, data) { users.reset(); } - chan.addMessage({ + chan.get("messages").add({ from: data[0].nick, message: data[0].client, type: "kick", @@ -248,7 +252,7 @@ function event(event, data) { case "mode": var chan = channels.findWhere({name: data[0].target}); if (typeof chan !== "undefined") { - chan.addMessage({ + chan.get("messages").add({ from: data[0].nick, message: data[0].mode + " " + data[0].client, type: "mode", @@ -259,13 +263,13 @@ function event(event, data) { case "motd": var messages = data[0].motd; messages.forEach(function(msg) { - channels.first().addMessage({message: msg}); + channels.first().get("messages").add({message: msg}); }); break; case "message": var chan = channels.findWhere({name: data[0].to}) || channels.add({type: "query", name: data[0].from}); - chan.addMessage({ + chan.get("messages").add({ from: data[0].from, message: data[0].message }); @@ -279,12 +283,11 @@ function event(event, data) { {silent: true} ); users.sort(); - users.trigger("reset", {}, users); break; case "nick": if (data[0].new == this.get("client").me) { - channels.first().addMessage({ + channels.first().get("messages").add({ message: "You're now known as " + data[0]["new"], }); } @@ -296,11 +299,9 @@ function event(event, data) { } user.set("name", data[0]["new"]); - users.sort(); - users.trigger("change", {}, users); - chan.addMessage({ + chan.get("messages").add({ from: data[0].nick, message: data[0]["new"], type: "nick", @@ -312,7 +313,7 @@ function event(event, data) { if (data[0].to = "*") { data[0].from = "-!-"; } - channels.first().addMessage(data[0]); + channels.first().get("messages").add(data[0]); break; case "part": @@ -324,7 +325,7 @@ function event(event, data) { var users = chan.get("users"); users.remove(users.findWhere({name: data[0].nick})); users.sort(); - chan.addMessage({ + chan.get("messages").add({ from: data[0].nick, type: "part", }); @@ -337,7 +338,7 @@ function event(event, data) { if (user) { users.remove(user); users.sort(); - chan.addMessage({ + chan.get("messages").add({ from: data[0].nick, type: "quit", }); @@ -348,7 +349,7 @@ function event(event, data) { case "topic": var chan = channels.findWhere({name: data[0].channel}); var from = data[0].nick || chan.get("name"); - chan.addMessage({ + chan.get("messages").add({ from: from, message: data[0].topic, type: "topic", @@ -356,7 +357,7 @@ function event(event, data) { break; case "welcome": - channels.first().addMessage([ + channels.first().get("messages").add([ {message: "You're now connected to " + this.get("host")}, {message: "You're now known as " + data[0]} ]); @@ -364,7 +365,7 @@ function event(event, data) { case "whois": if (data[1] == null) { - channels.first().addMessage({ + channels.first().get("messages").add({ type: "error", message: data[0] }); @@ -377,7 +378,7 @@ function event(event, data) { var i = 0; for (var k in data[1]) { if (i++ == 5) break; - chan.addMessage({ + chan.get("messages").add({ message: k + ": " + data[1][k] }); }