thelounge/client/js/chat.js

187 lines
3.5 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) {
// Debug
console.log(arguments);
2014-03-29 15:36:12 +00:00
switch (e) {
case "join":
chat.append(render("#windows", {
windows: [data.chan],
})).find(".window")
.last()
.find(".input")
2014-04-13 23:49:22 +00:00
.tabComplete({after: " ", 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();
$("#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":
$("#window-" + data.id).find(".messages").append(render("#messages", {
messages: [data.msg],
}));
break;
case "networks":
chat.html(render("#windows", {
windows: $.map(data.networks, function(n) { return n.channels; }),
})).find(".window")
.find(".input")
2014-04-13 23:49:22 +00:00
.tabComplete({after: " ", list: commands})
2014-04-14 00:47:59 +00:00
.inputHistory({submit: true})
2014-04-07 21:19:20 +00:00
.end()
2014-03-18 23:08:11 +00:00
.find(".messages")
.scrollGlue({speed: 400})
.end()
.last()
.bringToTop()
.end();
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":
// Not yet implemented.
2014-03-12 13:10:53 +00:00
break;
case "part":
$("#channel-" + data.id + ", #window-" + data.id).remove();
break;
case "users":
$("#window-" + data.id).find(".users").html(render("#users", {
users: data.users,
}));
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();
});
chat.on("click", ".user", function(e) {
e.preventDefault();
var user = $(this);
var id = parseInt(user.closest(".window").attr("id").replace("window-", ""));
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
});
sidebar.on("click", ".channel", function(e) {
e.preventDefault();
$("#window-" + $(this).attr("id").replace("channel-", ""))
.bringToTop();
2014-04-06 20:53:07 +00:00
});
// Utils
2014-04-21 10:27:43 +00:00
function uri(text) {
return URI.withinString(text, function(url) {
return "<a href='" + url.replace(/^www/, "//www") + "' target='_blank'>" + url + "</a>";
2014-04-21 10:27:43 +00:00
});
}
function escape(string) {
var e = {
"<": "&lt;",
">": "&gt;",
};
return string.replace(/[<>]/g, function (s) {
return e[s];
});
}
// Helpers
2014-04-21 10:27:43 +00:00
Handlebars.registerHelper(
"uri",
function(text) {
return uri(escape(text));
}
);
Handlebars.registerHelper(
"partial",
function(id) {
return new Handlebars.SafeString(render(id, this));
}
);
2014-03-22 23:42:07 +00:00
});