thelounge/lib/models.js

146 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-03-09 22:22:37 +01:00
var irc = require("irc");
2014-03-16 17:19:53 +01:00
var _ = require("lodash");
2014-03-07 22:24:02 +01:00
var Backbone = require("backbone");
2014-03-07 00:17:08 +01:00
var moment = require("moment");
2014-03-06 19:02:43 +01:00
2014-03-07 22:24:02 +01:00
var models =
module.exports =
{};
2014-03-07 04:18:53 +01:00
var id = 1;
2014-03-06 16:11:25 +01:00
2014-03-07 22:24:02 +01:00
models.User = Backbone.Model.extend({
defaults: {
2014-03-15 16:51:21 +01:00
mode: "",
2014-03-13 16:25:01 +01:00
name: ""
2014-03-07 22:24:02 +01:00
}
});
2014-03-06 16:11:25 +01:00
2014-03-24 14:44:41 +01:00
models.Users = Backbone.Collection.extend({
2014-03-12 16:09:37 +01:00
model: models.User,
2014-03-16 17:19:53 +01:00
sort: function(options) {
this.models = _.sortBy(
this.models,
function(user) {
return user.get("name").toLowerCase();
}
);
2014-03-16 21:07:27 +01:00
// Iterate all the modes and move users with these
// modes to the top of the collection.
2014-03-16 17:19:53 +01:00
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);
}
2014-03-12 16:09:37 +01:00
}
2014-03-09 22:22:37 +01:00
});
2014-03-07 22:24:02 +01:00
models.Message = Backbone.Model.extend({
defaults: {
2014-03-12 16:09:37 +01:00
time: "",
2014-03-09 22:22:37 +01:00
user: "",
2014-03-13 16:50:33 +01:00
text: "",
2014-03-22 21:37:09 +01:00
type: ""
2014-03-12 16:09:37 +01:00
},
initialize: function() {
this.set("time", moment().format("HH:mm"));
2014-03-07 22:24:02 +01:00
}
});
2014-03-06 19:02:43 +01:00
2014-03-24 14:44:41 +01:00
models.Messages = Backbone.Collection.extend({
2014-03-09 22:22:37 +01:00
model: models.Message
});
2014-03-07 22:24:02 +01:00
models.Channel = Backbone.Model.extend({
defaults: {
2014-03-06 23:36:56 +01:00
type: "channel",
2014-03-12 22:58:24 +01:00
name: ""
2014-03-07 22:24:02 +01:00
},
initialize: function() {
this.set({
2014-03-10 00:14:22 +01:00
id: id++
2014-03-07 22:24:02 +01:00
});
2014-03-10 00:14:22 +01:00
2014-03-24 14:44:41 +01:00
this.set("messages", new models.Messages());
2014-03-17 17:24:32 +01:00
this.get("messages").on("all", function(action, data) {
2014-03-14 22:57:54 +01:00
this.trigger("MESSAGES", {
2014-03-13 13:44:54 +01:00
target: this.get("id"),
2014-03-17 17:24:32 +01:00
type: "message",
data: data,
action: action
2014-03-13 13:44:54 +01:00
});
}, this);
2014-03-16 17:19:53 +01:00
2014-03-24 14:44:41 +01:00
this.set("users", new models.Users());
2014-03-17 17:24:32 +01:00
this.get("users").on("all", function(action) {
2014-03-16 17:19:53 +01:00
this.trigger("USERS", {
target: this.get("id"),
2014-03-17 17:24:32 +01:00
type: "user",
data: this.get("users"),
action: action
2014-03-16 17:19:53 +01:00
});
}, this);
2014-03-07 22:24:02 +01:00
}
});
2014-03-06 16:11:25 +01:00
2014-03-24 14:44:41 +01:00
models.Channels = Backbone.Collection.extend({
2014-03-07 22:24:02 +01:00
model: models.Channel
});
2014-03-06 16:11:25 +01:00
2014-03-07 22:24:02 +01:00
models.Network = Backbone.Model.extend({
defaults: {
host: "",
2014-03-24 14:44:41 +01:00
nick: "nick"
2014-03-07 22:24:02 +01:00
},
initialize: function() {
this.set({
2014-03-13 16:25:01 +01:00
id: id++
2014-03-07 22:24:02 +01:00
});
2014-03-10 00:14:22 +01:00
2014-03-24 14:44:41 +01:00
this.set("channels", new models.Channels());
2014-03-17 17:24:32 +01:00
this.get("channels").on("all", function(action, data) {
if (action == "USERS" || action == "MESSAGES") {
this.trigger(action, data);
2014-03-13 13:44:54 +01:00
} else {
2014-03-17 17:24:32 +01:00
this.trigger("CHANNELS", {
target: this.get("id"),
type: "channel",
data: data,
action: action
});
2014-03-13 13:44:54 +01:00
}
}, this);
2014-03-17 17:24:32 +01:00
2014-03-10 00:14:22 +01:00
this.get("channels").add(new models.Channel({
type: "network",
name: this.get("host")
}));
2014-03-07 22:24:02 +01:00
}
});
2014-03-07 04:18:53 +01:00
2014-03-24 14:44:41 +01:00
models.Networks = Backbone.Collection.extend({
2014-03-07 22:24:02 +01:00
model: models.Network,
initialize: function() {
this.add(new models.Network({
2014-03-10 00:14:22 +01:00
host: "Lobby",
connect: false
2014-03-07 22:24:02 +01:00
}));
2014-03-09 20:27:44 +01:00
},
2014-03-09 22:22:37 +01:00
find: function(id) {
2014-03-09 20:27:44 +01:00
var networks = this.models;
for (var i = 0; i < networks.length; i++) {
var find = networks[i].get("channels").findWhere({id: id});
if (find) {
2014-03-09 22:22:37 +01:00
return {
network: networks[i],
channel: find
};
2014-03-09 20:27:44 +01:00
}
}
2014-03-07 22:24:02 +01:00
}
});