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