]> git.wh0rd.org Git - tt-rss.git/blob - js/feedlist.js
enclosures: rewrite relative urls on import, duh
[tt-rss.git] / js / feedlist.js
1 var _infscroll_disable = 0;
2 var _infscroll_request_sent = 0;
3
4 var _search_query = false;
5 var _viewfeed_last = 0;
6 var _viewfeed_timeout = false;
7
8 var counters_last_request = 0;
9 var _counters_prev = [];
10
11 function resetCounterCache() {
12         _counters_prev = [];
13 }
14
15 function loadMoreHeadlines() {
16         console.log("loadMoreHeadlines");
17
18         var offset = 0;
19
20         var view_mode = document.forms["main_toolbar_form"].view_mode.value;
21         var unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length;
22         var num_all = $$("#headlines-frame > div[id*=RROW]").length;
23         var num_unread = getFeedUnread(getActiveFeedId(), activeFeedIsCat());
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 (_search_query) {
36                 offset = num_all;
37         } else if (view_mode == "adaptive" && !(getActiveFeedId() == -1 && !activeFeedIsCat())) {
38                 // ^ starred feed shows both unread & read articles in adaptive mode
39                 offset = num_unread > 0 ? unread_in_buffer : num_all;
40         } else {
41                 offset = num_all;
42         }
43
44         console.log("offset: " + offset);
45
46         viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat(), offset: offset, infscroll_req: true});
47
48 }
49
50 function cleanup_memory(root) {
51         var dijits = dojo.query("[widgetid]", dijit.byId(root).domNode).map(dijit.byNode);
52
53         dijits.each(function (d) {
54                 dojo.destroy(d.domNode);
55         });
56
57         $$("#" + root + " *").each(function (i) {
58                 i.parentNode ? i.parentNode.removeChild(i) : true;
59         });
60 }
61
62 function viewfeed(params) {
63         var feed = params.feed;
64         var is_cat = params.is_cat;
65         var offset = params.offset;
66         var background = params.background;
67         var infscroll_req = params.infscroll_req;
68         var can_wait = params.can_wait;
69         var viewfeed_debug = params.viewfeed_debug;
70         var method = params.method;
71
72         if (is_cat == undefined)
73                 is_cat = false;
74         else
75                 is_cat = !!is_cat;
76
77         if (offset == undefined) offset = 0;
78         if (background == undefined) background = false;
79         if (infscroll_req == undefined) infscroll_req = false;
80
81         last_requested_article = 0;
82
83         if (feed != getActiveFeedId() || activeFeedIsCat() != is_cat) {
84                 if (!background && _search_query) _search_query = false;
85         }
86
87         if (!background) {
88                 _viewfeed_last = get_timestamp();
89
90                 if (getActiveFeedId() != feed || !infscroll_req) {
91                         setActiveArticleId(0);
92                         _infscroll_disable = 0;
93
94                         cleanup_memory("headlines-frame");
95                         _headlines_scroll_offset = 0;
96                 }
97
98                 if (infscroll_req) {
99                         var timestamp = get_timestamp();
100
101                         if (_infscroll_request_sent && _infscroll_request_sent + 30 > timestamp) {
102                                 //console.log("infscroll request in progress, aborting");
103                                 return;
104                         }
105
106                         _infscroll_request_sent = timestamp;
107                 }
108         }
109
110         Form.enable("main_toolbar_form");
111
112         var toolbar_query = Form.serialize("main_toolbar_form");
113
114         var query = "?op=feeds&method=view&feed=" + param_escape(feed) + "&" +
115                 toolbar_query;
116
117         if (method) query += "&m=" + param_escape(method);
118
119         if (offset > 0) {
120                 if (current_first_id) {
121                         query = query + "&fid=" + param_escape(current_first_id);
122                 }
123         }
124
125         if (!background) {
126                 if (_search_query) {
127                         force_nocache = true;
128                         query = query + "&" + _search_query;
129                         //_search_query = false;
130                 }
131
132                 if (offset != 0) {
133                         query = query + "&skip=" + offset;
134
135                         // to prevent duplicate feed titles when showing grouped vfeeds
136                         if (vgroup_last_feed) {
137                                 query = query + "&vgrlf=" + param_escape(vgroup_last_feed);
138                         }
139                 } else {
140                         if (!is_cat && feed == getActiveFeedId() && !params.method) {
141                                 query = query + "&m=ForceUpdate";
142                         }
143                 }
144
145                 Form.enable("main_toolbar_form");
146
147                 if (!setFeedExpandoIcon(feed, is_cat,
148                         (is_cat) ? 'images/indicator_tiny.gif' : 'images/indicator_white.gif'))
149                                 notify_progress("Loading, please wait...", true);
150         }
151
152         query += "&cat=" + is_cat;
153
154         console.log(query);
155
156         if (can_wait && _viewfeed_timeout) {
157                 setFeedExpandoIcon(getActiveFeedId(), activeFeedIsCat(), 'images/blank_icon.gif');
158                 clearTimeout(_viewfeed_timeout);
159         }
160
161         setActiveFeedId(feed, is_cat);
162
163         if (viewfeed_debug) {
164                 window.open("backend.php" + query + "&debug=1&csrf_token=" + getInitParam("csrf_token"));
165         }
166
167         timeout_ms = can_wait ? 250 : 0;
168         _viewfeed_timeout = setTimeout(function() {
169
170                 new Ajax.Request("backend.php", {
171                         parameters: query,
172                         onComplete: function(transport) {
173                                 try {
174                                         setFeedExpandoIcon(feed, is_cat, 'images/blank_icon.gif');
175                                         headlines_callback2(transport, offset, background, infscroll_req);
176                                         PluginHost.run(PluginHost.HOOK_FEED_LOADED, [feed, is_cat]);
177                                 } catch (e) {
178                                         exception_error(e);
179                                 }
180                         } });
181         }, timeout_ms); // Wait 250ms
182
183 }
184
185 function feedlist_init() {
186         console.log("in feedlist init");
187
188         loading_set_progress(50);
189
190         document.onkeydown = hotkey_handler;
191         setTimeout(hotkey_prefix_timeout, 5*1000);
192
193         if (!getActiveFeedId()) {
194                 viewfeed({feed: -3});
195         } else {
196                 viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat()});
197         }
198
199         hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
200
201         request_counters(true);
202         timeout();
203
204 }
205
206
207 function request_counters(force) {
208         var date = new Date();
209         var timestamp = Math.round(date.getTime() / 1000);
210
211         if (force || timestamp - counters_last_request > 5) {
212                 console.log("scheduling request of counters...");
213
214                 counters_last_request = timestamp;
215
216                 var query = "?op=rpc&method=getAllCounters&seq=" + next_seq();
217
218                 if (!force)
219                         query = query + "&last_article_id=" + getInitParam("last_article_id");
220
221                 console.log(query);
222
223                 new Ajax.Request("backend.php", {
224                         parameters: query,
225                         onComplete: function(transport) {
226                                 handle_rpc_json(transport);
227                         } });
228
229         } else {
230                 console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request));
231         }
232 }
233
234 // NOTE: this implementation is incomplete
235 // for general objects but good enough for counters
236 // http://adripofjavascript.com/blog/drips/object-equality-in-javascript.html
237 function counter_is_equal(a, b) {
238         // Create arrays of property names
239         var aProps = Object.getOwnPropertyNames(a);
240         var bProps = Object.getOwnPropertyNames(b);
241
242         // If number of properties is different,
243         // objects are not equivalent
244         if (aProps.length != bProps.length) {
245                 return false;
246         }
247
248         for (var i = 0; i < aProps.length; i++) {
249                 var propName = aProps[i];
250
251                 // If values of same property are not equal,
252                 // objects are not equivalent
253                 if (a[propName] !== b[propName]) {
254                         return false;
255                 }
256         }
257
258         // If we made it this far, objects
259         // are considered equivalent
260         return true;
261 }
262
263
264 function parse_counters(elems) {
265         for (var l = 0; l < elems.length; l++) {
266
267                 if (_counters_prev[l] && counter_is_equal(elems[l], _counters_prev[l])) {
268                         continue;
269                 }
270
271                 var id = elems[l].id;
272                 var kind = elems[l].kind;
273                 var ctr = parseInt(elems[l].counter);
274                 var error = elems[l].error;
275                 var has_img = elems[l].has_img;
276                 var updated = elems[l].updated;
277                 var auxctr = parseInt(elems[l].auxcounter);
278
279                 if (id == "global-unread") {
280                         global_unread = ctr;
281                         updateTitle();
282                         continue;
283                 }
284
285                 if (id == "subscribed-feeds") {
286                         /* feeds_found = ctr; */
287                         continue;
288                 }
289
290                 /*if (getFeedUnread(id, (kind == "cat")) != ctr ||
291                                 (kind == "cat")) {
292                 }*/
293
294                 setFeedUnread(id, (kind == "cat"), ctr);
295                 setFeedValue(id, (kind == "cat"), 'auxcounter', auxctr);
296
297                 if (kind != "cat") {
298                         setFeedValue(id, false, 'error', error);
299                         setFeedValue(id, false, 'updated', updated);
300
301                         if (id > 0) {
302                                 if (has_img) {
303                                         setFeedIcon(id, false,
304                                                 getInitParam("icons_url") + "/" + id + ".ico");
305                                 } else {
306                                         setFeedIcon(id, false, 'images/blank_icon.gif');
307                                 }
308                         }
309                 }
310         }
311
312         hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
313
314         _counters_prev = elems;
315 }
316
317 function getFeedUnread(feed, is_cat) {
318         try {
319                 var tree = dijit.byId("feedTree");
320
321                 if (tree && tree.model)
322                         return tree.model.getFeedUnread(feed, is_cat);
323
324         } catch (e) {
325                 //
326         }
327
328         return -1;
329 }
330
331 function getFeedCategory(feed) {
332         try {
333                 var tree = dijit.byId("feedTree");
334
335                 if (tree && tree.model)
336                         return tree.getFeedCategory(feed);
337
338         } catch (e) {
339                 //
340         }
341
342         return false;
343 }
344
345 function hideOrShowFeeds(hide) {
346         var tree = dijit.byId("feedTree");
347
348         if (tree)
349                 return tree.hideRead(hide, getInitParam("hide_read_shows_special"));
350 }
351
352 function getFeedName(feed, is_cat) {
353
354         if (isNaN(feed)) return feed; // it's a tag
355
356         var tree = dijit.byId("feedTree");
357
358         if (tree && tree.model)
359                 return tree.model.getFeedValue(feed, is_cat, 'name');
360 }
361
362 function getFeedValue(feed, is_cat, key) {
363         try {
364                 var tree = dijit.byId("feedTree");
365
366                 if (tree && tree.model)
367                         return tree.model.getFeedValue(feed, is_cat, key);
368
369         } catch (e) {
370                 //
371         }
372         return '';
373 }
374
375 function setFeedUnread(feed, is_cat, unread) {
376         var tree = dijit.byId("feedTree");
377
378         if (tree && tree.model)
379                 return tree.model.setFeedUnread(feed, is_cat, unread);
380 }
381
382 function setFeedValue(feed, is_cat, key, value) {
383         try {
384                 var tree = dijit.byId("feedTree");
385
386                 if (tree && tree.model)
387                         return tree.model.setFeedValue(feed, is_cat, key, value);
388
389         } catch (e) {
390                 //
391         }
392 }
393
394 function selectFeed(feed, is_cat) {
395         var tree = dijit.byId("feedTree");
396
397         if (tree) return tree.selectFeed(feed, is_cat);
398 }
399
400 function setFeedIcon(feed, is_cat, src) {
401         var tree = dijit.byId("feedTree");
402
403         if (tree) return tree.setFeedIcon(feed, is_cat, src);
404 }
405
406 function setFeedExpandoIcon(feed, is_cat, src) {
407         var tree = dijit.byId("feedTree");
408
409         if (tree) return tree.setFeedExpandoIcon(feed, is_cat, src);
410
411         return false;
412 }
413
414 function getNextUnreadFeed(feed, is_cat) {
415         var tree = dijit.byId("feedTree");
416         var nuf = tree.model.getNextUnreadFeed(feed, is_cat);
417
418         if (nuf)
419                 return tree.model.store.getValue(nuf, 'bare_id');
420 }
421
422 function catchupCurrentFeed(mode) {
423         catchupFeed(getActiveFeedId(), activeFeedIsCat(), mode);
424 }
425
426 function catchupFeedInGroup(id) {
427         var title = getFeedName(id);
428
429         var str = __("Mark all articles in %s as read?").replace("%s", title);
430
431         if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
432
433                 var rows = $$("#headlines-frame > div[id*=RROW][data-orig-feed-id='"+id+"']");
434
435                 if (rows.length > 0) {
436
437                         rows.each(function (row) {
438                                 row.removeClassName("Unread");
439
440                                 if (row.getAttribute("data-article-id") != getActiveArticleId()) {
441                                         new Effect.Fade(row, {duration: 0.5});
442                                 }
443
444                         });
445
446                         var feedTitles = $$("#headlines-frame > div[class='cdmFeedTitle']");
447
448                         for (var i = 0; i < feedTitles.length; i++) {
449                                 if (feedTitles[i].getAttribute("data-feed-id") == id) {
450
451                                         if (i < feedTitles.length - 1) {
452                                                 new Effect.Fade(feedTitles[i], {duration: 0.5});
453                                         }
454
455                                         break;
456                                 }
457                         }
458
459                         updateFloatingTitle(true);
460                 }
461
462                 var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" +
463                                 id + "&is_cat=false";
464
465                 console.log(catchup_query);
466
467                 notify_progress("Loading, please wait...", true);
468
469                 new Ajax.Request("backend.php", {
470                         parameters: catchup_query,
471                         onComplete: function (transport) {
472                                 handle_rpc_json(transport);
473                         }
474                 } );
475
476                 //return viewCurrentFeed('MarkAllReadGR:' + id);
477         }
478 }
479
480 function catchupFeed(feed, is_cat, mode) {
481         if (is_cat == undefined) is_cat = false;
482
483         var str = false;
484
485         switch (mode) {
486         case "1day":
487                 str = __("Mark all articles in %s older than 1 day as read?");
488                 break;
489         case "1week":
490                 str = __("Mark all articles in %s older than 1 week as read?");
491                 break;
492         case "2week":
493                 str = __("Mark all articles in %s older than 2 weeks as read?");
494                 break;
495         default:
496                 str = __("Mark all articles in %s as read?");
497         }
498
499         var fn = getFeedName(feed, is_cat);
500
501         str = str.replace("%s", fn);
502
503         if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
504                 return;
505         }
506
507         var catchup_query = "?op=rpc&method=catchupFeed&feed_id=" +
508                 feed + "&is_cat=" + is_cat + "&mode=" + mode;
509
510         console.log(catchup_query);
511
512         notify_progress("Loading, please wait...", true);
513
514         new Ajax.Request("backend.php", {
515                 parameters: catchup_query,
516                 onComplete: function(transport) {
517                                 handle_rpc_json(transport);
518
519                                 var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1";
520
521                                 if (show_next_feed) {
522                                         var nuf = getNextUnreadFeed(feed, is_cat);
523
524                                         if (nuf) {
525                                                 viewfeed({feed: nuf, is_cat: is_cat});
526                                         }
527                                 } else {
528                                         if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) {
529                                                 viewCurrentFeed();
530                                         }
531                                 }
532
533                                 notify("");
534                         } });
535
536 }
537
538 function decrementFeedCounter(feed, is_cat) {
539         var ctr = getFeedUnread(feed, is_cat);
540
541         if (ctr > 0) {
542                 setFeedUnread(feed, is_cat, ctr - 1);
543                 global_unread = global_unread - 1;
544                 updateTitle();
545
546                 if (!is_cat) {
547                         var cat = parseInt(getFeedCategory(feed));
548
549                         if (!isNaN(cat)) {
550                                 ctr = getFeedUnread(cat, true);
551
552                                 if (ctr > 0) {
553                                         setFeedUnread(cat, true, ctr - 1);
554                                 }
555                         }
556                 }
557         }
558
559 }
560
561