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