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