hardlounge/client/js/lounge.js

984 lines
21 KiB
JavaScript
Raw Normal View History

2014-08-16 16:15:17 +00:00
$(function() {
var path = window.location.pathname + "socket.io/";
var socket = io({path:path});
2014-08-16 16:15:17 +00:00
var commands = [
"/close",
"/connect",
"/deop",
"/devoice",
"/disconnect",
"/invite",
"/join",
"/kick",
"/leave",
"/mode",
"/msg",
"/nick",
"/notice",
"/op",
"/part",
"/query",
"/quit",
"/raw",
"/say",
"/send",
"/server",
"/slap",
"/topic",
"/voice",
"/whois"
];
2014-09-19 23:12:17 +00:00
var sidebar = $("#sidebar, #footer");
2014-08-16 16:15:17 +00:00
var chat = $("#chat");
2014-09-10 19:23:56 +00:00
2014-11-18 22:50:55 +00:00
if (navigator.standalone) {
$("html").addClass("web-app-mode");
}
2015-09-30 22:39:57 +00:00
var pop;
2014-09-10 17:04:27 +00:00
try {
2015-09-30 22:39:57 +00:00
pop = new Audio();
pop.src = "audio/pop.ogg";
2015-09-30 22:39:57 +00:00
} catch (e) {
pop = {
2014-09-10 17:04:27 +00:00
play: $.noop
};
}
2014-08-16 16:15:17 +00:00
$("#play").on("click", function() { pop.play(); });
$("#footer .icon").tooltip();
2014-09-19 23:12:17 +00:00
$(".tse-scrollable").TrackpadScrollEmulator();
2014-08-16 16:15:17 +00:00
var favico = new Favico({
animation: "none"
});
function render(name, data) {
2014-08-25 23:13:47 +00:00
return Handlebars.templates[name](data);
2014-08-16 16:15:17 +00:00
}
Handlebars.registerHelper(
"partial", function(id) {
return new Handlebars.SafeString(render(id, this));
}
);
socket.on("error", function(e) {
console.log(e);
});
2014-09-25 23:51:53 +00:00
$.each(["connect_error", "disconnect"], function(i, e) {
socket.on(e, function() {
refresh();
});
2014-09-10 20:26:59 +00:00
});
2015-09-30 22:39:57 +00:00
socket.on("auth", function(/* data */) {
2014-08-16 16:15:17 +00:00
var body = $("body");
var login = $("#sign-in");
2014-09-10 20:26:59 +00:00
if (!login.length) {
2014-09-15 18:32:25 +00:00
refresh();
2014-09-10 20:26:59 +00:00
return;
}
2014-10-09 14:35:29 +00:00
login.find(".btn").prop("disabled", false);
2014-09-15 21:13:03 +00:00
var token = $.cookie("token");
if (token) {
$.removeCookie("token");
socket.emit("auth", {token: token});
}
2014-08-16 16:15:17 +00:00
if (body.hasClass("signed-out")) {
var error = login.find(".error");
error.show().closest("form").one("submit", function() {
error.hide();
});
}
2014-10-08 20:16:10 +00:00
if (!token) {
body.addClass("signed-out");
}
2014-09-10 19:33:15 +00:00
var input = login.find("input[name='user']");
if (input.val() === "") {
input.val($.cookie("user") || "");
}
2014-10-08 20:16:10 +00:00
if (token) {
return;
}
sidebar.find(".sign-in")
.click()
.end()
.find(".networks")
.html("")
.next()
.show();
2014-08-16 16:15:17 +00:00
});
socket.on("init", function(data) {
if (data.networks.length === 0) {
$("#footer").find(".connect").trigger("click");
2014-09-14 19:13:34 +00:00
} else {
2014-09-15 18:16:45 +00:00
sidebar.find(".empty").hide();
2014-09-14 19:13:34 +00:00
sidebar.find(".networks").html(
render("network", {
networks: data.networks
})
);
var channels = $.map(data.networks, function(n) {
return n.channels;
});
chat.html(
render("chat", {
channels: channels
})
);
channels.forEach(renderChannelMessages);
2014-09-15 18:32:25 +00:00
confirmExit();
2014-08-16 16:15:17 +00:00
}
2014-09-15 21:13:03 +00:00
if (data.token) {
2014-09-22 09:58:42 +00:00
$.cookie(
"token",
data.token, {
expires: expire(30)
}
);
2014-09-15 21:13:03 +00:00
}
2014-08-16 16:15:17 +00:00
$("body").removeClass("signed-out");
2014-09-11 10:08:33 +00:00
$("#sign-in").detach();
2014-08-16 16:15:17 +00:00
var id = data.active;
var target = sidebar.find("[data-id='" + id + "']").trigger("click");
2014-08-16 16:15:17 +00:00
if (target.length === 0) {
var first = sidebar.find(".chan")
.eq(0)
.trigger("click");
if (first.length === 0) {
$("#footer").find(".connect").trigger("click");
}
}
2014-09-23 23:05:07 +00:00
sortable();
2014-08-16 16:15:17 +00:00
});
socket.on("join", function(data) {
var id = data.network;
var network = sidebar.find("#network-" + id);
network.append(
2014-08-25 23:13:47 +00:00
render("chan", {
2014-08-16 16:15:17 +00:00
channels: [data.chan]
})
);
chat.append(
render("chat", {
channels: [data.chan]
})
);
renderChannelMessages(data.chan);
2014-09-15 18:25:47 +00:00
var chan = sidebar.find(".chan")
2014-09-13 17:23:36 +00:00
.sort(function(a, b) { return $(a).data("id") - $(b).data("id"); })
2014-09-15 18:25:47 +00:00
.last();
if (!whois) {
chan = chan.filter(":not(.query)");
}
whois = false;
chan.click();
2014-08-16 16:15:17 +00:00
});
function buildChatMessage(data) {
2016-01-23 14:39:54 +00:00
var type = data.msg.type;
2014-10-08 21:21:41 +00:00
var target = "#chan-" + data.chan;
2016-01-23 14:39:54 +00:00
if (type === "error") {
2014-10-08 21:21:41 +00:00
target = "#chan-" + chat.find(".active").data("id");
2014-08-16 16:15:17 +00:00
}
2014-10-08 21:21:41 +00:00
var chan = chat.find(target);
var from = data.msg.from;
2016-02-12 11:24:13 +00:00
var msg;
2014-10-08 21:21:41 +00:00
2016-01-23 14:39:54 +00:00
if ([
2016-02-12 11:24:13 +00:00
"invite",
2016-01-23 14:39:54 +00:00
"join",
"mode",
"kick",
"nick",
"part",
"quit",
"topic",
"action",
].indexOf(type) !== -1) {
switch (type) {
2016-02-12 11:24:13 +00:00
case "invite": data.msg.formattedAction = "invited " + data.msg.target + " to"; break;
2016-01-23 14:39:54 +00:00
case "join": data.msg.formattedAction = "has joined the channel"; break;
case "mode": data.msg.formattedAction = "sets mode"; break;
case "kick": data.msg.formattedAction = "has kicked"; break;
case "nick": data.msg.formattedAction = "is now known as"; break;
case "part": data.msg.formattedAction = "has left the channel"; break;
case "quit": data.msg.formattedAction = "has quit"; break;
case "topic": data.msg.formattedAction = "has changed the topic to:"; break;
default: data.msg.formattedAction = "";
}
2016-02-12 11:24:13 +00:00
msg = $(render("msg_action", data.msg));
} else {
msg = $(render("msg", data.msg));
2016-01-23 14:39:54 +00:00
}
var text = msg.find(".text");
if (text.find("i").size() === 1) {
text = text.find("i");
}
// Channels names are strings (beginning with a '&' or '#' character)
// of length up to 200 characters.
// See https://tools.ietf.org/html/rfc1459#section-1.3
text.html(text.html().replace(/(^|\s)([#&][^\x07\x2C\s]{0,199})/ig,
'$1<span class="inline-channel" role="button" tabindex="0" data-chan="$2">$2</span>'));
text.find("span.inline-channel")
.on("click", function() {
var chan = $(".network")
.find(".chan.active")
.parent(".network")
.find(".chan[data-title='" + $(this).data("chan") + "']");
if (chan.size() === 1) {
chan.click();
} else {
socket.emit("input", {
target: chat.data("id"),
text: "/join " + $(this).data("chan")
});
}
});
2016-01-23 14:39:54 +00:00
if ((type === "message" || type === "action") && chan.hasClass("channel")) {
var nicks = chan.find(".users").data("nicks");
if (nicks) {
var find = nicks.indexOf(from);
if (find !== -1 && typeof move === "function") {
move(nicks, find, 0);
2014-10-08 21:21:41 +00:00
}
}
}
return msg;
}
function buildChannelMessages(channel, messages) {
return messages.reduce(function(docFragment, message) {
docFragment.append(buildChatMessage({
chan: channel,
msg: message
}));
return docFragment;
}, $(document.createDocumentFragment()));
}
function renderChannelMessages(data) {
var documentFragment = buildChannelMessages(data.id, data.messages);
chat.find("#chan-" + data.id + " .messages").append(documentFragment);
}
socket.on("msg", function(data) {
var msg = buildChatMessage(data);
var target = "#chan-" + data.chan;
chat.find(target + " .messages")
.append(msg)
.trigger("msg", [
target,
data.msg
]);
2014-08-16 16:15:17 +00:00
});
2014-09-10 19:23:56 +00:00
socket.on("more", function(data) {
var documentFragment = buildChannelMessages(data.chan, data.messages);
2014-09-10 19:23:56 +00:00
var chan = chat
.find("#chan-" + data.chan)
2014-08-16 16:15:17 +00:00
.find(".messages")
.prepend(documentFragment)
2014-08-16 16:15:17 +00:00
.end();
2015-09-30 22:39:57 +00:00
if (data.messages.length !== 100) {
2014-09-28 21:51:24 +00:00
chan.find(".show-more").removeClass("show");
2014-09-10 19:23:56 +00:00
}
2014-08-16 16:15:17 +00:00
});
socket.on("network", function(data) {
sidebar.find(".empty").hide();
sidebar.find(".networks").append(
2014-08-25 23:13:47 +00:00
render("network", {
2014-08-16 16:15:17 +00:00
networks: [data.network]
})
);
chat.append(
render("chat", {
channels: data.network.channels
})
);
sidebar.find(".chan")
.last()
.trigger("click");
$("#connect")
.find(".btn")
.prop("disabled", false)
2014-09-14 21:26:22 +00:00
.end();
2014-09-15 18:32:25 +00:00
confirmExit();
2014-09-23 23:05:07 +00:00
sortable();
2014-08-16 16:15:17 +00:00
});
socket.on("nick", function(data) {
2014-09-25 23:51:53 +00:00
var id = data.network;
var nick = data.nick;
var network = sidebar.find("#network-" + id).data("nick", nick);
if (network.find(".active").length) {
setNick(nick);
}
2014-08-16 16:15:17 +00:00
});
socket.on("part", function(data) {
var id = data.chan;
2014-09-19 23:56:38 +00:00
sidebar.find(".chan[data-id='" + id + "']").remove();
$("#chan-" + id).remove();
var next = null;
var highest = -1;
chat.find(".chan").each(function() {
var self = $(this);
var z = parseInt(self.css("z-index"));
if (z > highest) {
highest = z;
next = self;
}
});
if (next !== null) {
2015-09-30 22:39:57 +00:00
id = next.data("id");
2014-09-19 23:56:38 +00:00
sidebar.find("[data-id=" + id + "]").click();
} else {
sidebar.find(".chan")
.eq(0)
.click();
}
2014-08-16 16:15:17 +00:00
});
socket.on("quit", function(data) {
var id = data.network;
sidebar.find("#network-" + id)
.remove()
.end();
var chan = sidebar.find(".chan")
.eq(0)
.trigger("click");
if (chan.length === 0) {
sidebar.find(".empty").show();
}
});
2014-09-27 19:17:05 +00:00
socket.on("toggle", function(data) {
2014-09-27 23:22:34 +00:00
var toggle = $("#toggle-" + data.id);
2014-09-27 23:47:04 +00:00
toggle.parent().after(render("toggle", {toggle: data}));
2014-09-27 23:22:34 +00:00
switch (data.type) {
case "link":
if (options.links) {
toggle.click();
}
break;
case "image":
if (options.thumbnails) {
toggle.click();
}
break;
}
2014-09-27 19:17:05 +00:00
});
2014-12-11 22:42:22 +00:00
socket.on("topic", function(data) {
var topic = $("#chan-" + data.chan).find(".header .topic");
topic.html(Handlebars.helpers.parse(data.topic));
// .attr() is safe escape-wise but consider the capabilities of the attribute
topic.attr("title", data.topic);
});
2014-08-16 16:15:17 +00:00
socket.on("users", function(data) {
var chan = chat.find("#chan-" + data.chan);
if (chan.hasClass("active")) {
socket.emit("names", {
target: data.chan
});
}
else {
chan.data("needsNamesRefresh", true);
}
});
socket.on("names", function(data) {
2014-10-08 21:21:41 +00:00
var users = chat.find("#chan-" + data.chan).find(".users").html(render("user", data));
var nicks = [];
for (var i in data.users) {
nicks.push(data.users[i].name);
}
users.data("nicks", nicks);
2014-08-16 16:15:17 +00:00
});
$.cookie.json = true;
var settings = $("#settings");
2014-09-10 20:08:32 +00:00
var options = $.extend({
2014-09-15 18:16:45 +00:00
badge: false,
2014-09-30 18:00:23 +00:00
colors: false,
2014-09-10 20:08:32 +00:00
join: true,
2014-09-27 23:22:34 +00:00
links: true,
2014-09-10 20:08:32 +00:00
mode: true,
2014-09-11 17:24:32 +00:00
motd: false,
2014-09-10 20:08:32 +00:00
nick: true,
notification: true,
part: true,
2014-09-27 23:22:34 +00:00
thumbnails: true,
2014-09-10 20:08:32 +00:00
quit: true,
notifyAllMessages: false,
2014-09-10 20:08:32 +00:00
}, $.cookie("settings"));
2014-08-16 16:15:17 +00:00
for (var i in options) {
if (options[i]) {
settings.find("input[name=" + i + "]").prop("checked", true);
}
}
settings.on("change", "input", function() {
var self = $(this);
2014-09-10 20:08:32 +00:00
var name = self.attr("name");
options[name] = self.prop("checked");
2014-09-22 09:58:42 +00:00
$.cookie(
"settings",
options, {
expires: expire(365)
}
);
2014-09-10 20:08:32 +00:00
if ([
"join",
2014-09-11 17:24:32 +00:00
"mode",
"motd",
2014-09-10 20:08:32 +00:00
"nick",
"part",
"quit",
"notifyAllMessages",
2014-09-10 20:08:32 +00:00
].indexOf(name) !== -1) {
chat.toggleClass("hide-" + name, !self.prop("checked"));
}
2015-09-30 22:39:57 +00:00
if (name === "colors") {
2014-09-30 18:00:23 +00:00
chat.toggleClass("no-colors", !self.prop("checked"));
}
2014-08-16 16:15:17 +00:00
}).find("input")
.trigger("change");
2014-09-15 18:16:45 +00:00
$("#badge").on("change", function() {
var self = $(this);
if (self.prop("checked")) {
2015-09-30 22:39:57 +00:00
if (Notification.permission !== "granted") {
2014-09-15 18:16:45 +00:00
Notification.requestPermission();
}
}
});
2014-08-16 16:15:17 +00:00
var viewport = $("#viewport");
viewport.on("click", ".lt, .rt", function(e) {
var self = $(this);
viewport.toggleClass(self.attr("class"));
if (viewport.is(".lt, .rt")) {
e.stopPropagation();
chat.find(".chat").one("click", function() {
2014-10-10 19:21:40 +00:00
viewport.removeClass("lt");
2014-08-16 16:15:17 +00:00
});
}
});
var input = $("#input")
.history()
.tab(complete, {hint: false});
2014-09-10 19:23:56 +00:00
2014-09-21 15:21:26 +00:00
var form = $("#form");
form.on("submit", function(e) {
2014-08-16 16:15:17 +00:00
e.preventDefault();
var text = input.val();
input.val("");
2014-09-19 20:24:11 +00:00
if (text.indexOf("/clear") === 0) {
2014-09-21 15:21:26 +00:00
clear();
return;
2014-09-19 20:24:11 +00:00
}
2014-08-16 16:15:17 +00:00
socket.emit("input", {
target: chat.data("id"),
text: text
});
});
2014-09-29 19:48:14 +00:00
chat.on("click", ".messages", function() {
setTimeout(function() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
2015-09-30 22:39:57 +00:00
} else if (document.selection && document.selection.type !== "Control") {
2014-09-29 19:48:14 +00:00
text = document.selection.createRange().text;
}
if (!text) {
focus();
}
}, 2);
});
2014-09-18 10:02:52 +00:00
$(window).on("focus", focus);
function focus() {
var chan = chat.find(".active");
if (screen.width > 768 && chan.hasClass("chan")) {
input.focus();
}
}
2014-08-16 16:15:17 +00:00
var top = 1;
sidebar.on("click", ".chan, button", function() {
2014-08-16 16:15:17 +00:00
var self = $(this);
var target = self.data("target");
if (!target) {
return;
}
chat.data(
"id",
self.data("id")
);
socket.emit(
"open",
self.data("id")
);
2014-08-16 16:15:17 +00:00
sidebar.find(".active").removeClass("active");
self.addClass("active")
.find(".badge")
.removeClass("highlight")
2014-09-12 23:27:27 +00:00
.data("count", "")
2014-08-16 16:15:17 +00:00
.empty();
2014-09-13 17:36:59 +00:00
if (sidebar.find(".highlight").length === 0) {
2014-08-16 16:15:17 +00:00
favico.badge("");
}
2014-10-10 19:21:40 +00:00
viewport.removeClass("lt");
2014-09-11 10:08:33 +00:00
$("#windows .active").removeClass("active");
2014-08-16 16:15:17 +00:00
var chan = $(target)
.addClass("active")
.trigger("show")
.css("z-index", top++)
.find(".chat")
.sticky()
.end();
2014-12-11 22:42:22 +00:00
var title = "The Lounge";
2014-11-07 19:52:38 +00:00
if (chan.data("title")) {
title = chan.data("title") + " — " + title;
}
document.title = title;
2014-12-11 22:42:22 +00:00
2014-09-25 23:51:53 +00:00
if (self.hasClass("chan")) {
var nick = self
.closest(".network")
.data("nick");
if (nick) {
setNick(nick);
}
}
if (chan.data("needsNamesRefresh") === true) {
chan.data("needsNamesRefresh", false);
socket.emit("names", {target: self.data("id")});
}
2014-08-16 16:15:17 +00:00
if (screen.width > 768 && chan.hasClass("chan")) {
input.focus();
}
});
sidebar.on("click", "#sign-out", function() {
2014-09-15 21:13:03 +00:00
$.removeCookie("token");
2014-08-16 16:15:17 +00:00
location.reload();
});
sidebar.on("click", ".close", function() {
var cmd = "/close";
var chan = $(this).closest(".chan");
if (chan.hasClass("lobby")) {
cmd = "/quit";
var server = chan.find(".name").html();
2014-08-16 16:15:17 +00:00
if (!confirm("Disconnect from " + server + "?")) {
return false;
}
}
socket.emit("input", {
target: chan.data("id"),
text: cmd
});
chan.css({
transition: "none",
2014-09-13 17:37:10 +00:00
opacity: 0.4
2014-08-16 16:15:17 +00:00
});
return false;
});
chat.on("input", ".search", function() {
var value = $(this).val().toLowerCase();
2014-08-16 16:15:17 +00:00
var names = $(this).closest(".users").find(".names");
names.find("button").each(function() {
var btn = $(this);
var name = btn.text().toLowerCase().replace(/[+%@~]/, "");
if (name.indexOf(value) === 0) {
btn.show();
} else {
btn.hide();
}
});
});
2014-09-15 18:25:47 +00:00
var whois = false;
2014-08-16 16:15:17 +00:00
chat.on("click", ".user", function() {
2014-09-30 18:00:23 +00:00
var user = $(this).text().trim().replace(/[+%@~&]/, "");
2014-08-16 16:15:17 +00:00
if (user.indexOf("#") !== -1) {
return;
}
2014-09-15 18:25:47 +00:00
whois = true;
2014-08-16 16:15:17 +00:00
var text = "/whois " + user;
socket.emit("input", {
target: chat.data("id"),
text: text
});
});
2014-09-21 15:59:50 +00:00
chat.on("click", ".close", function() {
var id = $(this)
.closest(".chan")
.data("id");
sidebar.find(".chan[data-id='" + id + "']")
.find(".close")
.click();
});
2014-08-16 16:15:17 +00:00
chat.on("msg", ".messages", function(e, target, msg) {
2014-09-14 13:18:53 +00:00
var button = sidebar.find(".chan[data-target=" + target + "]");
var isQuery = button.hasClass("query");
2014-08-16 16:15:17 +00:00
var type = msg.type;
var highlight = type.contains("highlight");
var message = type.contains("message");
var settings = $.cookie("settings") || {};
if (highlight || isQuery || (settings.notifyAllMessages && message)) {
if (!document.hasFocus() || !$(target).hasClass("active")) {
2014-09-15 21:13:03 +00:00
if (settings.notification) {
pop.play();
}
2014-08-16 16:15:17 +00:00
favico.badge("!");
2015-09-30 22:39:57 +00:00
if (settings.badge && Notification.permission === "granted") {
2014-09-21 19:48:26 +00:00
var notify = new Notification(msg.from + " says:", {
2014-09-14 13:18:53 +00:00
body: msg.text.trim(),
icon: "img/logo-64.png",
tag: target
2014-09-21 19:48:26 +00:00
});
notify.onclick = function() {
window.focus();
2014-09-14 13:18:53 +00:00
button.click();
this.close();
};
window.setTimeout(function() {
2014-09-21 19:48:26 +00:00
notify.close();
}, 5 * 1000);
}
2014-08-16 16:15:17 +00:00
}
}
2014-09-14 13:18:53 +00:00
button = button.filter(":not(.active)");
if (button.length === 0) {
2014-08-16 16:15:17 +00:00
return;
}
2014-09-10 19:23:56 +00:00
2014-08-16 16:15:17 +00:00
var ignore = [
"join",
"part",
"quit",
2014-10-16 21:29:18 +00:00
"nick",
"mode",
2014-08-16 16:15:17 +00:00
];
if ($.inArray(type, ignore) !== -1){
return;
}
2014-09-14 13:18:53 +00:00
var badge = button.find(".badge");
2014-08-16 16:15:17 +00:00
if (badge.length !== 0) {
2014-09-12 23:27:27 +00:00
var i = (badge.data("count") || 0) + 1;
badge.data("count", i);
badge.html(i > 999 ? (i / 1000).toFixed(1) + "k" : i);
2014-09-14 13:18:53 +00:00
if (highlight || isQuery) {
2014-08-16 16:15:17 +00:00
badge.addClass("highlight");
}
}
});
2014-09-28 21:51:24 +00:00
chat.on("click", ".show-more-button", function() {
2014-08-16 16:15:17 +00:00
var self = $(this);
2014-09-28 21:51:24 +00:00
var count = self.parent().next(".messages").children().length;
2014-09-10 19:23:56 +00:00
socket.emit("more", {
2014-09-28 21:51:24 +00:00
target: self.data("id"),
2014-08-16 16:15:17 +00:00
count: count
});
});
2014-09-27 19:17:05 +00:00
chat.on("click", ".toggle-button", function() {
var self = $(this);
var chat = self.closest(".chat");
var bottom = chat.isScrollBottom();
var content = self.parent().next(".toggle-content");
if (bottom && !content.hasClass("show")) {
var img = content.find("img");
2015-09-30 22:39:57 +00:00
if (img.length !== 0 && !img.width()) {
img.on("load", function() {
chat.scrollBottom();
});
}
}
content.toggleClass("show");
if (bottom) {
chat.scrollBottom();
}
2014-09-27 19:17:05 +00:00
});
2014-08-16 16:15:17 +00:00
var windows = $("#windows");
var forms = $("#sign-in, #connect");
windows.on("show", "#sign-in", function() {
var self = $(this);
var inputs = self.find("input");
inputs.each(function() {
var self = $(this);
if (self.val() === "") {
self.focus();
return false;
}
2014-09-13 17:23:36 +00:00
});
2014-08-16 16:15:17 +00:00
});
windows.on("click", ".input", function() {
$(this).select();
});
forms.on("submit", "form", function(e) {
2014-09-13 17:23:36 +00:00
e.preventDefault();
2014-08-16 16:15:17 +00:00
var event = "auth";
var form = $(this);
2014-10-09 14:35:29 +00:00
form.find(".btn")
.attr("disabled", true)
.end();
2015-09-30 22:39:57 +00:00
if (form.closest(".window").attr("id") === "connect") {
2014-08-16 16:15:17 +00:00
event = "conn";
}
var values = {};
$.each(form.serializeArray(), function(i, obj) {
if (obj.value !== "") {
values[obj.name] = obj.value;
}
});
if (values.user) {
2014-09-22 09:58:42 +00:00
$.cookie(
"user",
values.user, {
expires: expire(30)
}
);
2014-08-16 16:15:17 +00:00
}
socket.emit(
event, values
);
});
2014-12-11 22:42:22 +00:00
2014-11-06 12:00:14 +00:00
forms.on("input", ".nick", function() {
2014-11-05 22:19:29 +00:00
var nick = $(this).val();
2014-11-06 12:00:14 +00:00
forms.find(".username").val(nick);
2014-11-05 22:19:29 +00:00
});
2014-08-16 16:15:17 +00:00
Mousetrap.bind([
"command+up",
"command+down",
"ctrl+up",
"ctrl+down"
], function(e, keys) {
var channels = sidebar.find(".chan");
var index = channels.index(channels.filter(".active"));
var direction = keys.split("+").pop();
switch (direction) {
case "up":
2014-09-29 19:48:14 +00:00
// Loop
var upTarget = (channels.length + (index - 1 + channels.length)) % channels.length;
channels.eq(upTarget).click();
2014-08-16 16:15:17 +00:00
break;
2014-09-10 19:23:56 +00:00
2014-08-16 16:15:17 +00:00
case "down":
2014-09-29 19:48:14 +00:00
// Loop
var downTarget = (channels.length + (index + 1 + channels.length)) % channels.length;
channels.eq(downTarget).click();
2014-08-16 16:15:17 +00:00
break;
}
});
2014-09-19 11:18:02 +00:00
Mousetrap.bind([
"command+k",
"ctrl+shift+l"
2015-09-30 22:39:57 +00:00
], function(e) {
if (e.target === input[0]) {
2014-09-19 20:24:11 +00:00
clear();
e.preventDefault();
}
2014-09-19 11:18:02 +00:00
});
setInterval(function() {
chat.find(".chan:not(.active)").each(function() {
var chan = $(this);
if (chan.find(".messages").children().slice(0, -100).remove().length) {
chan.find(".show-more").addClass("show");
}
});
}, 1000 * 10);
2014-09-19 20:24:11 +00:00
function clear() {
chat.find(".active .messages").empty();
chat.find(".active .show-more").addClass("show");
2014-09-19 20:24:11 +00:00
}
2014-08-16 16:15:17 +00:00
function complete(word) {
var words = commands.slice();
2014-10-08 21:21:41 +00:00
var users = chat.find(".active").find(".users");
var nicks = users.data("nicks");
if (!nicks) {
nicks = [];
users.find(".user").each(function() {
var nick = $(this).text().replace(/[~&@%+]/, "");
nicks.push(nick);
2014-08-16 16:15:17 +00:00
});
2014-10-08 21:21:41 +00:00
users.data("nicks", nicks);
}
for (var i in nicks) {
words.push(nicks[i]);
}
2015-09-30 22:39:57 +00:00
sidebar.find(".chan")
2014-08-16 16:15:17 +00:00
.each(function() {
2014-10-08 21:21:41 +00:00
var self = $(this);
if (!self.hasClass("lobby")) {
words.push(self.data("title"));
}
2014-08-16 16:15:17 +00:00
});
2014-10-08 21:21:41 +00:00
2014-08-16 16:15:17 +00:00
return $.grep(
words,
function(w) {
return !w.toLowerCase().indexOf(word.toLowerCase());
}
);
}
2014-09-15 18:32:25 +00:00
function confirmExit() {
2014-12-11 22:42:22 +00:00
if ($("body").hasClass("public")) {
window.onbeforeunload = function() {
return "Are you sure you want to navigate away from this page?";
};
}
2014-09-15 18:32:25 +00:00
}
function refresh() {
window.onbeforeunload = null;
location.reload();
}
2014-09-22 09:58:42 +00:00
function expire(days) {
var date = new Date();
date.setTime(date.getTime() + ((3600 * 1000 * 24) * days));
return date;
}
2014-09-23 23:05:07 +00:00
function sortable() {
sidebar.sortable({
axis: "y",
containment: "parent",
cursor: "grabbing",
2014-10-10 20:42:04 +00:00
distance: 12,
2014-09-23 23:05:07 +00:00
items: ".network",
handle: ".lobby",
placeholder: "network-placeholder",
forcePlaceholderSize: true,
update: function() {
2014-09-24 19:42:36 +00:00
var order = [];
sidebar.find(".network").each(function() {
var id = $(this).data("id");
order.push(id);
});
socket.emit(
"sort", {
type: "networks",
order: order
}
);
2014-09-23 23:05:07 +00:00
}
});
sidebar.find(".network").sortable({
axis: "y",
containment: "parent",
cursor: "grabbing",
2014-10-10 20:42:04 +00:00
distance: 12,
2014-09-23 23:05:07 +00:00
items: ".chan:not(.lobby)",
placeholder: "chan-placeholder",
forcePlaceholderSize: true,
2014-09-24 19:42:36 +00:00
update: function(e, ui) {
var order = [];
var network = ui.item.parent();
network.find(".chan").each(function() {
var id = $(this).data("id");
order.push(id);
});
socket.emit(
"sort", {
type: "channels",
target: network.data("id"),
order: order
}
);
2014-09-23 23:05:07 +00:00
}
});
}
2014-09-25 23:51:53 +00:00
function setNick(nick) {
var width = $("#nick")
.html(nick + ":")
.width();
if (width) {
2014-09-27 00:21:14 +00:00
width += 31;
2014-09-25 23:51:53 +00:00
input.css("padding-left", width);
}
}
2014-10-08 21:21:41 +00:00
function move(array, old_index, new_index) {
if (new_index >= array.length) {
var k = new_index - array.length;
while ((k--) + 1) {
this.push(undefined);
}
}
array.splice(new_index, 0, array.splice(old_index, 1)[0]);
return array;
2015-09-30 22:39:57 +00:00
}
2014-10-08 21:21:41 +00:00
2014-08-16 16:15:17 +00:00
document.addEventListener(
"visibilitychange",
function() {
2014-09-13 17:36:59 +00:00
if (sidebar.find(".highlight").length === 0) {
2014-08-16 16:15:17 +00:00
favico.badge("");
}
}
);
});