2014-03-16 16:19:53 +00:00
|
|
|
var _ = require("lodash");
|
2014-03-24 16:47:14 +00:00
|
|
|
var Backbone = require("backbone");
|
2014-03-24 15:47:29 +00:00
|
|
|
var moment = require("moment");
|
2014-03-06 18:02:43 +00:00
|
|
|
|
2014-03-24 15:47:29 +00:00
|
|
|
var id = 1;
|
2014-03-07 21:24:02 +00:00
|
|
|
var models =
|
|
|
|
module.exports =
|
|
|
|
{};
|
|
|
|
|
2014-03-24 16:47:14 +00:00
|
|
|
models.User = Backbone.Model.extend({
|
2014-03-07 21:24:02 +00:00
|
|
|
defaults: {
|
2014-03-15 15:51:21 +00:00
|
|
|
mode: "",
|
2014-03-24 15:47:29 +00:00
|
|
|
name: "",
|
2014-03-07 21:24:02 +00:00
|
|
|
}
|
|
|
|
});
|
2014-03-06 15:11:25 +00:00
|
|
|
|
2014-03-24 16:47:14 +00:00
|
|
|
models.Users = Backbone.Collection.extend({
|
2014-03-12 15:09:37 +00:00
|
|
|
model: models.User,
|
2014-03-16 16:19:53 +00:00
|
|
|
sort: function(options) {
|
|
|
|
this.models = _.sortBy(
|
|
|
|
this.models,
|
|
|
|
function(user) {
|
|
|
|
return user.get("name").toLowerCase();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-03-16 20:07:27 +00:00
|
|
|
// Iterate all the modes and move users with these
|
|
|
|
// modes to the top of the collection.
|
2014-03-16 16:19:53 +00: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 15:09:37 +00:00
|
|
|
}
|
2014-03-09 21:22:37 +00:00
|
|
|
});
|
|
|
|
|
2014-03-24 16:47:14 +00:00
|
|
|
models.Message = Backbone.Model.extend({
|
2014-03-07 21:24:02 +00:00
|
|
|
defaults: {
|
2014-03-24 15:47:29 +00:00
|
|
|
type: "",
|
2014-03-12 15:09:37 +00:00
|
|
|
time: "",
|
2014-03-24 15:47:29 +00:00
|
|
|
from: "",
|
|
|
|
message: "",
|
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-24 16:47:14 +00:00
|
|
|
models.Messages = Backbone.Collection.extend({
|
2014-03-09 21:22:37 +00:00
|
|
|
model: models.Message
|
|
|
|
});
|
|
|
|
|
2014-03-24 16:47:14 +00:00
|
|
|
models.Channel = Backbone.Model.extend({
|
2014-03-07 21:24:02 +00:00
|
|
|
defaults: {
|
2014-03-06 22:36:56 +00:00
|
|
|
type: "channel",
|
2014-03-24 16:47:14 +00:00
|
|
|
name: "",
|
|
|
|
},
|
|
|
|
addUser: function(models) {
|
|
|
|
this.get("users").add(models);
|
|
|
|
},
|
|
|
|
addMessage: function(models) {
|
|
|
|
this.get("messages").add(models);
|
2014-03-07 21:24:02 +00:00
|
|
|
},
|
|
|
|
initialize: function() {
|
|
|
|
this.set({
|
2014-03-24 16:47:14 +00:00
|
|
|
id: id++,
|
|
|
|
messages: new models.Messages,
|
|
|
|
users: new models.Users,
|
2014-03-07 21:24:02 +00:00
|
|
|
});
|
2014-03-09 23:14:22 +00:00
|
|
|
|
2014-03-17 16:24:32 +00:00
|
|
|
this.get("messages").on("all", function(action, data) {
|
2014-03-24 15:47:29 +00:00
|
|
|
this.trigger("message", {
|
2014-03-13 12:44:54 +00:00
|
|
|
target: this.get("id"),
|
2014-03-24 16:47:14 +00:00
|
|
|
type: "message",
|
|
|
|
data: data,
|
|
|
|
action: action,
|
2014-03-13 12:44:54 +00:00
|
|
|
});
|
|
|
|
}, this);
|
2014-03-16 16:19:53 +00:00
|
|
|
|
2014-03-24 16:47:14 +00:00
|
|
|
this.get("users").on("all", function(action, data) {
|
2014-03-24 15:47:29 +00:00
|
|
|
this.trigger("user", {
|
2014-03-16 16:19:53 +00:00
|
|
|
target: this.get("id"),
|
2014-03-24 16:47:14 +00:00
|
|
|
type: "user",
|
|
|
|
data: data,
|
|
|
|
action: action,
|
2014-03-16 16:19:53 +00:00
|
|
|
});
|
|
|
|
}, this);
|
2014-03-07 21:24:02 +00:00
|
|
|
}
|
|
|
|
});
|
2014-03-06 15:11:25 +00:00
|
|
|
|
2014-03-24 16:47:14 +00:00
|
|
|
models.Channels = Backbone.Collection.extend({
|
2014-03-07 21:24:02 +00:00
|
|
|
model: models.Channel
|
|
|
|
});
|
2014-03-06 15:11:25 +00:00
|
|
|
|
2014-03-24 16:47:14 +00:00
|
|
|
models.Network = Backbone.Model.extend({
|
2014-03-07 21:24:02 +00:00
|
|
|
defaults: {
|
2014-03-24 15:47:29 +00:00
|
|
|
host: ""
|
2014-03-07 21:24:02 +00:00
|
|
|
},
|
2014-03-24 16:47:14 +00:00
|
|
|
addChannel: function(models) {
|
|
|
|
this.get("channels").add(models);
|
|
|
|
},
|
2014-03-07 21:24:02 +00:00
|
|
|
initialize: function() {
|
|
|
|
this.set({
|
2014-03-24 16:47:14 +00:00
|
|
|
id: id++,
|
|
|
|
channels: new models.Channels,
|
2014-03-07 21:24:02 +00:00
|
|
|
});
|
2014-03-09 23:14:22 +00:00
|
|
|
|
2014-03-24 16:47:14 +00:00
|
|
|
this.get("channels").on("message user", function() { this.trigger(action, data); }, this);
|
2014-03-17 16:24:32 +00:00
|
|
|
this.get("channels").on("all", function(action, data) {
|
2014-03-24 16:47:14 +00:00
|
|
|
this.trigger("channel", {
|
|
|
|
target: this.get("id"),
|
|
|
|
type: "channel",
|
|
|
|
data: data,
|
|
|
|
action: action,
|
|
|
|
});
|
2014-03-13 12:44:54 +00:00
|
|
|
}, this);
|
2014-03-17 16:24:32 +00:00
|
|
|
|
2014-03-24 16:47:14 +00:00
|
|
|
this.addChannel({
|
2014-03-09 23:14:22 +00:00
|
|
|
type: "network",
|
|
|
|
name: this.get("host")
|
2014-03-24 16:47:14 +00:00
|
|
|
});
|
2014-03-07 21:24:02 +00:00
|
|
|
}
|
|
|
|
});
|
2014-03-07 03:18:53 +00:00
|
|
|
|
2014-03-24 16:47:14 +00:00
|
|
|
models.Networks = Backbone.Collection.extend({
|
2014-03-07 21:24:02 +00:00
|
|
|
model: models.Network,
|
|
|
|
initialize: function() {
|
2014-03-24 16:47:14 +00:00
|
|
|
this.add({host: "Status"});
|
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
|
|
|
}
|
|
|
|
});
|