]> git.wh0rd.org Git - tt-rss.git/blob - js/feedlist.js
add counter hack to combined mode
[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                         if (!background && isCdmMode() && getInitParam("cdm_auto_catchup") == 1 && parseInt(getActiveFeedId()) > 0) {
75
76                                 $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
77                                         function(child) {
78                                                 var hf = $("headlines-frame");
79
80                                                 if (hf.scrollTop + hf.offsetHeight >=
81                                                                 child.offsetTop + child.offsetHeight) {
82
83                                                         var id = child.id.replace("RROW-", "");
84
85                                                         if (catchup_id_batch.indexOf(id) == -1)
86                                                                 catchup_id_batch.push(id);
87
88                                                 }
89
90                                                 if (catchup_id_batch.length > 0) {
91                                                         window.clearTimeout(catchup_timeout_id);
92
93                                                         if (!_infscroll_request_sent) {
94                                                                 catchup_timeout_id = window.setTimeout('catchupBatchedArticles()',
95                                                                         2000);
96                                                         }
97                                                 }
98
99                                         });
100                         }
101                 }
102
103                 if (offset == 0 && !background)
104                         dijit.byId("content-tabs").selectChild(
105                                 dijit.byId("content-tabs").getChildren()[0]);
106
107                 if (!background) {
108                         if (getActiveFeedId() != feed || offset == 0) {
109                                 active_post_id = 0;
110                                 _infscroll_disable = 0;
111                         }
112
113                         if (!offset && !method && cached_headlines && !background) {
114                                 try {
115                                         render_local_headlines(feed, is_cat, JSON.parse(cached_headlines));
116                                         return;
117                                 } catch (e) {
118                                         console.warn("render_local_headlines failed: " + e);
119                                 }
120                         }
121
122                         if (offset != 0 && !method) {
123                                 var date = new Date();
124                                 var timestamp = Math.round(date.getTime() / 1000);
125
126                                 if (_infscroll_request_sent && _infscroll_request_sent + 30 > timestamp) {
127                                         //console.log("infscroll request in progress, aborting");
128                                         return;
129                                 }
130
131                                 _infscroll_request_sent = timestamp;
132                         }
133
134                         hideAuxDlg();
135                 }
136
137                 Form.enable("main_toolbar_form");
138
139                 var toolbar_query = Form.serialize("main_toolbar_form");
140
141                 var query = "?op=feeds&method=view&feed=" + feed + "&" +
142                         toolbar_query + "&m=" + param_escape(method);
143
144                 if (!background) {
145                         if (_search_query) {
146                                 force_nocache = true;
147                                 query = query + "&" + _search_query;
148                                 _search_query = false;
149                         }
150
151                         if (offset != 0) {
152                                 query = query + "&skip=" + offset;
153
154                                 // to prevent duplicate feed titles when showing grouped vfeeds
155                                 if (vgroup_last_feed) {
156                                         query = query + "&vgrlf=" + param_escape(vgroup_last_feed);
157                                 }
158                         }
159
160                         Form.enable("main_toolbar_form");
161
162                         if (!offset)
163                                 if (!is_cat) {
164                                         if (!setFeedExpandoIcon(feed, is_cat, 'images/indicator_white.gif'))
165                                                 notify_progress("Loading, please wait...", true);
166                                 } else {
167                                         notify_progress("Loading, please wait...", true);
168                                 }
169                 }
170
171                 query += "&cat=" + is_cat;
172
173                 console.log(query);
174
175                 new Ajax.Request("backend.php", {
176                         parameters: query,
177                         onComplete: function(transport) {
178                                 setFeedExpandoIcon(feed, is_cat, 'images/blank_icon.gif');
179                                 headlines_callback2(transport, offset, background, infscroll_req);
180                         } });
181
182         } catch (e) {
183                 exception_error("viewfeed", e);
184         }
185 }
186
187 function feedlist_init() {
188         try {
189                 console.log("in feedlist init");
190
191                 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
192                 document.onkeydown = hotkey_handler;
193                 setTimeout("hotkey_prefix_timeout()", 5*1000);
194
195                  if (!getActiveFeedId()) {
196                         setTimeout("viewfeed(-3)", 100);
197                 }
198
199                 console.log("T:" +
200                                 getInitParam("cdm_auto_catchup") + " " + getFeedUnread(-3));
201
202                 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
203
204                 setTimeout("timeout()", 5000);
205                 setTimeout("precache_headlines_idle()", 3000);
206
207         } catch (e) {
208                 exception_error("feedlist/init", e);
209         }
210 }
211
212 function request_counters_real() {
213         try {
214                 console.log("requesting counters...");
215
216                 var query = "?op=rpc&method=getAllCounters&seq=" + next_seq();
217
218                 query = query + "&omode=flc";
219
220                 new Ajax.Request("backend.php", {
221                         parameters: query,
222                         onComplete: function(transport) {
223                                 try {
224                                         handle_rpc_json(transport);
225                                 } catch (e) {
226                                         exception_error("viewfeed/getcounters", e);
227                                 }
228                         } });
229
230         } catch (e) {
231                 exception_error("request_counters_real", e);
232         }
233 }
234
235
236 function request_counters() {
237
238         try {
239
240                 if (getInitParam("bw_limit") == "1") return;
241
242                 var date = new Date();
243                 var timestamp = Math.round(date.getTime() / 1000);
244
245                 if (timestamp - counters_last_request > 5) {
246                         console.log("scheduling request of counters...");
247
248                         window.clearTimeout(counter_timeout_id);
249                         counter_timeout_id = window.setTimeout("request_counters_real()", 1000);
250
251                         counters_last_request = timestamp;
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                         // TODO: enable new content notification for categories
301
302                         if (!activeFeedIsCat() && id == getActiveFeedId()
303                                         && ctr > getFeedUnread(id) && scheduled_call) {
304                                 displayNewContentPrompt(id);
305                         }
306
307                         if (getFeedUnread(id, (kind == "cat")) != ctr)
308                                 cache_delete("feed:" + id + ":" + (kind == "cat"));
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                 var str = __("Mark all articles in %s as read?");
483                 var fn = getFeedName(feed, is_cat);
484
485                 str = str.replace("%s", fn);
486
487                 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
488                         return;
489                 }
490
491                 var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" +
492                         feed + "&is_cat=" + is_cat;
493
494                 notify_progress("Loading, please wait...", true);
495
496                 new Ajax.Request("backend.php", {
497                         parameters: catchup_query,
498                         onComplete: function(transport) {
499                                         handle_rpc_json(transport);
500
501                                         if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
502
503                                                 $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
504                                                         function(child) {
505                                                                 child.removeClassName("Unread");
506                                                         }
507                                                 );
508                                         }
509
510                                         var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1";
511
512                                         if (show_next_feed) {
513                                                 var nuf = getNextUnreadFeed(feed, is_cat);
514
515                                                 if (nuf) {
516                                                         viewfeed(nuf, '', is_cat);
517                                                 }
518                                         }
519
520                                         notify("");
521                                 } });
522
523         } catch (e) {
524                 exception_error("catchupFeed", e);
525         }
526 }
527
528 function decrementFeedCounter(feed, is_cat) {
529         try {
530                 var ctr = getFeedUnread(feed, is_cat);
531
532                 if (ctr > 0) {
533                         setFeedUnread(feed, is_cat, ctr - 1);
534
535                         if (!is_cat) {
536                                 var cat = parseInt(getFeedCategory(feed));
537
538                                 if (!isNaN(cat)) {
539                                         ctr = getFeedUnread(cat, true);
540
541                                         if (ctr > 0) {
542                                                 setFeedUnread(cat, true, ctr - 1);
543                                         }
544                                 }
545                         }
546                 }
547
548         } catch (e) {
549                 exception_error("decrement_feed_counter", e);
550         }
551 }