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