]> git.wh0rd.org - tt-rss.git/blobdiff - js/viewfeed.js
unfuck catchupbatchedarticles a bit
[tt-rss.git] / js / viewfeed.js
index c319a8ed02e2c40c3ea362841d3680a110696ede..e50e3dab8be1af4449fd1ddbd17614ca4454d091 100644 (file)
@@ -13,6 +13,9 @@ var catchup_timeout_id = false;
 var cids_requested = [];
 var loaded_article_ids = [];
 var _last_headlines_update = 0;
+var current_first_id = 0;
+
+var _catchup_request_sent = false;
 
 var has_storage = 'sessionStorage' in window && window['sessionStorage'] !== null;
 
@@ -45,7 +48,7 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
                                return;
                        }
 
-                       if (feed_id != getActiveFeedId() || is_cat != activeFeedIsCat())
+                       if (feed_id != -7 && (feed_id != getActiveFeedId() || is_cat != activeFeedIsCat()))
                                return;
 
                        /* dijit.getEnclosingWidget(
@@ -56,7 +59,7 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
                                if (infscroll_req == false) {
                                        $("headlines-frame").scrollTop = 0;
 
-                                       Element.hide("floatingTitle");
+                                       $("floatingTitle").style.visibility = "hidden";
                                        $("floatingTitle").setAttribute("rowid", 0);
                                        $("floatingTitle").innerHTML = "";
                                }
@@ -77,6 +80,7 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
                                _infscroll_disable = 0;
                        }
 
+                       current_first_id = reply['headlines']['first_id'];
                        var counters = reply['counters'];
                        var articles = reply['articles'];
                        //var runtime_info = reply['runtime-info'];
@@ -84,22 +88,16 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
                        if (infscroll_req == false) {
                                loaded_article_ids = [];
 
-                               dijit.byId("headlines-frame").attr('content',
-                                       reply['headlines']['content']);
-
-                               //dijit.byId("headlines-toolbar").attr('content',
-                               //      reply['headlines']['toolbar']);
-
                                dojo.html.set($("headlines-toolbar"),
                                                reply['headlines']['toolbar'],
                                                {parseContent: true});
 
-                               $$("#headlines-frame > div[id*=RROW]").each(function(row) {
-                                       if (loaded_article_ids.indexOf(row.id) != -1) {
-                                               row.parentNode.removeChild(row);
-                                       } else {
-                                               loaded_article_ids.push(row.id);
-                                       }
+                               dojo.html.set($("headlines-frame"),
+                                       reply['headlines']['content'],
+                                       {parseContent: true});
+
+                               $$("#headlines-frame div[id*='RROW']").each(function(row) {
+                                       loaded_article_ids.push(row.id);
                                });
 
                                var hsp = $("headlines-spacer");
@@ -125,40 +123,31 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
 
                                        var c = dijit.byId("headlines-frame");
                                        var ids = getSelectedArticleIds2();
-                                       var new_elems = [];
-
-                                       $("headlines-tmp").innerHTML = reply['headlines']['content'];
 
                                        var hsp = $("headlines-spacer");
 
                                        if (hsp)
                                                c.domNode.removeChild(hsp);
 
-                                       $$("#headlines-tmp > div").each(function(row) {
-                                               if (row.className == 'cdmFeedTitle') {
-                                                       row.style.display = 'none';
-                                                       c.domNode.appendChild(row);
-                                                       new_elems.push(row);
-                                               } else if (loaded_article_ids.indexOf(row.id) == -1) {
-                                                       row.style.display = 'none';
-                                                       c.domNode.appendChild(row);
-                                                       new_elems.push(row);
+                                       var tmp = new Element("div");
+                                       tmp.innerHTML = reply['headlines']['content'];
+                                       dojo.parser.parse(tmp);
+
+                                       while (tmp.hasChildNodes()) {
+                                               var row = tmp.removeChild(tmp.firstChild);
+
+                                               if (loaded_article_ids.indexOf(row.id) == -1 || row.hasClassName("cdmFeedTitle")) {
+                                                       dijit.byId("headlines-frame").domNode.appendChild(row);
+                                                       //Element.hide(row);
+                                                       //new Effect.Appear(row, {duration:0.5});
                                                        loaded_article_ids.push(row.id);
-                                               } else {
-                                                       row.parentNode.removeChild(row);
                                                }
-                                       });
+                                       }
 
                                        if (!hsp) hsp = new Element("DIV", {"id": "headlines-spacer"});
+                                       c.domNode.appendChild(hsp);
 
-//                                     if (getInitParam("cdm_auto_catchup") == 1) {
-                                               c.domNode.appendChild(hsp);
-//                                     }
-
-                                       console.log("added " + new_elems.size() + " headlines");
-
-                                       if (new_elems.size() == 0)
-                                               _infscroll_disable = true;
+                                       if (headlines_count < 30) _infscroll_disable = true;
 
                                        console.log("restore selected ids: " + ids);
 
@@ -168,20 +157,30 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
 
                                        initHeadlinesMenu();
 
-                                       new_elems.each(function(child) {
-                                               dojo.parser.parse(child);
-
-                                               if (!Element.visible(child))
-                                                       new Effect.Appear(child, { duration : 0.5 });
-                                       });
+                                       if (_infscroll_disable) {
+                                               hsp.innerHTML = "<a href='#' onclick='openNextUnreadFeed()'>" +
+                                               __("Click to open next unread feed.") + "</a>";
+                                       }
 
                                } else {
                                        console.log("no new headlines received");
 
+                                       var first_id_changed = reply['headlines']['first_id_changed'];
+                                       console.log("first id changed:" + first_id_changed);
+
                                        var hsp = $("headlines-spacer");
 
-                                       if (hsp) hsp.innerHTML = "<a href='#' onclick='openNextUnreadFeed()'>" +
-                                               __("Click to open next unread feed.") + "</a>";
+                                       if (hsp) {
+                                               if (first_id_changed) {
+                                                       hsp.innerHTML = "<a href='#' onclick='viewCurrentFeed()'>" +
+                                                       __("New articles found, reload feed to continue.") + "</a>";
+                                               } else {
+                                                       hsp.innerHTML = "<a href='#' onclick='openNextUnreadFeed()'>" +
+                                                       __("Click to open next unread feed.") + "</a>";
+                                               }
+
+                                       }
+
                                }
                        }
 
@@ -239,9 +238,8 @@ function render_article(article) {
                        c.domNode.scrollTop = 0;
                } catch (e) { };
 
-               PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED, article);
-
                c.attr('content', article);
+               PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED, c.domNode);
 
                correctHeadlinesOffset(getActiveArticleId());
 
@@ -330,18 +328,6 @@ function article_callback2(transport, id) {
                var unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length
                request_counters(unread_in_buffer == 0);
 
-               //headlines_scroll_handler($("headlines-frame"));
-
-/*             try {
-                       if (!_infscroll_disable &&
-                                       $$("#headlines-frame > div[id*=RROW]").last().hasClassName("Selected")) {
-
-                               loadMoreHeadlines();
-                       }
-               } catch (e) {
-                       console.warn(e);
-               } */
-
                notify("");
        } catch (e) {
                exception_error("article_callback2", e, transport);
@@ -407,19 +393,6 @@ function view(id, activefeed, noexpand) {
                        // if we don't need to request any relative ids, we might as well skip
                        // the server roundtrip altogether
                        if (cids_to_request.length == 0) {
-
-/*                             try {
-                                       if (!_infscroll_disable &&
-                                               $$("#headlines-frame > div[id*=RROW]").last().hasClassName("Selected")) {
-
-                                                       loadMoreHeadlines();
-                                       }
-                               } catch (e) {
-                                       console.warn(e);
-                               } */
-
-                               //headlines_scroll_handler($("headlines-frame"));
-
                                return;
                        }
                }
@@ -1196,7 +1169,7 @@ function editArticleTags(id) {
                                        }});
                                }
                        },
