]> git.wh0rd.org - tt-rss.git/blobdiff - js/viewfeed.js
fix infinite scrolling for search results
[tt-rss.git] / js / viewfeed.js
index 363121b198b54a90bd81c1b9a97695c6f24c6926..cb0a759603c15a393f2bb502d45df408e94f87ff 100644 (file)
@@ -80,6 +80,11 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
                                dijit.byId("headlines-toolbar").attr('content',
                                        reply['headlines']['toolbar']);
 
+                               $$("#headlines-frame > div[id*=RROW]").each(function(row) {
+                                       if ($$("#headlines-frame DIV[id="+row.id+"]").length > 1) {
+                                               row.parentNode.removeChild(row);
+                                       }
+                               });
 
                                if (getInitParam("cdm_auto_catchup") == 1) {
                                        var hsp = $("headlines-spacer");
@@ -92,10 +97,11 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
                        } else {
 
                                if (headlines_count > 0 && feed_id == getActiveFeedId() && is_cat == activeFeedIsCat()) {
-                                       console.log("adding some more headlines...");
+                                       console.log("adding some more headlines: " + headlines_count);
 
                                        var c = dijit.byId("headlines-frame");
                                        var ids = getSelectedArticleIds2();
+                                       var num_added = 0;
 
                                        $("headlines-tmp").innerHTML = reply['headlines']['content'];
 
@@ -108,6 +114,7 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
                                                if ($$("#headlines-frame DIV[id="+row.id+"]").length == 0) {
                                                        row.style.display = 'none';
                                                        c.domNode.appendChild(row);
+                                                       ++num_added;
                                                } else {
                                                        row.parentNode.removeChild(row);
                                                }
@@ -121,6 +128,11 @@ function headlines_callback2(transport, offset, background, infscroll_req) {
                                                c.domNode.appendChild(hsp);
                                        }
 
+                                       console.log("added " + num_added + " headlines");
+
+                                       if (num_added == 0)
+                                               _infscroll_disable = true;
+
                                        console.log("restore selected ids: " + ids);
 
                                        for (var i = 0; i < ids.length; i++) {
@@ -296,7 +308,9 @@ function article_callback2(transport, id) {
 
                request_counters();
 
-               try {
+               headlines_scroll_handler($("headlines-frame"));
+
+/*             try {
                        if (!_infscroll_disable &&
                                        $$("#headlines-frame > div[id*=RROW]").last().hasClassName("Selected")) {
 
@@ -304,7 +318,7 @@ function article_callback2(transport, id) {
                        }
                } catch (e) {
                        console.warn(e);
-               }
+               } */
 
                notify("");
        } catch (e) {
@@ -322,7 +336,7 @@ function view(id) {
 
                hideAuxDlg();
 
-               var query = "?op=view&id=" + param_escape(id);
+               var query = "?op=article&method=view&id=" + param_escape(id);
 
                var neighbor_ids = getRelativePostIds(id);
 
@@ -373,7 +387,7 @@ function view(id) {
                        // the server roundtrip altogether
                        if (cids_to_request.length == 0) {
 
-                               try {
+/*                             try {
                                        if (!_infscroll_disable &&
                                                $$("#headlines-frame > div[id*=RROW]").last().hasClassName("Selected")) {
 
@@ -381,7 +395,9 @@ function view(id) {
                                        }
                                } catch (e) {
                                        console.warn(e);
-                               }
+                               } */
+
+                               headlines_scroll_handler($("headlines-frame"));
 
                                return;
                        }
@@ -391,6 +407,10 @@ function view(id) {
 
                console.log(query);
 
+               if (article_is_unread) {
+                       decrementFeedCounter(getActiveFeedId(), activeFeedIsCat());
+               }
+
                new Ajax.Request("backend.php", {
                        parameters: query,
                        onComplete: function(transport) {
@@ -493,13 +513,22 @@ function moveToPost(mode) {
                }
 
                if (active_post_id == false) {
-                       next_id = getFirstVisibleHeadlineId();
-                       prev_id = getLastVisibleHeadlineId();
+                       next_id = rows[0];
+                       prev_id = rows[rows.length-1]
                } else {
                        for (var i = 0; i < rows.length; i++) {
                                if (rows[i] == active_post_id) {
-                                       prev_id = rows[i-1];
-                                       next_id = rows[i+1];
+
+                                       // Account for adjacent identical article ids.
+                                       if (i > 0) prev_id = rows[i-1];
+
+                                       for (var j = i+1; j < rows.length; j++) {
+                                               if (rows[j] != active_post_id) {
+                                                       next_id = rows[j];
+                                                       break;
+                                               }
+                                       }
+                                       break;
                                }
                        }
                }
@@ -825,7 +854,7 @@ function getLoadedArticleIds() {
 
 }
 
-// mode = all,none,unread,invert
+// mode = all,none,unread,invert,marked,published
 function selectArticles(mode) {
        try {
 
@@ -846,6 +875,27 @@ function selectArticles(mode) {
                                        child.removeClassName("Selected");
                                        cb.checked = false;
                                }
+                       } else if (mode == "marked") {
+                               var img = $("FMPIC-" + child.id.replace("RROW-", ""));
+
+                               if (img && img.src.match("mark_set")) {
+                                       child.addClassName("Selected");
+                                       cb.checked = true;
+                               } else {
+                                       child.removeClassName("Selected");
+                                       cb.checked = false;
+                               }
+                       } else if (mode == "published") {
+                               var img = $("FPPIC-" + child.id.replace("RROW-", ""));
+
+                               if (img && img.src.match("pub_set")) {
+                                       child.addClassName("Selected");
+                                       cb.checked = true;
+                               } else {
+                                       child.removeClassName("Selected");
+                                       cb.checked = false;
+                               }
+
                        } else if (mode == "invert") {
                                if (child.hasClassName("Selected")) {
                                        child.removeClassName("Selected");
@@ -1006,7 +1056,7 @@ function catchupSelection() {
 }
 
 function editArticleTags(id) {
-               var query = "backend.php?op=dlg&id=editArticleTags&param=" + param_escape(id);
+               var query = "backend.php?op=dlg&method=editArticleTags&param=" + param_escape(id);
 
                if (dijit.byId("editTagsDlg"))
                        dijit.byId("editTagsDlg").destroyRecursive();
@@ -1091,8 +1141,9 @@ function headlines_scroll_handler(e) {
                var hsp = $("headlines-spacer");
 
                if (!_infscroll_disable) {
-                       if (hsp && (e.scrollTop + e.offsetHeight > hsp.offsetTop) ||
-                                       e.scrollTop + e.offsetHeight > e.scrollHeight - 100) {
+                       if ((hsp && e.scrollTop + e.offsetHeight >= hsp.offsetTop - hsp.offsetHeight) ||
+                                       (e.scrollHeight != 0 &&
+                                               ((e.scrollTop + e.offsetHeight) / e.scrollHeight >= 0.7))) {
 
                                if (hsp)
                                        hsp.innerHTML = "<img src='images/indicator_tiny.gif'> " +
@@ -1141,20 +1192,24 @@ function catchupBatchedArticles() {
        try {
                if (catchup_id_batch.length > 0 && !_infscroll_request_sent) {
 
+                       // make a copy of the array
+                       var batch = catchup_id_batch.slice();
                        var query = "?op=rpc&method=catchupSelected" +
-                               "&cmode=0&ids=" + param_escape(catchup_id_batch.toString());
+                               "&cmode=0&ids=" + param_escape(batch.toString());
+
+                       console.log(query);
 
                        new Ajax.Request("backend.php", {
                                parameters: query,
                                onComplete: function(transport) {
                                        handle_rpc_json(transport);
 
-                                       catchup_id_batch.each(function(id) {
+                                       batch.each(function(id) {
                                                var elem = $("RROW-" + id);
                                                if (elem) elem.removeClassName("Unread");
+                                               catchup_id_batch.remove(id);
                                        });
 
-                                       catchup_id_batch = [];
                                } });
                }
 
@@ -1498,76 +1553,6 @@ function show_labels_in_headlines(transport) {
        }
 } */
 
-function emailArticle(id) {
-       try {
-               if (!id) {
-                       var ids = getSelectedArticleIds2();
-
-                       if (ids.length == 0) {
-                               alert(__("No articles are selected."));
-                               return;
-                       }
-
-                       id = ids.toString();
-               }
-
-               if (dijit.byId("emailArticleDlg"))
-                       dijit.byId("emailArticleDlg").destroyRecursive();
-
-               var query = "backend.php?op=dlg&id=emailArticle&param=" + param_escape(id);
-
-               dialog = new dijit.Dialog({
-                       id: "emailArticleDlg",
-                       title: __("Forward article by email"),
-                       style: "width: 600px",
-                       execute: function() {
-                               if (this.validate()) {
-
-                                       new Ajax.Request("backend.php", {
-                                               parameters: dojo.objectToQuery(this.attr('value')),
-                                               onComplete: function(transport) {
-
-                                                       var reply = JSON.parse(transport.responseText);
-
-                                                       var error = reply['error'];
-
-                                                       if (error) {
-                                                               alert(__('Error sending email:') + ' ' + error);
-                                                       } else {
-                                                               notify_info('Your message has been sent.');
-                                                               dialog.hide();
-                                                       }
-
-                                       } });
-                               }
-                       },
-                       href: query});
-
-               var tmph = dojo.connect(dialog, 'onLoad', function() {
-               dojo.disconnect(tmph);
-
-                  new Ajax.Autocompleter('emailArticleDlg_destination', 'emailArticleDlg_dst_choices',
-                          "backend.php?op=rpc&method=completeEmails",
-                          { tokens: '', paramName: "search" });
-               });
-
-               dialog.show();
-
-               /* displayDlg('emailArticle', id,
-                  function () {
-                               document.forms['article_email_form'].destination.focus();
-
-                          new Ajax.Autocompleter('destination', 'destination_choices',
-                                  "backend.php?op=rpc&method=completeEmails",
-                                  { tokens: '', paramName: "search" });
-
-                       }); */
-
-       } catch (e) {
-               exception_error("emailArticle", e);
-       }
-}
-
 function dismissArticle(id) {
        try {
                var elem = $("RROW-" + id);
@@ -1671,6 +1656,7 @@ function cdmClicked(event, id) {
                                toggleSelected(id);
 
                                var elem = $("RROW-" + id);
+                               var article_is_unread = elem.hasClassName("Unread");
 
                                if (elem)
                                        elem.removeClassName("Unread");
@@ -1685,6 +1671,10 @@ function cdmClicked(event, id) {
 
                                active_post_id = id;
 
+                               if (article_is_unread) {
+                                       decrementFeedCounter(getActiveFeedId(), activeFeedIsCat());
+                               }
+
                                var query = "?op=rpc&method=catchupSelected" +
                                        "&cmode=0&ids=" + param_escape(id);
 
@@ -1699,6 +1689,14 @@ function cdmClicked(event, id) {
 
                } else {
                        toggleSelected(id, true);
+
+                       var elem = $("RROW-" + id);
+                       var article_is_unread = elem.hasClassName("Unread");
+
+                       if (article_is_unread) {
+                               decrementFeedCounter(getActiveFeedId(), activeFeedIsCat());
+                       }
+
                        toggleUnread(id, 0, false);
                        zoomToArticle(event, id);
                }
@@ -1770,7 +1768,7 @@ function getLastVisibleHeadlineId() {
 
 function openArticleInNewWindow(id) {
        toggleUnread(id, 0, false);
-       window.open("backend.php?op=la&id=" + id);
+       window.open("backend.php?op=article&method=redirect&id=" + id);
 }
 
 function isCdmMode() {
@@ -1824,6 +1822,8 @@ function correctHeadlinesOffset(id) {
                var container = $("headlines-frame");
                var row = $("RROW-" + id);
 
+               if (!container || !row) return;
+
                var viewport = container.offsetHeight;
 
                var rel_offset_top = row.offsetTop - container.scrollTop;
@@ -1961,16 +1961,26 @@ function initHeadlinesMenu() {
                                        label: name,
                                        labelId: bare_id,
                                        onClick: function(event) {
-                                               selectionAssignLabel(this.labelId,
-                                                       [this.getParent().ownerMenu.callerRowId]);
+                                               var ids = getSelectedArticleIds2();
+                                               // cast to string
+                                               var id = this.getParent().ownerMenu.callerRowId + "";
+
+                                               ids = ids.size() != 0 && ids.indexOf(id) != -1 ? ids : [id];
+
+                                               selectionAssignLabel(this.labelId, ids);
                                }}));
 
                                labelDelMenu.addChild(new dijit.MenuItem({
                                        label: name,
                                        labelId: bare_id,
                                        onClick: function(event) {
-                                               selectionRemoveLabel(this.labelId,
-                                                       [this.getParent().ownerMenu.callerRowId]);
+                                               var ids = getSelectedArticleIds2();
+                                               // cast to string
+                                               var id = this.getParent().ownerMenu.callerRowId + "";
+
+                                               ids = ids.size() != 0 && ids.indexOf(id) != -1 ? ids : [id];
+
+                                               selectionRemoveLabel(this.labelId, ids);
                                }}));
 
                        });
@@ -1994,87 +2004,6 @@ function initHeadlinesMenu() {
        }
 }
 
-function tweetArticle(id) {
-       try {
-               var query = "?op=rpc&method=getTweetInfo&id=" + param_escape(id);
-
-               console.log(query);
-
-               var d = new Date();
-      var ts = d.getTime();
-
-               var w = window.open('backend.php?op=loading', 'ttrss_tweet',
-                       "status=0,toolbar=0,location=0,width=500,height=400,scrollbars=1,menubar=0");
-
-               new Ajax.Request("backend.php", {
-                       parameters: query,
-                       onComplete: function(transport) {
-                               var ti = JSON.parse(transport.responseText);
-
-                               var share_url = "http://twitter.com/share?_=" + ts +
-                                       "&text=" + param_escape(ti.title) +
-                                       "&url=" + param_escape(ti.link);
-
-                               w.location.href = share_url;
-
-                       } });
-
-
-       } catch (e) {
-               exception_error("tweetArticle", e);
-       }
-}
-
-function editArticleNote(id) {
-       try {
-
-               var query = "backend.php?op=dlg&id=editArticleNote&param=" + param_escape(id);
-
-               if (dijit.byId("editNoteDlg"))
-                       dijit.byId("editNoteDlg").destroyRecursive();
-
-               dialog = new dijit.Dialog({
-                       id: "editNoteDlg",
-                       title: __("Edit article note"),
-                       style: "width: 600px",
-                       execute: function() {
-                               if (this.validate()) {
-                                       var query = dojo.objectToQuery(this.attr('value'));
-
-                                       notify_progress("Saving article note...", true);
-
-                                       new Ajax.Request("backend.php", {
-                                       parameters: query,
-                                       onComplete: function(transport) {
-                                               notify('');
-                                               dialog.hide();
-
-                                               var reply = JSON.parse(transport.responseText);
-
-                                               cache_delete("article:" + id);
-
-                                               var elem = $("POSTNOTE-" + id);
-
-                                               if (elem) {
-                                                       Element.hide(elem);
-                                                       elem.innerHTML = reply.note;
-
-                                                       if (reply.raw_length != 0)
-                                                               new Effect.Appear(elem);
-                                               }
-
-                                       }});
-                               }
-                       },
-                       href: query,
-               });
-
-               dialog.show();
-
-       } catch (e) {
-               exception_error("editArticleNote", e);
-       }
-}
 
 function player(elem) {
        var aid = elem.getAttribute("audio-id");
@@ -2177,17 +2106,20 @@ function render_local_headlines(feed, is_cat, obj) {
 function precache_headlines_idle() {
        try {
                if (!feed_precache_timeout_id) {
-                       var feeds = dijit.byId("feedTree").getVisibleUnreadFeeds();
-                       var uncached = [];
+                       if (get_timestamp() - _viewfeed_last > 120) {
 
-                       feeds.each(function(item) {
-                               if (parseInt(item[0]) > 0 && !cache_get("feed:" + item[0] + ":" + item[1]))
-                                       uncached.push(item);
-                       });
+                               var feeds = dijit.byId("feedTree").getVisibleUnreadFeeds();
+                               var uncached = [];
+
+                               feeds.each(function(item) {
+                                       if (parseInt(item[0]) > 0 && !cache_get("feed:" + item[0] + ":" + item[1]))
+                                               uncached.push(item);
+                               });
 
-                       if (uncached.length > 0) {
-                               var rf = uncached[Math.floor(Math.random()*uncached.length)];
-                               viewfeed(rf[0], '', rf[1], 0, true);
+                               if (uncached.length > 0) {
+                                       var rf = uncached[Math.floor(Math.random()*uncached.length)];
+                                       viewfeed(rf[0], '', rf[1], 0, true);
+                               }
                        }
                }
                precache_idle_timeout_id = setTimeout("precache_headlines_idle()", 1000*30);
@@ -2208,7 +2140,7 @@ function precache_headlines() {
                                if (nuf && !cache_get("feed:" + nuf + ":" + activeFeedIsCat()))
                                        viewfeed(nuf, '', activeFeedIsCat(), 0, true);
 
-                               if (nf != nuf && nf && !cache_get("feed:" + nf[0] + ":" + nf[1]))
+                               if (nf && nf[0] != nuf && !cache_get("feed:" + nf[0] + ":" + nf[1]))
                                        viewfeed(nf[0], '', nf[1], 0, true);
 
                                window.setTimeout(function() {
@@ -2222,24 +2154,4 @@ function precache_headlines() {
        }
 }
 
-function shareArticle(id) {
-       try {
-               if (dijit.byId("shareArticleDlg"))
-                       dijit.byId("shareArticleDlg").destroyRecursive();
-
-               var query = "backend.php?op=dlg&id=shareArticle&param=" + param_escape(id);
-
-               dialog = new dijit.Dialog({
-                       id: "shareArticleDlg",
-                       title: __("Share article by URL"),
-                       style: "width: 600px",
-                       href: query});
-
-               dialog.show();
-
-       } catch (e) {
-               exception_error("emailArticle", e);
-       }
-}
-