From 629592d6410b2b754d07093f101afd0fcd1f3e7d Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Thu, 29 Jun 2017 12:19:37 +0300 Subject: [PATCH] Implement infinite scroll using IntersectionObserver --- client/js/render.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/client/js/render.js b/client/js/render.js index 7535e9aa..209cebbc 100644 --- a/client/js/render.js +++ b/client/js/render.js @@ -12,6 +12,11 @@ const condensed = require("./condensed"); const chat = $("#chat"); const sidebar = $("#sidebar"); +const historyObserver = window.IntersectionObserver ? + new window.IntersectionObserver(loadMoreHistory, { + root: chat.get(0) + }) : null; + module.exports = { appendMessage, buildChannelMessages, @@ -145,6 +150,10 @@ function renderChannel(data) { if (data.type === "channel") { renderChannelUsers(data); } + + if (historyObserver) { + historyObserver.observe(chat.find("#chan-" + data.id + " .show-more").get(0)); + } } function renderChannelMessages(data) { @@ -220,3 +229,19 @@ function renderNetworks(data) { utils.toggleNotificationMarkers(true); } } + +function loadMoreHistory(entries) { + entries.forEach((entry) => { + if (!entry.isIntersecting) { + return; + } + + var target = $(entry.target).find(".show-more-button"); + + if (target.attr("disabled")) { + return; + } + + target.click(); + }); +}