From bb1e3ee9177a76c54b5ea70511055fb385a4ffe5 Mon Sep 17 00:00:00 2001 From: realies Date: Sat, 2 Sep 2017 19:28:36 +0300 Subject: [PATCH 1/5] 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; } From 3890aaad6bb4d207a7f5923a330a263da2009161 Mon Sep 17 00:00:00 2001 From: realies Date: Tue, 3 Oct 2017 23:29:19 +0300 Subject: [PATCH 2/5] Use jQuery for parsing, isolate user commands, remove /clear --- client/js/lounge.js | 13 ++++++------- client/js/utils.js | 12 +----------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/client/js/lounge.js b/client/js/lounge.js index 2ffa9f6d..b04471b3 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -185,19 +185,18 @@ $(function() { resetInputHeight(input.get(0)); 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) : ""; - if (typeof utils[cmd] === "function") { + const args = text.substr(1).split(" "); + const cmd = args.shift().toLowerCase(); + if (typeof utils.inputCommands[cmd] === "function") { if (cmd === "join") { - const channel = parameters.split(" ")[0]; + const channel = args.shift(); if (channel !== "") { - if (utils[cmd](channel)) { + if (utils.inputCommands[cmd](channel)) { return; } } } else { - if (utils[cmd]()) { + if (utils.inputCommands[cmd]()) { return; } } diff --git a/client/js/utils.js b/client/js/utils.js index 2eae9a29..b916dfc3 100644 --- a/client/js/utils.js +++ b/client/js/utils.js @@ -8,11 +8,8 @@ var serverHash = -1; var lastMessageId = -1; module.exports = { + inputCommands: { collapse, expand, join }, findCurrentNetworkChan, - clear, - collapse, - expand, - join, serverHash, lastMessageId, confirmExit, @@ -47,13 +44,6 @@ function forceFocus() { input.trigger("click").focus(); } -function clear() { - chat.find(".active") - .find(".show-more").addClass("show").end() - .find(".messages .msg, .date-marker-container").remove(); - return true; -} - function collapse() { $(".chan.active .toggle-button.opened").click(); return true; From 3244ec91e8a8fd6c9e2a79d743f56c2003c3074f Mon Sep 17 00:00:00 2001 From: realies Date: Tue, 3 Oct 2017 23:59:19 +0300 Subject: [PATCH 3/5] Remove unused var, satisfy linter rules, simplify calls --- client/js/lounge.js | 13 ++----------- client/js/utils.js | 17 +++++++++-------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/client/js/lounge.js b/client/js/lounge.js index b04471b3..24790c6a 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -188,17 +188,8 @@ $(function() { const args = text.substr(1).split(" "); const cmd = args.shift().toLowerCase(); if (typeof utils.inputCommands[cmd] === "function") { - if (cmd === "join") { - const channel = args.shift(); - if (channel !== "") { - if (utils.inputCommands[cmd](channel)) { - return; - } - } - } else { - if (utils.inputCommands[cmd]()) { - return; - } + if (utils.inputCommands[cmd](args)) { + return; } } } diff --git a/client/js/utils.js b/client/js/utils.js index b916dfc3..04974c13 100644 --- a/client/js/utils.js +++ b/client/js/utils.js @@ -1,14 +1,13 @@ "use strict"; const $ = require("jquery"); -const chat = $("#chat"); const input = $("#input"); var serverHash = -1; var lastMessageId = -1; module.exports = { - inputCommands: { collapse, expand, join }, + inputCommands: {collapse, expand, join}, findCurrentNetworkChan, serverHash, lastMessageId, @@ -54,12 +53,14 @@ function expand() { return true; } -function join(channel) { - var chan = findCurrentNetworkChan(channel); - - if (chan.length) { - chan.click(); - return true; +function join(args) { + const channel = args[0]; + if (channel) { + const chan = findCurrentNetworkChan(channel); + if (chan.length) { + chan.click(); + return true; + } } } From 960a6d42e1ee90dd2e162ef6c0aace2dab78d548 Mon Sep 17 00:00:00 2001 From: realies Date: Wed, 4 Oct 2017 13:31:02 +0300 Subject: [PATCH 4/5] Combine if statements to reduce nesting --- client/js/lounge.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/client/js/lounge.js b/client/js/lounge.js index 24790c6a..128e2ca0 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -187,10 +187,8 @@ $(function() { if (text.indexOf("/") === 0) { const args = text.substr(1).split(" "); const cmd = args.shift().toLowerCase(); - if (typeof utils.inputCommands[cmd] === "function") { - if (utils.inputCommands[cmd](args)) { - return; - } + if (typeof utils.inputCommands[cmd] === "function" && utils.inputCommands[cmd](args)) { + return; } } From 9a348e3eeb121268a848e0e9e05465508a7c95b4 Mon Sep 17 00:00:00 2001 From: realies Date: Thu, 5 Oct 2017 20:12:26 +0300 Subject: [PATCH 5/5] indexOf to charAt --- client/js/lounge.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/js/lounge.js b/client/js/lounge.js index 128e2ca0..df49e132 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -184,7 +184,7 @@ $(function() { input.val(""); resetInputHeight(input.get(0)); - if (text.indexOf("/") === 0) { + if (text.charAt(0) === "/") { const args = text.substr(1).split(" "); const cmd = args.shift().toLowerCase(); if (typeof utils.inputCommands[cmd] === "function" && utils.inputCommands[cmd](args)) {