]> git.wh0rd.org - tt-rss.git/blobdiff - js/viewfeed.js
scroll handler: performance improvements
[tt-rss.git] / js / viewfeed.js
index a24116e8808e777975ad2c3aa141415d6c1c5e6d..6ae9cdd8a503fe42cd51c65775de293d81e41d2c 100755 (executable)
@@ -490,7 +490,6 @@ function moveToPost(mode, noscroll, noexpand) {
                                        scrollArticle(ctr.offsetHeight/4);
 
                                } else if (next_id) {
-                                       cdmExpandArticle(next_id, noexpand);
                                        cdmScrollToArticleId(next_id, true);
                                }
 
@@ -509,24 +508,14 @@ function moveToPost(mode, noscroll, noexpand) {
                                const prev_article = $("RROW-" + prev_id);
                                var ctr = $("headlines-frame");
 
-                               if (!getInitParam("cdm_expanded")) {
-
-                                       if (!noscroll && article && article.offsetTop < ctr.scrollTop) {
-                                               scrollArticle(-ctr.offsetHeight/4);
-                                       } else {
-                                               cdmExpandArticle(prev_id, noexpand);
-                                               cdmScrollToArticleId(prev_id, true);
-                                       }
-                               } else if (!noscroll && article && article.offsetTop < ctr.scrollTop) {
-                                               scrollArticle(-ctr.offsetHeight/3);
-                                       } else if (!noscroll && prev_article &&
-                                                       prev_article.offsetTop < ctr.scrollTop) {
-                                               cdmExpandArticle(prev_id, noexpand);
-                                               scrollArticle(-ctr.offsetHeight/4);
-                                       } else if (prev_id) {
-                                               cdmExpandArticle(prev_id, noexpand);
-                                               cdmScrollToArticleId(prev_id, noscroll);
-                                       }
+                               if (!noscroll && article && article.offsetTop < ctr.scrollTop) {
+                                       scrollArticle(-ctr.offsetHeight/3);
+                               } else if (!noscroll && prev_article &&
+                                               prev_article.offsetTop < ctr.scrollTop) {
+                                       scrollArticle(-ctr.offsetHeight/4);
+                               } else if (prev_id) {
+                                       cdmScrollToArticleId(prev_id, noscroll);
+                               }
 
                        } else if (prev_id) {
                                correctHeadlinesOffset(prev_id);
@@ -1016,30 +1005,26 @@ function postMouseOut(id) {
 }
 
 function unpackVisibleHeadlines() {
-       if (!isCdmMode() || !getInitParam("cdm_expanded")) return;
+       if (!isCdmMode()) return;
 
-    $$("#headlines-frame div[id*=RROW][data-content]").each((row) => {
-       //console.log('checking', row.id);
+    const rows = $$("#headlines-frame div[id*=RROW][data-content]");
 
-       if (row.offsetTop <= $("headlines-frame").scrollTop + $("headlines-frame").offsetHeight) {
-            console.log("unpacking: " + row.id);
+    for (let i = 0; i < rows.length; i++) {
+        const row = rows[i];
 
-            let content;
+        if (row.offsetTop <= $("headlines-frame").scrollTop + $("headlines-frame").offsetHeight) {
+            console.log("unpacking: " + row.id);
 
-            try {
-                content = JSON.parse(row.getAttribute("data-content"));
-            } catch (e) {
-               content = "Error decoding content: " + row.getAttribute("data-content");
-                       }
+            const content = row.getAttribute("data-content");
 
-                       row.select(".cdmContentInner")[0].innerHTML = content;
+            row.select(".cdmContentInner")[0].innerHTML = content;
             row.removeAttribute("data-content");
 
             PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED_CDM, row);
-               } else {
-               throw $break;
-               }
-    });
+        } else {
+            break;
+        }
+    }
 }
 
 function headlines_scroll_handler(e) {
@@ -1052,30 +1037,27 @@ function headlines_scroll_handler(e) {
 
                _headlines_scroll_offset = e.scrollTop;
 
-               const hsp = $("headlines-spacer");
-
                unpackVisibleHeadlines();
 
                // set topmost child in the buffer as active
                if (isCdmMode() && getInitParam("cdm_auto_catchup") == 1 &&
-                               getSelectedArticleIds2().length <= 1 &&
-                               getInitParam("cdm_expanded")) {
+                               getSelectedArticleIds2().length <= 1) {
 
                        const rows = $$("#headlines-frame > div[id*=RROW]");
 
                        for (let i = 0; i < rows.length; i++) {
-                               const child = rows[i];
+                               const row = rows[i];
 
-                               if ($("headlines-frame").scrollTop <= child.offsetTop &&
-                                       child.offsetTop - $("headlines-frame").scrollTop < 100 &&
-                                       child.getAttribute("data-article-id") != _active_article_id) {
+                               if ($("headlines-frame").scrollTop <= row.offsetTop &&
+                                       row.offsetTop - $("headlines-frame").scrollTop < 100 &&
+                                       row.getAttribute("data-article-id") != _active_article_id) {
 
                                        if (_active_article_id) {
                                                const row = $("RROW-" + _active_article_id);
                                                if (row) row.removeClassName("active");
                                        }
 
-                                       _active_article_id = child.getAttribute("data-article-id");
+                                       _active_article_id = row.getAttribute("data-article-id");
                                        showArticleInHeadlines(_active_article_id, true);
                                        updateSelectedPrompt();
                                        break;
@@ -1084,6 +1066,8 @@ function headlines_scroll_handler(e) {
                }
 
                if (!_infscroll_disable) {
+            const hsp = $("headlines-spacer");
+
                        if (hsp && hsp.offsetTop - 250 <= e.scrollTop + e.offsetHeight) {
 
                                hsp.innerHTML = "<span class='loading'><img src='images/indicator_tiny.gif'> " +
@@ -1099,33 +1083,31 @@ function headlines_scroll_handler(e) {
                        updateFloatingTitle();
                }
 
-               catchupCurrentBatchIfNeeded();
-
                if (getInitParam("cdm_auto_catchup") == 1) {
 
-                       // let's get DOM some time to settle down
-                       const ts = new Date().getTime();
-                       if (ts - _last_headlines_update < 100) return;
+                       let rows = $$("#headlines-frame > div[id*=RROW][class*=Unread]");
 
-                       $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
-                               function(child) {
-                                       if ($("headlines-frame").scrollTop > (child.offsetTop + child.offsetHeight/2)) {
-
-                                               const id = child.getAttribute("data-article-id")
+                       for (let i = 0; i < rows.length; i++) {
+                const row = rows[i];
+                               
+                               if ($("headlines-frame").scrollTop > (row.offsetTop + row.offsetHeight/2)) {
 
-                                               if (catchup_id_batch.indexOf(id) == -1)
-                                                       catchup_id_batch.push(id);
+                    const id = row.getAttribute("data-article-id")
 
-                                               //console.log("auto_catchup_batch: " + catchup_id_batch.toString());
-                                       }
+                    if (catchup_id_batch.indexOf(id) == -1)
+                        catchup_id_batch.push(id);
 
-                               });
+                    //console.log("auto_catchup_batch: " + catchup_id_batch.toString());
+                } else {
+                                       break;
+                               }
+            }
 
                        if (_infscroll_disable) {
-                               const child = $$("#headlines-frame div[id*=RROW]").last();
+                               const row = $$("#headlines-frame div[id*=RROW]").last();
 
-                               if (child && $("headlines-frame").scrollTop >
-                                               (child.offsetTop + child.offsetHeight - 50)) {
+                               if (row && $("headlines-frame").scrollTop >
+                                               (row.offsetTop + row.offsetHeight - 50)) {
 
                                        console.log("we seem to be at an end");
 
@@ -1150,7 +1132,7 @@ function openNextUnreadFeed() {
 function catchupBatchedArticles() {
        if (catchup_id_batch.length > 0 && !_infscroll_request_sent && !_catchup_request_sent) {
 
-               console.log("catchupBatchedArticles: working");
+               console.log("catchupBatchedArticles, size=", catchup_id_batch.length);
 
                // make a copy of the array
                const batch = catchup_id_batch.slice();
@@ -1168,7 +1150,6 @@ function catchupBatchedArticles() {
                 const batch = reply.ids;
 
                 batch.each(function (id) {
-                    console.log(id);
                     const elem = $("RROW-" + id);
                     if (elem) elem.removeClassName("Unread");
                     catchup_id_batch.remove(id);
@@ -1241,111 +1222,6 @@ function catchupRelativeToArticle(below, id) {
        }
 }
 
-function cdmCollapseArticle(event, id, unmark) {
-       if (unmark == undefined) unmark = true;
-
-       const row = $("RROW-" + id);
-       const elem = $("CICD-" + id);
-
-       if (elem && row) {
-               const collapse = row.select("span[class='collapseBtn']")[0];
-
-               Element.hide(elem);
-               Element.show("CEXC-" + id);
-               Element.hide(collapse);
-
-               if (unmark) {
-                       row.removeClassName("active");
-
-                       markHeadline(id, false);
-
-                       if (id == getActiveArticleId()) {
-                               setActiveArticleId(0);
-                       }
-
-                       updateSelectedPrompt();
-               }
-
-               if (event) Event.stop(event);
-
-               PluginHost.run(PluginHost.HOOK_ARTICLE_COLLAPSED, id);
-
-               if (row.offsetTop < $("headlines-frame").scrollTop)
-                       scrollToRowId(row.id);
-
-               $("floatingTitle").style.visibility = "hidden";
-               $("floatingTitle").setAttribute("data-article-id", 0);
-       }
-}
-
-function cdmExpandArticle(id, noexpand) {
-       console.log("cdmExpandArticle " + id);
-
-       const row = $("RROW-" + id);
-
-       if (!row) return false;
-
-       const oldrow = $("RROW-" + getActiveArticleId());
-
-       let elem = $("CICD-" + getActiveArticleId());
-
-       if (id == getActiveArticleId() && Element.visible(elem))
-               return true;
-
-       selectArticles("none");
-
-       const old_offset = row.offsetTop;
-
-       if (getActiveArticleId() && elem && !getInitParam("cdm_expanded")) {
-               let collapse = oldrow.select("span[class='collapseBtn']")[0];
-
-               Element.hide(elem);
-               Element.show("CEXC-" + getActiveArticleId());
-               Element.hide(collapse);
-       }
-
-       if (oldrow) oldrow.removeClassName("active");
-
-       setActiveArticleId(id);
-
-       elem = $("CICD-" + id);
-
-       let collapse = row.select("span[class='collapseBtn']")[0];
-
-       const cencw = $("CENCW-" + id);
-
-       if (!Element.visible(elem) && !noexpand) {
-               if (cencw) {
-                       cencw.innerHTML = htmlspecialchars_decode(cencw.innerHTML);
-                       cencw.setAttribute('id', '');
-                       Element.show(cencw);
-               }
-
-               Element.show(elem);
-               Element.hide("CEXC-" + id);
-               Element.show(collapse);
-       }
-
-       const new_offset = row.offsetTop;
-
-       if (old_offset > new_offset)
-               $("headlines-frame").scrollTop -= (old_offset - new_offset);
-
-       if (!noexpand) {
-               if (catchup_id_batch.indexOf(id) == -1)
-                       catchup_id_batch.push(id);
-
-               catchupCurrentBatchIfNeeded();
-       }
-
-       toggleSelected(id);
-       row.addClassName("active");
-
-       PluginHost.run(PluginHost.HOOK_ARTICLE_EXPANDED, id);
-
-       return false;
-}
-
 function getArticleUnderPointer() {
        return post_under_pointer;
 }
@@ -1382,42 +1258,37 @@ function cdmClicked(event, id, in_body) {
 
        if (!event.ctrlKey && !event.metaKey) {
 
-               if (!getInitParam("cdm_expanded")) {
-                       return cdmExpandArticle(id);
-               } else {
-
-                       let elem = $("RROW-" + getActiveArticleId());
-
-                       if (elem) elem.removeClassName("active");
+               let elem = $("RROW-" + getActiveArticleId());
 
-                       selectArticles("none");
-                       toggleSelected(id);
+               if (elem) elem.removeClassName("active");
 
-                       elem = $("RROW-" + id);
-                       const article_is_unread = elem.hasClassName("Unread");
+               selectArticles("none");
+               toggleSelected(id);
 
-                       elem.removeClassName("Unread");
-                       elem.addClassName("active");
+               elem = $("RROW-" + id);
+               const article_is_unread = elem.hasClassName("Unread");
 
-                       setActiveArticleId(id);
+               elem.removeClassName("Unread");
+               elem.addClassName("active");
 
-                       if (article_is_unread) {
-                decrementFeedCounter(getActiveFeedId(), activeFeedIsCat());
-                updateFloatingTitle(true);
+               setActiveArticleId(id);
 
-                const query = {
-                    op: "rpc", method: "catchupSelected",
-                    cmode: 0, ids: id
-                };
+               if (article_is_unread) {
+                       decrementFeedCounter(getActiveFeedId(), activeFeedIsCat());
+                       updateFloatingTitle(true);
 
-                xhrPost("backend.php", query, (transport) => {
-                    handle_rpc_json(transport);
-                });
-            }
+                       const query = {
+                               op: "rpc", method: "catchupSelected",
+                               cmode: 0, ids: id
+                       };
 
-                       return !event.shiftKey;
+                       xhrPost("backend.php", query, (transport) => {
+                               handle_rpc_json(transport);
+                       });
                }
 
+               return !event.shiftKey;
+
        } else if (!in_body) {
 
                toggleSelected(id, true);
@@ -1929,14 +1800,3 @@ function updateFloatingTitle(unread_only) {
                }
        }
 }
-
-function catchupCurrentBatchIfNeeded() {
-       if (catchup_id_batch.length > 0) {
-               window.clearTimeout(catchup_timeout_id);
-               catchup_timeout_id = window.setTimeout(catchupBatchedArticles, 1000);
-
-               if (catchup_id_batch.length >= 10) {
-                       catchupBatchedArticles();
-               }
-       }
-}