hardlounge/client/js/chat.js

84 lines
1.6 KiB
JavaScript
Raw Normal View History

2014-03-06 10:11:25 -05:00
$(function() {
var socket = io.connect("");
2014-03-07 16:24:02 -05:00
socket.on("event", function(data) {
render(data);
// Debug
console.log(data);
2014-03-06 22:18:53 -05:00
});
2014-03-06 10:11:25 -05: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 16:24:02 -05:00
function render(data) {
2014-03-06 22:18:53 -05: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
})
);
chat.find(".messages").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();
}
2014-03-06 10:11:25 -05: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-06 13:53:02 -05:00
2014-03-06 10:11:25 -05:00
(function() {
var highest = 1;
$.fn.bringToTop = function() {
2014-03-06 22:18:53 -05:00
return this
.css('z-index', highest++)
.find("input")
.focus();
2014-03-06 10:11:25 -05:00
};
2014-03-06 13:53:02 -05:00
$.fn.scrollToBottom = function() {
2014-03-06 14:31:06 -05:00
return this.scrollTop(this.prop("scrollHeight"));
2014-03-06 13:53:02 -05:00
};
$.fn.isScrollBottom = function() {
var height = this.outerHeight();
var scroll = this.scrollTop();
if ((scroll + height + 5) >= this.prop("scrollHeight")) {
return true;
}
};
2014-03-06 10:11:25 -05:00
})();