From 765f8ad4cc40623af09b901b2c56bc8b665651f3 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sun, 16 Mar 2014 17:19:53 +0100 Subject: [PATCH] Sort users by mode --- client/js/chat.js | 2 +- lib/models.js | 38 ++++++++++++++++++++++++++++---------- lib/server.js | 14 +++++--------- package.json | 1 + 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/client/js/chat.js b/client/js/chat.js index 3d93478..bcb5478 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -60,7 +60,7 @@ $(function() { case "MESSAGES": var message = data.data; - if (message.type == "error") { + if (message.type == "error" || message.type == "notice") { target = target.parent().find(".active"); } target = target.find(".messages"); diff --git a/lib/models.js b/lib/models.js index 1317451..86e9c83 100644 --- a/lib/models.js +++ b/lib/models.js @@ -1,4 +1,5 @@ var irc = require("irc"); +var _ = require("lodash"); var Backbone = require("backbone"); var moment = require("moment"); @@ -18,10 +19,27 @@ models.User = Backbone.Model.extend({ } }); +var x = 1; models.UserCollection = Backbone.Collection.extend({ model: models.User, - comparator: function(user) { - return user.get("name"); + sort: function(options) { + this.models = _.sortBy( + this.models, + function(user) { + return user.get("name").toLowerCase(); + } + ); + + // Lets iterate all modes and move users with these + // modes to top of the list. + 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); + } } }); @@ -51,14 +69,6 @@ models.Channel = Backbone.Model.extend({ id: id++ }); - this.set("users", new models.UserCollection()); - this.get("users").on("all", function() { - this.trigger("USERS", { - target: this.get("id"), - data: this.get("users") - }); - }, this); - this.set("messages", new models.MessageCollection()); this.get("messages").on("all", function() { this.trigger("MESSAGES", { @@ -66,6 +76,14 @@ models.Channel = Backbone.Model.extend({ data: this.get("messages").last() }); }, this); + + this.set("users", new models.UserCollection()); + this.get("users").on("all", function() { + this.trigger("USERS", { + target: this.get("id"), + data: this.get("users") + }); + }, this); } }); diff --git a/lib/server.js b/lib/server.js index a1c8867..f1f7dd1 100644 --- a/lib/server.js +++ b/lib/server.js @@ -284,15 +284,11 @@ function handleEvent(argv) { case "NOTICE": var from = argv.nick ? argv.nick : "-!-"; - var message = new models.Message({ + channels.first().get("messages").add(new models.Message({ user: from, text: "notice: " + argv.args[1], type: "notice" - }); - - channels.each(function(channel) { - channel.get("messages").add(message); - }); + })); break; case "JOIN": @@ -314,6 +310,7 @@ function handleEvent(argv) { name: argv.nick }) ); + users.sort(); } messages.add( @@ -512,6 +509,8 @@ function handleEvent(argv) { // .. here! delete rpl_namreply; + + users.sort(); users.trigger( "add", {}, users ); @@ -565,7 +564,4 @@ function handleEvent(argv) { break; } - - // Debug - console.log(argv); } diff --git a/package.json b/package.json index 7200af5..673fc45 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "commander": "2.1.0", "connect": "2.13.0", "irc": "0.3.6", + "lodash": "2.4.1", "moment": "2.5.1", "socket.io": "0.9.16" },