Call the openForm function specifically when clicking on the context menu instead of relying on click handler

Not defining a `toggleForm` function has the advantage of "fixing" the fact that clicking "Join a channel..." from the context menu would close it when it was already open
This commit is contained in:
Jérémie Astori 2017-12-22 12:18:10 -05:00
parent 1063d7b1d6
commit 47f95c234d
No known key found for this signature in database
GPG Key ID: B9A4F245CD67BDE8
2 changed files with 29 additions and 10 deletions

View File

@ -8,6 +8,11 @@ const utils = require("./utils");
const sidebar = $("#sidebar"); const sidebar = $("#sidebar");
module.exports = {
handleKeybinds,
openForm,
};
function toggleButton(network) { function toggleButton(network) {
// Transform the + button to a × // Transform the + button to a ×
network.find("button.add-channel").toggleClass("opened"); network.find("button.add-channel").toggleClass("opened");
@ -21,12 +26,26 @@ function toggleButton(network) {
function closeForm(network) { function closeForm(network) {
const form = network.find(".join-form"); const form = network.find(".join-form");
if (form.is(":visible")) {
form.find("input[name='channel']").val(""); form.find("input[name='channel']").val("");
form.find("input[name='key']").val(""); form.find("input[name='key']").val("");
form.hide(); form.hide();
toggleButton(network); 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
form.find(".input[name='channel']").focus();
}
sidebar.on("click", ".add-channel", function(e) { sidebar.on("click", ".add-channel", function(e) {
const id = $(e.target).data("id"); const id = $(e.target).data("id");
@ -36,9 +55,7 @@ sidebar.on("click", ".add-channel", function(e) {
if (joinForm.is(":visible")) { if (joinForm.is(":visible")) {
closeForm(network); closeForm(network);
} else { } else {
joinForm.show(); openForm(network);
joinForm.find(".input[name='channel']").focus();
toggleButton(network);
} }
return false; return false;
@ -63,9 +80,9 @@ sidebar.on("submit", ".join-form", function() {
return false; return false;
}); });
exports.handleKeybinds = function() { function handleKeybinds() {
sidebar.find(".join-form input, .join-form button").each(function() { sidebar.find(".join-form input, .join-form button").each(function() {
const network = $(this).closest(".network"); const network = $(this).closest(".network");
Mousetrap(this).bind("esc", () => closeForm(network)); Mousetrap(this).bind("esc", () => closeForm(network));
}); });
}; }

View File

@ -20,6 +20,7 @@ const utils = require("./utils");
require("./webpush"); require("./webpush");
require("./keybinds"); require("./keybinds");
require("./clipboard"); require("./clipboard");
const JoinChannel = require("./join-channel");
$(function() { $(function() {
var sidebar = $("#sidebar, #footer"); var sidebar = $("#sidebar, #footer");
@ -485,7 +486,8 @@ $(function() {
const contextMenuActions = { const contextMenuActions = {
join: function(itemData) { join: function(itemData) {
$(`#join-channel-${itemData}`).closest(".network").find("button.add-channel").click(); const network = $(`#join-channel-${itemData}`).closest(".network");
JoinChannel.openForm(network);
}, },
close: function(itemData) { close: function(itemData) {
closeChan($(`.networks .chan[data-target="${itemData}"]`)); closeChan($(`.networks .chan[data-target="${itemData}"]`));