Sort users by mode

This commit is contained in:
Mattias Erming 2014-03-16 17:19:53 +01:00
parent 5eefa31400
commit 765f8ad4cc
4 changed files with 35 additions and 20 deletions

View File

@ -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");

View File

@ -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);
}
});

View File

@ -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);
}

View File

@ -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"
},