1 var _infscroll_disable = 0;
2 var _infscroll_request_sent = 0;
3 var _search_query = false;
4 var _viewfeed_last = 0;
6 var counters_last_request = 0;
8 function viewCategory(cat) {
9 viewfeed(cat, '', true);
13 function loadMoreHeadlines() {
15 console.log("loadMoreHeadlines");
19 var view_mode = document.forms["main_toolbar_form"].view_mode.value;
20 var unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length;
21 var num_all = $$("#headlines-frame > div[id*=RROW]").length;
22 var num_unread = getFeedUnread(getActiveFeedId(), activeFeedIsCat());
24 // TODO implement marked & published
26 if (view_mode == "marked") {
27 console.warn("loadMoreHeadlines: marked is not implemented, falling back.");
29 } else if (view_mode == "published") {
30 console.warn("loadMoreHeadlines: published is not implemented, falling back.");
32 } else if (view_mode == "unread") {
33 offset = unread_in_buffer;
34 } else if (_search_query) {
36 } else if (view_mode == "adaptive") {
38 offset = unread_in_buffer;
45 console.log("offset: " + offset);
47 viewfeed(getActiveFeedId(), '', activeFeedIsCat(), offset, false, true);
50 exception_error("viewNextFeedPage", e);
55 function viewfeed(feed, method, is_cat, offset, background, infscroll_req) {
57 if (is_cat == undefined)
62 if (method == undefined) method = '';
63 if (offset == undefined) offset = 0;
64 if (background == undefined) background = false;
65 if (infscroll_req == undefined) infscroll_req = false;
67 last_requested_article = 0;
69 var cached_headlines = false;
71 if (feed == getActiveFeedId() && activeFeedIsCat() == is_cat) {
72 cache_delete("feed:" + feed + ":" + is_cat);
74 cached_headlines = cache_get("feed:" + feed + ":" + is_cat);
76 if (!background && _search_query) _search_query = false;
78 // switching to a different feed, we might as well catchup stuff visible
79 // in headlines buffer (if any)
80 // disabled for now because this behavior is considered confusing -fox
81 /* if (!background && isCdmMode() && getInitParam("cdm_auto_catchup") == 1 && parseInt(getActiveFeedId()) > 0) {
83 $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
85 var hf = $("headlines-frame");
87 if (hf.scrollTop + hf.offsetHeight >=
88 child.offsetTop + child.offsetHeight) {
90 var id = child.id.replace("RROW-", "");
92 if (catchup_id_batch.indexOf(id) == -1)
93 catchup_id_batch.push(id);
97 if (catchup_id_batch.length > 0) {
98 window.clearTimeout(catchup_timeout_id);
100 if (!_infscroll_request_sent) {
101 catchup_timeout_id = window.setTimeout('catchupBatchedArticles()',
111 _viewfeed_last = get_timestamp();
113 if (getActiveFeedId() != feed || offset == 0) {
114 setActiveArticleId(0);
115 _infscroll_disable = 0;
118 if (!offset && !method && cached_headlines && !background) {
120 render_local_headlines(feed, is_cat, JSON.parse(cached_headlines));
123 console.warn("render_local_headlines failed: " + e);
127 if (offset != 0 && !method) {
128 var timestamp = get_timestamp();
130 if (_infscroll_request_sent && _infscroll_request_sent + 30 > timestamp) {
131 //console.log("infscroll request in progress, aborting");
135 _infscroll_request_sent = timestamp;
141 Form.enable("main_toolbar_form");
143 var toolbar_query = Form.serialize("main_toolbar_form");
145 var query = "?op=feeds&method=view&feed=" + feed + "&" +
149 query = query + "&m=" + param_escape(method);
154 force_nocache = true;
155 query = query + "&" + _search_query;
156 //_search_query = false;
160 query = query + "&skip=" + offset;
162 // to prevent duplicate feed titles when showing grouped vfeeds
163 if (vgroup_last_feed) {
164 query = query + "&vgrlf=" + param_escape(vgroup_last_feed);
167 if (!method && !is_cat && feed == getActiveFeedId()) {
168 query = query + "&m=ForceUpdate";
172 Form.enable("main_toolbar_form");
176 if (!setFeedExpandoIcon(feed, is_cat, 'images/indicator_white.gif'))
177 notify_progress("Loading, please wait...", true);
179 notify_progress("Loading, please wait...", true);
183 query += "&cat=" + is_cat;
187 new Ajax.Request("backend.php", {
189 onComplete: function(transport) {
190 setFeedExpandoIcon(feed, is_cat, 'images/blank_icon.gif');
191 headlines_callback2(transport, offset, background, infscroll_req);
195 exception_error("viewfeed", e);
199 function feedlist_init() {
201 console.log("in feedlist init");
203 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
204 document.onkeydown = hotkey_handler;
205 setTimeout("hotkey_prefix_timeout()", 5*1000);
207 if (!getActiveFeedId()) {
210 viewfeed(getActiveFeedId(), '', activeFeedIsCat());
213 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
215 request_counters(true);
217 setTimeout("precache_headlines_idle()", 15000);
220 exception_error("feedlist/init", e);
225 function request_counters(force) {
227 var date = new Date();
228 var timestamp = Math.round(date.getTime() / 1000);
230 if (force || timestamp - counters_last_request > 5) {
231 console.log("scheduling request of counters...");
233 counters_last_request = timestamp;
235 var query = "?op=rpc&method=getAllCounters&seq=" + next_seq();
238 query = query + "&last_article_id=" + getInitParam("last_article_id");
242 new Ajax.Request("backend.php", {
244 onComplete: function(transport) {
246 handle_rpc_json(transport);
248 exception_error("request_counters", e);
253 console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request));
257 exception_error("request_counters", e);
261 function displayNewContentPrompt(id) {
264 var msg = "<a href='#' onclick='viewCurrentFeed()'>" +
265 __("New articles available in this feed (click to show)") + "</a>";
267 msg = msg.replace("%s", getFeedName(id));
269 $('auxDlg').innerHTML = msg;
271 new Effect.Appear('auxDlg', {duration : 0.5});
274 exception_error("displayNewContentPrompt", e);
278 function parse_counters(elems, scheduled_call) {
280 for (var l = 0; l < elems.length; l++) {
282 var id = elems[l].id;
283 var kind = elems[l].kind;
284 var ctr = parseInt(elems[l].counter);
285 var error = elems[l].error;
286 var has_img = elems[l].has_img;
287 var updated = elems[l].updated;
289 if (id == "global-unread") {
295 if (id == "subscribed-feeds") {
300 if (id == getActiveFeedId() && ctr > getFeedUnread(id) && scheduled_call) {
301 displayNewContentPrompt(id);
304 if (getFeedUnread(id, (kind == "cat")) != ctr ||
307 cache_delete("feed:" + id + ":" + (kind == "cat"));
310 setFeedUnread(id, (kind == "cat"), ctr);
313 setFeedValue(id, false, 'error', error);
314 setFeedValue(id, false, 'updated', updated);
318 setFeedIcon(id, false,
319 getInitParam("icons_url") + "/" + id + ".ico");
321 setFeedIcon(id, false, 'images/blank_icon.gif');
327 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
330 exception_error("parse_counters", e);
334 function getFeedUnread(feed, is_cat) {
336 var tree = dijit.byId("feedTree");
338 if (tree && tree.model)
339 return tree.model.getFeedUnread(feed, is_cat);
348 function getFeedCategory(feed) {
350 var tree = dijit.byId("feedTree");
352 if (tree && tree.model)
353 return tree.getFeedCategory(feed);
362 function hideOrShowFeeds(hide) {
363 var tree = dijit.byId("feedTree");
366 return tree.hideRead(hide, getInitParam("hide_read_shows_special"));
369 function getFeedName(feed, is_cat) {
370 var tree = dijit.byId("feedTree");
372 if (tree && tree.model)
373 return tree.model.getFeedValue(feed, is_cat, 'name');
376 function getFeedValue(feed, is_cat, key) {
378 var tree = dijit.byId("feedTree");
380 if (tree && tree.model)
381 return tree.model.getFeedValue(feed, is_cat, key);
389 function setFeedUnread(feed, is_cat, unread) {
391 var tree = dijit.byId("feedTree");
393 if (tree && tree.model)
394 return tree.model.setFeedUnread(feed, is_cat, unread);
397 exception_error("setFeedUnread", e);
401 function setFeedValue(feed, is_cat, key, value) {
403 var tree = dijit.byId("feedTree");
405 if (tree && tree.model)
406 return tree.model.setFeedValue(feed, is_cat, key, value);
413 function selectFeed(feed, is_cat) {
415 var tree = dijit.byId("feedTree");
417 if (tree) return tree.selectFeed(feed, is_cat);
420 exception_error("selectFeed", e);
424 function setFeedIcon(feed, is_cat, src) {
426 var tree = dijit.byId("feedTree");
428 if (tree) return tree.setFeedIcon(feed, is_cat, src);
431 exception_error("setFeedIcon", e);
435 function setFeedExpandoIcon(feed, is_cat, src) {
437 var tree = dijit.byId("feedTree");
439 if (tree) return tree.setFeedExpandoIcon(feed, is_cat, src);
442 exception_error("setFeedIcon", e);
447 function getNextUnreadFeed(feed, is_cat) {
449 var tree = dijit.byId("feedTree");
450 var nuf = tree.model.getNextUnreadFeed(feed, is_cat);
453 return tree.model.store.getValue(nuf, 'bare_id');
456 exception_error("getNextUnreadFeed", e);
460 function catchupCurrentFeed() {
461 return catchupFeed(getActiveFeedId(), activeFeedIsCat());
464 function catchupFeedInGroup(id) {
467 var title = getFeedName(id);
469 var str = __("Mark all articles in %s as read?").replace("%s", title);
471 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
472 return viewCurrentFeed('MarkAllReadGR:' + id);
476 exception_error("catchupFeedInGroup", e);
480 function catchupFeed(feed, is_cat) {
482 if (is_cat == undefined) is_cat = false;
484 var str = __("Mark all articles in %s as read?");
485 var fn = getFeedName(feed, is_cat);
487 str = str.replace("%s", fn);
489 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
495 if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
496 $$("#headlines-frame > div[id*=RROW]").each(
498 var id = parseInt(child.id.replace("RROW-", ""));
500 if (id > max_id) max_id = id;
505 var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" +
506 feed + "&is_cat=" + is_cat + "&max_id=" + max_id;
508 console.log(catchup_query);
510 notify_progress("Loading, please wait...", true);
512 new Ajax.Request("backend.php", {
513 parameters: catchup_query,
514 onComplete: function(transport) {
515 handle_rpc_json(transport);
517 if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
519 $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
521 child.removeClassName("Unread");
526 var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1";
528 if (show_next_feed) {
529 var nuf = getNextUnreadFeed(feed, is_cat);
532 viewfeed(nuf, '', is_cat);
540 exception_error("catchupFeed", e);
544 function decrementFeedCounter(feed, is_cat) {
546 var ctr = getFeedUnread(feed, is_cat);
549 setFeedUnread(feed, is_cat, ctr - 1);
550 global_unread = global_unread - 1;
554 var cat = parseInt(getFeedCategory(feed));
557 ctr = getFeedUnread(cat, true);
560 setFeedUnread(cat, true, ctr - 1);
566 cache_delete("feed:" + feed + ":" + is_cat);
569 exception_error("decrement_feed_counter", e);