2014-03-09 21:22:37 +00:00
|
|
|
var irc = require("irc");
|
2014-03-07 21:24:02 +00:00
|
|
|
var Backbone = require("backbone");
|
2014-03-06 23:17:08 +00:00
|
|
|
var moment = require("moment");
|
2014-03-06 18:02:43 +00:00
|
|
|
|
2014-03-07 21:24:02 +00:00
|
|
|
var models =
|
|
|
|
module.exports =
|
|
|
|
{};
|
|
|
|
|
2014-03-07 03:18:53 +00:00
|
|
|
var id = 1;
|
2014-03-06 15:11:25 +00:00
|
|
|
|
2014-03-07 21:24:02 +00:00
|
|
|
models.User = Backbone.Model.extend({
|
|
|
|
defaults: {
|
|
|
|
mode: "",
|
2014-03-12 19:45:41 +00:00
|
|
|
name: "user"
|
2014-03-07 21:24:02 +00:00
|
|
|
}
|
|
|
|
});
|
2014-03-06 15:11:25 +00:00
|
|
|
|
2014-03-09 21:22:37 +00:00
|
|
|
models.UserCollection = Backbone.Collection.extend({
|
2014-03-12 15:09:37 +00:00
|
|
|
model: models.User,
|
|
|
|
comparator: function(user) {
|
|
|
|
// Keep the collection sorted in alphabetical order
|
|
|
|
return user.get("name");
|
|
|
|
}
|
2014-03-09 21:22:37 +00:00
|
|
|
});
|
|
|
|
|
2014-03-07 21:24:02 +00:00
|
|
|
models.Message = Backbone.Model.extend({
|
|
|
|
defaults: {
|
2014-03-12 15:09:37 +00:00
|
|
|
time: "",
|
2014-03-09 21:22:37 +00:00
|
|
|
user: "",
|
|
|
|
text: ""
|
2014-03-12 15:09:37 +00:00
|
|
|
},
|
|
|
|
initialize: function() {
|
|
|
|
this.set("time", moment().format("HH:mm"));
|
2014-03-07 21:24:02 +00:00
|
|
|
}
|
|
|
|
});
|
2014-03-06 18:02:43 +00:00
|
|
|
|
2014-03-09 21:22:37 +00:00
|
|
|
models.MessageCollection = Backbone.Collection.extend({
|
|
|
|
model: models.Message
|
|
|
|
});
|
|
|
|
|
2014-03-07 21:24:02 +00:00
|
|
|
models.Channel = Backbone.Model.extend({
|
|
|
|
defaults: {
|
2014-03-06 22:36:56 +00:00
|
|
|
type: "channel",
|
2014-03-12 21:58:24 +00:00
|
|
|
name: ""
|
2014-03-07 21:24:02 +00:00
|
|
|
},
|
|
|
|
initialize: function() {
|
|
|
|
this.set({
|
2014-03-09 23:14:22 +00:00
|
|
|
id: id++
|
2014-03-07 21:24:02 +00:00
|
|
|
});
|
2014-03-09 23:14:22 +00:00
|
|
|
|
|
|
|
this.set("users", new models.UserCollection());
|
|
|
|
this.get("users").on(
|
2014-03-12 15:09:37 +00:00
|
|
|
"add remove",
|
|
|
|
function(model, collection) {
|
2014-03-09 23:14:22 +00:00
|
|
|
// Bubble event
|
2014-03-12 13:10:53 +00:00
|
|
|
this.trigger(
|
|
|
|
"users", {
|
|
|
|
target: this.get("id"),
|
2014-03-12 15:09:37 +00:00
|
|
|
data: collection
|
2014-03-12 13:10:53 +00:00
|
|
|
}
|
|
|
|
);
|
2014-03-09 23:14:22 +00:00
|
|
|
},
|
|
|
|
this
|
|
|
|
);
|
|
|
|
|
|
|
|
this.set("messages", new models.MessageCollection());
|
|
|
|
this.get("messages").on(
|
2014-03-11 22:57:29 +00:00
|
|
|
"add",
|
2014-03-12 15:09:37 +00:00
|
|
|
function(model, collection) {
|
2014-03-09 23:14:22 +00:00
|
|
|
// Bubble event
|
2014-03-12 13:10:53 +00:00
|
|
|
this.trigger(
|
|
|
|
"messages", {
|
|
|
|
target: this.get("id"),
|
2014-03-12 15:09:37 +00:00
|
|
|
data: collection.last()
|
2014-03-12 13:10:53 +00:00
|
|
|
}
|
|
|
|
);
|
2014-03-09 23:14:22 +00:00
|
|
|
},
|
|
|
|
this
|
|
|
|
);
|
2014-03-07 21:24:02 +00:00
|
|
|
}
|
|
|
|
});
|
2014-03-06 15:11:25 +00:00
|
|
|
|
2014-03-07 21:24:02 +00:00
|
|
|
models.ChannelCollection = Backbone.Collection.extend({
|
|
|
|
model: models.Channel
|
|
|
|
});
|
2014-03-06 15:11:25 +00:00
|
|
|
|
2014-03-07 21:24:02 +00:00
|
|
|
models.Network = Backbone.Model.extend({
|
|
|
|
defaults: {
|
|
|
|
host: "",
|
2014-03-09 21:22:37 +00:00
|
|
|
nick: "default_username",
|
2014-03-09 23:14:22 +00:00
|
|
|
connect: true
|
2014-03-07 21:24:02 +00:00
|
|
|
},
|
|
|
|
initialize: function() {
|
|
|
|
this.set({
|
|
|
|
id: id++,
|
|
|
|
});
|
2014-03-09 23:14:22 +00:00
|
|
|
|
|
|
|
this.set("channels", new models.ChannelCollection());
|
|
|
|
this.get("channels").on(
|
|
|
|
"all",
|
2014-03-12 15:09:37 +00:00
|
|
|
function(type, model, collection) {
|
2014-03-12 13:10:53 +00:00
|
|
|
if ([
|
|
|
|
"users",
|
|
|
|
"messages"
|
|
|
|
].indexOf(type) != -1) {
|
|
|
|
this.trigger(type, model);
|
|
|
|
} else {
|
|
|
|
// Bubble event
|
|
|
|
this.trigger(
|
|
|
|
"channels", {
|
|
|
|
target: this.get("id"),
|
2014-03-12 15:09:37 +00:00
|
|
|
data: collection
|
2014-03-12 13:10:53 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2014-03-09 23:14:22 +00:00
|
|
|
},
|
|
|
|
this
|
|
|
|
);
|
|
|
|
this.get("channels").add(new models.Channel({
|
|
|
|
type: "network",
|
|
|
|
name: this.get("host")
|
|
|
|
}));
|
|
|
|
|
2014-03-09 21:22:37 +00:00
|
|
|
if (this.get("connect")) {
|
2014-03-09 23:14:22 +00:00
|
|
|
this.irc = new irc.Client(
|
2014-03-09 21:22:37 +00:00
|
|
|
this.get("host"),
|
|
|
|
this.get("nick"), {
|
2014-03-09 23:14:22 +00:00
|
|
|
channels: ["#test_channel"]
|
2014-03-09 21:22:37 +00:00
|
|
|
}
|
|
|
|
);
|
2014-03-12 21:58:24 +00:00
|
|
|
this.irc.addListener(
|
|
|
|
"error", function() {
|
|
|
|
// ..
|
|
|
|
}
|
|
|
|
);
|
2014-03-09 21:22:37 +00:00
|
|
|
}
|
2014-03-09 23:14:22 +00:00
|
|
|
|
|
|
|
this.on("remove", function() {
|
|
|
|
if (typeof this.irc !== "undefined") {
|
|
|
|
this.irc.disconnect();
|
|
|
|
}
|
|
|
|
});
|
2014-03-07 21:24:02 +00:00
|
|
|
}
|
|
|
|
});
|
2014-03-07 03:18:53 +00:00
|
|
|
|
2014-03-07 21:24:02 +00:00
|
|
|
models.NetworkCollection = Backbone.Collection.extend({
|
|
|
|
model: models.Network,
|
|
|
|
initialize: function() {
|
|
|
|
this.add(new models.Network({
|
2014-03-09 23:14:22 +00:00
|
|
|
host: "Lobby",
|
|
|
|
connect: false
|
2014-03-07 21:24:02 +00:00
|
|
|
}));
|
2014-03-09 19:27:44 +00:00
|
|
|
},
|
2014-03-09 21:22:37 +00:00
|
|
|
find: function(id) {
|
2014-03-09 19:27:44 +00: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 21:22:37 +00:00
|
|
|
return {
|
|
|
|
network: networks[i],
|
|
|
|
channel: find
|
|
|
|
};
|
2014-03-09 19:27:44 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-07 21:24:02 +00:00
|
|
|
}
|
|
|
|
});
|