]> git.wh0rd.org Git - tt-rss.git/commitdiff
getNeighborIds: add scope, add automatic prefetch for articles under mouse
authorAndrew Dolgov <fox@bah.spb.su>
Sun, 18 May 2008 08:51:07 +0000 (09:51 +0100)
committerAndrew Dolgov <fox@bah.spb.su>
Sun, 18 May 2008 08:51:07 +0000 (09:51 +0100)
functions.js
modules/backend-rpc.php
viewfeed.js

index 22d1ebb9ad2826fe4983fa39783c94b35d847e20..f716f350ec319cb79a08610efc3de3b8120b4e5c 100644 (file)
@@ -1477,9 +1477,11 @@ function explainError(code) {
 }
 
 // this only searches loaded headlines list, not in CDM
-function getRelativePostIds(id) {
+function getRelativePostIds(id, limit) {
 
-       debug("getRelativePostIds: " + id);
+       if (!limit) limit = 3;
+
+       debug("getRelativePostIds: " + id + " limit=" + limit);
 
        var ids = new Array();
        var container = document.getElementById("headlinesList");
@@ -1491,13 +1493,23 @@ function getRelativePostIds(id) {
                        var r_id = rows[i].id.replace("RROW-", "");
 
                        if (r_id == id) {
-                               if (i > 0) ids.push(rows[i-1].id.replace("RROW-", ""));
+/*                             if (i > 0) ids.push(rows[i-1].id.replace("RROW-", ""));
                                if (i > 1) ids.push(rows[i-2].id.replace("RROW-", ""));
                                if (i > 2) ids.push(rows[i-3].id.replace("RROW-", ""));
 
                                if (i < rows.length-1) ids.push(rows[i+1].id.replace("RROW-", ""));
                                if (i < rows.length-2) ids.push(rows[i+2].id.replace("RROW-", ""));
-                               if (i < rows.length-3) ids.push(rows[i+3].id.replace("RROW-", ""));
+                               if (i < rows.length-3) ids.push(rows[i+3].id.replace("RROW-", "")); */
+
+                               for (var k = 1; k <= limit; k++) {
+                                       var nid = false;
+
+                                       if (i > k-1) var nid = rows[i-k].id.replace("RROW-", "");
+                                       if (nid) ids.push(nid);
+
+                                       if (i < rows.length-k) nid = rows[i+k].id.replace("RROW-", "");
+                                       if (nid) ids.push(nid);
+                               }
 
                                return ids;
                        }
index 1bff702a7a410ed9d7a9dc19b3aa7055080afedb..2b3fe8726d128b027b16b835cf7941bef2a1fb7d 100644 (file)
 
                }
 
+               if ($subop == "getArticles") {
+                       $ids = split(",", db_escape_string($_REQUEST["ids"]));
+
+                       print "<rpc-reply>";
+
+                       foreach ($ids as $id) {
+                               if ($id) {
+                                       outputArticleXML($link, $id, 0, false);
+                               }
+                       }
+                       print "</rpc-reply>";
+
+                       return;
+               }
+
                print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
        }
 ?>
index a01c8a4a86f28e7fba96ac3a0f342124312b1f2d..b2a79b0399204bbe63911ee231686c810117a373 100644 (file)
@@ -1604,9 +1604,66 @@ function cdmClicked(elem) {
        } 
 }
 
+function preload_article_callback(transport) {
+       try {
+               if (transport.responseXML) {
+                       var articles = transport.responseXML.getElementsByTagName("article");
+
+                       for (var i = 0; i < articles.length; i++) {
+                               var id = articles[i].getAttribute("id");
+                               if (!cache_find(id)) {
+                                       cache_inject(id, articles[i].firstChild.nodeValue);                             
+                                       debug("preloaded article: " + id);
+                               }
+                       }
+               }
+       } catch (e) {
+               exception_error("preload_article_callback", e);
+       }
+}
+
+function preloadArticleUnderPointer(id) {
+       try {
+               if (post_under_pointer == id && !cache_find(id)) {
+
+                       debug("trying to preload article " + id);
+
+                       var neighbor_ids = getRelativePostIds(id, 1);
+
+                       /* only request uncached articles */
+
+                       var cids_to_request = Array();
+
+                       for (var i = 0; i < neighbor_ids.length; i++) {
+                               if (!cache_check(neighbor_ids[i])) {
+                                       cids_to_request.push(neighbor_ids[i]);
+                               }
+                       }
+                       debug("additional ids: " + cids_to_request.toString());
+
+                       cids_to_request.push(id);
+
+                       var query = "backend.php?op=rpc&subop=getArticles&ids=" + 
+                               cids_to_request.toString();
+                       new Ajax.Request(query, {
+                               onComplete: function(transport) { 
+                                       preload_article_callback(transport);
+                       } });
+               }
+       } catch (e) {
+               exception_error("preloadArticleUnderPointer", e);
+       }
+}
+
 function postMouseIn(id) {
        try {
-               post_under_pointer = id;
+               if (post_under_pointer != id) {
+                       post_under_pointer = id;
+                       if (!isCdmMode()) {
+                               window.setTimeout("preloadArticleUnderPointer(" + id + ")", 250);
+                       }
+               }
+
        } catch (e) {
                exception_error("postMouseIn", e);
        }