1 var _infscroll_disable = 0;
2 var _infscroll_request_sent = 0;
3 var _search_query = false;
4 var _viewfeed_last = 0;
6 var counter_timeout_id = false;
8 var counters_last_request = 0;
10 function viewCategory(cat) {
11 viewfeed(cat, '', true);
15 function loadMoreHeadlines() {
17 console.log("loadMoreHeadlines");
21 var view_mode = document.forms["main_toolbar_form"].view_mode.value;
22 var num_unread = getFeedUnread(getActiveFeedId(), activeFeedIsCat());
23 var unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length;
24 var num_all = $$("#headlines-frame > div[id*=RROW]").length;
26 // TODO implement marked & published
28 if (view_mode == "marked") {
29 console.warn("loadMoreHeadlines: marked is not implemented, falling back.");
31 } else if (view_mode == "published") {
32 console.warn("loadMoreHeadlines: published is not implemented, falling back.");
34 } else if (view_mode == "unread") {
36 } else if (view_mode == "adaptive") {
38 offset = unread_in_buffer;
45 viewfeed(getActiveFeedId(), '', activeFeedIsCat(), offset, false, true);
48 exception_error("viewNextFeedPage", e);
53 function viewfeed(feed, method, is_cat, offset, background, infscroll_req) {
55 if (is_cat == undefined)
60 if (method == undefined) method = '';
61 if (offset == undefined) offset = 0;
62 if (background == undefined) background = false;
63 if (infscroll_req == undefined) infscroll_req = false;
65 last_requested_article = 0;
67 var cached_headlines = false;
69 if (feed == getActiveFeedId()) {
70 cache_delete("feed:" + feed + ":" + is_cat);
72 cached_headlines = cache_get("feed:" + feed + ":" + is_cat);
74 // switching to a different feed, we might as well catchup stuff visible
75 // in headlines buffer (if any)
76 // disabled for now because this behavior is considered confusing -fox
77 /* if (!background && isCdmMode() && getInitParam("cdm_auto_catchup") == 1 && parseInt(getActiveFeedId()) > 0) {
79 $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
81 var hf = $("headlines-frame");
83 if (hf.scrollTop + hf.offsetHeight >=
84 child.offsetTop + child.offsetHeight) {
86 var id = child.id.replace("RROW-", "");
88 if (catchup_id_batch.indexOf(id) == -1)
89 catchup_id_batch.push(id);
93 if (catchup_id_batch.length > 0) {
94 window.clearTimeout(catchup_timeout_id);
96 if (!_infscroll_request_sent) {
97 catchup_timeout_id = window.setTimeout('catchupBatchedArticles()',
106 if (offset == 0 && !background)
107 dijit.byId("content-tabs").selectChild(
108 dijit.byId("content-tabs").getChildren()[0]);
111 _viewfeed_last = get_timestamp();
113 if (getActiveFeedId() != feed || offset == 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 + "&" +
146 toolbar_query + "&m=" + param_escape(method);
150 force_nocache = true;
151 query = query + "&" + _search_query;
152 _search_query = false;
156 query = query + "&skip=" + offset;
158 // to prevent duplicate feed titles when showing grouped vfeeds
159 if (vgroup_last_feed) {
160 query = query + "&vgrlf=" + param_escape(vgroup_last_feed);
164 Form.enable("main_toolbar_form");
168 if (!setFeedExpandoIcon(feed, is_cat, 'images/indicator_white.gif'))
169 notify_progress("Loading, please wait...", true);
171 notify_progress("Loading, please wait...", true);
175 query += "&cat=" + is_cat;
176 query += "&include_children=" + dijit.byId("include_children").attr("checked");
180 new Ajax.Request("backend.php", {
182 onComplete: function(transport) {
183 setFeedExpandoIcon(feed, is_cat, 'images/blank_icon.gif');
184 headlines_callback2(transport, offset, background, infscroll_req);
188 exception_error("viewfeed", e);
192 function feedlist_init() {
194 console.log("in feedlist init");
196 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
197 document.onkeydown = hotkey_handler;
198 setTimeout("hotkey_prefix_timeout()", 5*1000);
200 if (!getActiveFeedId()) {
201 setTimeout("viewfeed(-3)", 100);
205 getInitParam("cdm_auto_catchup") + " " + getFeedUnread(-3));
207 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
209 setTimeout("timeout()", 5000);
210 setTimeout("precache_headlines_idle()", 15000);
213 exception_error("feedlist/init", e);
217 function request_counters_real() {
219 console.log("requesting counters...");
221 var query = "?op=rpc&method=getAllCounters&seq=" + next_seq();
223 query = query + "&omode=flc";
225 new Ajax.Request("backend.php", {
227 onComplete: function(transport) {
229 handle_rpc_json(transport);
231 exception_error("viewfeed/getcounters", e);
236 exception_error("request_counters_real", e);
241 function request_counters() {
245 //if (getInitParam("bw_limit") == "1") return;
247 var date = new Date();
248 var timestamp = Math.round(date.getTime() / 1000);
250 if (timestamp - counters_last_request > 5) {
251 console.log("scheduling request of counters...");
253 window.clearTimeout(counter_timeout_id);
254 counter_timeout_id = window.setTimeout("request_counters_real()", 1000);
256 counters_last_request = timestamp;
258 console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request));
262 exception_error("request_counters", e);
266 function displayNewContentPrompt(id) {
269 var msg = "<a href='#' onclick='viewCurrentFeed()'>" +
270 __("New articles available in this feed (click to show)") + "</a>";
272 msg = msg.replace("%s", getFeedName(id));
274 $('auxDlg').innerHTML = msg;
276 new Effect.Appear('auxDlg', {duration : 0.5});
279 exception_error("displayNewContentPrompt", e);
283 function parse_counters(elems, scheduled_call) {
285 for (var l = 0; l < elems.length; l++) {
287 var id = elems[l].id;
288 var kind = elems[l].kind;
289 var ctr = parseInt(elems[l].counter);
290 var error = elems[l].error;
291 var has_img = elems[l].has_img;
292 var updated = elems[l].updated;
293 var child_unread = parseInt(elems[l].child_counter);
295 if (id == "global-unread") {
301 if (id == "subscribed-feeds") {
306 // TODO: enable new content notification for categories
308 if (!activeFeedIsCat() && id == getActiveFeedId()
309 && ctr > getFeedUnread(id) && scheduled_call) {
310 displayNewContentPrompt(id);
313 if (getFeedUnread(id, (kind == "cat")) != ctr)
314 cache_delete("feed:" + id + ":" + (kind == "cat"));
316 setFeedUnread(id, (kind == "cat"), ctr);
319 setFeedValue(id, false, 'error', error);
320 setFeedValue(id, false, 'updated', updated);
324 setFeedIcon(id, false,
325 getInitParam("icons_url") + "/" + id + ".ico");
327 setFeedIcon(id, false, 'images/blank_icon.gif');
331 setCatParam(id, child_unread);
335 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
338 exception_error("parse_counters", e);
342 function getFeedUnread(feed, is_cat) {
344 var tree = dijit.byId("feedTree");
346 if (tree && tree.model)
347 return tree.model.getFeedUnread(feed, is_cat);
356 function getFeedCategory(feed) {
358 var tree = dijit.byId("feedTree");
360 if (tree && tree.model)
361 return tree.getFeedCategory(feed);
370 function hideOrShowFeeds(hide) {
371 var tree = dijit.byId("feedTree");
374 return tree.hideRead(hide, getInitParam("hide_read_shows_special"));
377 function getFeedName(feed, is_cat) {
378 var tree = dijit.byId("feedTree");
380 if (tree && tree.model)
381 return tree.model.getFeedValue(feed, is_cat, 'name');
384 function getFeedValue(feed, is_cat, key) {
386 var tree = dijit.byId("feedTree");
388 if (tree && tree.model)
389 return tree.model.getFeedValue(feed, is_cat, key);
397 function setFeedUnread(feed, is_cat, unread) {
399 var tree = dijit.byId("feedTree");
401 if (tree && tree.model)
402 return tree.model.setFeedUnread(feed, is_cat, unread);
405 exception_error("setFeedUnread", e);
409 function setFeedValue(feed, is_cat, key, value) {
411 var tree = dijit.byId("feedTree");
413 if (tree && tree.model)
414 return tree.model.setFeedValue(feed, is_cat, key, value);
421 function setCatParam(cat, value) {
423 var tree = dijit.byId("feedTree");
425 if (tree && tree.model)
426 return tree.setCatParam(cat, value);
434 function selectFeed(feed, is_cat) {
436 var tree = dijit.byId("feedTree");
438 if (tree) return tree.selectFeed(feed, is_cat);
441 exception_error("selectFeed", e);
445 function setFeedIcon(feed, is_cat, src) {
447 var tree = dijit.byId("feedTree");
449 if (tree) return tree.setFeedIcon(feed, is_cat, src);
452 exception_error("setFeedIcon", e);
456 function setFeedExpandoIcon(feed, is_cat, src) {
458 var tree = dijit.byId("feedTree");
460 if (tree) return tree.setFeedExpandoIcon(feed, is_cat, src);
463 exception_error("setFeedIcon", e);
468 function getNextUnreadFeed(feed, is_cat) {
470 var tree = dijit.byId("feedTree");
471 var nuf = tree.model.getNextUnreadFeed(feed, is_cat);
474 return tree.model.store.getValue(nuf, 'bare_id');
477 exception_error("getNextUnreadFeed", e);
481 function catchupCurrentFeed() {
482 return catchupFeed(getActiveFeedId(), activeFeedIsCat());
485 function catchupFeedInGroup(id) {
488 var title = getFeedName(id);
490 var str = __("Mark all articles in %s as read?").replace("%s", title);
492 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
493 return viewCurrentFeed('MarkAllReadGR:' + id);
497 exception_error("catchupFeedInGroup", e);
501 function catchupFeed(feed, is_cat) {
503 if (is_cat == undefined) is_cat = false;
505 var str = __("Mark all articles in %s as read?");
506 var fn = getFeedName(feed, is_cat);
508 str = str.replace("%s", fn);
510 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
516 if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
517 $$("#headlines-frame > div[id*=RROW]").each(
519 var id = parseInt(child.id.replace("RROW-", ""));
521 if (id > max_id) max_id = id;
526 var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" +
527 feed + "&is_cat=" + is_cat + "&max_id=" + max_id;
529 console.log(catchup_query);
531 notify_progress("Loading, please wait...", true);
533 new Ajax.Request("backend.php", {
534 parameters: catchup_query,
535 onComplete: function(transport) {
536 handle_rpc_json(transport);
538 if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
540 $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
542 child.removeClassName("Unread");
547 var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1";
549 if (show_next_feed) {
550 var nuf = getNextUnreadFeed(feed, is_cat);
553 viewfeed(nuf, '', is_cat);
561 exception_error("catchupFeed", e);
565 function decrementFeedCounter(feed, is_cat) {
567 var ctr = getFeedUnread(feed, is_cat);
570 setFeedUnread(feed, is_cat, ctr - 1);
573 var cat = parseInt(getFeedCategory(feed));
576 ctr = getFeedUnread(cat, true);
579 setFeedUnread(cat, true, ctr - 1);
585 cache_delete("feed:" + feed + ":" + is_cat);
588 exception_error("decrement_feed_counter", e);