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