Implemented TOPIC events

This commit is contained in:
Mattias Erming 2014-03-12 22:58:24 +01:00
parent 57bd684c74
commit dab3f4350c
5 changed files with 53 additions and 126 deletions

View File

@ -1,112 +0,0 @@
$(function() {
var socket = io.connect("");
socket.on("event", function(data) {
render(data);
// Debug
console.log(data);
});
var chat = $("#chat");
var sidebar = $("#sidebar");
// Templates
var networks = $("#networks").html();
var channels = $("#channels").html();
var messages = $("#messages").html();
var users = $("#users").html()
function render(data) {
chat.html("");
var partials = {
users: users,
messages: messages
};
data.forEach(function(network) {
chat.append(Mustache.render(channels, network, partials));
});
sidebar.html(
Mustache.render(networks, {
networks: data
})
);
chat.find(".messages").sticky().scrollToBottom();
chat.find(".window")
// Sort windows by `data-id` value.
.sort(function(a, b) { return ($(a).data("id") - $(b).data("id")); })
.last()
.bringToTop()
.find(".input")
.focus();
}
chat.on("submit", "form", function() {
var input = $(this).find(".input");
var text = input.val();
if (text != "") {
input.val("");
socket.emit("input", {
id: input.data("target"),
text: text
});
}
});
sidebar.on("click", ".channel", function() {
chat.find(".window[data-id='" + $(this).data("id") + "']")
.bringToTop();
});
});
(function($) {
var highest = 1;
$.fn.bringToTop = function() {
return this
.css('z-index', highest++)
.find("input")
.focus();
};
})(jQuery);
// Sticky plugin
// https://github.com/erming/sticky
(function($) {
var append = $.fn.append;
$.fn.append = function() {
return append.apply(this, arguments).trigger("append");
};
$.fn.sticky = function() {
var self = this;
if (self.size() > 1) {
return self.each(function() {
$(this).sticky();
});
}
var sticky = false;
self.on("scroll", function() {
sticky = self.isScrollAtBottom();
});
self.trigger("scroll");
self.on("append", function() {
if (sticky) {
self.scrollToBottom();
}
});
return this;
};
$.fn.scrollToBottom = function() {
this.scrollTop(1e10);
};
$.fn.isScrollAtBottom = function() {
if ((this.scrollTop() + this.outerHeight()) >= this.prop("scrollHeight")) {
return true;
}
};
})(jQuery);

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -41,8 +41,7 @@ models.MessageCollection = Backbone.Collection.extend({
models.Channel = Backbone.Model.extend({ models.Channel = Backbone.Model.extend({
defaults: { defaults: {
type: "channel", type: "channel",
name: "", name: ""
topic: ""
}, },
initialize: function() { initialize: function() {
this.set({ this.set({
@ -129,6 +128,11 @@ models.Network = Backbone.Model.extend({
channels: ["#test_channel"] channels: ["#test_channel"]
} }
); );
this.irc.addListener(
"error", function() {
// ..
}
);
} }
this.on("remove", function() { this.on("remove", function() {

View File

@ -102,7 +102,23 @@ function handleInput(input) {
case "NAMES": case "NAMES":
var irc = target.network.irc; var irc = target.network.irc;
if (typeof irc !== "undefined") { if (typeof irc !== "undefined") {
irc.send("NAMES", argv[1] ? argv[1] : target.channel.get("name")); var channel = argv[1] ? argv[1] : target.channel.get("name");
irc.send("NAMES", channel);
}
break;
case "TOPIC":
var irc = target.network.irc;
if (typeof irc !== "undefined") {
if (!argv[1] || argv[1].charAt(0) != "#") {
argv.splice(1, 0, target.channel.get("name"));
}
var topic = argv.slice(2).join(" ");
irc.send.apply(
irc,
argv.length == 2 ? argv
: argv.slice(0, 2).concat(topic)
);
} }
break; break;
@ -112,9 +128,6 @@ function handleInput(input) {
var network = this.networks.add( var network = this.networks.add(
new models.Network({host: argv[1]}) new models.Network({host: argv[1]})
); );
network.irc.addListener(
"error", function() { /* .. */ }
);
network.irc.addListener( network.irc.addListener(
"raw", "raw",
function() { function() {
@ -224,6 +237,32 @@ function handleEvent(argv) {
} }
break; break;
case "TOPIC":
var channel = channels.findWhere({name: argv.args[0]});
var messages = channel.get("messages");
messages.add(
new models.Message({
user: argv.nick,
text: "changed the topic of " + argv.args[0] + " to: "
+ argv.args[1]
})
);
break;
case "rpl_notopic":
case "rpl_topic":
var channel = channels.findWhere({name: argv.args[1]});
if (typeof channel !== "undefined") {
var messages = channel.get("messages");
messages.add(
new models.Message({
text: "Topic for " + argv.args[1] + ": "
+ argv.args[2]
})
);
}
break;
case "rpl_namreply": case "rpl_namreply":
var names = argv.args[3].split(' '); var names = argv.args[3].split(' ');
var channel = network.get("channels").findWhere({name: argv.args[2]}); var channel = network.get("channels").findWhere({name: argv.args[2]});
@ -249,11 +288,12 @@ function handleEvent(argv) {
); );
break; break;
case "err_chanoprivsneeded":
case "err_notonchannel":
case "err_nosuchchannel":
case "err_cannotsendtochan": case "err_cannotsendtochan":
case "err_nosuchnick": case "err_nosuchnick":
var channel = network.get("channels").findWhere({name: argv.args[1]}); network_lobby.add(
var messages = channel.get("messages");
messages.add(
new models.Message({ new models.Message({
text: argv.args[2] text: argv.args[2]
}) })