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