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