From a418ba15e10fe047a7e75116788500cdc21ad9be Mon Sep 17 00:00:00 2001 From: Jordan Date: Fri, 10 Jan 2014 22:35:27 -0700 Subject: [PATCH] Add in Zixaphirs infinite scrolling --- builds/4chan-X.user.js | 34 ++++++++++++++++++++++++++++++++++ builds/crx/script.js | 34 ++++++++++++++++++++++++++++++++++ src/General/Index.coffee | 21 +++++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index 602ab9497..1a84bd3d8 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -2249,6 +2249,7 @@ this.searchInput = $('#index-search', this.navLinks); this.currentPage = this.getCurrentPage(); $.on(window, 'popstate', this.cb.popstate); + $.on(d, 'scroll', Index.scroll); $.on(this.pagelist, 'click', this.cb.pageNav); $.on(this.searchInput, 'input', this.onSearchInput); $.on($('#index-search-clear', this.navLinks), 'click', this.clearSearch); @@ -2273,6 +2274,39 @@ }); }); }, + scroll: $.debounce(100, function() { + var nodes, nodesPerPage, pageNum; + if (Conf['Index Mode'] !== 'paged' || ((d.body.scrollTop || doc.scrollTop) <= doc.scrollHeight - (300 + window.innerHeight))) { + return; + } + pageNum = Index.getCurrentPage() + 1; + if (pageNum >= Index.pagesNum) { + return Index.endNotice(); + } + nodesPerPage = Index.threadsNumPerPage * 2; + history.pushState(null, '', "/" + g.BOARD + "/" + pageNum); + nodes = Index.sortedNodes.slice(nodesPerPage * pageNum, nodesPerPage * (pageNum + 1)); + if (Conf['Show Replies']) { + Index.buildReplies(nodes); + } + $.add(Index.root, nodes); + return Index.setPage(); + }), + endNotice: (function() { + var notify, reset; + notify = false; + reset = function() { + return notify = false; + }; + return function() { + if (notify) { + return; + } + notify = true; + new Notice('info', "Last page reached.", 2); + return setTimeout(reset, 3 * $.SECOND); + }; + })(), cb: { mode: function() { Index.togglePagelist(); diff --git a/builds/crx/script.js b/builds/crx/script.js index 4aa074777..09e53f318 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -2259,6 +2259,7 @@ this.searchInput = $('#index-search', this.navLinks); this.currentPage = this.getCurrentPage(); $.on(window, 'popstate', this.cb.popstate); + $.on(d, 'scroll', Index.scroll); $.on(this.pagelist, 'click', this.cb.pageNav); $.on(this.searchInput, 'input', this.onSearchInput); $.on($('#index-search-clear', this.navLinks), 'click', this.clearSearch); @@ -2283,6 +2284,39 @@ }); }); }, + scroll: $.debounce(100, function() { + var nodes, nodesPerPage, pageNum; + if (Conf['Index Mode'] !== 'paged' || ((d.body.scrollTop || doc.scrollTop) <= doc.scrollHeight - (300 + window.innerHeight))) { + return; + } + pageNum = Index.getCurrentPage() + 1; + if (pageNum >= Index.pagesNum) { + return Index.endNotice(); + } + nodesPerPage = Index.threadsNumPerPage * 2; + history.pushState(null, '', "/" + g.BOARD + "/" + pageNum); + nodes = Index.sortedNodes.slice(nodesPerPage * pageNum, nodesPerPage * (pageNum + 1)); + if (Conf['Show Replies']) { + Index.buildReplies(nodes); + } + $.add(Index.root, nodes); + return Index.setPage(); + }), + endNotice: (function() { + var notify, reset; + notify = false; + reset = function() { + return notify = false; + }; + return function() { + if (notify) { + return; + } + notify = true; + new Notice('info', "Last page reached.", 2); + return setTimeout(reset, 3 * $.SECOND); + }; + })(), cb: { mode: function() { Index.togglePagelist(); diff --git a/src/General/Index.coffee b/src/General/Index.coffee index ab1f36b28..6f0f1e77c 100644 --- a/src/General/Index.coffee +++ b/src/General/Index.coffee @@ -79,6 +79,7 @@ Index = @searchInput = $ '#index-search', @navLinks @currentPage = @getCurrentPage() $.on window, 'popstate', @cb.popstate + $.on d, 'scroll', Index.scroll $.on @pagelist, 'click', @cb.pageNav $.on @searchInput, 'input', @onSearchInput $.on $('#index-search-clear', @navLinks), 'click', @clearSearch @@ -101,6 +102,26 @@ Index = $.asap (-> $('.pagelist') or d.readyState isnt 'loading'), -> $.replace $('.pagelist'), Index.pagelist + scroll: $.debounce 100, -> + return if Conf['Index Mode'] isnt 'paged' or ((d.body.scrollTop or doc.scrollTop) <= doc.scrollHeight - (300 + window.innerHeight)) + pageNum = Index.getCurrentPage() + 1 + return Index.endNotice() if pageNum >= Index.pagesNum + nodesPerPage = Index.threadsNumPerPage * 2 + history.pushState null, '', "/#{g.BOARD}/#{pageNum}" + nodes = Index.sortedNodes[nodesPerPage * pageNum ... nodesPerPage * (pageNum + 1)] + Index.buildReplies nodes if Conf['Show Replies'] + $.add Index.root, nodes + Index.setPage() + + endNotice: do -> + notify = false + reset = -> notify = false + return -> + return if notify + notify = true + new Notice 'info', "Last page reached.", 2 + setTimeout reset, 3 * $.SECOND + cb: mode: -> Index.togglePagelist()