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