thelounge/client/js/chat.js

265 lines
5.1 KiB
JavaScript
Raw Normal View History

2014-06-30 01:20:54 +00:00
$(function() {
var socket = io();
var commands = [
"/ame",
"/amsg",
"/close",
"/connect",
"/deop",
"/devoice",
"/disconnect",
"/invite",
"/join",
"/kick",
"/leave",
"/mode",
"/msg",
"/nick",
"/notice",
"/op",
"/part",
"/partall",
"/query",
"/quit",
"/raw",
"/say",
"/send",
"/server",
"/slap",
"/topic",
"/voice",
"/whois"
];
2014-07-07 00:44:31 +00:00
var sidebar = $("#sidebar");
2014-06-30 01:20:54 +00:00
var chat = $("#chat");
2014-07-07 00:44:31 +00:00
var networks = $("#networks");
2014-06-30 01:20:54 +00:00
var channels = [];
2014-07-07 00:44:31 +00:00
var active = null;
2014-06-30 01:20:54 +00:00
var tpl = [];
function render(name, data) {
tpl[name] = tpl[name] || Handlebars.compile($("#templates ." + name).html());
return tpl[name](data);
}
socket.on("auth", function(data) {
console.log(data);
});
socket.on("init", function(data) {
2014-07-04 23:32:40 +00:00
networks.empty();
2014-06-30 01:20:54 +00:00
channels = $.map(data.networks, function(n) {
return n.channels;
});
networks.append(
render("networks", {
networks: data.networks
})
2014-07-07 00:44:31 +00:00
).fadeIn();
var active = $($.cookie("active"));
if (active.length === 0) {
active = networks.find(".chan").eq(0);
}
active.trigger("click");
2014-06-30 01:20:54 +00:00
});
socket.on("join", function(data) {
channels.push(data.chan);
2014-07-07 00:44:31 +00:00
var id = data.network;
2014-06-30 01:20:54 +00:00
var network = networks
2014-07-07 00:44:31 +00:00
.find("#network-" + id)
2014-06-30 01:20:54 +00:00
.eq(0);
network.append(
render("channels", {
channels: [data.chan]
})
);
2014-07-05 00:31:20 +00:00
network.find(".chan")
.last()
.trigger("click");
2014-06-30 01:20:54 +00:00
});
socket.on("msg", function(data) {
var chan = find(data.chan);
if (typeof chan !== "undefined") {
chan.messages.push(data.msg);
if (isActive(chan)) {
chat.find("#messages").append(
2014-07-07 00:44:31 +00:00
render("messages", {messages: [data.msg]})
2014-06-30 01:20:54 +00:00
);
}
}
});
socket.on("network", function(data) {
2014-07-06 15:22:43 +00:00
var lobby = data.network.channels[0];
channels.push(lobby);
2014-06-30 01:20:54 +00:00
networks.append(
render("networks", {
networks: [data.network]
})
);
2014-07-07 00:44:31 +00:00
networks.find(".chan")
.last()
.trigger("click");
2014-06-30 01:20:54 +00:00
});
socket.on("nick", function(data) {
console.log(data);
});
socket.on("part", function(data) {
2014-07-07 00:44:31 +00:00
var id = data.chan;
networks.find("#chan-" + id)
2014-07-04 23:32:40 +00:00
.remove()
.end()
.find(".chan")
.eq(0)
.trigger("click");
2014-06-30 01:20:54 +00:00
});
socket.on("quit", function(data) {
2014-07-07 00:44:31 +00:00
var id = data.network;
networks.find("#network-" + id)
2014-07-04 23:32:40 +00:00
.remove()
.end()
.find(".chan")
.eq(0)
.trigger("click");
2014-06-30 01:20:54 +00:00
});
socket.on("users", function(data) {
var chan = find(data.chan);
if (typeof chan !== "undefined") {
chan.users = data.users;
if (isActive(chan)) {
2014-07-07 00:44:31 +00:00
chat.find(".sidebar").html(render("users", chan));
2014-06-30 01:20:54 +00:00
}
}
});
2014-07-06 15:58:57 +00:00
var input = $("#input")
.history()
.tab(complete, {hint: false});
2014-06-30 01:20:54 +00:00
var form = $("#form").on("submit", function(e) {
e.preventDefault();
var value = input.val();
input.val("");
socket.emit("input", {
2014-07-07 00:44:31 +00:00
target: active.id || -1,
2014-07-02 22:00:11 +00:00
text: value
2014-06-30 01:20:54 +00:00
});
});
2014-07-07 00:44:31 +00:00
sidebar.on("click", "button:not(.active)", function() {
var btn = $(this);
var id = "#" + btn.attr("id");
2014-07-04 23:32:40 +00:00
2014-07-07 00:44:31 +00:00
$.cookie("active", id);
2014-07-04 23:32:40 +00:00
2014-07-07 00:44:31 +00:00
sidebar.find(".active").removeClass("active");
btn.addClass("active");
active = null;
if (btn.hasClass("chan")) {
var chan = find(id.replace("#chan-", ""));
if (typeof chan !== "undefined") {
active = chan;
chat.fadeIn();
chat.siblings().hide();
chat.html(render("chat", chan));
chat.find(".window")
.sticky()
.scrollBottom();
}
} else {
chat.empty();
var target = $(btn.data("target"));
if (target.length !== 0) {
target.fadeIn();
target.siblings().hide();
}
2014-07-02 22:00:11 +00:00
}
});
2014-07-04 23:32:40 +00:00
chat.on("input", "#search", function() {
var val = $(this).val();
$("#users").find("button").each(function() {
var btn = $(this);
if (btn.text().toLowerCase().indexOf(val) === 0) {
btn.show();
} else {
btn.hide();
}
});
});
2014-07-07 00:44:31 +00:00
chat.on("click", ".user", function() {
var user = $(this).text();
if (user.indexOf("#") !== -1) {
return;
}
socket.emit("input", {
target: active.id || -1,
text: "/whois " + user
});
});
2014-06-30 01:20:54 +00:00
function isActive(chan) {
2014-07-07 00:44:31 +00:00
return active !== null && chan == active;
2014-06-30 01:20:54 +00:00
}
function find(id) {
return $.grep(channels, function(c) {
return c.id == id;
})[0];
}
function complete(word) {
return $.grep(
commands,
function(w) {
return !w.toLowerCase().indexOf(word.toLowerCase());
}
);
}
function escape(text) {
var e = {
"<": "&lt;",
">": "&gt;"
};
return text.replace(/[<>]/g, function (c) {
return e[c];
});
}
Handlebars.registerHelper(
"partial", function(id) {
return new Handlebars.SafeString(render(id, this));
}
);
Handlebars.registerHelper(
"uri", function(text) {
var urls = [];
text = URI.withinString(text, function(url) {
urls.push(url);
return "$(" + (urls.length - 1) + ")";
});
text = escape(text);
for (var i in urls) {
var url = escape(urls[i]);
text = text.replace(
"$(" + i + ")",
"<a href='" + url.replace(/^www/, "//www") + "' target='_blank'>" + url + "</a>"
);
}
return text;
}
);
});