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