hardlounge/client/js/lounge.js

1444 lines
34 KiB
JavaScript
Raw Normal View History

"use strict";
2014-08-16 16:15:17 +00:00
$(function() {
$("#loading-page-message").text("Connecting…");
var path = window.location.pathname + "socket.io/";
2016-06-05 18:48:34 +00:00
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",
2014-08-16 16:15:17 +00:00
"/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
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();
});
2014-08-16 16:15:17 +00:00
var favicon = $("#favicon");
2014-08-16 16:15:17 +00:00
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
}
function setLocalStorageItem(key, value) {
try {
window.localStorage.setItem(key, value);
} catch (e) {
// Do nothing. If we end up here, web storage quota exceeded, or user is
// in Safari's private browsing where localStorage's setItem is not
// available. See http://stackoverflow.com/q/14555347/1935861.
}
}
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
});
2016-09-25 06:52:16 +00:00
socket.on("authorized", function() {
$("#loading-page-message").text("Authorized, loading messages…");
});
socket.on("auth", function(data) {
2014-08-16 16:15:17 +00:00
var login = $("#sign-in");
2016-10-09 08:54:44 +00:00
var token;
2014-10-09 14:35:29 +00:00
login.find(".btn").prop("disabled", false);
if (!data.success) {
2016-02-18 19:19:18 +00:00
window.localStorage.removeItem("token");
2014-08-16 16:15:17 +00:00
var error = login.find(".error");
error.show().closest("form").one("submit", function() {
error.hide();
});
} else {
2016-10-09 08:54:44 +00:00
token = window.localStorage.getItem("token");
if (token) {
$("#loading-page-message").text("Authorizing…");
socket.emit("auth", {token: token});
}
2014-08-16 16:15:17 +00:00
}
2014-09-10 19:33:15 +00:00
var input = login.find("input[name='user']");
if (input.val() === "") {
2016-02-18 19:19:18 +00:00
input.val(window.localStorage.getItem("user") || "");
2014-09-10 19:33:15 +00:00
}
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("change-password", function(data) {
var passwordForm = $("#change-password");
if (data.error || data.success) {
var message = data.success ? data.success : data.error;
var feedback = passwordForm.find(".feedback");
if (data.success) {
feedback.addClass("success").removeClass("error");
} else {
feedback.addClass("error").removeClass("success");
}
feedback.text(message).show();
feedback.closest("form").one("submit", function() {
feedback.hide();
});
}
2016-05-31 21:28:31 +00:00
if (data.token && window.localStorage.getItem("token") !== null) {
setLocalStorageItem("token", data.token);
2016-05-31 21:28:31 +00:00
}
passwordForm
.find("input")
.val("")
.end()
.find(".btn")
.prop("disabled", false);
});
2014-08-16 16:15:17 +00:00
socket.on("init", function(data) {
2016-09-25 06:52:16 +00:00
$("#loading-page-message").text("Rendering…");
2014-08-16 16:15:17 +00:00
if (data.networks.length === 0) {
$("#footer").find(".connect").trigger("click");
2014-09-14 19:13:34 +00:00
} else {
renderNetworks(data);
2014-08-16 16:15:17 +00:00
}
2016-05-31 21:28:31 +00:00
if (data.token && $("#sign-in-remember").is(":checked")) {
setLocalStorageItem("token", data.token);
2016-05-31 21:28:31 +00:00
} else {
window.localStorage.removeItem("token");
2014-09-15 21:13:03 +00:00
}
2014-08-16 16:15:17 +00:00
$("body").removeClass("signed-out");
$("#loading").remove();
$("#sign-in").remove();
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");
}
}
});
socket.on("open", function(id) {
// Another client opened the channel, clear the unread counter
sidebar.find(".chan[data-id='" + id + "'] .badge")
.removeClass("highlight")
.empty();
});
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]
})
);
renderChannel(data.chan);
// Queries do not automatically focus, unless the user did a whois
if (data.chan.type === "query" && !data.shouldOpen) {
return;
}
sidebar.find(".chan")
.sort(function(a, b) {
return $(a).data("id") - $(b).data("id");
})
.last()
.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 template = "msg";
2014-10-08 21:21:41 +00:00
if (!data.msg.highlight && !data.msg.self && (type === "message" || type === "notice") && highlights.some(function(h) {
return data.msg.text.toLocaleLowerCase().indexOf(h.toLocaleLowerCase()) > -1;
2016-06-21 19:27:49 +00:00
})) {
data.msg.highlight = true;
}
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",
"topic_set_by",
2016-01-23 14:39:54 +00:00
"action",
2016-03-07 15:10:46 +00:00
"whois",
"ctcp",
"channel_list",
2016-01-23 14:39:54 +00:00
].indexOf(type) !== -1) {
2016-02-23 16:22:41 +00:00
data.msg.template = "actions/" + type;
template = "msg_action";
} else if (type === "unhandled") {
template = "msg_unhandled";
2016-01-23 14:39:54 +00:00
}
var msg = $(render(template, data.msg));
var text = msg.find(".text");
if (text.find("i").size() === 1) {
text = text.find("i");
}
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) {
2016-03-07 15:10:46 +00:00
var find = nicks.indexOf(data.msg.from);
2016-01-23 14:39:54 +00:00
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 renderChannel(data) {
renderChannelMessages(data);
renderChannelUsers(data);
}
function renderChannelMessages(data) {
var documentFragment = buildChannelMessages(data.id, data.messages);
2016-05-13 10:23:05 +00:00
var channel = chat.find("#chan-" + data.id + " .messages").append(documentFragment);
if (data.firstUnread > 0) {
var first = channel.find("#msg-" + data.firstUnread);
// TODO: If the message is far off in the history, we still need to append the marker into DOM
if (!first.length) {
channel.prepend(render("unread_marker"));
} else {
first.before(render("unread_marker"));
}
} else {
channel.append(render("unread_marker"));
}
2016-10-03 18:03:19 +00:00
if (data.type !== "lobby") {
var lastDate;
$(chat.find("#chan-" + data.id + " .messages .msg[data-time]")).each(function() {
var msg = $(this);
var msgDate = new Date(msg.attr("data-time"));
// Top-most message in a channel
if (!lastDate) {
lastDate = msgDate;
msg.before(render("date-marker", {msgDate: msgDate}));
}
if (lastDate.toDateString() !== msgDate.toDateString()) {
msg.before(render("date-marker", {msgDate: msgDate}));
}
lastDate = msgDate;
});
}
}
function renderChannelUsers(data) {
var users = chat.find("#chan-" + data.id).find(".users");
var nicks = users.data("nicks") || [];
var i, oldSortOrder = {};
for (i in nicks) {
oldSortOrder[nicks[i]] = i;
}
nicks = [];
for (i in data.users) {
nicks.push(data.users[i].name);
}
nicks = nicks.sort(function(a, b) {
return (oldSortOrder[a] || Number.MAX_VALUE) - (oldSortOrder[b] || Number.MAX_VALUE);
});
users.html(render("user", data)).data("nicks", nicks);
}
function renderNetworks(data) {
sidebar.find(".empty").hide();
sidebar.find(".networks").append(
render("network", {
networks: data.networks
})
);
var channels = $.map(data.networks, function(n) {
return n.channels;
});
chat.append(
render("chat", {
channels: channels
})
);
channels.forEach(renderChannel);
confirmExit();
sortable();
if (sidebar.find(".highlight").length) {
toggleNotificationMarkers(true);
}
}
socket.on("msg", function(data) {
var msg = buildChatMessage(data);
var target = "#chan-" + data.chan;
2016-05-13 10:23:05 +00:00
var container = chat.find(target + " .messages");
2016-10-03 18:03:19 +00:00
// Check if date changed
var prevMsg = $(container.find(".msg")).last();
var prevMsgTime = new Date(prevMsg.attr("data-time"));
var msgTime = new Date(msg.attr("data-time"));
// It's the first message in a channel/query
if (prevMsg.length === 0) {
container.append(render("date-marker", {msgDate: msgTime}));
}
if (prevMsgTime.toDateString() !== msgTime.toDateString()) {
prevMsg.append(render("date-marker", {msgDate: msgTime}));
}
// Add message to the container
container
.append(msg)
.trigger("msg", [
target,
data
]);
2016-05-13 10:23:05 +00:00
if (data.msg.self) {
container
.find(".unread-marker")
.appendTo(container);
}
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)
.find(".messages");
2016-10-03 18:03:19 +00:00
// Remove the date-change marker we put at the top, because it may
// not actually be a date change now
var children = $(chan).children();
if (children.eq(0).hasClass("date-marker")) { // Check top most child
children.eq(0).remove();
} else if (children.eq(0).hasClass("unread-marker") && children.eq(1).hasClass("date-marker")) {
2016-11-28 17:55:16 +00:00
// Otherwise the date-marker would get 'stuck' because of the new-message marker
children.eq(1).remove();
2016-10-03 18:03:19 +00:00
}
// get the scrollable wrapper around messages
var scrollable = chan.closest(".chat");
var heightOld = chan.height();
chan.prepend(documentFragment).end();
// restore scroll position
var position = chan.height() - heightOld;
scrollable.scrollTop(position);
2015-09-30 22:39:57 +00:00
if (data.messages.length !== 100) {
scrollable.find(".show-more").removeClass("show");
2014-09-10 19:23:56 +00:00
}
2016-10-03 18:03:19 +00:00
// Date change detect
// Have to use data instaid of the documentFragment because it's being weird
var lastDate;
$(data.messages).each(function() {
var msgData = this;
var msgDate = new Date(msgData.time);
var msg = $(chat.find("#chan-" + data.chan + " .messages #msg-" + msgData.id));
// Top-most message in a channel
if (!lastDate) {
lastDate = msgDate;
msg.before(render("date-marker", {msgDate: msgDate}));
}
if (lastDate.toDateString() !== msgDate.toDateString()) {
msg.before(render("date-marker", {msgDate: msgDate}));
}
lastDate = msgDate;
});
2014-08-16 16:15:17 +00:00
});
socket.on("network", function(data) {
renderNetworks(data);
2014-08-16 16:15:17 +00:00
sidebar.find(".chan")
.last()
.trigger("click");
2014-08-16 16:15:17 +00:00
$("#connect")
.find(".btn")
.prop("disabled", false)
2014-09-14 21:26:22 +00:00
.end();
2014-08-16 16:15:17 +00:00
});
socket.on("network_changed", function(data) {
sidebar.find("#network-" + data.network).data("options", data.serverOptions);
});
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 chanMenuItem = sidebar.find(".chan[data-id='" + data.chan + "']");
2014-09-19 23:56:38 +00:00
// When parting from the active channel/query, jump to the network's lobby
if (chanMenuItem.hasClass("active")) {
chanMenuItem.parent(".network").find(".lobby").click();
2014-09-19 23:56:38 +00:00
}
chanMenuItem.remove();
$("#chan-" + data.chan).remove();
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", renderChannelUsers);
2014-08-16 16:15:17 +00:00
var userStyles = $("#user-specified-css");
2016-10-09 08:54:44 +00:00
var highlights = [];
2014-09-10 20:08:32 +00:00
var options = $.extend({
coloredNicks: true,
desktopNotifications: 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,
notifyAllMessages: false,
2014-09-10 20:08:32 +00:00
part: true,
quit: true,
theme: $("#theme").attr("href").replace(/^themes\/(.*).css$/, "$1"), // Extracts default theme name, set on the server configuration
thumbnails: true,
userStyles: userStyles.text(),
2016-02-18 19:19:18 +00:00
}, JSON.parse(window.localStorage.getItem("settings")));
2014-08-16 16:15:17 +00:00
var windows = $("#windows");
2016-10-09 08:54:44 +00:00
(function SettingsScope() {
var settings = $("#settings");
for (var i in options) {
if (i === "userStyles") {
if (!/[?&]nocss/.test(window.location.search)) {
2016-10-09 08:54:44 +00:00
$(document.head).find("#user-specified-css").html(options[i]);
}
settings.find("#user-specified-css-input").val(options[i]);
} else if (i === "highlights") {
settings.find("input[name=" + i + "]").val(options[i]);
} else if (i === "theme") {
$("#theme").attr("href", "themes/" + options[i] + ".css");
settings.find("select[name=" + i + "]").val(options[i]);
} else if (options[i]) {
settings.find("input[name=" + i + "]").prop("checked", true);
}
2014-08-16 16:15:17 +00:00
}
2016-10-09 08:54:44 +00:00
settings.on("change", "input, select, textarea", function() {
var self = $(this);
var name = self.attr("name");
2016-10-09 08:54:44 +00:00
if (self.attr("type") === "checkbox") {
options[name] = self.prop("checked");
} else {
options[name] = self.val();
}
2016-10-09 08:54:44 +00:00
setLocalStorageItem("settings", JSON.stringify(options));
if ([
"join",
"mode",
"motd",
"nick",
"part",
"quit",
"notifyAllMessages",
].indexOf(name) !== -1) {
chat.toggleClass("hide-" + name, !self.prop("checked"));
} else if (name === "coloredNicks") {
chat.toggleClass("colored-nicks", self.prop("checked"));
} else if (name === "theme") {
$("#theme").attr("href", "themes/" + options[name] + ".css");
} else if (name === "userStyles") {
userStyles.html(options[name]);
} else if (name === "highlights") {
var highlightString = options[name];
highlights = highlightString.split(",").map(function(h) {
return h.trim();
}).filter(function(h) {
// Ensure we don't have empty string in the list of highlights
// otherwise, users get notifications for everything
return h !== "";
});
}
}).find("input")
.trigger("change");
2014-08-16 16:15:17 +00:00
2016-10-09 08:54:44 +00:00
$("#desktopNotifications").on("change", function() {
if ($(this).prop("checked") && Notification.permission !== "granted") {
Notification.requestPermission(updateDesktopNotificationStatus);
2014-09-15 18:16:45 +00:00
}
2016-10-09 08:54:44 +00:00
});
// Updates the checkbox and warning in settings when the Settings page is
// opened or when the checkbox state is changed.
// When notifications are not supported, this is never called (because
// checkbox state can not be changed).
var updateDesktopNotificationStatus = function() {
if (Notification.permission === "denied") {
desktopNotificationsCheckbox.attr("disabled", true);
desktopNotificationsCheckbox.attr("checked", false);
warningBlocked.show();
} else {
if (Notification.permission === "default" && desktopNotificationsCheckbox.prop("checked")) {
desktopNotificationsCheckbox.attr("checked", false);
}
desktopNotificationsCheckbox.attr("disabled", false);
warningBlocked.hide();
}
};
// If browser does not support notifications, override existing settings and
// display proper message in settings.
var desktopNotificationsCheckbox = $("#desktopNotifications");
var warningUnsupported = $("#warnUnsupportedDesktopNotifications");
var warningBlocked = $("#warnBlockedDesktopNotifications");
warningBlocked.hide();
if (("Notification" in window)) {
warningUnsupported.hide();
windows.on("show", "#settings", updateDesktopNotificationStatus);
} else {
options.desktopNotifications = false;
desktopNotificationsCheckbox.attr("disabled", true);
desktopNotificationsCheckbox.attr("checked", false);
}
2016-10-09 08:54:44 +00:00
}());
2014-09-15 18:16:45 +00:00
2014-08-16 16:15:17 +00:00
var viewport = $("#viewport");
2016-06-12 02:16:17 +00:00
var sidebarSlide = window.slideoutMenu(viewport[0], sidebar[0]);
2016-02-12 11:34:10 +00:00
var contextMenuContainer = $("#context-menu-container");
var contextMenu = $("#context-menu");
2014-08-16 16:15:17 +00:00
2016-06-12 02:16:17 +00:00
$("#main").on("click", function(e) {
if ($(e.target).is(".lt")) {
sidebarSlide.toggle(!sidebarSlide.isOpen());
} else if (sidebarSlide.isOpen()) {
sidebarSlide.toggle(false);
}
});
viewport.on("click", ".rt", function(e) {
2014-08-16 16:15:17 +00:00
var self = $(this);
viewport.toggleClass(self.attr("class"));
2016-06-12 02:16:17 +00:00
e.stopPropagation();
2014-08-16 16:15:17 +00:00
});
2016-03-19 18:20:11 +00:00
function positionContextMenu(that, e) {
var offset;
2016-02-12 11:34:10 +00:00
var menuWidth = contextMenu.outerWidth();
var menuHeight = contextMenu.outerHeight();
2016-03-19 18:20:11 +00:00
if (that.hasClass("menu")) {
offset = that.offset();
offset.left -= menuWidth - that.outerWidth();
offset.top += that.outerHeight();
return offset;
2016-02-12 11:34:10 +00:00
}
2016-03-19 18:20:11 +00:00
offset = {left: e.pageX, top: e.pageY};
if ((window.innerWidth - offset.left) < menuWidth) {
offset.left = window.innerWidth - menuWidth;
2016-02-12 11:34:10 +00:00
}
2016-03-19 18:20:11 +00:00
if ((window.innerHeight - offset.top) < menuHeight) {
offset.top = window.innerHeight - menuHeight;
}
return offset;
2016-02-12 11:34:10 +00:00
}
2016-03-19 18:20:11 +00:00
function showContextMenu(that, e) {
2016-02-12 11:34:10 +00:00
var target = $(e.currentTarget);
var output = "";
if (target.hasClass("user")) {
output = render("contextmenu_item", {
class: "user",
text: target.text(),
data: target.data("name")
});
} else if (target.hasClass("chan")) {
2016-02-12 11:34:10 +00:00
output = render("contextmenu_item", {
class: "chan",
text: target.data("title"),
data: target.data("target")
});
output += render("contextmenu_divider");
2016-02-12 11:34:10 +00:00
output += render("contextmenu_item", {
class: "close",
text: target.hasClass("lobby") ? "Disconnect" : target.hasClass("channel") ? "Leave" : "Close",
2016-02-12 11:34:10 +00:00
data: target.data("target")
});
}
contextMenuContainer.show();
contextMenu
.html(output)
2016-03-19 18:20:11 +00:00
.css(positionContextMenu($(that), e));
2016-02-12 11:34:10 +00:00
return false;
2016-03-19 18:20:11 +00:00
}
viewport.on("contextmenu", ".user, .network .chan", function(e) {
return showContextMenu(this, e);
});
viewport.on("click", "#chat .menu", function(e) {
e.currentTarget = $(e.currentTarget).closest(".chan")[0];
return showContextMenu(this, e);
2016-02-12 11:34:10 +00:00
});
contextMenuContainer.on("click contextmenu", function() {
contextMenuContainer.hide();
return false;
});
function resetInputHeight(input) {
input.style.height = input.style.minHeight;
}
2014-08-16 16:15:17 +00:00
var input = $("#input")
.history()
.on("input keyup", function() {
var style = window.getComputedStyle(this);
2016-07-15 06:42:47 +00:00
// Start by resetting height before computing as scrollHeight does not
// decrease when deleting characters
resetInputHeight(this);
2016-07-15 06:42:47 +00:00
this.style.height = Math.min(
Math.round(window.innerHeight - 100), // prevent overflow
this.scrollHeight
+ Math.round(parseFloat(style.borderTopWidth) || 0)
+ Math.round(parseFloat(style.borderBottomWidth) || 0)
) + "px";
$("#chat .chan.active .chat").trigger("msg.sticky"); // fix growing
})
2014-08-16 16:15:17 +00:00
.tab(complete, {hint: false});
2014-09-10 19:23:56 +00:00
// Cycle through nicks for the current word, just like hitting "Tab"
$("#cycle-nicks").on("click", function() {
input.triggerHandler($.Event("keydown.tabcomplete", {which: 9}));
focus();
});
2016-10-09 08:54:44 +00:00
$("#form").on("submit", function(e) {
2014-08-16 16:15:17 +00:00
e.preventDefault();
focus();
2014-08-16 16:15:17 +00:00
var text = input.val();
2016-05-25 07:23:03 +00:00
if (text.length === 0) {
return;
}
2014-08-16 16:15:17 +00:00
input.val("");
resetInputHeight(input.get(0));
2016-05-25 07:23:03 +00:00
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
}
2016-05-25 07:23:03 +00:00
2014-08-16 16:15:17 +00:00
socket.emit("input", {
target: chat.data("id"),
text: text
});
});
function findCurrentNetworkChan(name) {
name = name.toLowerCase();
return $(".network .chan.active")
.parent(".network")
.find(".chan")
.filter(function() {
return $(this).data("title").toLowerCase() === name;
})
.first();
}
$("button#set-nick").on("click", function() {
toggleNickEditor(true);
// Selects existing nick in the editable text field
var element = document.querySelector("#nick-value");
element.focus();
var range = document.createRange();
range.selectNodeContents(element);
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
});
$("button#cancel-nick").on("click", cancelNick);
$("button#submit-nick").on("click", submitNick);
function toggleNickEditor(toggle) {
$("#nick").toggleClass("editable", toggle);
$("#nick-value").attr("contenteditable", toggle);
}
function submitNick() {
var newNick = $("#nick-value").text().trim();
if (newNick.length === 0) {
cancelNick();
return;
}
toggleNickEditor(false);
socket.emit("input", {
target: chat.data("id"),
text: "/nick " + newNick
});
}
function cancelNick() {
setNick(sidebar.find(".chan.active").closest(".network").data("nick"));
}
$("#nick-value").keypress(function(e) {
switch (e.keyCode ? e.keyCode : e.which) {
case 13: // Enter
// Ensures a new line is not added when pressing Enter
e.preventDefault();
break;
}
}).keyup(function(e) {
switch (e.keyCode ? e.keyCode : e.which) {
case 13: // Enter
submitNick();
break;
case 27: // Escape
cancelNick();
break;
}
});
chat.on("click", ".inline-channel", function() {
var name = $(this).data("chan");
var chan = findCurrentNetworkChan(name);
if (chan.length) {
chan.click();
} else {
socket.emit("input", {
target: chat.data("id"),
text: "/join " + name
});
}
});
chat.on("click", ".user", function() {
var name = $(this).data("name");
var chan = findCurrentNetworkChan(name);
if (chan.length) {
chan.click();
}
socket.emit("input", {
target: chat.data("id"),
text: "/whois " + name
});
});
chat.on("click", ".chat", function() {
2014-09-29 19:48:14 +00:00
setTimeout(function() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
2016-10-09 08:54:44 +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();
}
}
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")
.empty();
2014-09-13 17:36:59 +00:00
if (sidebar.find(".highlight").length === 0) {
toggleNotificationMarkers(false);
2014-08-16 16:15:17 +00:00
}
2016-06-12 02:16:17 +00:00
sidebarSlide.toggle(false);
var lastActive = $("#windows > .active");
2016-05-13 10:23:05 +00:00
lastActive
2016-04-17 10:50:03 +00:00
.removeClass("active")
.find(".chat")
.unsticky();
var lastActiveChan = lastActive
.find(".chan.active")
.removeClass("active");
2016-07-05 23:23:46 +00:00
lastActiveChan
.find(".unread-marker")
.appendTo(lastActiveChan.find(".messages"));
2014-08-16 16:15:17 +00:00
var chan = $(target)
.addClass("active")
2016-07-05 23:23:46 +00:00
.trigger("show");
2016-04-17 10:50:03 +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")) {
$("#chat-container").addClass("active");
setNick(self.closest(".network").data("nick"));
2014-09-25 23:51:53 +00:00
}
var chanChat = chan.find(".chat");
if (chanChat.length > 0) {
chanChat.sticky();
}
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() {
2016-02-18 19:19:18 +00:00
window.localStorage.removeItem("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;
});
2016-02-12 11:34:10 +00:00
contextMenu.on("click", ".context-menu-item", function() {
switch ($(this).data("action")) {
case "close":
$(".networks .chan[data-target=" + $(this).data("data") + "] .close").click();
break;
case "chan":
$(".networks .chan[data-target=" + $(this).data("data") + "]").click();
break;
case "user":
$(".channel.active .users .user[data-name=" + $(this).data("data") + "]").click();
break;
}
});
2014-08-16 16:15:17 +00:00
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(".user").each(function() {
2014-08-16 16:15:17 +00:00
var btn = $(this);
var name = btn.text().toLowerCase().replace(/[+%@~]/, "");
if (name.indexOf(value) === 0) {
btn.show();
} else {
btn.hide();
}
});
});
chat.on("msg", ".messages", function(e, target, msg) {
var unread = msg.unread;
msg = msg.msg;
if (msg.self) {
return;
}
var button = sidebar.find(".chan[data-target='" + target + "']");
if (msg.highlight || (options.notifyAllMessages && msg.type === "message")) {
if (!document.hasFocus() || !$(target).hasClass("active")) {
2016-02-18 19:19:18 +00:00
if (options.notification) {
try {
pop.play();
} catch (exception) {
// On mobile, sounds can not be played without user interaction.
}
2014-09-15 21:13:03 +00:00
}
toggleNotificationMarkers(true);
2016-02-29 07:25:40 +00:00
if (options.desktopNotifications && Notification.permission === "granted") {
2016-02-29 07:25:40 +00:00
var title;
var body;
if (msg.type === "invite") {
title = "New channel invite:";
body = msg.from + " invited you to " + msg.channel;
2016-02-29 07:25:40 +00:00
} else {
title = msg.from;
if (!button.hasClass("query")) {
title += " (" + button.data("title").trim() + ")";
2016-02-29 07:25:40 +00:00
}
title += " says:";
body = msg.text.replace(/\x02|\x1D|\x1F|\x16|\x0F|\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?/g, "").trim();
}
try {
var notify = new Notification(title, {
body: body,
icon: "img/logo-64.png",
tag: target
});
notify.addEventListener("click", function() {
window.focus();
button.click();
this.close();
});
} catch (exception) {
// `new Notification(...)` is not supported and should be silenced.
}
}
2014-08-16 16:15:17 +00:00
}
}
if (button.hasClass("active")) {
2014-08-16 16:15:17 +00:00
return;
}
2014-09-10 19:23:56 +00:00
if (!unread) {
2014-08-16 16:15:17 +00:00
return;
}
var badge = button.find(".badge").html(Handlebars.helpers.roundBadgeNumber(unread));
if (msg.highlight) {
badge.addClass("highlight");
2014-08-16 16:15:17 +00:00
}
});
2014-08-16 16:15:17 +00:00
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);
2016-10-09 08:54:44 +00:00
var localChat = self.closest(".chat");
var bottom = localChat.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() {
2016-10-09 08:54:44 +00:00
localChat.scrollBottom();
});
}
}
content.toggleClass("show");
if (bottom) {
2016-10-09 08:54:44 +00:00
localChat.scrollBottom();
}
2014-09-27 19:17:05 +00:00
});
var forms = $("#sign-in, #connect, #change-password");
2014-08-16 16:15:17 +00:00
windows.on("show", "#sign-in", function() {
2016-10-09 08:54:44 +00:00
$(this).find("input").each(function() {
2014-08-16 16:15:17 +00:00
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
});
if ($("body").hasClass("public")) {
$("#connect").one("show", function() {
var params = window.URI(document.location.search);
params = params.search(true);
// Possible parameters: name, host, port, password, tls, nick, username, realname, join
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#Iterating_over_own_properties_only
for (var key in params) {
if (params.hasOwnProperty(key)) {
var value = params[key];
// \W searches for non-word characters
key = key.replace(/\W/g, "");
var element = $("#connect input[name='" + key + "']");
// if the element exists, it isn't disabled, and it isn't hidden
if (element.length > 0 && !element.is(":disabled") && !element.is(":hidden")) {
if (element.is(":checkbox")) {
element.prop("checked", (value === "1" || value === "true") ? true : false);
} else {
element.val(value);
}
}
}
}
});
}
2014-08-16 16:15:17 +00:00
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";
} else if (form.closest("div").attr("id") === "change-password") {
event = "change-password";
2014-08-16 16:15:17 +00:00
}
var values = {};
$.each(form.serializeArray(), function(i, obj) {
if (obj.value !== "") {
values[obj.name] = obj.value;
}
});
if (values.user) {
setLocalStorageItem("user", values.user);
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
});
2016-02-12 11:34:10 +00:00
Mousetrap.bind([
"escape"
], function() {
contextMenuContainer.hide();
});
setInterval(function() {
chat.find(".chan:not(.active)").each(function() {
var chan = $(this);
2016-05-13 10:23:05 +00:00
if (chan.find(".messages .msg:not(.unread-marker)").slice(0, -100).remove().length) {
chan.find(".show-more").addClass("show");
2016-10-03 18:03:19 +00:00
// Remove date-seperators that would otherwise be "stuck" at the top
// of the channel
var prev;
$(chan.find(".messages").children()).each(function() {
if (!prev) {
// Should always be a date-seperator, because it's always added
prev = $(this);
} else {
var current = $(this);
if (current.attr("class") === "date-marker") {
prev.remove();
} else {
return false;
}
prev = current;
}
});
}
});
}, 1000 * 10);
2014-09-19 20:24:11 +00:00
function clear() {
2016-05-13 10:23:05 +00:00
chat.find(".active .messages .msg:not(.unread-marker)").remove();
chat.find(".active .show-more").addClass("show");
2016-10-03 18:03:19 +00:00
chat.find(".active .date-marker").remove();
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");
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-23 23:05:07 +00:00
function sortable() {
2016-10-18 21:53:39 +00:00
sidebar.find(".networks").sortable({
2014-09-23 23:05:07 +00:00
axis: "y",
containment: "parent",
2016-10-18 21:53:39 +00:00
cursor: "move",
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,
2016-10-18 21:53:39 +00:00
tolerance: "pointer", // Use the pointer to figure out where the network is in the list
2014-09-23 23:05:07 +00:00
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",
2016-10-18 21:53:39 +00:00
cursor: "move",
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,
2016-10-18 21:53:39 +00:00
tolerance: "pointer", // Use the pointer to figure out where the channel is in the list
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) {
// Closes the nick editor when canceling, changing channel, or when a nick
// is set in a different tab / browser / device.
toggleNickEditor(false);
$("#nick-value").text(nick);
2014-09-25 23:51:53 +00:00
}
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
function toggleNotificationMarkers(newState) {
// Toggles the favicon to red when there are unread notifications
if (favicon.data("toggled") !== newState) {
var old = favicon.attr("href");
favicon.attr("href", favicon.data("other"));
favicon.data("other", old);
favicon.data("toggled", newState);
}
// Toggles a dot on the menu icon when there are unread notifications
$("#viewport .lt").toggleClass("notified", newState);
}
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) {
toggleNotificationMarkers(false);
2014-08-16 16:15:17 +00:00
}
}
);
});