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