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