From d55420faaec4446d58b882de811b688847739da2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Sun, 8 Apr 2018 17:49:00 -0400 Subject: [PATCH] Bind a single resize event, debounced, instead of one handler per preview --- client/js/renderPreview.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/client/js/renderPreview.js b/client/js/renderPreview.js index 47364883..423d6398 100644 --- a/client/js/renderPreview.js +++ b/client/js/renderPreview.js @@ -9,6 +9,8 @@ const socket = require("./socket"); const templates = require("../views"); const chat = $("#chat"); +const {togglePreviewMoreButtonsIfNeeded} = require("./utils"); + module.exports = renderPreview; function renderPreview(preview, msg) { @@ -90,13 +92,6 @@ function appendPreview(preview, msg, template) { // "More" button only applies on text previews if (preview.type === "link") { - // On resize, only touch previews in the current channel that are expanded - $(window).on("resize", debounce(() => { - if (channel.hasClass("active") && previewContent.hasClass("show")) { - showMoreIfNeeded(); - } - }, 150)); - // This event is triggered when a side menu is opened/closed, or when the // preview gets expanded/collapsed. previewContent.on("showMoreIfNeeded", @@ -114,6 +109,10 @@ function appendPreview(preview, msg, template) { } } +// On resize, previews in the current channel that are expanded need to compute +// their "More" button. Debounced handler to avoid performance cost. +$(window).on("resize", debounce(togglePreviewMoreButtonsIfNeeded, 150)); + $("#chat").on("click", ".text .toggle-button", function() { const self = $(this); const container = self.closest(".chat");