]> git.wh0rd.org Git - tt-rss.git/blob - js/feedlist.js
loadMoreHeadlines: fix for unread view
[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
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 = unread_in_buffer;
35                 } else if (view_mode == "adaptive") {
36                         if (unread_in_buffer > 0)
37                                 offset = unread_in_buffer;
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                                         (kind == "cat" && getCatParam(id) != child_unread)) {
314
315                                 cache_delete("feed:" + id + ":" + (kind == "cat"));
316                         }
317
318                         setFeedUnread(id, (kind == "cat"), ctr);
319
320                         if (kind != "cat") {
321                                 setFeedValue(id, false, 'error', error);
322                                 setFeedValue(id, false, 'updated', updated);
323
324                                 if (id > 0) {
325                                         if (has_img) {
326                                                 setFeedIcon(id, false,
327                                                         getInitParam("icons_url") + "/" + id + ".ico");
328                                         } else {
329                                                 setFeedIcon(id, false, 'images/blank_icon.gif');
330                                         }
331                                 }
332                         } else {
333                                 setCatParam(id, child_unread);
334                         }
335                 }
336
337                 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
338
339         } catch (e) {
340                 exception_error("parse_counters", e);
341         }
342 }
343
344 function getFeedUnread(feed, is_cat) {
345         try {
346                 var tree = dijit.byId("feedTree");
347
348                 if (tree && tree.model)
349                         return tree.model.getFeedUnread(feed, is_cat);
350
351         } catch (e) {
352                 //
353         }
354
355         return -1;
356 }
357
358 function getFeedCategory(feed) {
359         try {
360                 var tree = dijit.byId("feedTree");
361
362                 if (tree && tree.model)
363                         return tree.getFeedCategory(feed);
364
365         } catch (e) {
366                 //
367         }
368
369         return false;
370 }
371
372 function hideOrShowFeeds(hide) {
373         var tree = dijit.byId("feedTree");
374
375         if (tree)
376                 return tree.hideRead(hide, getInitParam("hide_read_shows_special"));
377 }
378
379 function getFeedName(feed, is_cat) {
380         var tree = dijit.byId("feedTree");
381
382         if (tree && tree.model)
383                 return tree.model.getFeedValue(feed, is_cat, 'name');
384 }
385
386 function getFeedValue(feed, is_cat, key) {
387         try {
388                 var tree = dijit.byId("feedTree");
389
390                 if (tree && tree.model)
391                         return tree.model.getFeedValue(feed, is_cat, key);
392
393         } catch (e) {
394                 //
395         }
396         return '';
397 }
398
399 function setFeedUnread(feed, is_cat, unread) {
400         try {
401                 var tree = dijit.byId("feedTree");
402
403                 if (tree && tree.model)
404                         return tree.model.setFeedUnread(feed, is_cat, unread);
405
406         } catch (e) {
407                 exception_error("setFeedUnread", e);
408         }
409 }
410
411 function setFeedValue(feed, is_cat, key, value) {
412         try {
413                 var tree = dijit.byId("feedTree");
414
415                 if (tree && tree.model)
416                         return tree.model.setFeedValue(feed, is_cat, key, value);
417
418         } catch (e) {
419                 //
420         }
421 }
422
423 function setCatParam(cat, value) {
424         try {
425                 var tree = dijit.byId("feedTree");
426
427                 if (tree && tree.model)
428                         return tree.setCatParam(cat, value);
429
430         } catch (e) {
431                 //
432         }
433 }
434
435 function getCatParam(cat) {
436         try {
437                 return getFeedValue(cat, true, "child_unread");
438         } catch (e) {
439                 //
440         }
441 }
442
443
444 function selectFeed(feed, is_cat) {
445         try {
446                 var tree = dijit.byId("feedTree");
447
448                 if (tree) return tree.selectFeed(feed, is_cat);
449
450         } catch (e) {
451                 exception_error("selectFeed", e);
452         }
453 }
454
455 function setFeedIcon(feed, is_cat, src) {
456         try {
457                 var tree = dijit.byId("feedTree");
458
459                 if (tree) return tree.setFeedIcon(feed, is_cat, src);
460
461         } catch (e) {
462                 exception_error("setFeedIcon", e);
463         }
464 }
465
466 function setFeedExpandoIcon(feed, is_cat, src) {
467         try {
468                 var tree = dijit.byId("feedTree");
469
470                 if (tree) return tree.setFeedExpandoIcon(feed, is_cat, src);
471
472         } catch (e) {
473                 exception_error("setFeedIcon", e);
474         }
475         return false;
476 }
477
478 function getNextUnreadFeed(feed, is_cat) {
479         try {
480                 var tree = dijit.byId("feedTree");
481                 var nuf = tree.model.getNextUnreadFeed(feed, is_cat);
482
483                 if (nuf)
484                         return tree.model.store.getValue(nuf, 'bare_id');
485
486         } catch (e) {
487                 exception_error("getNextUnreadFeed", e);
488         }
489 }
490
491 function catchupCurrentFeed() {
492         return catchupFeed(getActiveFeedId(), activeFeedIsCat());
493 }
494
495 function catchupFeedInGroup(id) {
496         try {
497
498                 var title = getFeedName(id);
499
500                 var str = __("Mark all articles in %s as read?").replace("%s", title);
501
502                 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
503                         return viewCurrentFeed('MarkAllReadGR:' + id);
504                 }
505
506         } catch (e) {
507                 exception_error("catchupFeedInGroup", e);
508         }
509 }
510
511 function catchupFeed(feed, is_cat) {
512         try {
513                 if (is_cat == undefined) is_cat = false;
514
515                 var str = __("Mark all articles in %s as read?");
516                 var fn = getFeedName(feed, is_cat);
517
518                 str = str.replace("%s", fn);
519
520                 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
521                         return;
522                 }
523
524                 var max_id = 0;
525
526                 if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
527                         $$("#headlines-frame > div[id*=RROW]").each(
528                                 function(child) {
529                                         var id = parseInt(child.id.replace("RROW-", ""));
530
531                                         if (id > max_id) max_id = id;
532                                 }
533                         );
534                 }
535
536                 var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" +
537                         feed + "&is_cat=" + is_cat + "&max_id=" + max_id;
538
539                 console.log(catchup_query);
540
541                 notify_progress("Loading, please wait...", true);
542
543                 new Ajax.Request("backend.php", {
544                         parameters: catchup_query,
545                         onComplete: function(transport) {
546                                         handle_rpc_json(transport);
547
548                                         if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
549
550                                                 $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
551                                                         function(child) {
552                                                                 child.removeClassName("Unread");
553                                                         }
554                                                 );
555                                         }
556
557                                         var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1";
558
559                                         if (show_next_feed) {
560                                                 var nuf = getNextUnreadFeed(feed, is_cat);
561
562                                                 if (nuf) {
563                                                         viewfeed(nuf, '', is_cat);
564                                                 }
565                                         }
566
567                                         notify("");
568                                 } });
569
570         } catch (e) {
571                 exception_error("catchupFeed", e);
572         }
573 }
574
575 function decrementFeedCounter(feed, is_cat) {
576         try {
577                 // we have subcats, no way to figure out if this article is
578                 // actually from this category
579                 if (is_cat && getCatParam(feed) > 0)
580                         return;
581
582                 var ctr = getFeedUnread(feed, is_cat);
583
584                 if (ctr > 0) {
585                         setFeedUnread(feed, is_cat, ctr - 1);
586
587                         if (!is_cat) {
588                                 var cat = parseInt(getFeedCategory(feed));
589
590                                 if (!isNaN(cat)) {
591                                         ctr = getFeedUnread(cat, true);
592
593                                         if (ctr > 0) {
594                                                 setFeedUnread(cat, true, ctr - 1);
595                                         }
596                                 }
597                         }
598                 }
599
600                 cache_delete("feed:" + feed + ":" + is_cat);
601
602         } catch (e) {
603                 exception_error("decrement_feed_counter", e);
604         }
605 }