thelounge/lib/models.js

163 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-03-09 22:22:37 +01:00
var irc = require("irc");
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: {
mode: "",
name: "user"
2014-03-07 22:24:02 +01:00
}
});
2014-03-06 16:11:25 +01:00
2014-03-09 22:22:37 +01:00
models.UserCollection = Backbone.Collection.extend({
2014-03-12 16:09:37 +01:00
model: models.User,
comparator: function(user) {
// Keep the collection sorted in alphabetical order
return user.get("name");
}
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: "",
text: ""
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-09 22:22:37 +01:00
models.MessageCollection = Backbone.Collection.extend({
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-07 22:24:02 +01:00
name: "",
2014-03-09 22:22:37 +01:00
topic: ""
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
this.set("users", new models.UserCollection());
this.get("users").on(
2014-03-12 16:09:37 +01:00
"add remove",
function(model, collection) {
2014-03-10 00:14:22 +01:00
// Bubble event
2014-03-12 14:10:53 +01:00
this.trigger(
"users", {
target: this.get("id"),
2014-03-12 16:09:37 +01:00
data: collection
2014-03-12 14:10:53 +01:00
}
);
2014-03-10 00:14:22 +01:00
},
this
);
this.set("messages", new models.MessageCollection());
this.get("messages").on(
2014-03-11 23:57:29 +01:00
"add",
2014-03-12 16:09:37 +01:00
function(model, collection) {
2014-03-10 00:14:22 +01:00
// Bubble event
2014-03-12 14:10:53 +01:00
this.trigger(
"messages", {
target: this.get("id"),
2014-03-12 16:09:37 +01:00
data: collection.last()
2014-03-12 14:10:53 +01:00
}
);
2014-03-10 00:14:22 +01:00
},
this
);
2014-03-07 22:24:02 +01:00
}
});
2014-03-06 16:11:25 +01:00
2014-03-07 22:24:02 +01:00
models.ChannelCollection = Backbone.Collection.extend({
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-09 22:22:37 +01:00
nick: "default_username",
2014-03-10 00:14:22 +01:00
connect: true
2014-03-07 22:24:02 +01:00
},
initialize: function() {
this.set({
id: id++,
});
2014-03-10 00:14:22 +01:00
this.set("channels", new models.ChannelCollection());
this.get("channels").on(
"all",
2014-03-12 16:09:37 +01:00
function(type, model, collection) {
2014-03-12 14:10:53 +01:00
if ([
"users",
"messages"
].indexOf(type) != -1) {
this.trigger(type, model);
} else {
// Bubble event
this.trigger(
"channels", {
target: this.get("id"),
2014-03-12 16:09:37 +01:00
data: collection
2014-03-12 14:10:53 +01:00
}
);
}
2014-03-10 00:14:22 +01:00
},
this
);
this.get("channels").add(new models.Channel({
type: "network",
name: this.get("host")
}));
2014-03-09 22:22:37 +01:00
if (this.get("connect")) {
2014-03-10 00:14:22 +01:00
this.irc = new irc.Client(
2014-03-09 22:22:37 +01:00
this.get("host"),
this.get("nick"), {
2014-03-10 00:14:22 +01:00
channels: ["#test_channel"]
2014-03-09 22:22:37 +01:00
}
);
}
2014-03-10 00:14:22 +01:00
this.on("remove", function() {
if (typeof this.irc !== "undefined") {
this.irc.disconnect();
}
});
2014-03-07 22:24:02 +01:00
}
});
2014-03-07 04:18:53 +01:00
2014-03-07 22:24:02 +01:00
models.NetworkCollection = Backbone.Collection.extend({
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
}
});