]> git.wh0rd.org Git - tt-rss.git/blob - js/viewfeed.js
916ff7dfc43b357ab88ceb8bf28c23c01cd6a659
[tt-rss.git] / js / viewfeed.js
1 /* global dijit, __ */
2
3 let _active_article_id = 0;
4
5 let vgroup_last_feed = false;
6 let post_under_pointer = false;
7
8 let last_requested_article = false;
9
10 let catchup_id_batch = [];
11 let catchup_timeout_id = false;
12
13 let cids_requested = [];
14 let loaded_article_ids = [];
15 let _last_headlines_update = 0;
16 let _headlines_scroll_offset = 0;
17 let current_first_id = 0;
18 let last_search_query;
19
20 let _catchup_request_sent = false;
21
22 let has_storage = 'sessionStorage' in window && window['sessionStorage'] !== null;
23
24 function headlines_callback2(transport, offset, background, infscroll_req) {
25         handle_rpc_json(transport);
26
27         console.log("headlines_callback2 [offset=" + offset + "] B:" + background + " I:" + infscroll_req);
28
29         let is_cat = false;
30         let feed_id = false;
31
32         let reply = false;
33
34         try {
35                 reply = JSON.parse(transport.responseText);
36         } catch (e) {
37                 console.error(e);
38         }
39
40         if (reply) {
41
42                 is_cat = reply['headlines']['is_cat'];
43                 feed_id = reply['headlines']['id'];
44                 last_search_query = reply['headlines']['search_query'];
45
46                 if (background) {
47                         let content = reply['headlines']['content'];
48
49                         content = content + "<div id='headlines-spacer'></div>";
50                         return;
51                 }
52
53                 if (feed_id != -7 && (feed_id != getActiveFeedId() || is_cat != activeFeedIsCat()))
54                         return;
55
56                 /* dijit.getEnclosingWidget(
57                         document.forms["main_toolbar_form"].update).attr('disabled',
58                                 is_cat || feed_id <= 0); */
59
60                 try {
61                         if (infscroll_req == false) {
62                                 $("headlines-frame").scrollTop = 0;
63
64                                 $("floatingTitle").style.visibility = "hidden";
65                                 $("floatingTitle").setAttribute("data-article-id", 0);
66                                 $("floatingTitle").innerHTML = "";
67                         }
68                 } catch (e) { }
69
70                 $("headlines-frame").removeClassName("cdm");
71                 $("headlines-frame").removeClassName("normal");
72
73                 $("headlines-frame").addClassName(isCdmMode() ? "cdm" : "normal");
74
75                 const headlines_count = reply['headlines-info']['count'];
76
77                 vgroup_last_feed = reply['headlines-info']['vgroup_last_feed'];
78
79                 if (parseInt(headlines_count) < 30) {
80                         _infscroll_disable = 1;
81                 } else {
82                         _infscroll_disable = 0;
83                 }
84
85                 current_first_id = reply['headlines']['first_id'];
86                 const counters = reply['counters'];
87                 const articles = reply['articles'];
88                 //var runtime_info = reply['runtime-info'];
89
90                 if (infscroll_req == false) {
91                         loaded_article_ids = [];
92
93                         dojo.html.set($("headlines-toolbar"),
94                                         reply['headlines']['toolbar'],
95                                         {parseContent: true});
96
97                         /*dojo.html.set($("headlines-frame"),
98                                 reply['headlines']['content'],
99                                 {parseContent: true});
100
101                         $$("#headlines-frame div[id*='RROW']").each(function(row) {
102                                 loaded_article_ids.push(row.id);
103                         });*/
104
105                         $("headlines-frame").innerHTML = '';
106
107                         var tmp = new Element("div");
108                         tmp.innerHTML = reply['headlines']['content'];
109                         dojo.parser.parse(tmp);
110
111                         while (tmp.hasChildNodes()) {
112                                 var row = tmp.removeChild(tmp.firstChild);
113
114                                 if (loaded_article_ids.indexOf(row.id) == -1 || row.hasClassName("cdmFeedTitle")) {
115                                         dijit.byId("headlines-frame").domNode.appendChild(row);
116
117                                         loaded_article_ids.push(row.id);
118                                 }
119                         }
120
121                         var hsp = $("headlines-spacer");
122                         if (!hsp) hsp = new Element("DIV", {"id": "headlines-spacer"});
123                         dijit.byId('headlines-frame').domNode.appendChild(hsp);
124
125                         initHeadlinesMenu();
126
127                         if (_infscroll_disable)
128                                 hsp.innerHTML = "<a href='#' onclick='openNextUnreadFeed()'>" +
129                                         __("Click to open next unread feed.") + "</a>";
130
131                         if (_search_query) {
132                                 $("feed_title").innerHTML += "<span id='cancel_search'>" +
133                                         " (<a href='#' onclick='cancelSearch()'>" + __("Cancel search") + "</a>)" +
134                                         "</span>";
135                         }
136
137                 } else if (headlines_count > 0 && feed_id == getActiveFeedId() && is_cat == activeFeedIsCat()) {
138                                 console.log("adding some more headlines: " + headlines_count);
139
140                                 const c = dijit.byId("headlines-frame");
141                                 const ids = getSelectedArticleIds2();
142
143                                 var hsp = $("headlines-spacer");
144
145                                 if (hsp)
146                                         c.domNode.removeChild(hsp);
147
148                                 var tmp = new Element("div");
149                                 tmp.innerHTML = reply['headlines']['content'];
150                                 dojo.parser.parse(tmp);
151
152                                 while (tmp.hasChildNodes()) {
153                                         var row = tmp.removeChild(tmp.firstChild);
154
155                                         if (loaded_article_ids.indexOf(row.id) == -1 || row.hasClassName("cdmFeedTitle")) {
156                                                 dijit.byId("headlines-frame").domNode.appendChild(row);
157
158                                                 loaded_article_ids.push(row.id);
159                                         }
160                                 }
161
162                                 if (!hsp) hsp = new Element("DIV", {"id": "headlines-spacer"});
163                                 c.domNode.appendChild(hsp);
164
165                                 if (headlines_count < 30) _infscroll_disable = true;
166
167                                 console.log("restore selected ids: " + ids);
168
169                                 for (var i = 0; i < ids.length; i++) {
170                                         markHeadline(ids[i]);
171                                 }
172
173                                 initHeadlinesMenu();
174
175                                 if (_infscroll_disable) {
176                                         hsp.innerHTML = "<a href='#' onclick='openNextUnreadFeed()'>" +
177                                         __("Click to open next unread feed.") + "</a>";
178                                 }
179
180                         } else {
181                                 console.log("no new headlines received");
182
183                                 const first_id_changed = reply['headlines']['first_id_changed'];
184                                 console.log("first id changed:" + first_id_changed);
185
186                                 var hsp = $("headlines-spacer");
187
188                                 if (hsp) {
189                                         if (first_id_changed) {
190                                                 hsp.innerHTML = "<a href='#' onclick='viewCurrentFeed()'>" +
191                                                 __("New articles found, reload feed to continue.") + "</a>";
192                                         } else {
193                                                 hsp.innerHTML = "<a href='#' onclick='openNextUnreadFeed()'>" +
194                                                 __("Click to open next unread feed.") + "</a>";
195                                         }
196
197                                 }
198
199                         }
200
201                 if (articles) {
202                         for (var i = 0; i < articles.length; i++) {
203                                 const a_id = articles[i]['id'];
204                                 cache_set("article:" + a_id, articles[i]['content']);
205                         }
206                 } else {
207                         console.log("no cached articles received");
208                 }
209
210                 if (counters)
211                         parse_counters(counters);
212                 else
213                         request_counters();
214
215         } else {
216                 console.error("Invalid object received: " + transport.responseText);
217                 dijit.byId("headlines-frame").attr('content', "<div class='whiteBox'>" +
218                                 __('Could not update headlines (invalid object received - see error console for details)') +
219                                 "</div>");
220         }
221
222         _infscroll_request_sent = 0;
223         _last_headlines_update = new Date().getTime();
224
225         unpackVisibleHeadlines();
226
227         // if we have some more space in the buffer, why not try to fill it
228
229         if (!_infscroll_disable && $("headlines-spacer") &&
230                         $("headlines-spacer").offsetTop < $("headlines-frame").offsetHeight) {
231
232                 window.setTimeout(function() {
233                         loadMoreHeadlines();
234                 }, 250);
235         }
236
237         notify("");
238 }
239
240 function render_article(article) {
241         cleanup_memory("content-insert");
242
243         dijit.byId("headlines-wrap-inner").addChild(
244                         dijit.byId("content-insert"));
245
246         const c = dijit.byId("content-insert");
247
248         try {
249                 c.domNode.scrollTop = 0;
250         } catch (e) { }
251
252         c.attr('content', article);
253         PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED, c.domNode);
254
255         correctHeadlinesOffset(getActiveArticleId());
256
257         try {
258                 c.focus();
259         } catch (e) { }
260 }
261
262 function showArticleInHeadlines(id, noexpand) {
263         const row = $("RROW-" + id);
264         if (!row) return;
265
266         if (!noexpand)
267                 row.removeClassName("Unread");
268
269         row.addClassName("active");
270
271         selectArticles('none');
272
273         markHeadline(id);
274 }
275
276 function article_callback2(transport, id) {
277         console.log("article_callback2 " + id);
278
279         handle_rpc_json(transport);
280
281         let reply = false;
282
283         try {
284                 reply = JSON.parse(transport.responseText);
285         } catch (e) {
286                 console.error(e);
287         }
288
289         if (reply) {
290
291                 reply.each(function(article) {
292                         if (getActiveArticleId() == article['id']) {
293                                 render_article(article['content']);
294                         }
295                         cids_requested.remove(article['id']);
296
297                         cache_set("article:" + article['id'], article['content']);
298                 });
299
300 //                      if (id != last_requested_article) {
301 //                              console.log("requested article id is out of sequence, aborting");
302 //                              return;
303 //                      }
304
305         } else {
306                 console.error("Invalid object received: " + transport.responseText);
307
308                 render_article("<div class='whiteBox'>" +
309                                 __('Could not display article (invalid object received - see error console for details)') + "</div>");
310         }
311
312         const unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length
313         request_counters(unread_in_buffer == 0);
314
315         notify("");
316 }
317
318 function view(id, activefeed, noexpand) {
319         const oldrow = $("RROW-" + getActiveArticleId());
320         if (oldrow) oldrow.removeClassName("active");
321
322         const crow = $("RROW-" + id);
323
324         if (!crow) return;
325         if (noexpand) {
326                 setActiveArticleId(id);
327                 showArticleInHeadlines(id, noexpand);
328                 return;
329         }
330
331         console.log("loading article: " + id);
332
333         const cached_article = cache_get("article:" + id);
334
335         console.log("cache check result: " + (cached_article != false));
336
337         let query = "?op=article&method=view&id=" + param_escape(id);
338
339         const neighbor_ids = getRelativePostIds(id);
340
341         /* only request uncached articles */
342
343         const cids_to_request = [];
344
345         for (let i = 0; i < neighbor_ids.length; i++) {
346                 if (cids_requested.indexOf(neighbor_ids[i]) == -1)
347                         if (!cache_get("article:" + neighbor_ids[i])) {
348                                 cids_to_request.push(neighbor_ids[i]);
349                                 cids_requested.push(neighbor_ids[i]);
350                         }
351         }
352
353         console.log("additional ids: " + cids_to_request.toString());
354
355         query = query + "&cids=" + cids_to_request.toString();
356
357         const article_is_unread = crow.hasClassName("Unread");
358
359         setActiveArticleId(id);
360         showArticleInHeadlines(id);
361
362         if (cached_article && article_is_unread) {
363
364                 query = query + "&mode=prefetch";
365
366                 render_article(cached_article);
367
368         } else if (cached_article) {
369
370                 query = query + "&mode=prefetch_old";
371                 render_article(cached_article);
372
373                 // if we don't need to request any relative ids, we might as well skip
374                 // the server roundtrip altogether
375                 if (cids_to_request.length == 0) {
376                         return;
377                 }
378         }
379
380         last_requested_article = id;
381
382         console.log(query);
383
384         if (article_is_unread) {
385                 decrementFeedCounter(getActiveFeedId(), activeFeedIsCat());
386         }
387
388         new Ajax.Request("backend.php", {
389                 parameters: query,
390                 onComplete: function(transport) {
391                         article_callback2(transport, id);
392                 } });
393
394         return false;
395
396 }
397
398 function toggleMark(id, client_only) {
399         let query = "?op=rpc&id=" + id + "&method=mark";
400
401         const row = $("RROW-" + id);
402         if (!row) return;
403
404         const imgs = [];
405
406         const row_imgs = row.getElementsByClassName("markedPic");
407
408         for (var i = 0; i < row_imgs.length; i++)
409                 imgs.push(row_imgs[i]);
410
411         const ft = $("floatingTitle");
412
413         if (ft && ft.getAttribute("data-article-id") == id) {
414                 const fte = ft.getElementsByClassName("markedPic");
415
416                 for (var i = 0; i < fte.length; i++)
417                         imgs.push(fte[i]);
418         }
419
420         for (i = 0; i < imgs.length; i++) {
421                 const img = imgs[i];
422
423                 if (!row.hasClassName("marked")) {
424                         img.src = img.src.replace("mark_unset", "mark_set");
425                         query = query + "&mark=1";
426                 } else {
427                         img.src = img.src.replace("mark_set", "mark_unset");
428                         query = query + "&mark=0";
429                 }
430         }
431
432         row.toggleClassName("marked");
433
434         if (!client_only) {
435                 new Ajax.Request("backend.php", {
436                         parameters: query,
437                         onComplete: function (transport) {
438                                 handle_rpc_json(transport);
439                         }
440                 });
441         }
442 }
443
444 function togglePub(id, client_only, no_effects, note) {
445         let query = "?op=rpc&id=" + id + "&method=publ";
446
447         if (note != undefined) {
448                 query = query + "&note=" + param_escape(note);
449         } else {
450                 query = query + "&note=undefined";
451         }
452
453         const row = $("RROW-" + id);
454         if (!row) return;
455
456         const imgs = [];
457
458         const row_imgs = row.getElementsByClassName("pubPic");
459
460         for (var i = 0; i < row_imgs.length; i++)
461                 imgs.push(row_imgs[i]);
462
463         const ft = $("floatingTitle");
464
465         if (ft && ft.getAttribute("data-article-id") == id) {
466                 const fte = ft.getElementsByClassName("pubPic");
467
468                 for (var i = 0; i < fte.length; i++)
469                         imgs.push(fte[i]);
470         }
471
472         for (var i = 0; i < imgs.length; i++) {
473                 const img = imgs[i];
474
475                 if (!row.hasClassName("published") || note != undefined) {
476                         img.src = img.src.replace("pub_unset", "pub_set");
477                         query = query + "&pub=1";
478                 } else {
479                         img.src = img.src.replace("pub_set", "pub_unset");
480                         query = query + "&pub=0";
481                 }
482         }
483
484         if (note != undefined)
485                 row.addClassName("published");
486         else
487                 row.toggleClassName("published");
488
489         if (!client_only) {
490                 new Ajax.Request("backend.php", {
491                         parameters: query,
492                         onComplete: function(transport) {
493                                 handle_rpc_json(transport);
494                         } });
495         }
496
497 }
498
499 function moveToPost(mode, noscroll, noexpand) {
500         const rows = getLoadedArticleIds();
501
502         let prev_id = false;
503         let next_id = false;
504
505         if (!$('RROW-' + getActiveArticleId())) {
506                 setActiveArticleId(0);
507         }
508
509         if (!getActiveArticleId()) {
510                 next_id = rows[0];
511                 prev_id = rows[rows.length-1]
512         } else {
513                 for (let i = 0; i < rows.length; i++) {
514                         if (rows[i] == getActiveArticleId()) {
515
516                                 // Account for adjacent identical article ids.
517                                 if (i > 0) prev_id = rows[i-1];
518
519                                 for (let j = i+1; j < rows.length; j++) {
520                                         if (rows[j] != getActiveArticleId()) {
521                                                 next_id = rows[j];
522                                                 break;
523                                         }
524                                 }
525                                 break;
526                         }
527                 }
528         }
529
530         console.log("cur: " + getActiveArticleId() + " next: " + next_id);
531
532         if (mode == "next") {
533                 if (next_id || getActiveArticleId()) {
534                         if (isCdmMode()) {
535
536                                 var article = $("RROW-" + getActiveArticleId());
537                                 var ctr = $("headlines-frame");
538
539                                 if (!noscroll && article && article.offsetTop + article.offsetHeight >
540                                                 ctr.scrollTop + ctr.offsetHeight) {
541
542                                         scrollArticle(ctr.offsetHeight/4);
543
544                                 } else if (next_id) {
545                                         cdmExpandArticle(next_id, noexpand);
546                                         cdmScrollToArticleId(next_id, true);
547                                 }
548
549                         } else if (next_id) {
550                                 correctHeadlinesOffset(next_id);
551                                 view(next_id, getActiveFeedId(), noexpand);
552                         }
553                 }
554         }
555
556         if (mode == "prev") {
557                 if (prev_id || getActiveArticleId()) {
558                         if (isCdmMode()) {
559
560                                 var article = $("RROW-" + getActiveArticleId());
561                                 const prev_article = $("RROW-" + prev_id);
562                                 var ctr = $("headlines-frame");
563
564                                 if (!getInitParam("cdm_expanded")) {
565
566                                         if (!noscroll && article && article.offsetTop < ctr.scrollTop) {
567                                                 scrollArticle(-ctr.offsetHeight/4);
568                                         } else {
569                                                 cdmExpandArticle(prev_id, noexpand);
570                                                 cdmScrollToArticleId(prev_id, true);
571                                         }
572                                 } else if (!noscroll && article && article.offsetTop < ctr.scrollTop) {
573                                                 scrollArticle(-ctr.offsetHeight/3);
574                                         } else if (!noscroll && prev_article &&
575                                                         prev_article.offsetTop < ctr.scrollTop) {
576                                                 cdmExpandArticle(prev_id, noexpand);
577                                                 scrollArticle(-ctr.offsetHeight/4);
578                                         } else if (prev_id) {
579                                                 cdmExpandArticle(prev_id, noexpand);
580                                                 cdmScrollToArticleId(prev_id, noscroll);
581                                         }
582
583                         } else if (prev_id) {
584                                 correctHeadlinesOffset(prev_id);
585                                 view(prev_id, getActiveFeedId(), noexpand);
586                         }
587                 }
588         }
589
590 }
591
592 function toggleSelected(id, force_on) {
593         const row = $("RROW-" + id);
594
595         if (row) {
596                 const cb = dijit.getEnclosingWidget(
597                                 row.getElementsByClassName("rchk")[0]);
598
599                 if (row.hasClassName('Selected') && !force_on) {
600                         row.removeClassName('Selected');
601                         if (cb) cb.attr("checked", false);
602                 } else {
603                         row.addClassName('Selected');
604                         if (cb) cb.attr("checked", true);
605                 }
606         }
607
608         updateSelectedPrompt();
609 }
610
611 function updateSelectedPrompt() {
612         const count = getSelectedArticleIds2().length;
613         const elem = $("selected_prompt");
614
615         if (elem) {
616                 elem.innerHTML = ngettext("%d article selected",
617                                 "%d articles selected", count).replace("%d", count);
618
619                 if (count > 0)
620                         Element.show(elem);
621                 else
622                         Element.hide(elem);
623         }
624
625 }
626
627 function toggleUnread(id, cmode, effect) {
628         const row = $("RROW-" + id);
629         if (row) {
630                 const tmpClassName = row.className;
631
632                 if (cmode == undefined || cmode == 2) {
633                         if (row.hasClassName("Unread")) {
634                                 row.removeClassName("Unread");
635
636                         } else {
637                                 row.addClassName("Unread");
638                         }
639
640                 } else if (cmode == 0) {
641
642                         row.removeClassName("Unread");
643
644                 } else if (cmode == 1) {
645                         row.addClassName("Unread");
646                 }
647
648                 if (cmode == undefined) cmode = 2;
649
650                 const query = "?op=rpc&method=catchupSelected" +
651                         "&cmode=" + param_escape(cmode) + "&ids=" + param_escape(id);
652
653 //                      notify_progress("Loading, please wait...");
654
655                 if (tmpClassName != row.className) {
656                         new Ajax.Request("backend.php", {
657                                 parameters: query,
658                                 onComplete: function (transport) {
659                                         handle_rpc_json(transport);
660                                 }
661                         });
662                 }
663
664         }
665 }
666
667 function selectionRemoveLabel(id, ids) {
668         if (!ids) ids = getSelectedArticleIds2();
669
670         if (ids.length == 0) {
671                 alert(__("No articles are selected."));
672                 return;
673         }
674
675         const query = "?op=article&method=removeFromLabel&ids=" +
676                 param_escape(ids.toString()) + "&lid=" + param_escape(id);
677
678         console.log(query);
679
680         new Ajax.Request("backend.php", {
681                 parameters: query,
682                 onComplete: function(transport) {
683                         handle_rpc_json(transport);
684                         show_labels_in_headlines(transport);
685                 } });
686
687 }
688
689 function selectionAssignLabel(id, ids) {
690         if (!ids) ids = getSelectedArticleIds2();
691
692         if (ids.length == 0) {
693                 alert(__("No articles are selected."));
694                 return;
695         }
696
697         const query = "?op=article&method=assignToLabel&ids=" +
698                 param_escape(ids.toString()) + "&lid=" + param_escape(id);
699
700         console.log(query);
701
702         new Ajax.Request("backend.php", {
703                 parameters: query,
704                 onComplete: function(transport) {
705                         handle_rpc_json(transport);
706                         show_labels_in_headlines(transport);
707                 } });
708 }
709
710 function selectionToggleUnread(set_state, callback, no_error, ids) {
711         const rows = ids ? ids : getSelectedArticleIds2();
712
713         if (rows.length == 0 && !no_error) {
714                 alert(__("No articles are selected."));
715                 return;
716         }
717
718         for (let i = 0; i < rows.length; i++) {
719                 const row = $("RROW-" + rows[i]);
720                 if (row) {
721                         if (set_state == undefined) {
722                                 if (row.hasClassName("Unread")) {
723                                         row.removeClassName("Unread");
724                                 } else {
725                                         row.addClassName("Unread");
726                                 }
727                         }
728
729                         if (set_state == false) {
730                                 row.removeClassName("Unread");
731                         }
732
733                         if (set_state == true) {
734                                 row.addClassName("Unread");
735                         }
736                 }
737         }
738
739         updateFloatingTitle(true);
740
741         if (rows.length > 0) {
742
743                 let cmode = "";
744
745                 if (set_state == undefined) {
746                         cmode = "2";
747                 } else if (set_state == true) {
748                         cmode = "1";
749                 } else if (set_state == false) {
750                         cmode = "0";
751                 }
752
753                 const query = "?op=rpc&method=catchupSelected" +
754                         "&cmode=" + cmode + "&ids=" + param_escape(rows.toString());
755
756                 notify_progress("Loading, please wait...");
757
758                 new Ajax.Request("backend.php", {
759                         parameters: query,
760                         onComplete: function(transport) {
761                                 handle_rpc_json(transport);
762                                 if (callback) callback(transport);
763                         } });
764
765         }
766 }
767
768 // sel_state ignored
769 function selectionToggleMarked(sel_state, callback, no_error, ids) {
770         const rows = ids ? ids : getSelectedArticleIds2();
771
772         if (rows.length == 0 && !no_error) {
773                 alert(__("No articles are selected."));
774                 return;
775         }
776
777         for (let i = 0; i < rows.length; i++) {
778                 toggleMark(rows[i], true, true);
779         }
780
781         if (rows.length > 0) {
782
783                 const query = "?op=rpc&method=markSelected&ids=" +
784                         param_escape(rows.toString()) + "&cmode=2";
785
786                 new Ajax.Request("backend.php", {
787                         parameters: query,
788                         onComplete: function(transport) {
789                                 handle_rpc_json(transport);
790                                 if (callback) callback(transport);
791                         } });
792
793         }
794 }
795
796 // sel_state ignored
797 function selectionTogglePublished(sel_state, callback, no_error, ids) {
798         const rows = ids ? ids : getSelectedArticleIds2();
799
800         if (rows.length == 0 && !no_error) {
801                 alert(__("No articles are selected."));
802                 return;
803         }
804
805         for (let i = 0; i < rows.length; i++) {
806                 togglePub(rows[i], true, true);
807         }
808
809         if (rows.length > 0) {
810
811                 const query = "?op=rpc&method=publishSelected&ids=" +
812                         param_escape(rows.toString()) + "&cmode=2";
813
814                 new Ajax.Request("backend.php", {
815                         parameters: query,
816                         onComplete: function(transport) {
817                                 handle_rpc_json(transport);
818                         } });
819
820         }
821 }
822
823 function getSelectedArticleIds2() {
824
825         const rv = [];
826
827         $$("#headlines-frame > div[id*=RROW][class*=Selected]").each(
828                 function(child) {
829                         rv.push(child.getAttribute("data-article-id"));
830                 });
831
832         return rv;
833 }
834
835 function getLoadedArticleIds() {
836         const rv = [];
837
838         const children = $$("#headlines-frame > div[id*=RROW-]");
839
840         children.each(function(child) {
841                 if (Element.visible(child)) {
842                         rv.push(child.getAttribute("data-article-id"));
843                 }
844         });
845
846         return rv;
847
848 }
849
850 // mode = all,none,unread,invert,marked,published
851 function selectArticles(mode, query) {
852         if (!query) query = "#headlines-frame > div[id*=RROW]";
853
854         const children = $$(query);
855
856         children.each(function(child) {
857                 const id = child.getAttribute("data-article-id");
858
859                 const cb = dijit.getEnclosingWidget(
860                                 child.getElementsByClassName("rchk")[0]);
861
862                 if (mode == "all") {
863                         child.addClassName("Selected");
864                         if (cb) cb.attr("checked", true);
865                 } else if (mode == "unread") {
866                         if (child.hasClassName("Unread")) {
867                                 child.addClassName("Selected");
868                                 if (cb) cb.attr("checked", true);
869                         } else {
870                                 child.removeClassName("Selected");
871                                 if (cb) cb.attr("checked", false);
872                         }
873                 } else if (mode == "marked") {
874                         if (child.hasClassName("marked")) {
875                                 child.addClassName("Selected");
876                                 if (cb) cb.attr("checked", true);
877                         } else {
878                                 child.removeClassName("Selected");
879                                 if (cb) cb.attr("checked", false);
880                         }
881                 } else if (mode == "published") {
882                         if (child.hasClassName("published")) {
883                                 child.addClassName("Selected");
884                                 if (cb) cb.attr("checked", true);
885                         } else {
886                                 child.removeClassName("Selected");
887                                 if (cb) cb.attr("checked", false);
888                         }
889
890                 } else if (mode == "invert") {
891                         if (child.hasClassName("Selected")) {
892                                 child.removeClassName("Selected");
893                                 if (cb) cb.attr("checked", false);
894                         } else {
895                                 child.addClassName("Selected");
896                                 if (cb) cb.attr("checked", true);
897                         }
898
899                 } else {
900                         child.removeClassName("Selected");
901                         if (cb) cb.attr("checked", false);
902                 }
903         });
904
905         updateSelectedPrompt();
906 }
907
908 function deleteSelection() {
909
910         const rows = getSelectedArticleIds2();
911
912         if (rows.length == 0) {
913                 alert(__("No articles are selected."));
914                 return;
915         }
916
917         const fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
918         let str;
919
920         if (getActiveFeedId() != 0) {
921                 str = ngettext("Delete %d selected article in %s?", "Delete %d selected articles in %s?", rows.length);
922         } else {
923                 str = ngettext("Delete %d selected article?", "Delete %d selected articles?", rows.length);
924         }
925
926         str = str.replace("%d", rows.length);
927         str = str.replace("%s", fn);
928
929         if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
930                 return;
931         }
932
933         const query = "?op=rpc&method=delete&ids=" + param_escape(rows);
934
935         console.log(query);
936
937         new Ajax.Request("backend.php", {
938                 parameters: query,
939                 onComplete: function (transport) {
940                         handle_rpc_json(transport);
941                         viewCurrentFeed();
942                 }
943         });
944 }
945
946 function archiveSelection() {
947
948         const rows = getSelectedArticleIds2();
949
950         if (rows.length == 0) {
951                 alert(__("No articles are selected."));
952                 return;
953         }
954
955         const fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
956         let str;
957         let op;
958
959         if (getActiveFeedId() != 0) {
960                 str = ngettext("Archive %d selected article in %s?", "Archive %d selected articles in %s?", rows.length);
961                 op = "archive";
962         } else {
963                 str = ngettext("Move %d archived article back?", "Move %d archived articles back?", rows.length);
964
965                 str += " " + __("Please note that unstarred articles might get purged on next feed update.");
966
967                 op = "unarchive";
968         }
969
970         str = str.replace("%d", rows.length);
971         str = str.replace("%s", fn);
972
973         if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
974                 return;
975         }
976
977         const query = "?op=rpc&method="+op+"&ids=" + param_escape(rows);
978
979         console.log(query);
980
981         for (let i = 0; i < rows.length; i++) {
982                 cache_delete("article:" + rows[i]);
983         }
984
985         new Ajax.Request("backend.php", {
986                 parameters: query,
987                 onComplete: function(transport) {
988                                 handle_rpc_json(transport);
989                                 viewCurrentFeed();
990                         } });
991
992 }
993
994 function catchupSelection() {
995
996         const rows = getSelectedArticleIds2();
997
998         if (rows.length == 0) {
999                 alert(__("No articles are selected."));
1000                 return;
1001         }
1002
1003         const fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1004
1005         let str = ngettext("Mark %d selected article in %s as read?", "Mark %d selected articles in %s as read?", rows.length);
1006
1007         str = str.replace("%d", rows.length);
1008         str = str.replace("%s", fn);
1009
1010         if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1011                 return;
1012         }
1013
1014         selectionToggleUnread(false, 'viewCurrentFeed()', true);
1015 }
1016
1017 function editArticleTags(id) {
1018         const query = "backend.php?op=article&method=editArticleTags&param=" + param_escape(id);
1019
1020         if (dijit.byId("editTagsDlg"))
1021                 dijit.byId("editTagsDlg").destroyRecursive();
1022
1023         var dialog = new dijit.Dialog({
1024                 id: "editTagsDlg",
1025                 title: __("Edit article Tags"),
1026                 style: "width: 600px",
1027                 execute: function() {
1028                         if (this.validate()) {
1029                                 const query = dojo.objectToQuery(this.attr('value'));
1030
1031                                 notify_progress("Saving article tags...", true);
1032
1033                                 new Ajax.Request("backend.php", {
1034                                 parameters: query,
1035                                 onComplete: function(transport) {
1036                                         try {
1037                                                 notify('');
1038                                                 dialog.hide();
1039
1040                                                 const data = JSON.parse(transport.responseText);
1041
1042                                                 if (data) {
1043                                                         const id = data.id;
1044
1045                                                         console.log(id);
1046
1047                                                         const tags = $("ATSTR-" + id);
1048                                                         const tooltip = dijit.byId("ATSTRTIP-" + id);
1049
1050                                                         if (tags) tags.innerHTML = data.content;
1051                                                         if (tooltip) tooltip.attr('label', data.content_full);
1052                                                 }
1053                                         } catch (e) {
1054                                                 exception_error(e);
1055                                         }
1056
1057                                 }});
1058                         }
1059                 },
1060                 href: query
1061         });
1062
1063         var tmph = dojo.connect(dialog, 'onLoad', function() {
1064                 dojo.disconnect(tmph);
1065
1066                 new Ajax.Autocompleter('tags_str', 'tags_choices',
1067                    "backend.php?op=article&method=completeTags",
1068                    { tokens: ',', paramName: "search" });
1069         });
1070
1071         dialog.show();
1072
1073 }
1074
1075 function cdmScrollToArticleId(id, force) {
1076         const ctr = $("headlines-frame");
1077         const e = $("RROW-" + id);
1078
1079         if (!e || !ctr) return;
1080
1081         if (force || e.offsetTop+e.offsetHeight > (ctr.scrollTop+ctr.offsetHeight) ||
1082                         e.offsetTop < ctr.scrollTop) {
1083
1084                 // expanded cdm has a 4px margin now
1085                 ctr.scrollTop = parseInt(e.offsetTop) - 4;
1086         }
1087 }
1088
1089 function setActiveArticleId(id) {
1090         console.log("setActiveArticleId:" + id);
1091
1092         _active_article_id = id;
1093         PluginHost.run(PluginHost.HOOK_ARTICLE_SET_ACTIVE, _active_article_id);
1094 }
1095
1096 function getActiveArticleId() {
1097         return _active_article_id;
1098 }
1099
1100 function postMouseIn(e, id) {
1101         post_under_pointer = id;
1102 }
1103
1104 function postMouseOut(id) {
1105         post_under_pointer = false;
1106 }
1107
1108 function unpackVisibleHeadlines() {
1109         if (!isCdmMode() || !getInitParam("cdm_expanded")) return;
1110
1111         $$("#headlines-frame span.cencw[id]").each(
1112                 function (child) {
1113                         const row = $("RROW-" + child.id.replace("CENCW-", ""));
1114
1115                         if (row && row.offsetTop <= $("headlines-frame").scrollTop +
1116                                 $("headlines-frame").offsetHeight) {
1117
1118                                 //console.log("unpacking: " + child.id);
1119
1120                                 child.innerHTML = htmlspecialchars_decode(child.innerHTML);
1121                                 child.removeAttribute('id');
1122
1123                                 PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED_CDM, row);
1124
1125                                 Element.show(child);
1126                         }
1127                 }
1128         );
1129 }
1130
1131 function headlines_scroll_handler(e) {
1132         try {
1133
1134                 // rate-limit in case of smooth scrolling and similar abominations
1135                 if (Math.max(e.scrollTop, _headlines_scroll_offset) - Math.min(e.scrollTop, _headlines_scroll_offset) < 25) {
1136                         return;
1137                 }
1138
1139                 _headlines_scroll_offset = e.scrollTop;
1140
1141                 const hsp = $("headlines-spacer");
1142
1143                 unpackVisibleHeadlines();
1144
1145                 // set topmost child in the buffer as active
1146                 if (isCdmMode() && getInitParam("cdm_auto_catchup") == 1 &&
1147                                 getSelectedArticleIds2().length <= 1 &&
1148                                 getInitParam("cdm_expanded")) {
1149
1150                         const rows = $$("#headlines-frame > div[id*=RROW]");
1151
1152                         for (let i = 0; i < rows.length; i++) {
1153                                 const child = rows[i];
1154
1155                                 if ($("headlines-frame").scrollTop <= child.offsetTop &&
1156                                         child.offsetTop - $("headlines-frame").scrollTop < 100 &&
1157                                         child.getAttribute("data-article-id") != _active_article_id) {
1158
1159                                         if (_active_article_id) {
1160                                                 const row = $("RROW-" + _active_article_id);
1161                                                 if (row) row.removeClassName("active");
1162                                         }
1163
1164                                         _active_article_id = child.getAttribute("data-article-id");
1165                                         showArticleInHeadlines(_active_article_id, true);
1166                                         updateSelectedPrompt();
1167                                         break;
1168                                 }
1169                         }
1170                 }
1171
1172                 if (!_infscroll_disable) {
1173                         if (hsp && hsp.offsetTop - 250 <= e.scrollTop + e.offsetHeight) {
1174
1175                                 hsp.innerHTML = "<span class='loading'><img src='images/indicator_tiny.gif'> " +
1176                                         __("Loading, please wait...") + "</span>";
1177
1178                                 loadMoreHeadlines();
1179                                 return;
1180
1181                         }
1182                 }
1183
1184                 if (isCdmMode()) {
1185                         updateFloatingTitle();
1186                 }
1187
1188                 catchupCurrentBatchIfNeeded();
1189
1190                 if (getInitParam("cdm_auto_catchup") == 1) {
1191
1192                         // let's get DOM some time to settle down
1193                         const ts = new Date().getTime();
1194                         if (ts - _last_headlines_update < 100) return;
1195
1196                         $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
1197                                 function(child) {
1198                                         if ($("headlines-frame").scrollTop > (child.offsetTop + child.offsetHeight/2)) {
1199
1200                                                 const id = child.getAttribute("data-article-id")
1201
1202                                                 if (catchup_id_batch.indexOf(id) == -1)
1203                                                         catchup_id_batch.push(id);
1204
1205                                                 //console.log("auto_catchup_batch: " + catchup_id_batch.toString());
1206                                         }
1207
1208                                 });
1209
1210                         if (_infscroll_disable) {
1211                                 const child = $$("#headlines-frame div[id*=RROW]").last();
1212
1213                                 if (child && $("headlines-frame").scrollTop >
1214                                                 (child.offsetTop + child.offsetHeight - 50)) {
1215
1216                                         console.log("we seem to be at an end");
1217
1218                                         if (getInitParam("on_catchup_show_next_feed") == "1") {
1219                                                 openNextUnreadFeed();
1220                                         }
1221                                 }
1222                         }
1223                 }
1224
1225         } catch (e) {
1226                 console.warn("headlines_scroll_handler: " + e);
1227         }
1228 }
1229
1230 function openNextUnreadFeed() {
1231         const is_cat = activeFeedIsCat();
1232         const nuf = getNextUnreadFeed(getActiveFeedId(), is_cat);
1233         if (nuf) viewfeed({feed: nuf, is_cat: is_cat});
1234 }
1235
1236 function catchupBatchedArticles() {
1237         if (catchup_id_batch.length > 0 && !_infscroll_request_sent && !_catchup_request_sent) {
1238
1239                 console.log("catchupBatchedArticles: working");
1240
1241                 // make a copy of the array
1242                 const batch = catchup_id_batch.slice();
1243                 const query = "?op=rpc&method=catchupSelected" +
1244                         "&cmode=0&ids=" + param_escape(batch.toString());
1245
1246                 console.log(query);
1247
1248                 _catchup_request_sent = true;
1249
1250                 new Ajax.Request("backend.php", {
1251                         parameters: query,
1252                         onComplete: function (transport) {
1253                                 handle_rpc_json(transport);
1254
1255                                 _catchup_request_sent = false;
1256
1257                                 const reply = JSON.parse(transport.responseText);
1258                                 const batch = reply.ids;
1259
1260                                 batch.each(function (id) {
1261                                         console.log(id);
1262                                         const elem = $("RROW-" + id);
1263                                         if (elem) elem.removeClassName("Unread");
1264                                         catchup_id_batch.remove(id);
1265                                 });
1266
1267                                 updateFloatingTitle(true);
1268
1269                         }
1270                 });
1271         }
1272 }
1273
1274 function catchupRelativeToArticle(below, id) {
1275
1276         if (!id) id = getActiveArticleId();
1277
1278         if (!id) {
1279                 alert(__("No article is selected."));
1280                 return;
1281         }
1282
1283         const visible_ids = getLoadedArticleIds();
1284
1285         const ids_to_mark = [];
1286
1287         if (!below) {
1288                 for (var i = 0; i < visible_ids.length; i++) {
1289                         if (visible_ids[i] != id) {
1290                                 var e = $("RROW-" + visible_ids[i]);
1291
1292                                 if (e && e.hasClassName("Unread")) {
1293                                         ids_to_mark.push(visible_ids[i]);
1294                                 }
1295                         } else {
1296                                 break;
1297                         }
1298                 }
1299         } else {
1300                 for (var i = visible_ids.length - 1; i >= 0; i--) {
1301                         if (visible_ids[i] != id) {
1302                                 var e = $("RROW-" + visible_ids[i]);
1303
1304                                 if (e && e.hasClassName("Unread")) {
1305                                         ids_to_mark.push(visible_ids[i]);
1306                                 }
1307                         } else {
1308                                 break;
1309                         }
1310                 }
1311         }
1312
1313         if (ids_to_mark.length == 0) {
1314                 alert(__("No articles found to mark"));
1315         } else {
1316                 const msg = ngettext("Mark %d article as read?", "Mark %d articles as read?", ids_to_mark.length).replace("%d", ids_to_mark.length);
1317
1318                 if (getInitParam("confirm_feed_catchup") != 1 || confirm(msg)) {
1319
1320                         for (var i = 0; i < ids_to_mark.length; i++) {
1321                                 var e = $("RROW-" + ids_to_mark[i]);
1322                                 e.removeClassName("Unread");
1323                         }
1324
1325                         const query = "?op=rpc&method=catchupSelected" +
1326                                 "&cmode=0" + "&ids=" + param_escape(ids_to_mark.toString());
1327
1328                         new Ajax.Request("backend.php", {
1329                                 parameters: query,
1330                                 onComplete: function (transport) {
1331                                         handle_rpc_json(transport);
1332                                 }
1333                         });
1334
1335                 }
1336         }
1337 }
1338
1339 function cdmCollapseArticle(event, id, unmark) {
1340         if (unmark == undefined) unmark = true;
1341
1342         const row = $("RROW-" + id);
1343         const elem = $("CICD-" + id);
1344
1345         if (elem && row) {
1346                 const collapse = row.select("span[class='collapseBtn']")[0];
1347
1348                 Element.hide(elem);
1349                 Element.show("CEXC-" + id);
1350                 Element.hide(collapse);
1351
1352                 if (unmark) {
1353                         row.removeClassName("active");
1354
1355                         markHeadline(id, false);
1356
1357                         if (id == getActiveArticleId()) {
1358                                 setActiveArticleId(0);
1359                         }
1360
1361                         updateSelectedPrompt();
1362                 }
1363
1364                 if (event) Event.stop(event);
1365
1366                 PluginHost.run(PluginHost.HOOK_ARTICLE_COLLAPSED, id);
1367
1368                 if (row.offsetTop < $("headlines-frame").scrollTop)
1369                         scrollToRowId(row.id);
1370
1371                 $("floatingTitle").style.visibility = "hidden";
1372                 $("floatingTitle").setAttribute("data-article-id", 0);
1373         }
1374 }
1375
1376 function cdmExpandArticle(id, noexpand) {
1377         console.log("cdmExpandArticle " + id);
1378
1379         const row = $("RROW-" + id);
1380
1381         if (!row) return false;
1382
1383         const oldrow = $("RROW-" + getActiveArticleId());
1384
1385         let elem = $("CICD-" + getActiveArticleId());
1386
1387         if (id == getActiveArticleId() && Element.visible(elem))
1388                 return true;
1389
1390         selectArticles("none");
1391
1392         const old_offset = row.offsetTop;
1393
1394         if (getActiveArticleId() && elem && !getInitParam("cdm_expanded")) {
1395                 let collapse = oldrow.select("span[class='collapseBtn']")[0];
1396
1397                 Element.hide(elem);
1398                 Element.show("CEXC-" + getActiveArticleId());
1399                 Element.hide(collapse);
1400         }
1401
1402         if (oldrow) oldrow.removeClassName("active");
1403
1404         setActiveArticleId(id);
1405
1406         elem = $("CICD-" + id);
1407
1408         let collapse = row.select("span[class='collapseBtn']")[0];
1409
1410         const cencw = $("CENCW-" + id);
1411
1412         if (!Element.visible(elem) && !noexpand) {
1413                 if (cencw) {
1414                         cencw.innerHTML = htmlspecialchars_decode(cencw.innerHTML);
1415                         cencw.setAttribute('id', '');
1416                         Element.show(cencw);
1417                 }
1418
1419                 Element.show(elem);
1420                 Element.hide("CEXC-" + id);
1421                 Element.show(collapse);
1422         }
1423
1424         const new_offset = row.offsetTop;
1425
1426         if (old_offset > new_offset)
1427                 $("headlines-frame").scrollTop -= (old_offset - new_offset);
1428
1429         if (!noexpand) {
1430                 if (catchup_id_batch.indexOf(id) == -1)
1431                         catchup_id_batch.push(id);
1432
1433                 catchupCurrentBatchIfNeeded();
1434         }
1435
1436         toggleSelected(id);
1437         row.addClassName("active");
1438
1439         PluginHost.run(PluginHost.HOOK_ARTICLE_EXPANDED, id);
1440
1441         return false;
1442 }
1443
1444 function getArticleUnderPointer() {
1445         return post_under_pointer;
1446 }
1447
1448 function scrollArticle(offset) {
1449         if (!isCdmMode()) {
1450                 const ci = $("content-insert");
1451                 if (ci) {
1452                         ci.scrollTop += offset;
1453                 }
1454         } else {
1455                 const hi = $("headlines-frame");
1456                 if (hi) {
1457                         hi.scrollTop += offset;
1458                 }
1459
1460         }
1461 }
1462
1463 function show_labels_in_headlines(transport) {
1464         const data = JSON.parse(transport.responseText);
1465
1466         if (data) {
1467                 data['info-for-headlines'].each(function (elem) {
1468                         $$(".HLLCTR-" + elem.id).each(function (ctr) {
1469                                 ctr.innerHTML = elem.labels;
1470                         });
1471                 });
1472         }
1473 }
1474
1475 function cdmClicked(event, id, in_body) {
1476         //var shift_key = event.shiftKey;
1477
1478         if (!event.ctrlKey && !event.metaKey) {
1479
1480                 if (!getInitParam("cdm_expanded")) {
1481                         return cdmExpandArticle(id);
1482                 } else {
1483
1484                         let elem = $("RROW-" + getActiveArticleId());
1485
1486                         if (elem) elem.removeClassName("active");
1487
1488                         selectArticles("none");
1489                         toggleSelected(id);
1490
1491                         elem = $("RROW-" + id);
1492                         const article_is_unread = elem.hasClassName("Unread");
1493
1494                         elem.removeClassName("Unread");
1495                         elem.addClassName("active");
1496
1497                         setActiveArticleId(id);
1498
1499                         if (article_is_unread) {
1500                                 decrementFeedCounter(getActiveFeedId(), activeFeedIsCat());
1501                                 updateFloatingTitle(true);
1502                         }
1503
1504                         const query = "?op=rpc&method=catchupSelected" +
1505                                 "&cmode=0&ids=" + param_escape(id);
1506
1507                         new Ajax.Request("backend.php", {
1508                                 parameters: query,
1509                                 onComplete: function (transport) {
1510                                         handle_rpc_json(transport);
1511                                 }
1512                         });
1513
1514                         return !event.shiftKey;
1515                 }
1516
1517         } else if (!in_body) {
1518
1519                 toggleSelected(id, true);
1520
1521                 let elem = $("RROW-" + id);
1522                 const article_is_unread = elem.hasClassName("Unread");
1523
1524                 if (article_is_unread) {
1525                         decrementFeedCounter(getActiveFeedId(), activeFeedIsCat());
1526                 }
1527
1528                 toggleUnread(id, 0, false);
1529
1530                 openArticleInNewWindow(id);
1531         } else {
1532                 return true;
1533         }
1534
1535         const unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length
1536         request_counters(unread_in_buffer == 0);
1537
1538         return false;
1539 }
1540
1541 function hlClicked(event, id) {
1542         if (event.which == 2) {
1543                 view(id);
1544                 return true;
1545         } else if (event.ctrlKey || event.metaKey) {
1546                 openArticleInNewWindow(id);
1547                 return false;
1548         } else {
1549                 view(id);
1550                 return false;
1551         }
1552 }
1553
1554 function openArticleInNewWindow(id) {
1555         toggleUnread(id, 0, false);
1556
1557         const w = window.open("");
1558         w.opener = null;
1559         w.location = "backend.php?op=article&method=redirect&id=" + id;
1560 }
1561
1562 function isCdmMode() {
1563         return getInitParam("combined_display_mode");
1564 }
1565
1566 function markHeadline(id, marked) {
1567         if (marked == undefined) marked = true;
1568
1569         const row = $("RROW-" + id);
1570         if (row) {
1571                 const check = dijit.getEnclosingWidget(
1572                                 row.getElementsByClassName("rchk")[0]);
1573
1574                 if (check) {
1575                         check.attr("checked", marked);
1576                 }
1577
1578                 if (marked)
1579                         row.addClassName("Selected");
1580                 else
1581                         row.removeClassName("Selected");
1582         }
1583 }
1584
1585 function getRelativePostIds(id, limit) {
1586
1587         const tmp = [];
1588
1589         if (!limit) limit = 6; //3
1590
1591         const ids = getLoadedArticleIds();
1592
1593         for (let i = 0; i < ids.length; i++) {
1594                 if (ids[i] == id) {
1595                         for (let k = 1; k <= limit; k++) {
1596                                 //if (i > k-1) tmp.push(ids[i-k]);
1597                                 if (i < ids.length - k) tmp.push(ids[i + k]);
1598                         }
1599                         break;
1600                 }
1601         }
1602
1603         return tmp;
1604 }
1605
1606 function correctHeadlinesOffset(id) {
1607
1608         const container = $("headlines-frame");
1609         const row = $("RROW-" + id);
1610
1611         if (!container || !row) return;
1612
1613         const viewport = container.offsetHeight;
1614
1615         const rel_offset_top = row.offsetTop - container.scrollTop;
1616         const rel_offset_bottom = row.offsetTop + row.offsetHeight - container.scrollTop;
1617
1618         //console.log("Rtop: " + rel_offset_top + " Rbtm: " + rel_offset_bottom);
1619         //console.log("Vport: " + viewport);
1620
1621         if (rel_offset_top <= 0 || rel_offset_top > viewport) {
1622                 container.scrollTop = row.offsetTop;
1623         } else if (rel_offset_bottom > viewport) {
1624
1625                 /* doesn't properly work with Opera in some cases because
1626                  Opera fucks up element scrolling */
1627
1628                 container.scrollTop = row.offsetTop + row.offsetHeight - viewport;
1629         }
1630 }
1631
1632 function headlineActionsChange(elem) {
1633         eval(elem.value);
1634         elem.attr('value', 'false');
1635 }
1636
1637 function closeArticlePanel() {
1638
1639         if (dijit.byId("content-insert"))
1640                 dijit.byId("headlines-wrap-inner").removeChild(
1641                         dijit.byId("content-insert"));
1642 }
1643
1644 function initFloatingMenu() {
1645         if (!dijit.byId("floatingMenu")) {
1646
1647                 const menu = new dijit.Menu({
1648                         id: "floatingMenu",
1649                         targetNodeIds: ["floatingTitle"]
1650                 });
1651
1652                 headlinesMenuCommon(menu);
1653
1654                 menu.startup();
1655         }
1656 }
1657
1658 function headlinesMenuCommon(menu) {
1659
1660         menu.addChild(new dijit.MenuItem({
1661                 label: __("Open original article"),
1662                 onClick: function (event) {
1663                         openArticleInNewWindow(this.getParent().currentTarget.getAttribute("data-article-id"));
1664                 }
1665         }));
1666
1667         menu.addChild(new dijit.MenuItem({
1668                 label: __("Display article URL"),
1669                 onClick: function (event) {
1670                         displayArticleUrl(this.getParent().currentTarget.getAttribute("data-article-id"));
1671                 }
1672         }));
1673
1674         menu.addChild(new dijit.MenuSeparator());
1675
1676         menu.addChild(new dijit.MenuItem({
1677                 label: __("Toggle unread"),
1678                 onClick: function () {
1679
1680                         let ids = getSelectedArticleIds2();
1681                         // cast to string
1682                         const id = (this.getParent().currentTarget.getAttribute("data-article-id")) + "";
1683                         ids = ids.length != 0 && ids.indexOf(id) != -1 ? ids : [id];
1684
1685                         selectionToggleUnread(undefined, false, true, ids);
1686                 }
1687         }));
1688
1689         menu.addChild(new dijit.MenuItem({
1690                 label: __("Toggle starred"),
1691                 onClick: function () {
1692                         let ids = getSelectedArticleIds2();
1693                         // cast to string
1694                         const id = (this.getParent().currentTarget.getAttribute("data-article-id")) + "";
1695                         ids = ids.length != 0 && ids.indexOf(id) != -1 ? ids : [id];
1696
1697                         selectionToggleMarked(undefined, false, true, ids);
1698                 }
1699         }));
1700
1701         menu.addChild(new dijit.MenuItem({
1702                 label: __("Toggle published"),
1703                 onClick: function () {
1704                         let ids = getSelectedArticleIds2();
1705                         // cast to string
1706                         const id = (this.getParent().currentTarget.getAttribute("data-article-id")) + "";
1707                         ids = ids.length != 0 && ids.indexOf(id) != -1 ? ids : [id];
1708
1709                         selectionTogglePublished(undefined, false, true, ids);
1710                 }
1711         }));
1712
1713         menu.addChild(new dijit.MenuSeparator());
1714
1715         menu.addChild(new dijit.MenuItem({
1716                 label: __("Mark above as read"),
1717                 onClick: function () {
1718                         catchupRelativeToArticle(0, this.getParent().currentTarget.getAttribute("data-article-id"));
1719                 }
1720         }));
1721
1722         menu.addChild(new dijit.MenuItem({
1723                 label: __("Mark below as read"),
1724                 onClick: function () {
1725                         catchupRelativeToArticle(1, this.getParent().currentTarget.getAttribute("data-article-id"));
1726                 }
1727         }));
1728
1729
1730         const labels = getInitParam("labels");
1731
1732         if (labels && labels.length) {
1733
1734                 menu.addChild(new dijit.MenuSeparator());
1735
1736                 const labelAddMenu = new dijit.Menu({ownerMenu: menu});
1737                 const labelDelMenu = new dijit.Menu({ownerMenu: menu});
1738
1739                 labels.each(function (label) {
1740                         const bare_id = label.id;
1741                         const name = label.caption;
1742
1743                         labelAddMenu.addChild(new dijit.MenuItem({
1744                                 label: name,
1745                                 labelId: bare_id,
1746                                 onClick: function () {
1747
1748                                         let ids = getSelectedArticleIds2();
1749                                         // cast to string
1750                                         const id = (this.getParent().ownerMenu.currentTarget.getAttribute("data-article-id")) + "";
1751
1752                                         ids = ids.length != 0 && ids.indexOf(id) != -1 ? ids : [id];
1753
1754                                         selectionAssignLabel(this.labelId, ids);
1755                                 }
1756                         }));
1757
1758                         labelDelMenu.addChild(new dijit.MenuItem({
1759                                 label: name,
1760                                 labelId: bare_id,
1761                                 onClick: function () {
1762                                         let ids = getSelectedArticleIds2();
1763                                         // cast to string
1764                                         const id = (this.getParent().ownerMenu.currentTarget.getAttribute("data-article-id")) + "";
1765
1766                                         ids = ids.length != 0 && ids.indexOf(id) != -1 ? ids : [id];
1767
1768                                         selectionRemoveLabel(this.labelId, ids);
1769                                 }
1770                         }));
1771
1772                 });
1773
1774                 menu.addChild(new dijit.PopupMenuItem({
1775                         label: __("Assign label"),
1776                         popup: labelAddMenu
1777                 }));
1778
1779                 menu.addChild(new dijit.PopupMenuItem({
1780                         label: __("Remove label"),
1781                         popup: labelDelMenu
1782                 }));
1783
1784         }
1785 }
1786
1787 function initHeadlinesMenu() {
1788         if (!dijit.byId("headlinesMenu")) {
1789
1790                 const menu = new dijit.Menu({
1791                         id: "headlinesMenu",
1792                         targetNodeIds: ["headlines-frame"],
1793                         selector: ".hlMenuAttach"
1794                 });
1795
1796                 headlinesMenuCommon(menu);
1797
1798                 menu.startup();
1799         }
1800
1801         /* vgroup feed title menu */
1802
1803         if (!dijit.byId("headlinesFeedTitleMenu")) {
1804
1805                 const menu = new dijit.Menu({
1806                         id: "headlinesFeedTitleMenu",
1807                         targetNodeIds: ["headlines-frame"],
1808                         selector: "div.cdmFeedTitle"
1809                 });
1810
1811                 menu.addChild(new dijit.MenuItem({
1812                         label: __("Select articles in group"),
1813                         onClick: function (event) {
1814                                 selectArticles("all",
1815                                         "#headlines-frame > div[id*=RROW]" +
1816                                         "[data-orig-feed-id='" + this.getParent().currentTarget.getAttribute("data-feed-id") + "']");
1817
1818                         }
1819                 }));
1820
1821                 menu.addChild(new dijit.MenuItem({
1822                         label: __("Mark group as read"),
1823                         onClick: function () {
1824                                 selectArticles("none");
1825                                 selectArticles("all",
1826                                         "#headlines-frame > div[id*=RROW]" +
1827                                         "[data-orig-feed-id='" + this.getParent().currentTarget.getAttribute("data-feed-id") + "']");
1828
1829                                 catchupSelection();
1830                         }
1831                 }));
1832
1833                 menu.addChild(new dijit.MenuItem({
1834                         label: __("Mark feed as read"),
1835                         onClick: function () {
1836                                 catchupFeedInGroup(this.getParent().currentTarget.getAttribute("data-feed-id"));
1837                         }
1838                 }));
1839
1840                 menu.addChild(new dijit.MenuItem({
1841                         label: __("Edit feed"),
1842                         onClick: function () {
1843                                 editFeed(this.getParent().currentTarget.getAttribute("data-feed-id"));
1844                         }
1845                 }));
1846
1847                 menu.startup();
1848         }
1849 }
1850
1851 function cache_set(id, obj) {
1852         //console.log("cache_set: " + id);
1853         if (has_storage)
1854                 try {
1855                         sessionStorage[id] = obj;
1856                 } catch (e) {
1857                         sessionStorage.clear();
1858                 }
1859 }
1860
1861 function cache_get(id) {
1862         if (has_storage)
1863                 return sessionStorage[id];
1864 }
1865
1866 function cache_clear() {
1867         if (has_storage)
1868                 sessionStorage.clear();
1869 }
1870
1871 function cache_delete(id) {
1872         if (has_storage)
1873                 sessionStorage.removeItem(id);
1874 }
1875
1876 function cancelSearch() {
1877         _search_query = "";
1878         viewCurrentFeed();
1879 }
1880
1881 function setSelectionScore() {
1882         const ids = getSelectedArticleIds2();
1883
1884         if (ids.length > 0) {
1885                 console.log(ids);
1886
1887                 const score = prompt(__("Please enter new score for selected articles:"));
1888
1889                 if (score != undefined) {
1890                         const query = "op=article&method=setScore&id=" + param_escape(ids.toString()) +
1891                                 "&score=" + param_escape(score);
1892
1893                         new Ajax.Request("backend.php", {
1894                                 parameters: query,
1895                                 onComplete: function (transport) {
1896                                         const reply = JSON.parse(transport.responseText);
1897                                         if (reply) {
1898                                                 console.log(ids);
1899
1900                                                 ids.each(function (id) {
1901                                                         const row = $("RROW-" + id);
1902
1903                                                         if (row) {
1904                                                                 const pic = row.getElementsByClassName("hlScorePic")[0];
1905
1906                                                                 if (pic) {
1907                                                                         pic.src = pic.src.replace(/score_.*?\.png/,
1908                                                                                 reply["score_pic"]);
1909                                                                         pic.setAttribute("score", score);
1910                                                                 }
1911                                                         }
1912                                                 });
1913                                         }
1914                                 }
1915                         });
1916                 }
1917
1918         } else {
1919                 alert(__("No articles are selected."));
1920         }
1921 }
1922
1923 function updateScore(id) {
1924         const pic = $$("#RROW-" + id + " .hlScorePic")[0];
1925
1926         if (pic) {
1927
1928                 const query = "op=article&method=getScore&id=" + param_escape(id);
1929
1930                 new Ajax.Request("backend.php", {
1931                         parameters: query,
1932                         onComplete: function (transport) {
1933                                 console.log(transport.responseText);
1934
1935                                 const reply = JSON.parse(transport.responseText);
1936
1937                                 if (reply) {
1938                                         pic.src = pic.src.replace(/score_.*?\.png/, reply["score_pic"]);
1939                                         pic.setAttribute("score", reply["score"]);
1940                                         pic.setAttribute("title", reply["score"]);
1941                                 }
1942                         }
1943                 });
1944         }
1945 }
1946
1947 function changeScore(id, pic) {
1948         const score = pic.getAttribute("score");
1949
1950         const new_score = prompt(__("Please enter new score for this article:"), score);
1951
1952         if (new_score != undefined) {
1953
1954                 const query = "op=article&method=setScore&id=" + param_escape(id) +
1955                         "&score=" + param_escape(new_score);
1956
1957                 new Ajax.Request("backend.php", {
1958                         parameters: query,
1959                         onComplete: function (transport) {
1960                                 const reply = JSON.parse(transport.responseText);
1961
1962                                 if (reply) {
1963                                         pic.src = pic.src.replace(/score_.*?\.png/, reply["score_pic"]);
1964                                         pic.setAttribute("score", new_score);
1965                                         pic.setAttribute("title", new_score);
1966                                 }
1967                         }
1968                 });
1969         }
1970 }
1971
1972 function displayArticleUrl(id) {
1973         const query = "op=rpc&method=getlinktitlebyid&id=" + param_escape(id);
1974
1975         new Ajax.Request("backend.php", {
1976                 parameters: query,
1977                 onComplete: function (transport) {
1978                         const reply = JSON.parse(transport.responseText);
1979
1980                         if (reply && reply.link) {
1981                                 prompt(__("Article URL:"), reply.link);
1982                         }
1983                 }
1984         });
1985 }
1986
1987 function scrollToRowId(id) {
1988         const row = $(id);
1989
1990         if (row)
1991                 $("headlines-frame").scrollTop = row.offsetTop - 4;
1992 }
1993
1994 function updateFloatingTitle(unread_only) {
1995         if (!isCdmMode()) return;
1996
1997         const hf = $("headlines-frame");
1998
1999         const elems = $$("#headlines-frame > div[id*=RROW]");
2000
2001         for (let i = 0; i < elems.length; i++) {
2002
2003                 const child = elems[i];
2004
2005                 if (child && child.offsetTop + child.offsetHeight > hf.scrollTop) {
2006
2007                         const header = child.getElementsByClassName("cdmHeader")[0];
2008
2009                         if (unread_only || child.getAttribute("data-article-id") != $("floatingTitle").getAttribute("data-article-id")) {
2010                                 if (child.getAttribute("data-article-id") != $("floatingTitle").getAttribute("data-article-id")) {
2011
2012                                         $("floatingTitle").setAttribute("data-article-id", child.getAttribute("data-article-id"));
2013                                         $("floatingTitle").innerHTML = header.innerHTML;
2014                                         $("floatingTitle").firstChild.innerHTML = "<img class='anchor markedPic' src='images/page_white_go.png' onclick=\"scrollToRowId('" + child.id + "')\">" + $("floatingTitle").firstChild.innerHTML;
2015
2016                                         initFloatingMenu();
2017
2018                                         const cb = $$("#floatingTitle .dijitCheckBox")[0];
2019
2020                                         if (cb)
2021                                                 cb.parentNode.removeChild(cb);
2022                                 }
2023
2024                                 if (child.hasClassName("Unread"))
2025                                         $("floatingTitle").addClassName("Unread");
2026                                 else
2027                                         $("floatingTitle").removeClassName("Unread");
2028
2029                                 PluginHost.run(PluginHost.HOOK_FLOATING_TITLE, child);
2030                         }
2031
2032                         $("floatingTitle").style.marginRight = hf.offsetWidth - child.offsetWidth + "px";
2033                         if (header.offsetTop + header.offsetHeight < hf.scrollTop + $("floatingTitle").offsetHeight - 5 &&
2034                                 child.offsetTop + child.offsetHeight >= hf.scrollTop + $("floatingTitle").offsetHeight - 5)
2035                                 $("floatingTitle").style.visibility = "visible";
2036                         else
2037                                 $("floatingTitle").style.visibility = "hidden";
2038
2039                         return;
2040
2041                 }
2042         }
2043 }
2044
2045 function catchupCurrentBatchIfNeeded() {
2046         if (catchup_id_batch.length > 0) {
2047                 window.clearTimeout(catchup_timeout_id);
2048                 catchup_timeout_id = window.setTimeout(catchupBatchedArticles, 1000);
2049
2050                 if (catchup_id_batch.length >= 10) {
2051                         catchupBatchedArticles();
2052                 }
2053         }
2054 }