-                       href: query,
+                       href: query
                });
 
                var tmph = dojo.connect(dialog, 'onLoad', function() {
@@ -1259,10 +1232,11 @@ function unpackVisibleHeadlines() {
                                        var cencw = $("CENCW-" + child.id.replace("RROW-", ""));
 
                                        if (cencw) {
-                                               PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED_CDM, child);
-
                                                cencw.innerHTML = htmlspecialchars_decode(cencw.innerHTML);
                                                cencw.setAttribute('id', '');
+
+                                               PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED_CDM, child);
+
                                                Element.show(cencw);
                                        }
                                }
@@ -1307,25 +1281,15 @@ function headlines_scroll_handler(e) {
                }
 
                if (!_infscroll_disable) {
-                       if ((hsp && e.scrollTop + e.offsetHeight >= hsp.offsetTop - hsp.offsetHeight) ||
-                                       (e.scrollHeight != 0 &&
-                                               ((e.scrollTop + e.offsetHeight) / e.scrollHeight >= 0.7))) {
+                       if (hsp && hsp.offsetTop - 100 <= e.scrollTop + e.offsetHeight) {
 
-                               if (hsp)
-                                       hsp.innerHTML = "<span class='loading'><img src='images/indicator_tiny.gif'> " +
-                                               __("Loading, please wait...") + "</span>";
+                               hsp.innerHTML = "<span class='loading'><img src='images/indicator_tiny.gif'> " +
+                                       __("Loading, please wait...") + "</span>";
 
                                loadMoreHeadlines();
                                return;
 
                        }
-               } else {
-                       if (hsp)
-                               if (_infscroll_disable)
-                                       hsp.innerHTML = "<a href='#' onclick='openNextUnreadFeed()'>" +
-                                               __("Click to open next unread feed.") + "</a>";
-                               else
-                                       hsp.innerHTML = "";
                }
 
                if (isCdmMode()) {
@@ -1356,9 +1320,10 @@ function headlines_scroll_handler(e) {
                        if (catchup_id_batch.length > 0) {
                                window.clearTimeout(catchup_timeout_id);
 
-                               if (!_infscroll_request_sent) {
-                                       catchup_timeout_id = window.setTimeout('catchupBatchedArticles()',
-                                               500);
+                               catchup_timeout_id = window.setTimeout('catchupBatchedArticles()', 500);
+
+                               if (catchup_id_batch.length >= 10) {
+                                       catchupBatchedArticles();
                                }
                        }
 
@@ -1394,7 +1359,7 @@ function openNextUnreadFeed() {
 
 function catchupBatchedArticles() {
        try {
-               if (catchup_id_batch.length > 0 && !_infscroll_request_sent) {
+               if (catchup_id_batch.length > 0 && !_infscroll_request_sent && !_catchup_request_sent) {
 
                        // make a copy of the array
                        var batch = catchup_id_batch.slice();
@@ -1403,11 +1368,15 @@ function catchupBatchedArticles() {
 
                        console.log(query);
 
+                       _catchup_request_sent = true;
+
                        new Ajax.Request("backend.php", {
                                parameters: query,
                                onComplete: function(transport) {
                                        handle_rpc_json(transport);
 
+                                       _catchup_request_sent = false;
+
                                        reply = JSON.parse(transport.responseText);
                                        var batch = reply.ids;
 
@@ -1532,7 +1501,7 @@ function cdmCollapseArticle(event, id, unmark) {
                        if (row.offsetTop < $("headlines-frame").scrollTop)
                                scrollToRowId(row.id);
 
-                       Element.hide("floatingTitle");
+                       $("floatingTitle").style.visibility = "hidden";
                        $("floatingTitle").setAttribute("rowid", false);
                }
 
@@ -1543,6 +1512,8 @@ function cdmCollapseArticle(event, id, unmark) {
 
 function cdmExpandArticle(id, noexpand) {
        try {
+               if (getInitParam("cdm_expanded")) return;
+
                console.log("cdmExpandArticle " + id);
 
                if (!$("RROW-" + id)) return false;
@@ -1597,6 +1568,7 @@ function cdmExpandArticle(id, noexpand) {
 
                if (!noexpand)
                        toggleUnread(id, 0, true);
+
                toggleSelected(id);
                $("RROW-" + id).addClassName("active");
 
@@ -1736,8 +1708,11 @@ function dismissReadArticles() {
        }
 }
 
+// we don't really hide rows anymore
 function getVisibleArticleIds() {
-       var ids = [];
+       return getLoadedArticleIds();
+
+       /*var ids = [];
 
        try {
 
@@ -1751,7 +1726,7 @@ function getVisibleArticleIds() {
                exception_error("getVisibleArticleIds", e);
        }
 
-       return ids;
+       return ids; */
 }
 
 function cdmClicked(event, id) {
@@ -2088,12 +2063,12 @@ function headlinesMenuCommon(menu, base_id) {
 
                        menu.addChild(new dijit.PopupMenuItem({
                                label: __("Assign label"),
-                               popup: labelAddMenu,
+                               popup: labelAddMenu
                        }));
 
                        menu.addChild(new dijit.PopupMenuItem({
                                label: __("Remove label"),
-                               popup: labelDelMenu,
+                               popup: labelDelMenu
                        }));
 
                }
@@ -2123,7 +2098,7 @@ function initHeadlinesMenu() {
 
                var menu = new dijit.Menu({
                        id: "headlinesMenu",
-                       targetNodeIds: ids,
+                       targetNodeIds: ids
                });
 
                var tmph = dojo.connect(menu, '_openMyself', function (event) {
@@ -2158,7 +2133,7 @@ function initHeadlinesMenu() {
 
                        var menu = new dijit.Menu({
                                id: "headlinesFeedTitleMenu",
-                               targetNodeIds: ids,
+                               targetNodeIds: ids
                        });
 
                        var tmph = dojo.connect(menu, '_openMyself', function (event) {
@@ -2293,6 +2268,34 @@ function setSelectionScore() {
        }
 }
 
+function updateScore(id) {
+       try {
+               var pic = $$("#RROW-" + id + " .hlScorePic")[0];
+
+               if (pic) {
+
+                       var query = "op=article&method=getScore&id=" + param_escape(id);
+
+                       new Ajax.Request("backend.php", {
+                               parameters: query,
+                               onComplete: function(transport) {
+                                       console.log(transport.responseText);
+
+                                       var reply = JSON.parse(transport.responseText);
+
+                                       if (reply) {
+                                               pic.src = pic.src.replace(/score_.*?\.png/, reply["score_pic"]);
+                                               pic.setAttribute("score", reply["score"]);
+                                               pic.setAttribute("title", reply["score"]);
+                                       }
+                               } });
+               }
+
+       } catch (e) {
+               exception_error("updateScore", e);
+       }
+}
+
 function changeScore(id, pic) {
        try {
                var score = pic.getAttribute("score");
@@ -2312,6 +2315,7 @@ function changeScore(id, pic) {
                                        if (reply) {
                                                pic.src = pic.src.replace(/score_.*?\.png/, reply["score_pic"]);
                                                pic.setAttribute("score", new_score);
+                                               pic.setAttribute("title", new_score);
                                        }
                                } });
                }
@@ -2357,7 +2361,7 @@ function scrollToRowId(id) {
                var row = $(id);
 
                if (row)
-                       $("headlines-frame").scrollTop = row.offsetTop;
+                       $("headlines-frame").scrollTop = row.offsetTop - 4;
 
        } catch (e) {
                exception_error("scrollToRowId", e);
@@ -2402,11 +2406,12 @@ function updateFloatingTitle(unread_only) {
                                        PluginHost.run(PluginHost.HOOK_FLOATING_TITLE, child);
                                }
 
-                               if (child.offsetTop < hf.scrollTop - header.offsetHeight &&
-                                               child.offsetTop + child.offsetHeight - hf.scrollTop > header.offsetHeight)
-                                       Element.show("floatingTitle");
+                               $("floatingTitle").style.marginRight = hf.offsetWidth - child.offsetWidth + "px";
+                               if (header.offsetTop + header.offsetHeight < hf.scrollTop + $("floatingTitle").offsetHeight - 5 &&
+                                   child.offsetTop + child.offsetHeight >= hf.scrollTop + $("floatingTitle").offsetHeight - 5)
+                                       $("floatingTitle").style.visibility = "visible";
                                else
-                                       Element.hide("floatingTitle");
+                                       $("floatingTitle").style.visibility = "hidden";
 
                                return;
 
@@ -2417,3 +2422,7 @@ function updateFloatingTitle(unread_only) {
                exception_error("updateFloatingTitle", e);
        }
 }
+
+function cdmFooterClick(event) {
+       event.stopPropagation();
+}
\ No newline at end of file