getNeighborIds: add scope, add automatic prefetch for articles under mouse

This commit is contained in:
Andrew Dolgov 2008-05-18 09:51:07 +01:00
parent ebb4133380
commit aa0fa9df1e
3 changed files with 89 additions and 5 deletions

View 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;
}

View File

@ -414,6 +414,21 @@
}
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>";
}
?>

View 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);
}