2014-03-06 15:11:25 +00:00
|
|
|
$(function() {
|
|
|
|
var socket = io.connect("");
|
2014-03-07 21:24:02 +00:00
|
|
|
socket.on("event", function(data) {
|
|
|
|
render(data);
|
|
|
|
|
|
|
|
// Debug
|
|
|
|
console.log(data);
|
2014-03-07 03:18:53 +00:00
|
|
|
});
|
2014-03-06 15:11:25 +00:00
|
|
|
|
|
|
|
var chat = $("#chat");
|
|
|
|
var sidebar = $("#sidebar");
|
|
|
|
|
|
|
|
// Templates
|
|
|
|
var networks = $("#networks").html();
|
|
|
|
var channels = $("#channels").html();
|
|
|
|
var messages = $("#messages").html();
|
|
|
|
var users = $("#users").html()
|
|
|
|
|
2014-03-07 21:24:02 +00:00
|
|
|
function render(data) {
|
2014-03-07 03:18:53 +00:00
|
|
|
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
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2014-03-09 21:22:37 +00:00
|
|
|
chat.find(".messages").sticky().scrollToBottom();
|
2014-03-07 03:18:53 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-03-06 15:11:25 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-03-09 18:39:25 +00:00
|
|
|
(function($) {
|
2014-03-06 15:11:25 +00:00
|
|
|
var highest = 1;
|
|
|
|
$.fn.bringToTop = function() {
|
2014-03-07 03:18:53 +00:00
|
|
|
return this
|
|
|
|
.css('z-index', highest++)
|
|
|
|
.find("input")
|
|
|
|
.focus();
|
2014-03-06 15:11:25 +00:00
|
|
|
};
|
2014-03-09 18:39:25 +00:00
|
|
|
})(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;
|
|
|
|
};
|
2014-03-06 18:53:02 +00:00
|
|
|
|
|
|
|
$.fn.scrollToBottom = function() {
|
2014-03-09 18:39:25 +00:00
|
|
|
this.scrollTop(this.prop("scrollHeight"));
|
2014-03-06 18:53:02 +00:00
|
|
|
};
|
|
|
|
|
2014-03-09 18:39:25 +00:00
|
|
|
$.fn.isScrollAtBottom = function() {
|
|
|
|
if ((this.scrollTop() + this.outerHeight()) >= this.prop("scrollHeight")) {
|
2014-03-06 18:53:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2014-03-09 18:39:25 +00:00
|
|
|
})(jQuery);
|