1 var _infscroll_disable = 0;
2 var _infscroll_request_sent = 0;
4 var _search_query = false;
5 var _viewfeed_last = 0;
6 var _viewfeed_timeout = false;
8 var counters_last_request = 0;
9 var _counters_prev = [];
11 function resetCounterCache() {
15 function loadMoreHeadlines() {
16 console.log("loadMoreHeadlines");
20 var view_mode = document.forms["main_toolbar_form"].view_mode.value;
21 var unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length;
22 var num_all = $$("#headlines-frame > div[id*=RROW]").length;
23 var num_unread = getFeedUnread(getActiveFeedId(), activeFeedIsCat());
25 // TODO implement marked & published
27 if (view_mode == "marked") {
28 console.warn("loadMoreHeadlines: marked is not implemented, falling back.");
30 } else if (view_mode == "published") {
31 console.warn("loadMoreHeadlines: published is not implemented, falling back.");
33 } else if (view_mode == "unread") {
34 offset = unread_in_buffer;
35 } else if (_search_query) {
37 } else if (view_mode == "adaptive" && !(getActiveFeedId() == -1 && !activeFeedIsCat())) {
38 // ^ starred feed shows both unread & read articles in adaptive mode
39 offset = num_unread > 0 ? unread_in_buffer : num_all;
44 console.log("offset: " + offset);
46 viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat(), offset: offset, infscroll_req: true});
50 function cleanup_memory(root) {
51 var dijits = dojo.query("[widgetid]", dijit.byId(root).domNode).map(dijit.byNode);
53 dijits.each(function (d) {
54 dojo.destroy(d.domNode);
57 $$("#" + root + " *").each(function (i) {
58 i.parentNode ? i.parentNode.removeChild(i) : true;
62 function viewfeed(params) {
63 var feed = params.feed;
64 var is_cat = params.is_cat;
65 var offset = params.offset;
66 var background = params.background;
67 var infscroll_req = params.infscroll_req;
68 var can_wait = params.can_wait;
69 var viewfeed_debug = params.viewfeed_debug;
70 var method = params.method;
72 if (is_cat == undefined)
77 if (offset == undefined) offset = 0;
78 if (background == undefined) background = false;
79 if (infscroll_req == undefined) infscroll_req = false;
81 last_requested_article = 0;
83 if (feed != getActiveFeedId() || activeFeedIsCat() != is_cat) {
84 if (!background && _search_query) _search_query = false;
88 _viewfeed_last = get_timestamp();
90 if (getActiveFeedId() != feed || !infscroll_req) {
91 setActiveArticleId(0);
92 _infscroll_disable = 0;
94 cleanup_memory("headlines-frame");
95 _headlines_scroll_offset = 0;
99 var timestamp = get_timestamp();
101 if (_infscroll_request_sent && _infscroll_request_sent + 30 > timestamp) {
102 //console.log("infscroll request in progress, aborting");
106 _infscroll_request_sent = timestamp;
110 Form.enable("main_toolbar_form");
112 var toolbar_query = Form.serialize("main_toolbar_form");
114 var query = "?op=feeds&method=view&feed=" + param_escape(feed) + "&" +
117 if (method) query += "&m=" + param_escape(method);
120 if (current_first_id) {
121 query = query + "&fid=" + param_escape(current_first_id);
127 force_nocache = true;
128 query = query + "&" + _search_query;
129 //_search_query = false;
133 query = query + "&skip=" + offset;
135 // to prevent duplicate feed titles when showing grouped vfeeds
136 if (vgroup_last_feed) {
137 query = query + "&vgrlf=" + param_escape(vgroup_last_feed);
140 if (!is_cat && feed == getActiveFeedId() && !params.method) {
141 query = query + "&m=ForceUpdate";
145 Form.enable("main_toolbar_form");
147 if (!setFeedExpandoIcon(feed, is_cat,
148 (is_cat) ? 'images/indicator_tiny.gif' : 'images/indicator_white.gif'))
149 notify_progress("Loading, please wait...", true);
152 query += "&cat=" + is_cat;
156 if (can_wait && _viewfeed_timeout) {
157 setFeedExpandoIcon(getActiveFeedId(), activeFeedIsCat(), 'images/blank_icon.gif');
158 clearTimeout(_viewfeed_timeout);
161 setActiveFeedId(feed, is_cat);
163 if (viewfeed_debug) {
164 window.open("backend.php" + query + "&debug=1&csrf_token=" + getInitParam("csrf_token"));
167 timeout_ms = can_wait ? 250 : 0;
168 _viewfeed_timeout = setTimeout(function() {
170 new Ajax.Request("backend.php", {
172 onComplete: function(transport) {
174 setFeedExpandoIcon(feed, is_cat, 'images/blank_icon.gif');
175 headlines_callback2(transport, offset, background, infscroll_req);
176 PluginHost.run(PluginHost.HOOK_FEED_LOADED, [feed, is_cat]);
181 }, timeout_ms); // Wait 250ms
185 function feedlist_init() {
186 console.log("in feedlist init");
188 loading_set_progress(50);
190 document.onkeydown = hotkey_handler;
191 setTimeout(hotkey_prefix_timeout, 5*1000);
193 if (!getActiveFeedId()) {
194 viewfeed({feed: -3});
196 viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat()});
199 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
201 request_counters(true);
207 function request_counters(force) {
208 var date = new Date();
209 var timestamp = Math.round(date.getTime() / 1000);
211 if (force || timestamp - counters_last_request > 5) {
212 console.log("scheduling request of counters...");
214 counters_last_request = timestamp;
216 var query = "?op=rpc&method=getAllCounters&seq=" + next_seq();
219 query = query + "&last_article_id=" + getInitParam("last_article_id");
223 new Ajax.Request("backend.php", {
225 onComplete: function(transport) {
226 handle_rpc_json(transport);
230 console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request));
234 // NOTE: this implementation is incomplete
235 // for general objects but good enough for counters
236 // http://adripofjavascript.com/blog/drips/object-equality-in-javascript.html
237 function counter_is_equal(a, b) {
238 // Create arrays of property names
239 var aProps = Object.getOwnPropertyNames(a);
240 var bProps = Object.getOwnPropertyNames(b);
242 // If number of properties is different,
243 // objects are not equivalent
244 if (aProps.length != bProps.length) {
248 for (var i = 0; i < aProps.length; i++) {
249 var propName = aProps[i];
251 // If values of same property are not equal,
252 // objects are not equivalent
253 if (a[propName] !== b[propName]) {
258 // If we made it this far, objects
259 // are considered equivalent
264 function parse_counters(elems) {
265 for (var l = 0; l < elems.length; l++) {
267 if (_counters_prev[l] && counter_is_equal(elems[l], _counters_prev[l])) {
271 var id = elems[l].id;
272 var kind = elems[l].kind;
273 var ctr = parseInt(elems[l].counter);
274 var error = elems[l].error;
275 var has_img = elems[l].has_img;
276 var updated = elems[l].updated;
277 var auxctr = parseInt(elems[l].auxcounter);
279 if (id == "global-unread") {
285 if (id == "subscribed-feeds") {
286 /* feeds_found = ctr; */
290 /*if (getFeedUnread(id, (kind == "cat")) != ctr ||
294 setFeedUnread(id, (kind == "cat"), ctr);
295 setFeedValue(id, (kind == "cat"), 'auxcounter', auxctr);
298 setFeedValue(id, false, 'error', error);
299 setFeedValue(id, false, 'updated', updated);
303 setFeedIcon(id, false,
304 getInitParam("icons_url") + "/" + id + ".ico");
306 setFeedIcon(id, false, 'images/blank_icon.gif');
312 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
314 _counters_prev = elems;
317 function getFeedUnread(feed, is_cat) {
319 var tree = dijit.byId("feedTree");
321 if (tree && tree.model)
322 return tree.model.getFeedUnread(feed, is_cat);
331 function getFeedCategory(feed) {
333 var tree = dijit.byId("feedTree");
335 if (tree && tree.model)
336 return tree.getFeedCategory(feed);
345 function hideOrShowFeeds(hide) {
346 var tree = dijit.byId("feedTree");
349 return tree.hideRead(hide, getInitParam("hide_read_shows_special"));
352 function getFeedName(feed, is_cat) {
354 if (isNaN(feed)) return feed; // it's a tag
356 var tree = dijit.byId("feedTree");
358 if (tree && tree.model)
359 return tree.model.getFeedValue(feed, is_cat, 'name');
362 function getFeedValue(feed, is_cat, key) {
364 var tree = dijit.byId("feedTree");
366 if (tree && tree.model)
367 return tree.model.getFeedValue(feed, is_cat, key);
375 function setFeedUnread(feed, is_cat, unread) {
376 var tree = dijit.byId("feedTree");
378 if (tree && tree.model)
379 return tree.model.setFeedUnread(feed, is_cat, unread);
382 function setFeedValue(feed, is_cat, key, value) {
384 var tree = dijit.byId("feedTree");
386 if (tree && tree.model)
387 return tree.model.setFeedValue(feed, is_cat, key, value);
394 function selectFeed(feed, is_cat) {
395 var tree = dijit.byId("feedTree");
397 if (tree) return tree.selectFeed(feed, is_cat);
400 function setFeedIcon(feed, is_cat, src) {
401 var tree = dijit.byId("feedTree");
403 if (tree) return tree.setFeedIcon(feed, is_cat, src);
406 function setFeedExpandoIcon(feed, is_cat, src) {
407 var tree = dijit.byId("feedTree");
409 if (tree) return tree.setFeedExpandoIcon(feed, is_cat, src);
414 function getNextUnreadFeed(feed, is_cat) {
415 var tree = dijit.byId("feedTree");
416 var nuf = tree.model.getNextUnreadFeed(feed, is_cat);
419 return tree.model.store.getValue(nuf, 'bare_id');
422 function catchupCurrentFeed(mode) {
423 catchupFeed(getActiveFeedId(), activeFeedIsCat(), mode);
426 function catchupFeedInGroup(id) {
427 var title = getFeedName(id);
429 var str = __("Mark all articles in %s as read?").replace("%s", title);
431 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
433 var rows = $$("#headlines-frame > div[id*=RROW][data-orig-feed-id='"+id+"']");
435 if (rows.length > 0) {
437 rows.each(function (row) {
438 row.removeClassName("Unread");
440 if (row.getAttribute("data-article-id") != getActiveArticleId()) {
441 new Effect.Fade(row, {duration: 0.5});
446 var feedTitles = $$("#headlines-frame > div[class='cdmFeedTitle']");
448 for (var i = 0; i < feedTitles.length; i++) {
449 if (feedTitles[i].getAttribute("data-feed-id") == id) {
451 if (i < feedTitles.length - 1) {
452 new Effect.Fade(feedTitles[i], {duration: 0.5});
459 updateFloatingTitle(true);
462 var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" +
463 id + "&is_cat=false";
465 console.log(catchup_query);
467 notify_progress("Loading, please wait...", true);
469 new Ajax.Request("backend.php", {
470 parameters: catchup_query,
471 onComplete: function (transport) {
472 handle_rpc_json(transport);
476 //return viewCurrentFeed('MarkAllReadGR:' + id);
480 function catchupFeed(feed, is_cat, mode) {
481 if (is_cat == undefined) is_cat = false;
487 str = __("Mark all articles in %s older than 1 day as read?");
490 str = __("Mark all articles in %s older than 1 week as read?");
493 str = __("Mark all articles in %s older than 2 weeks as read?");
496 str = __("Mark all articles in %s as read?");
499 var fn = getFeedName(feed, is_cat);
501 str = str.replace("%s", fn);
503 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
507 var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" +
508 feed + "&is_cat=" + is_cat + "&mode=" + mode;
510 console.log(catchup_query);
512 notify_progress("Loading, please wait...", true);
514 new Ajax.Request("backend.php", {
515 parameters: catchup_query,
516 onComplete: function(transport) {
517 handle_rpc_json(transport);
519 var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1";
521 if (show_next_feed) {
522 var nuf = getNextUnreadFeed(feed, is_cat);
525 viewfeed({feed: nuf, is_cat: is_cat});
528 if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
538 function decrementFeedCounter(feed, is_cat) {
539 var ctr = getFeedUnread(feed, is_cat);
542 setFeedUnread(feed, is_cat, ctr - 1);
543 global_unread = global_unread - 1;
547 var cat = parseInt(getFeedCategory(feed));
550 ctr = getFeedUnread(cat, true);
553 setFeedUnread(cat, true, ctr - 1);