]> git.wh0rd.org - tt-rss.git/blame - js/feedlist.js
loadMoreHeadlines: fix for unread view
[tt-rss.git] / js / feedlist.js
CommitLineData
29dfb258 1var _infscroll_disable = 0;
82764a41 2var _infscroll_request_sent = 0;
973fe3c6 3var _search_query = false;
4d5eed55 4var _viewfeed_last = 0;
ac541432 5
5225d420
AD
6var counter_timeout_id = false;
7
9922d56b 8var counters_last_request = 0;
91cdbe52 9
ff2c6e6a 10function viewCategory(cat) {
767e2486 11 viewfeed(cat, '', true);
0c3d1219 12 return false;
ff2c6e6a
AD
13}
14
71317973 15function loadMoreHeadlines() {
ac541432 16 try {
d36d86b9
AD
17 console.log("loadMoreHeadlines");
18
71317973
AD
19 var offset = 0;
20
21 var view_mode = document.forms["main_toolbar_form"].view_mode.value;
3d2c9f5a 22 var unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length;
71317973
AD
23 var num_all = $$("#headlines-frame > div[id*=RROW]").length;
24
25 // TODO implement marked & published
26
27 if (view_mode == "marked") {
28 console.warn("loadMoreHeadlines: marked is not implemented, falling back.");
29 offset = num_all;
30 } else if (view_mode == "published") {
31 console.warn("loadMoreHeadlines: published is not implemented, falling back.");
32 offset = num_all;
33 } else if (view_mode == "unread") {
455e774a 34 offset = unread_in_buffer;
71317973 35 } else if (view_mode == "adaptive") {
455e774a 36 if (unread_in_buffer > 0)
3d2c9f5a 37 offset = unread_in_buffer;
71317973
AD
38 else
39 offset = num_all;
40 } else {
41 offset = num_all;
42 }
2a8534a0 43
28364067 44 viewfeed(getActiveFeedId(), '', activeFeedIsCat(), offset, false, true);
ac541432
AD
45
46 } catch (e) {
8b365031 47 exception_error("viewNextFeedPage", e);
ac541432
AD
48 }
49}
50
6a2034f9 51
f30ef1fa 52function viewfeed(feed, method, is_cat, offset, background, infscroll_req) {
7719618b 53 try {
78b2c6ce
AD
54 if (is_cat == undefined)
55 is_cat = false;
56 else
57 is_cat = !!is_cat;
58
f30ef1fa 59 if (method == undefined) method = '';
71317973 60 if (offset == undefined) offset = 0;
37c03d3a 61 if (background == undefined) background = false;
77c3c5f9 62 if (infscroll_req == undefined) infscroll_req = false;
c50e2b30 63
32f0c8b0
AD
64 last_requested_article = 0;
65
78b2c6ce
AD
66 var cached_headlines = false;
67
61834616 68 if (feed == getActiveFeedId()) {
78b2c6ce 69 cache_delete("feed:" + feed + ":" + is_cat);
61834616 70 } else {
78b2c6ce 71 cached_headlines = cache_get("feed:" + feed + ":" + is_cat);
b8e6acea 72
61834616
AD
73 // switching to a different feed, we might as well catchup stuff visible
74 // in headlines buffer (if any)
e2d1c16e
AD
75 // disabled for now because this behavior is considered confusing -fox
76 /* if (!background && isCdmMode() && getInitParam("cdm_auto_catchup") == 1 && parseInt(getActiveFeedId()) > 0) {
61834616
AD
77
78 $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
79 function(child) {
80 var hf = $("headlines-frame");
81
82 if (hf.scrollTop + hf.offsetHeight >=
83 child.offsetTop + child.offsetHeight) {
84
85 var id = child.id.replace("RROW-", "");
86
87 if (catchup_id_batch.indexOf(id) == -1)
88 catchup_id_batch.push(id);
89
90 }
91
92 if (catchup_id_batch.length > 0) {
93 window.clearTimeout(catchup_timeout_id);
94
95 if (!_infscroll_request_sent) {
96 catchup_timeout_id = window.setTimeout('catchupBatchedArticles()',
97 2000);
98 }
99 }
100
101 });
e2d1c16e 102 } */
61834616
AD
103 }
104
17eb362b 105 if (offset == 0 && !background)
0612ce23
AD
106 dijit.byId("content-tabs").selectChild(
107 dijit.byId("content-tabs").getChildren()[0]);
f92d7c2b 108
37c03d3a 109 if (!background) {
4d5eed55
AD
110 _viewfeed_last = get_timestamp();
111
37c03d3a
AD
112 if (getActiveFeedId() != feed || offset == 0) {
113 active_post_id = 0;
114 _infscroll_disable = 0;
115 }
203de776 116
f30ef1fa 117 if (!offset && !method && cached_headlines && !background) {
37c03d3a
AD
118 try {
119 render_local_headlines(feed, is_cat, JSON.parse(cached_headlines));
120 return;
121 } catch (e) {
122 console.warn("render_local_headlines failed: " + e);
123 }
78b2c6ce 124 }
78b2c6ce 125
f30ef1fa 126 if (offset != 0 && !method) {
4d5eed55 127 var timestamp = get_timestamp();
82764a41 128
37c03d3a
AD
129 if (_infscroll_request_sent && _infscroll_request_sent + 30 > timestamp) {
130 //console.log("infscroll request in progress, aborting");
131 return;
132 }
133
134 _infscroll_request_sent = timestamp;
82764a41
AD
135 }
136
37c03d3a 137 hideAuxDlg();
82764a41
AD
138 }
139
fd98576c
AD
140 Form.enable("main_toolbar_form");
141
fd98576c 142 var toolbar_query = Form.serialize("main_toolbar_form");
86b682ce 143
3f363052
AD
144 var query = "?op=feeds&method=view&feed=" + feed + "&" +
145 toolbar_query + "&m=" + param_escape(method);
37c03d3a
AD
146
147 if (!background) {
148 if (_search_query) {
52d7e7da 149 force_nocache = true;
37c03d3a
AD
150 query = query + "&" + _search_query;
151 _search_query = false;
52d7e7da 152 }
164f4738 153
37c03d3a
AD
154 if (offset != 0) {
155 query = query + "&skip=" + offset;
60d4df4d 156
37c03d3a
AD
157 // to prevent duplicate feed titles when showing grouped vfeeds
158 if (vgroup_last_feed) {
159 query = query + "&vgrlf=" + param_escape(vgroup_last_feed);
160 }
60d4df4d 161 }
37c03d3a
AD
162
163 Form.enable("main_toolbar_form");
164
165 if (!offset)
166 if (!is_cat) {
167 if (!setFeedExpandoIcon(feed, is_cat, 'images/indicator_white.gif'))
168 notify_progress("Loading, please wait...", true);
169 } else {
170 notify_progress("Loading, please wait...", true);
171 }
203de776
AD
172 }
173
37c03d3a 174 query += "&cat=" + is_cat;
854a2ace 175 query += "&include_children=" + dijit.byId("include_children").attr("checked");
6b4163cb 176
84c7b824 177 console.log(query);
081e527d 178
71317973
AD
179 new Ajax.Request("backend.php", {
180 parameters: query,
181 onComplete: function(transport) {
182 setFeedExpandoIcon(feed, is_cat, 'images/blank_icon.gif');
77c3c5f9 183 headlines_callback2(transport, offset, background, infscroll_req);
71317973 184 } });
6b4163cb 185
7719618b
AD
186 } catch (e) {
187 exception_error("viewfeed", e);
bd202c3f 188 }
1a66d16e
AD
189}
190
6b4163cb 191function feedlist_init() {
1b0809ae 192 try {
fcf70c51 193 console.log("in feedlist init");
bd202c3f 194
60ea2377 195 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
1b0809ae 196 document.onkeydown = hotkey_handler;
164469f9
AD
197 setTimeout("hotkey_prefix_timeout()", 5*1000);
198
6e88da82 199 if (!getActiveFeedId()) {
bd202c3f
AD
200 setTimeout("viewfeed(-3)", 100);
201 }
fcf70c51 202
bd202c3f 203 console.log("T:" +
6e88da82 204 getInitParam("cdm_auto_catchup") + " " + getFeedUnread(-3));
7086277c 205
a73e6b97 206 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
782ddd70 207
cd1bb36d 208 setTimeout("timeout()", 5000);
973c4a64 209 setTimeout("precache_headlines_idle()", 15000);
cd1bb36d 210
1b0809ae 211 } catch (e) {
bbf4d0b2 212 exception_error("feedlist/init", e);
1b0809ae 213 }
3745788e 214}
22e06406 215
997d9d7c 216function request_counters_real() {
5aaf9b34 217 try {
84c7b824 218 console.log("requesting counters...");
06c88b8d 219
f30ef1fa 220 var query = "?op=rpc&method=getAllCounters&seq=" + next_seq();
5aaf9b34 221
9c99281f 222 query = query + "&omode=flc";
5aaf9b34 223
b4e75b2a
AD
224 new Ajax.Request("backend.php", {
225 parameters: query,
bd202c3f 226 onComplete: function(transport) {
5aaf9b34 227 try {
563b9c78 228 handle_rpc_json(transport);
5aaf9b34
AD
229 } catch (e) {
230 exception_error("viewfeed/getcounters", e);
231 }
232 } });
233
997d9d7c
AD
234 } catch (e) {
235 exception_error("request_counters_real", e);
236 }
237}
238
997d9d7c
AD
239
240function request_counters() {
241
242 try {
243
2c2cdaf7 244 //if (getInitParam("bw_limit") == "1") return;
a598370d 245
997d9d7c
AD
246 var date = new Date();
247 var timestamp = Math.round(date.getTime() / 1000);
248
b39b57ac 249 if (timestamp - counters_last_request > 5) {
84c7b824 250 console.log("scheduling request of counters...");
5225d420
AD
251
252 window.clearTimeout(counter_timeout_id);
253 counter_timeout_id = window.setTimeout("request_counters_real()", 1000);
254
997d9d7c
AD
255 counters_last_request = timestamp;
256 } else {
84c7b824 257 console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request));
f55b0b12 258 }
997d9d7c 259
5aaf9b34
AD
260 } catch (e) {
261 exception_error("request_counters", e);
262 }
263}
a4b906ec 264
e69fb880
AD
265function displayNewContentPrompt(id) {
266 try {
267
a8328019 268 var msg = "<a href='#' onclick='viewCurrentFeed()'>" +
e69fb880
AD
269 __("New articles available in this feed (click to show)") + "</a>";
270
271 msg = msg.replace("%s", getFeedName(id));
272
273 $('auxDlg').innerHTML = msg;
274
275 new Effect.Appear('auxDlg', {duration : 0.5});
276
277 } catch (e) {
278 exception_error("displayNewContentPrompt", e);
279 }
280}
281
563b9c78 282function parse_counters(elems, scheduled_call) {
e69fb880 283 try {
e69fb880
AD
284 for (var l = 0; l < elems.length; l++) {
285
e331188f 286 var id = elems[l].id;
e69fb880 287 var kind = elems[l].kind;
e331188f 288 var ctr = parseInt(elems[l].counter);
e69fb880
AD
289 var error = elems[l].error;
290 var has_img = elems[l].has_img;
291 var updated = elems[l].updated;
2c5f231e 292 var child_unread = parseInt(elems[l].child_counter);
563b9c78 293
e69fb880 294 if (id == "global-unread") {
e69fb880
AD
295 global_unread = ctr;
296 updateTitle();
297 continue;
298 }
299
300 if (id == "subscribed-feeds") {
301 feeds_found = ctr;
302 continue;
303 }
13e785e0 304
179db4a0
AD
305 // TODO: enable new content notification for categories
306
bd202c3f 307 if (!activeFeedIsCat() && id == getActiveFeedId()
179db4a0 308 && ctr > getFeedUnread(id) && scheduled_call) {
6e88da82
AD
309 displayNewContentPrompt(id);
310 }
311
183ca8b9
AD
312 if (getFeedUnread(id, (kind == "cat")) != ctr ||
313 (kind == "cat" && getCatParam(id) != child_unread)) {
314
78b2c6ce 315 cache_delete("feed:" + id + ":" + (kind == "cat"));
183ca8b9 316 }
78b2c6ce 317
13e785e0 318 setFeedUnread(id, (kind == "cat"), ctr);
e69fb880 319
fcf70c51 320 if (kind != "cat") {
700f1426 321 setFeedValue(id, false, 'error', error);
fcf70c51 322 setFeedValue(id, false, 'updated', updated);
2ef5c21f
AD
323
324 if (id > 0) {
325 if (has_img) {
bd202c3f 326 setFeedIcon(id, false,
2ef5c21f
AD
327 getInitParam("icons_url") + "/" + id + ".ico");
328 } else {
329 setFeedIcon(id, false, 'images/blank_icon.gif');
330 }
331 }
2c5f231e
AD
332 } else {
333 setCatParam(id, child_unread);
e69fb880 334 }
fcf70c51 335 }
bd202c3f 336
e69fb880
AD
337 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
338
e69fb880
AD
339 } catch (e) {
340 exception_error("parse_counters", e);
341 }
342}
343
6e88da82 344function getFeedUnread(feed, is_cat) {
e69fb880 345 try {
e032e2e7
AD
346 var tree = dijit.byId("feedTree");
347
bd202c3f 348 if (tree && tree.model)
e032e2e7
AD
349 return tree.model.getFeedUnread(feed, is_cat);
350
e69fb880 351 } catch (e) {
fcf70c51 352 //
e69fb880 353 }
fcf70c51
AD
354
355 return -1;
e69fb880
AD
356}
357
6c8e8fbd
AD
358function getFeedCategory(feed) {
359 try {
360 var tree = dijit.byId("feedTree");
361
362 if (tree && tree.model)
363 return tree.getFeedCategory(feed);
364
365 } catch (e) {
366 //
367 }
368
369 return false;
370}
371
e69fb880 372function hideOrShowFeeds(hide) {
fcf70c51
AD
373 var tree = dijit.byId("feedTree");
374
05f224a3
AD
375 if (tree)
376 return tree.hideRead(hide, getInitParam("hide_read_shows_special"));
e69fb880
AD
377}
378
bd202c3f 379function getFeedName(feed, is_cat) {
e032e2e7
AD
380 var tree = dijit.byId("feedTree");
381
bd202c3f 382 if (tree && tree.model)
e032e2e7 383 return tree.model.getFeedValue(feed, is_cat, 'name');
fcf70c51 384}
e69fb880 385
bd202c3f 386function getFeedValue(feed, is_cat, key) {
fcf70c51 387 try {
e032e2e7 388 var tree = dijit.byId("feedTree");
fcf70c51 389
bd202c3f 390 if (tree && tree.model)
e032e2e7 391 return tree.model.getFeedValue(feed, is_cat, key);
bd202c3f 392
fcf70c51
AD
393 } catch (e) {
394 //
e69fb880 395 }
fcf70c51 396 return '';
e69fb880
AD
397}
398
13e785e0
AD
399function setFeedUnread(feed, is_cat, unread) {
400 try {
e032e2e7
AD
401 var tree = dijit.byId("feedTree");
402
bd202c3f 403 if (tree && tree.model)
e032e2e7
AD
404 return tree.model.setFeedUnread(feed, is_cat, unread);
405
fcf70c51
AD
406 } catch (e) {
407 exception_error("setFeedUnread", e);
408 }
409}
410
411function setFeedValue(feed, is_cat, key, value) {
412 try {
e032e2e7 413 var tree = dijit.byId("feedTree");
13e785e0 414
bd202c3f 415 if (tree && tree.model)
e032e2e7 416 return tree.model.setFeedValue(feed, is_cat, key, value);
c8a9fe5b 417
13e785e0 418 } catch (e) {
e032e2e7 419 //
fcf70c51
AD
420 }
421}
422
2c5f231e
AD
423function setCatParam(cat, value) {
424 try {
425 var tree = dijit.byId("feedTree");
426
427 if (tree && tree.model)
428 return tree.setCatParam(cat, value);
429
430 } catch (e) {
431 //
432 }
433}
434
183ca8b9
AD
435function getCatParam(cat) {
436 try {
437 return getFeedValue(cat, true, "child_unread");
438 } catch (e) {
439 //
440 }
441}
442
2c5f231e 443
fcf70c51
AD
444function selectFeed(feed, is_cat) {
445 try {
446 var tree = dijit.byId("feedTree");
447
e032e2e7 448 if (tree) return tree.selectFeed(feed, is_cat);
fcf70c51
AD
449
450 } catch (e) {
451 exception_error("selectFeed", e);
13e785e0
AD
452 }
453}
2ef5c21f
AD
454
455function setFeedIcon(feed, is_cat, src) {
456 try {
457 var tree = dijit.byId("feedTree");
458
e032e2e7 459 if (tree) return tree.setFeedIcon(feed, is_cat, src);
2ef5c21f
AD
460
461 } catch (e) {
462 exception_error("setFeedIcon", e);
463 }
464}
9c99281f
AD
465
466function setFeedExpandoIcon(feed, is_cat, src) {
467 try {
468 var tree = dijit.byId("feedTree");
469
e032e2e7 470 if (tree) return tree.setFeedExpandoIcon(feed, is_cat, src);
9c99281f
AD
471
472 } catch (e) {
473 exception_error("setFeedIcon", e);
474 }
f92d7c2b 475 return false;
9c99281f 476}
37c03d3a
AD
477
478function getNextUnreadFeed(feed, is_cat) {
479 try {
480 var tree = dijit.byId("feedTree");
481 var nuf = tree.model.getNextUnreadFeed(feed, is_cat);
482
483 if (nuf)
484 return tree.model.store.getValue(nuf, 'bare_id');
485
486 } catch (e) {
487 exception_error("getNextUnreadFeed", e);
488 }
489}
490
44245618
AD
491function catchupCurrentFeed() {
492 return catchupFeed(getActiveFeedId(), activeFeedIsCat());
493}
494
495function catchupFeedInGroup(id) {
496 try {
497
498 var title = getFeedName(id);
499
500 var str = __("Mark all articles in %s as read?").replace("%s", title);
501
502 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
503 return viewCurrentFeed('MarkAllReadGR:' + id);
504 }
505
506 } catch (e) {
507 exception_error("catchupFeedInGroup", e);
508 }
509}
510
e3d2c029
AD
511function catchupFeed(feed, is_cat) {
512 try {
184f5195
AD
513 if (is_cat == undefined) is_cat = false;
514
a593cb62 515 var str = __("Mark all articles in %s as read?");
9dd22f1e 516 var fn = getFeedName(feed, is_cat);
a593cb62
AD
517
518 str = str.replace("%s", fn);
519
520 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
521 return;
522 }
e3d2c029 523
184f5195
AD
524 var max_id = 0;
525
705b97b7 526 if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
184f5195
AD
527 $$("#headlines-frame > div[id*=RROW]").each(
528 function(child) {
529 var id = parseInt(child.id.replace("RROW-", ""));
530
531 if (id > max_id) max_id = id;
532 }
533 );
534 }
535
f30ef1fa 536 var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" +
184f5195
AD
537 feed + "&is_cat=" + is_cat + "&max_id=" + max_id;
538
539 console.log(catchup_query);
e3d2c029
AD
540
541 notify_progress("Loading, please wait...", true);
542
543 new Ajax.Request("backend.php", {
544 parameters: catchup_query,
545 onComplete: function(transport) {
546 handle_rpc_json(transport);
44245618
AD
547
548 if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
549
550 $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
551 function(child) {
552 child.removeClassName("Unread");
553 }
554 );
555 }
556
557 var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1";
558
559 if (show_next_feed) {
560 var nuf = getNextUnreadFeed(feed, is_cat);
561
562 if (nuf) {
c86bf88d 563 viewfeed(nuf, '', is_cat);
44245618
AD
564 }
565 }
566
e3d2c029
AD
567 notify("");
568 } });
569
570 } catch (e) {
571 exception_error("catchupFeed", e);
572 }
573}
de44b9a6
AD
574
575function decrementFeedCounter(feed, is_cat) {
576 try {
455e774a
AD
577 // we have subcats, no way to figure out if this article is
578 // actually from this category
579 if (is_cat && getCatParam(feed) > 0)
580 return;
581
de44b9a6
AD
582 var ctr = getFeedUnread(feed, is_cat);
583
584 if (ctr > 0) {
585 setFeedUnread(feed, is_cat, ctr - 1);
586
587 if (!is_cat) {
588 var cat = parseInt(getFeedCategory(feed));
589
590 if (!isNaN(cat)) {
591 ctr = getFeedUnread(cat, true);
592
593 if (ctr > 0) {
594 setFeedUnread(cat, true, ctr - 1);
595 }
596 }
597 }
598 }
599
18f6961a 600 cache_delete("feed:" + feed + ":" + is_cat);
2b613b14 601
de44b9a6
AD
602 } catch (e) {
603 exception_error("decrement_feed_counter", e);
604 }
605}