2017-12-21 18:17:10 +00:00
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
2017-12-21 20:40:50 +00:00
|
|
|
|
const Mousetrap = require("mousetrap");
|
2017-12-21 18:17:10 +00:00
|
|
|
|
|
|
|
|
|
const socket = require("./socket");
|
|
|
|
|
const utils = require("./utils");
|
|
|
|
|
|
|
|
|
|
const sidebar = $("#sidebar");
|
|
|
|
|
|
2017-12-22 17:18:10 +00:00
|
|
|
|
module.exports = {
|
|
|
|
|
handleKeybinds,
|
|
|
|
|
openForm,
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-21 21:23:22 +00:00
|
|
|
|
function toggleButton(network) {
|
|
|
|
|
// Transform the + button to a ×
|
|
|
|
|
network.find("button.add-channel").toggleClass("opened");
|
|
|
|
|
|
|
|
|
|
// Toggle content of tooltip
|
|
|
|
|
const tooltip = network.find(".add-channel-tooltip");
|
|
|
|
|
const altLabel = tooltip.data("alt-label");
|
2017-12-28 12:15:28 +00:00
|
|
|
|
tooltip.data("alt-label", tooltip.attr("aria-label"));
|
|
|
|
|
tooltip.attr("aria-label", altLabel);
|
2017-12-21 21:23:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 20:40:50 +00:00
|
|
|
|
function closeForm(network) {
|
|
|
|
|
const form = network.find(".join-form");
|
2017-12-21 21:23:22 +00:00
|
|
|
|
|
2017-12-22 17:18:10 +00:00
|
|
|
|
if (form.is(":visible")) {
|
|
|
|
|
form.find("input[name='channel']").val("");
|
|
|
|
|
form.find("input[name='key']").val("");
|
|
|
|
|
form.hide();
|
|
|
|
|
toggleButton(network);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openForm(network) {
|
|
|
|
|
const form = network.find(".join-form");
|
|
|
|
|
|
|
|
|
|
if (form.is(":hidden")) {
|
|
|
|
|
form.show();
|
|
|
|
|
toggleButton(network);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Focus the "Channel" field even if the form was already open
|
2018-01-30 09:38:33 +00:00
|
|
|
|
form.find(".input[name='channel']").trigger("focus");
|
2017-12-21 20:40:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 21:23:22 +00:00
|
|
|
|
sidebar.on("click", ".add-channel", function(e) {
|
2018-05-02 13:57:19 +00:00
|
|
|
|
const id = $(e.target).closest(".lobby").data("id");
|
2017-12-21 18:17:10 +00:00
|
|
|
|
const joinForm = $(`#join-channel-${id}`);
|
2017-12-21 21:23:22 +00:00
|
|
|
|
const network = joinForm.closest(".network");
|
|
|
|
|
|
|
|
|
|
if (joinForm.is(":visible")) {
|
|
|
|
|
closeForm(network);
|
|
|
|
|
} else {
|
2017-12-22 17:18:10 +00:00
|
|
|
|
openForm(network);
|
2017-12-21 21:23:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 18:17:10 +00:00
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
2018-05-02 13:57:19 +00:00
|
|
|
|
function handleKeybinds(networks) {
|
|
|
|
|
for (const network of networks) {
|
|
|
|
|
const form = $(`.network[data-uuid="${network.uuid}"] .join-form`);
|
2018-02-20 07:28:04 +00:00
|
|
|
|
|
2018-05-02 13:57:19 +00:00
|
|
|
|
form.find("input, button").each(function() {
|
|
|
|
|
Mousetrap(this).bind("esc", () => {
|
|
|
|
|
closeForm(form.closest(".network"));
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
2017-12-21 18:17:10 +00:00
|
|
|
|
});
|
2018-02-20 07:28:04 +00:00
|
|
|
|
|
2018-05-02 13:57:19 +00:00
|
|
|
|
form.on("submit", () => {
|
|
|
|
|
const networkElement = form.closest(".network");
|
|
|
|
|
const channel = form.find("input[name='channel']").val();
|
|
|
|
|
const key = form.find("input[name='key']").val();
|
|
|
|
|
const existingChannel = utils.findCurrentNetworkChan(channel);
|
2017-12-21 20:40:50 +00:00
|
|
|
|
|
2018-07-09 17:31:48 +00:00
|
|
|
|
if (existingChannel) {
|
2018-07-10 09:40:55 +00:00
|
|
|
|
$(`#sidebar .chan[data-id="${existingChannel.id}"]`).trigger("click");
|
2018-05-02 13:57:19 +00:00
|
|
|
|
} else {
|
|
|
|
|
socket.emit("input", {
|
|
|
|
|
text: `/join ${channel} ${key}`,
|
|
|
|
|
target: networkElement.find(".lobby").data("id"),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closeForm(networkElement);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-12-22 17:18:10 +00:00
|
|
|
|
}
|