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