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