Focus windows on refresh

This commit is contained in:
Mattias Erming 2014-03-06 11:31:06 -08:00
parent b45a3e09fd
commit 3acb7402be
2 changed files with 26 additions and 16 deletions

View File

@ -20,24 +20,37 @@ $(function() {
var View = {}; var View = {};
View.refresh = function(event) { View.refresh = function(event) {
chat.html(""); var data = event.data;
event.data.forEach(function(network) {
chat.append(Mustache.render(channels, network, {
users: users,
messages: messages
}));
});
chat.find(".messages").scrollToBottom();
sidebar.html( sidebar.html(
Mustache.render(networks, { Mustache.render(networks, {
networks: event.data networks: data
}) })
); );
chat.html("");
var partials = {
users: users,
messages: messages
};
data.forEach(function(network) {
chat.append(Mustache.render(channels, network, partials));
});
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();
}; };
View.add = function(event) { View.add = function(event) {
var target = ""; var target = "";
var render = ""; var render = "";
switch (event.type) { switch (event.type) {
case "user": case "user":
target = ".users"; target = ".users";
@ -45,6 +58,7 @@ $(function() {
users, {users: event.data} users, {users: event.data}
); );
break; break;
case "message": case "message":
target = ".messages"; target = ".messages";
render = Mustache.render( render = Mustache.render(
@ -52,6 +66,7 @@ $(function() {
); );
break; break;
} }
if (target != "") { if (target != "") {
target = $("[data-id='" + event.target + "'] " + target); target = $("[data-id='" + event.target + "'] " + target);
var keepAtBottom = target.isScrollBottom(); var keepAtBottom = target.isScrollBottom();
@ -92,11 +107,11 @@ $(function() {
(function() { (function() {
var highest = 1; var highest = 1;
$.fn.bringToTop = function() { $.fn.bringToTop = function() {
this.css('z-index', highest++); return this.css('z-index', highest++);
}; };
$.fn.scrollToBottom = function() { $.fn.scrollToBottom = function() {
this.scrollTop(this.prop("scrollHeight")); return this.scrollTop(this.prop("scrollHeight"));
}; };
$.fn.isScrollBottom = function() { $.fn.isScrollBottom = function() {

View File

@ -51,7 +51,6 @@ function handleUserInput(input) {
var args = text.substr(1).split(" "); var args = text.substr(1).split(" ");
switch (args[0]) { switch (args[0]) {
case "connect": case "connect":
if (typeof args[1] !== "undefined") { if (typeof args[1] !== "undefined") {
addNetwork(args[1], true); addNetwork(args[1], true);
@ -59,7 +58,6 @@ function handleUserInput(input) {
break; break;
case "join": case "join":
console.log(args);
if (typeof args[1] === "undefined") { if (typeof args[1] === "undefined") {
return; return;
} }
@ -72,13 +70,11 @@ function handleUserInput(input) {
break; break;
default: default:
console.log("DEFAULT");
addMessage( addMessage(
target, target,
"Command '/" + args[0] + "' does not exist." "Command '/" + args[0] + "' does not exist."
); );
break; break;
} }
} }
@ -91,7 +87,6 @@ function addNetwork(addr, bool) {
); );
if (bool) { if (bool) {
console.log("IRC");
network.irc = new irc.Client(addr, "default_user", { network.irc = new irc.Client(addr, "default_user", {
channels: ["#default_channel"] channels: ["#default_channel"]
}); });