From 53a2101de367c486047bc449a5010f7c6f8ce629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Sat, 7 Apr 2018 22:48:04 -0400 Subject: [PATCH] Make sure preview "More" button computation does nothing when preview is collapsed --- client/js/renderPreview.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/client/js/renderPreview.js b/client/js/renderPreview.js index 0887ac02..de1b1301 100644 --- a/client/js/renderPreview.js +++ b/client/js/renderPreview.js @@ -70,12 +70,16 @@ function appendPreview(preview, msg, template) { previewContainer.append(template); const moreBtn = previewContainer.find(".more"); - const previewContent = previewContainer.find(".toggle-content")[0]; + const previewContent = previewContainer.find(".toggle-content"); const showMoreIfNeeded = () => { - if (preview.type === "link" && channel.hasClass("active")) { + // Only applies on: + if (preview.type === "link" && // link previews + channel.hasClass("active") && // in the current channels + previewContent.hasClass("show") // that are expanded + ) { const isVisible = moreBtn.is(":visible"); - const shouldShow = previewContent.offsetWidth >= previewContainer[0].offsetWidth; + const shouldShow = previewContent[0].offsetWidth >= previewContainer[0].offsetWidth; if (!isVisible && shouldShow) { moreBtn.show(); @@ -88,6 +92,7 @@ function appendPreview(preview, msg, template) { $(window).on("resize", debounce(showMoreIfNeeded, 150)); window.requestAnimationFrame(showMoreIfNeeded); + previewContent.on("showMoreIfNeeded", showMoreIfNeeded); if (activeChannelId === channelId) { container.trigger("keepToBottom"); @@ -104,6 +109,12 @@ $("#chat").on("click", ".text .toggle-button", function() { self.toggleClass("opened"); content.toggleClass("show"); + const isExpanded = content.hasClass("show"); + + if (isExpanded) { + content.trigger("showMoreIfNeeded"); + } + // Tell the server we're toggling so it remembers at page reload // TODO Avoid sending many single events when using `/collapse` or `/expand` // See https://github.com/thelounge/thelounge/issues/1377 @@ -111,7 +122,7 @@ $("#chat").on("click", ".text .toggle-button", function() { target: parseInt(self.closest(".chan").data("id"), 10), msgId: parseInt(self.closest(".msg").prop("id").replace("msg-", ""), 10), link: self.data("url"), - shown: content.hasClass("show"), + shown: isExpanded, }); // If scrollbar was at the bottom before toggling the preview, keep it at the bottom