From bb1e3ee9177a76c54b5ea70511055fb385a4ffe5 Mon Sep 17 00:00:00 2001 From: realies Date: Sat, 2 Sep 2017 19:28:36 +0300 Subject: [PATCH] Focus a channel by joining it, refactor user commands #1189 --- client/js/lounge.js | 51 +++++++++++++++++++++----------------------- client/js/modules.js | 35 ++++++++++++++++++++++++++++++ client/js/utils.js | 13 +++++++++++ 3 files changed, 72 insertions(+), 27 deletions(-) create mode 100644 client/js/modules.js diff --git a/client/js/lounge.js b/client/js/lounge.js index bd0da92d..d56aedd8 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -19,6 +19,7 @@ require("./socket-events"); const storage = require("./localStorage"); const options = require("./options"); const utils = require("./utils"); +const modules = require("./modules"); require("./autocompletion"); require("./webpush"); @@ -194,19 +195,27 @@ $(function() { input.val(""); resetInputHeight(input.get(0)); - if (text.indexOf("/clear") === 0) { - utils.clear(); - return; - } - - if (text.indexOf("/collapse") === 0) { - $(".chan.active .toggle-button.opened").click(); - return; - } - - if (text.indexOf("/expand") === 0) { - $(".chan.active .toggle-button:not(.opened)").click(); - return; + if (text.indexOf("/") === 0) { + const separatorPos = text.indexOf(" "); + const cmd = text.substring(1, separatorPos > 1 ? separatorPos : text.length); + const parameters = separatorPos > text.indexOf(cmd) ? text.substring(text.indexOf(cmd) + cmd.length + 1, text.length) : ""; + switch (cmd) { + case "clear": + if (modules.clear()) return; + break; + case "collapse": + if (modules.collapse()) return; + break; + case "expand": + if (modules.expand()) return; + break; + case "join": + const channel = parameters.split(" ")[0]; + if (channel != "") { + if (modules.join(channel)) return; + } + break; + } } socket.emit("input", { @@ -215,18 +224,6 @@ $(function() { }); }); - 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() { utils.toggleNickEditor(true); @@ -283,7 +280,7 @@ $(function() { chat.on("click", ".inline-channel", function() { var name = $(this).data("chan"); - var chan = findCurrentNetworkChan(name); + var chan = utils.findCurrentNetworkChan(name); if (chan.length) { chan.click(); @@ -301,7 +298,7 @@ $(function() { chat.on("click", ".user", function() { var name = $(this).data("name"); - var chan = findCurrentNetworkChan(name); + var chan = utils.findCurrentNetworkChan(name); if (chan.length) { chan.click(); diff --git a/client/js/modules.js b/client/js/modules.js new file mode 100644 index 00000000..dab0a22c --- /dev/null +++ b/client/js/modules.js @@ -0,0 +1,35 @@ +"use strict"; + +// vendor libraries +const $ = require("jquery"); + +// our libraries +const utils = require("./utils"); + +module.exports = { + clear, + collapse, + expand, + join +}; + +function clear() { + utils.clear(); +} + +function collapse() { + $(".chan.active .toggle-button.opened").click(); +} + +function expand() { + $(".chan.active .toggle-button:not(.opened)").click(); +} + +function join(channel) { + var chan = utils.findCurrentNetworkChan(channel); + + if (chan.length) { + chan.click(); + return true; + } +} diff --git a/client/js/utils.js b/client/js/utils.js index 00bb3416..dc0bd7fa 100644 --- a/client/js/utils.js +++ b/client/js/utils.js @@ -5,6 +5,7 @@ const chat = $("#chat"); const input = $("#input"); module.exports = { + findCurrentNetworkChan, clear, confirmExit, forceFocus, @@ -15,6 +16,18 @@ module.exports = { toggleNotificationMarkers }; +function findCurrentNetworkChan(name) { + name = name.toLowerCase(); + + return $(".network .chan.active") + .parent(".network") + .find(".chan") + .filter(function() { + return $(this).data("title").toLowerCase() === name; + }) + .first(); +} + function resetHeight(element) { element.style.height = element.style.minHeight; }