thelounge/client/js/chat.js

212 lines
4.4 KiB
JavaScript
Raw Normal View History

2014-03-06 15:11:25 +00:00
$(function() {
2014-04-19 21:40:36 +00:00
var chat = $("#chat");
var sidebar = $("#sidebar");
2014-04-07 21:19:20 +00:00
var commands = [
"/connect",
"/deop",
"/devoice",
"/disconnect",
"/join",
"/kick",
"/leave",
"/mode",
"/msg",
2014-04-07 21:19:20 +00:00
"/nick",
"/op",
"/part",
"/query",
"/quit",
"/server",
"/topic",
"/voice",
"/whois",
];
2014-03-06 15:11:25 +00:00
var socket = io.connect("");
var events = [
"join",
"msg",
"networks",
"nick",
"part",
"users",
].forEach(function(e) {
socket.on(e, function(data) {
event(e, data);
2014-03-12 13:10:53 +00:00
});
2014-03-07 03:18:53 +00:00
});
2014-04-08 14:18:06 +00:00
2014-03-19 17:30:06 +00:00
var tpl = [];
function render(id, data) {
tpl[id] = tpl[id] || Handlebars.compile($(id).remove().html());
return tpl[id](data);
2014-03-19 17:30:06 +00:00
}
2014-03-22 23:42:07 +00:00
function event(e, data) {
switch (e) {
case "join":
2014-04-26 15:26:30 +00:00
chat.append(render("#windows", {windows: [data.chan]}))
.find(".window")
.last()
.find(".input")
2014-04-28 18:05:19 +00:00
.tabComplete({list: commands})
2014-04-14 00:47:59 +00:00
.inputHistory({submit: true})
2014-04-07 21:19:20 +00:00
.end()
.bringToTop()
.find(".messages")
.scrollGlue({speed: 400})
2014-04-07 21:19:20 +00:00
.end();
2014-04-26 15:26:30 +00:00
$("#network-" + data.id)
.append(render("#channels", {channels: [data.chan]}))
.find(".channel")
2014-03-18 23:08:11 +00:00
.last()
.uniqueClass("active")
.end();
break;
case "msg":
2014-04-26 15:26:30 +00:00
$("#window-" + data.id)
.find(".messages")
.append(render("#messages", {messages: [data.msg]}));
break;
case "networks":
2014-04-26 15:26:30 +00:00
var channels = $.map(data.networks, function(n) { return n.channels; });
chat.html(render("#windows", {windows: channels}))
.find(".window")
.last()
.bringToTop()
.end()
.find(".input")
2014-04-28 18:05:19 +00:00
.tabComplete({list: commands})
2014-04-14 00:47:59 +00:00
.inputHistory({submit: true})
2014-04-07 21:19:20 +00:00
.end()
2014-04-26 15:26:30 +00:00
.find(".hidden")
.prev(".show-more")
2014-04-28 18:05:19 +00:00
.show();
2014-04-30 00:00:27 +00:00
chat.find(".messages")
.scrollGlue({speed: 400})
.end();
2014-04-26 15:26:30 +00:00
sidebar.html(render("#networks", {networks: data.networks}))
.find(".channel")
.last()
.addClass("active")
2014-04-07 21:19:20 +00:00
.end();
2014-03-17 16:24:32 +00:00
break;
case "nick":
2014-04-26 15:26:30 +00:00
// ..
2014-03-12 13:10:53 +00:00
break;
case "part":
2014-04-26 15:26:30 +00:00
$("#channel-" + data.id)
.add("#window-" + data.id)
.remove();
break;
case "users":
2014-04-28 18:05:19 +00:00
var users = $.map(data.users, function(u) { return u.name; });
var tabComplete = commands.concat(users);
2014-04-26 15:26:30 +00:00
$("#window-" + data.id)
2014-04-28 18:05:19 +00:00
.find(".input")
.data("list", tabComplete)
.end()
2014-04-26 15:26:30 +00:00
.find(".users")
2014-04-28 18:05:19 +00:00
.html(render("#users", {users: data.users}))
.end();
2014-03-12 13:10:53 +00:00
break;
}
2014-03-07 03:18:53 +00:00
}
2014-03-31 22:02:28 +00:00
chat.on("submit", "form", function() {
var input = $(this).find(".input");
var text = input.val();
2014-03-31 22:02:28 +00:00
if (text == "") {
return false;
2014-03-15 15:51:21 +00:00
}
2014-03-31 22:02:28 +00:00
input.val("");
2014-03-15 15:51:21 +00:00
socket.emit("input", {
2014-03-31 22:02:28 +00:00
id: input.data("target"),
2014-04-06 20:53:07 +00:00
text: text,
2014-03-15 15:51:21 +00:00
});
2014-03-06 15:11:25 +00:00
});
chat.on("focus", ".input", function() {
var input = $(this).parents().eq(1).find(".messages").scrollToBottom();
});
2014-04-23 22:03:35 +00:00
chat.on("click", ".user", function() {
var user = $(this);
2014-04-23 22:03:35 +00:00
var id = user.closest(".window").find(".input").data("target");
var name = user.text().trim();
2014-04-10 21:34:53 +00:00
if (name == "-!-" || name.indexOf(".") != -1) {
return;
}
2014-04-09 20:54:04 +00:00
socket.emit("input", {
id: id,
text: "/whois " + name,
2014-04-09 20:54:04 +00:00
});
2014-03-31 22:02:28 +00:00
});
2014-04-23 22:03:35 +00:00
chat.on("click", ".close", function() {
var id = $(this).closest(".window").find(".input").data("target");
socket.emit("input", {
id: id,
text: "/part",
});
});
2014-04-26 15:26:30 +00:00
2014-04-26 19:56:04 +00:00
chat.on("click", ".show-more-button", function() {
var more = $(this).parent();
var html = $.parseHTML(more.next(".hidden").remove().html());
more.replaceWith(html);
2014-04-26 15:26:30 +00:00
});
2014-03-31 22:02:28 +00:00
sidebar.on("click", ".channel", function(e) {
e.preventDefault();
2014-04-30 00:00:27 +00:00
sidebar.find(".channel").removeClass("active");
$("#window-" + $(this).addClass("active").attr("id").replace("channel-", ""))
.bringToTop();
2014-04-06 20:53:07 +00:00
});
2014-04-26 15:26:30 +00:00
function escape(text) {
2014-04-21 10:27:43 +00:00
var e = {
"<": "&lt;",
2014-04-26 15:26:30 +00:00
">": "&gt;"
2014-04-21 10:27:43 +00:00
};
2014-04-26 15:26:30 +00:00
return text.replace(/[<>]/g, function (c) {
return e[c];
2014-04-21 10:27:43 +00:00
});
}
2014-04-26 15:26:30 +00:00
Handlebars.registerHelper({
"partial": function(id) {
2014-04-21 10:27:43 +00:00
return new Handlebars.SafeString(render(id, this));
2014-04-26 15:26:30 +00:00
},
"slice": function(items, block) {
var limit = block.hash.limit;
var rows = $.map(items, function(i) {
return block.fn(i);
});
var html = "";
var hide = rows
.slice(0, Math.max(0, rows.length - limit))
.join("");
if (hide != "") {
html = "<script type='text/html' class='hidden'>" + hide + "</script>";
}
html += rows.slice(-limit).join("");
return html;
2014-04-26 17:44:16 +00:00
},
"uri": function(text) {
text = escape(text);
return URI.withinString(text, function(url) {
return "<a href='" + url.replace(/^www/, "//www") + "' target='_blank'>" + url + "</a>";
});
},
2014-04-26 15:26:30 +00:00
});
2014-03-22 23:42:07 +00:00
});