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) {
|
2017-12-21 18:17:10 +00:00
|
|
|
|
const id = $(e.target).data("id");
|
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
sidebar.on("submit", ".join-form", function() {
|
|
|
|
|
const form = $(this);
|
|
|
|
|
const channel = form.find("input[name='channel']");
|
|
|
|
|
const channelString = channel.val();
|
|
|
|
|
const key = form.find("input[name='key']");
|
|
|
|
|
const keyString = key.val();
|
|
|
|
|
const chan = utils.findCurrentNetworkChan(channelString);
|
2018-02-20 07:28:04 +00:00
|
|
|
|
|
2017-12-21 18:17:10 +00:00
|
|
|
|
if (chan.length) {
|
2018-01-30 09:38:33 +00:00
|
|
|
|
chan.trigger("click");
|
2017-12-21 18:17:10 +00:00
|
|
|
|
} else {
|
|
|
|
|
socket.emit("input", {
|
|
|
|
|
text: `/join ${channelString} ${keyString}`,
|
|
|
|
|
target: form.prev().data("id"),
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
|
2017-12-21 20:40:50 +00:00
|
|
|
|
closeForm(form.closest(".network"));
|
2017-12-21 18:17:10 +00:00
|
|
|
|
return false;
|
|
|
|
|
});
|
2017-12-21 20:40:50 +00:00
|
|
|
|
|
2017-12-22 17:18:10 +00:00
|
|
|
|
function handleKeybinds() {
|
2017-12-21 20:40:50 +00:00
|
|
|
|
sidebar.find(".join-form input, .join-form button").each(function() {
|
|
|
|
|
const network = $(this).closest(".network");
|
|
|
|
|
Mousetrap(this).bind("esc", () => closeForm(network));
|
|
|
|
|
});
|
2017-12-22 17:18:10 +00:00
|
|
|
|
}
|