]> git.wh0rd.org - tt-rss.git/blob - js/feedlist.js
only precache_headlines_idle() when actually idle
[tt-rss.git] / js / feedlist.js
1 var _infscroll_disable = 0;
2 var _infscroll_request_sent = 0;
3 var _search_query = false;
4 var _viewfeed_last = 0;
5
6 var counter_timeout_id = false;
7
8 var counters_last_request = 0;
9
10 function viewCategory(cat) {
11 viewfeed(cat, '', true);
12 return false;
13 }
14
15 function loadMoreHeadlines() {
16 try {
17 console.log("loadMoreHeadlines");
18
19 var offset = 0;
20
21 var view_mode = document.forms["main_toolbar_form"].view_mode.value;
22 var num_unread = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length;
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") {
34 offset = num_unread;
35 } else if (view_mode == "adaptive") {
36 if (num_unread > 0)
37 offset = num_unread;
38 else
39 offset = num_all;
40 } else {
41 offset = num_all;
42 }
43
44 viewfeed(getActiveFeedId(), '', activeFeedIsCat(), offset, false, true);
45
46 } catch (e) {
47 exception_error("viewNextFeedPage", e);
48 }
49 }
50
51
52 function viewfeed(feed, method, is_cat, offset, background, infscroll_req) {
53 try {
54 if (is_cat == undefined)
55 is_cat = false;
56 else
57 is_cat = !!is_cat;
58
59 if (method == undefined) method = '';
60 if (offset == undefined) offset = 0;
61 if (background == undefined) background = false;
62 if (infscroll_req == undefined) infscroll_req = false;
63
64 last_requested_article = 0;
65
66 var cached_headlines = false;
67
68 if (feed == getActiveFeedId()) {
69 cache_delete("feed:" + feed + ":" + is_cat);
70 } else {
71 cached_headlines = cache_get("feed:" + feed + ":" + is_cat);
72
73 // switching to a different feed, we might as well catchup stuff visible
74 // in headlines buffer (if any)
75 // disabled for now because this behavior is considered confusing -fox
76 /* if (!background && isCdmMode() && getInitParam("cdm_auto_catchup") == 1 && parseInt(getActiveFeedId()) > 0) {
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 });
102 } */
103 }
104
105 if (offset == 0 && !background)
106 dijit.byId("content-tabs").selectChild(
107 dijit.byId("content-tabs").getChildren()[0]);
108
109 if (!background) {
110 _viewfeed_last = get_timestamp();
111
112 if (getActiveFeedId() != feed || offset == 0) {
113 active_post_id = 0;
114 _infscroll_disable = 0;
115 }
116
117 if (!offset && !method && cached_headlines && !background) {
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 }
124 }
125
126 if (offset != 0 && !method) {
127 var timestamp = get_timestamp();
128
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;
135 }
136
137 hideAuxDlg();
138 }
139
140 Form.enable("main_toolbar_form");
141
142 var toolbar_query = Form.serialize("main_toolbar_form");
143
144 var query = "?op=feeds&method=view&feed=" + feed + "&" +
145 toolbar_query + "&m=" + param_escape(method);
146
147 if (!background) {
148 if (_search_query) {
149 force_nocache = true;
150 query = query + "&" + _search_query;
151 _search_query = false;
152 }
153
154 if (offset != 0) {
155 query = query + "&skip=" + offset;
156
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 }
161 }
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 }
172 }
173
174 query += "&cat=" + is_cat;
175 query += "&include_children=" + dijit.byId("include_children").attr("checked");
176
177 console.log(query);
178
179 new Ajax.Request("backend.php", {
180 parameters: query,
181 onComplete: function(transport) {
182 setFeedExpandoIcon(feed, is_cat, 'images/blank_icon.gif');
183 headlines_callback2(transport, offset, background, infscroll_req);
184 } });
185
186 } catch (e) {
187 exception_error("viewfeed", e);
188 }
189 }
190
191 function feedlist_init() {
192 try {
193 console.log("in feedlist init");
194
195 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
196 document.onkeydown = hotkey_handler;
197 setTimeout("hotkey_prefix_timeout()", 5*1000);
198
199 if (!getActiveFeedId()) {
200 setTimeout("viewfeed(-3)", 100);
201 }
202
203 console.log("T:" +
204 getInitParam("cdm_auto_catchup") + " " + getFeedUnread(-3));
205
206 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
207
208 setTimeout("timeout()", 5000);
209 setTimeout("precache_headlines_idle()", 15000);
210
211 } catch (e) {
212 exception_error("feedlist/init", e);
213 }
214 }
215
216 function request_counters_real() {
217 try {
218 console.log("requesting counters...");
219
220 var query = "?op=rpc&method=getAllCounters&seq=" + next_seq();
221
222 query = query + "&omode=flc";
223
224 new Ajax.Request("backend.php", {
225 parameters: query,
226 onComplete: function(transport) {
227 try {
228 handle_rpc_json(transport);
229 } catch (e) {
230 exception_error("viewfeed/getcounters", e);
231 }
232 } });
233
234 } catch (e) {
235 exception_error("request_counters_real", e);
236 }
237 }
238
239
240 function request_counters() {
241
242 try {
243
244 if (getInitParam("bw_limit") == "1") return;
245
246 var date = new Date();
247 var timestamp = Math.round(date.getTime() / 1000);
248
249 if (timestamp - counters_last_request > 5) {
250 console.log("scheduling request of counters...");
251
252 window.clearTimeout(counter_timeout_id);
253 counter_timeout_id = window.setTimeout("request_counters_real()", 1000);
254
255 counters_last_request = timestamp;
256 } else {
257 console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request));
258 }
259
260 } catch (e) {
261 exception_error("request_counters", e);
262 }
263 }
264
265 function displayNewContentPrompt(id) {
266 try {
267
268 var msg = "<a href='#' onclick='viewCurrentFeed()'>" +
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
282 function parse_counters(elems, scheduled_call) {
283 try {
284 for (var l = 0; l < elems.length; l++) {
285
286 var id = elems[l].id;
287 var kind = elems[l].kind;
288 var ctr = parseInt(elems[l].counter);
289 var error = elems[l].error;
290 var has_img = elems[l].has_img;
291 var updated = elems[l].updated;
292 var child_unread = parseInt(elems[l].child_counter);
293
294 if (id == "global-unread") {
295 global_unread = ctr;
296 updateTitle();
297 continue;
298 }
299
300 if (id == "subscribed-feeds") {
301 feeds_found = ctr;
302 continue;
303 }
304
305 // TODO: enable new content notification for categories
306
307 if (!activeFeedIsCat() && id == getActiveFeedId()
308 && ctr > getFeedUnread(id) && scheduled_call) {
309 displayNewContentPrompt(id);
310 }
311
312 if (getFeedUnread(id, (kind == "cat")) != ctr)
313 cache_delete("feed:" + id + ":" + (kind == "cat"));
314
315 setFeedUnread(id, (kind == "cat"), ctr);
316
317 if (kind != "cat") {
318 setFeedValue(id, false, 'error', error);
319 setFeedValue(id, false, 'updated', updated);
320
321 if (id > 0) {
322 if (has_img) {
323 setFeedIcon(id, false,
324 getInitParam("icons_url") + "/" + id + ".ico");
325 } else {
326 setFeedIcon(id, false, 'images/blank_icon.gif');
327 }
328 }
329 } else {
330 setCatParam(id, child_unread);
331 }
332 }
333
334 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
335
336 } catch (e) {
337 exception_error("parse_counters", e);
338 }
339 }
340
341 function getFeedUnread(feed, is_cat) {
342 try {
343 var tree = dijit.byId("feedTree");
344
345 if (tree && tree.model)
346 return tree.model.getFeedUnread(feed, is_cat);
347
348 } catch (e) {
349 //
350 }
351
352 return -1;
353 }
354
355 function getFeedCategory(feed) {
356 try {
357 var tree = dijit.byId("feedTree");
358
359 if (tree && tree.model)
360 return tree.getFeedCategory(feed);
361
362 } catch (e) {
363 //
364 }
365
366 return false;
367 }
368
369 function hideOrShowFeeds(hide) {
370 var tree = dijit.byId("feedTree");
371
372 if (tree)
373 return tree.hideRead(hide, getInitParam("hide_read_shows_special"));
374 }
375
376 function getFeedName(feed, is_cat) {
377 var tree = dijit.byId("feedTree");
378
379 if (tree && tree.model)
380 return tree.model.getFeedValue(feed, is_cat, 'name');
381 }
382
383 function getFeedValue(feed, is_cat, key) {
384 try {
385 var tree = dijit.byId("feedTree");
386
387 if (tree && tree.model)
388 return tree.model.getFeedValue(feed, is_cat, key);
389
390 } catch (e) {
391 //
392 }
393 return '';
394 }
395
396 function setFeedUnread(feed, is_cat, unread) {
397 try {
398 var tree = dijit.byId("feedTree");
399
400 if (tree && tree.model)
401 return tree.model.setFeedUnread(feed, is_cat, unread);
402
403 } catch (e) {
404 exception_error("setFeedUnread", e);
405 }
406 }
407
408 function setFeedValue(feed, is_cat, key, value) {
409 try {
410 var tree = dijit.byId("feedTree");
411
412 if (tree && tree.model)
413 return tree.model.setFeedValue(feed, is_cat, key, value);
414
415 } catch (e) {
416 //
417 }
418 }
419
420 function setCatParam(cat, value) {
421 try {
422 var tree = dijit.byId("feedTree");
423
424 if (tree && tree.model)
425 return tree.setCatParam(cat, value);
426
427 } catch (e) {
428 //
429 }
430 }
431
432
433 function selectFeed(feed, is_cat) {
434 try {
435 var tree = dijit.byId("feedTree");
436
437 if (tree) return tree.selectFeed(feed, is_cat);
438
439 } catch (e) {
440 exception_error("selectFeed", e);
441 }
442 }
443
444 function setFeedIcon(feed, is_cat, src) {
445 try {
446 var tree = dijit.byId("feedTree");
447
448 if (tree) return tree.setFeedIcon(feed, is_cat, src);
449
450 } catch (e) {
451 exception_error("setFeedIcon", e);
452 }
453 }
454
455 function setFeedExpandoIcon(feed, is_cat, src) {
456 try {
457 var tree = dijit.byId("feedTree");
458
459 if (tree) return tree.setFeedExpandoIcon(feed, is_cat, src);
460
461 } catch (e) {
462 exception_error("setFeedIcon", e);
463 }
464 return false;
465 }
466
467 function getNextUnreadFeed(feed, is_cat) {
468 try {
469 var tree = dijit.byId("feedTree");
470 var nuf = tree.model.getNextUnreadFeed(feed, is_cat);
471
472 if (nuf)
473 return tree.model.store.getValue(nuf, 'bare_id');
474
475 } catch (e) {
476 exception_error("getNextUnreadFeed", e);
477 }
478 }
479
480 function catchupCurrentFeed() {
481 return catchupFeed(getActiveFeedId(), activeFeedIsCat());
482 }
483
484 function catchupFeedInGroup(id) {
485 try {
486
487 var title = getFeedName(id);
488
489 var str = __("Mark all articles in %s as read?").replace("%s", title);
490
491 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
492 return viewCurrentFeed('MarkAllReadGR:' + id);
493 }
494
495 } catch (e) {
496 exception_error("catchupFeedInGroup", e);
497 }
498 }
499
500 function catchupFeed(feed, is_cat) {
501 try {
502 if (is_cat == undefined) is_cat = false;
503
504 var str = __("Mark all articles in %s as read?");
505 var fn = getFeedName(feed, is_cat);
506
507 str = str.replace("%s", fn);
508
509 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
510 return;
511 }
512
513 var max_id = 0;
514
515 if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
516 $$("#headlines-frame > div[id*=RROW]").each(
517 function(child) {
518 var id = parseInt(child.id.replace("RROW-", ""));
519
520 if (id > max_id) max_id = id;
521 }
522 );
523 }
524
525 var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" +
526 feed + "&is_cat=" + is_cat + "&max_id=" + max_id;
527
528 console.log(catchup_query);
529
530 notify_progress("Loading, please wait...", true);
531
532 new Ajax.Request("backend.php", {
533 parameters: catchup_query,
534 onComplete: function(transport) {
535 handle_rpc_json(transport);
536
537 if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
538
539 $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
540 function(child) {
541 child.removeClassName("Unread");
542 }
543 );
544 }
545
546 var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1";
547
548 if (show_next_feed) {
549 var nuf = getNextUnreadFeed(feed, is_cat);
550
551 if (nuf) {
552 viewfeed(nuf, '', is_cat);
553 }
554 }
555
556 notify("");
557 } });
558
559 } catch (e) {
560 exception_error("catchupFeed", e);
561 }
562 }
563
564 function decrementFeedCounter(feed, is_cat) {
565 try {
566 var ctr = getFeedUnread(feed, is_cat);
567
568 if (ctr > 0) {
569 setFeedUnread(feed, is_cat, ctr - 1);
570
571 if (!is_cat) {
572 var cat = parseInt(getFeedCategory(feed));
573
574 if (!isNaN(cat)) {
575 ctr = getFeedUnread(cat, true);
576
577 if (ctr > 0) {
578 setFeedUnread(cat, true, ctr - 1);
579 }
580 }
581 }
582 }
583
584 cache_delete("feed:" + feed + ":" + is_cat);
585
586 } catch (e) {
587 exception_error("decrement_feed_counter", e);
588 }
589 }