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