]> git.wh0rd.org - tt-rss.git/blob - viewfeed.js
tweak auto catchup a bit
[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 return;
1112
1113 }
1114 } else {
1115 if (hsp) hsp.innerHTML = "";
1116 }
1117
1118 if (getInitParam("cdm_auto_catchup") == 1) {
1119
1120 $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
1121 function(child) {
1122 if ($("headlines-frame").scrollTop >
1123 (child.offsetTop + child.offsetHeight/2)) {
1124
1125 var id = child.id.replace("RROW-", "");
1126
1127 if (catchup_id_batch.indexOf(id) == -1)
1128 catchup_id_batch.push(id);
1129
1130 //console.log("auto_catchup_batch: " + catchup_id_batch.toString());
1131 }
1132 });
1133
1134 if (catchup_id_batch.length > 0) {
1135 window.clearTimeout(catchup_timeout_id);
1136
1137 if (!_infscroll_request_sent) {
1138 catchup_timeout_id = window.setTimeout('catchupBatchedArticles()',
1139 2000);
1140 }
1141 }
1142 }
1143
1144 } catch (e) {
1145 console.warn("headlines_scroll_handler: " + e);
1146 }
1147 }
1148
1149 function catchupBatchedArticles() {
1150 try {
1151 if (catchup_id_batch.length > 0 && !_infscroll_request_sent) {
1152
1153 var query = "?op=rpc&subop=catchupSelected" +
1154 "&cmode=0&ids=" + param_escape(catchup_id_batch.toString());
1155
1156 new Ajax.Request("backend.php", {
1157 parameters: query,
1158 onComplete: function(transport) {
1159 handle_rpc_json(transport);
1160
1161 catchup_id_batch.each(function(id) {
1162 var elem = $("RROW-" + id);
1163 if (elem) elem.removeClassName("Unread");
1164 });
1165
1166 catchup_id_batch = [];
1167 } });
1168 }
1169
1170 } catch (e) {
1171 exception_error("catchupBatchedArticles", e);
1172 }
1173 }
1174
1175 function catchupRelativeToArticle(below) {
1176
1177 try {
1178
1179
1180 if (!getActiveArticleId()) {
1181 alert(__("No article is selected."));
1182 return;
1183 }
1184
1185 var visible_ids = getVisibleArticleIds();
1186
1187 var ids_to_mark = new Array();
1188
1189 if (!below) {
1190 for (var i = 0; i < visible_ids.length; i++) {
1191 if (visible_ids[i] != getActiveArticleId()) {
1192 var e = $("RROW-" + visible_ids[i]);
1193
1194 if (e && e.hasClassName("Unread")) {
1195 ids_to_mark.push(visible_ids[i]);
1196 }
1197 } else {
1198 break;
1199 }
1200 }
1201 } else {
1202 for (var i = visible_ids.length-1; i >= 0; i--) {
1203 if (visible_ids[i] != getActiveArticleId()) {
1204 var e = $("RROW-" + visible_ids[i]);
1205
1206 if (e && e.hasClassName("Unread")) {
1207 ids_to_mark.push(visible_ids[i]);
1208 }
1209 } else {
1210 break;
1211 }
1212 }
1213 }
1214
1215 if (ids_to_mark.length == 0) {
1216 alert(__("No articles found to mark"));
1217 } else {
1218 var msg = __("Mark %d article(s) as read?").replace("%d", ids_to_mark.length);
1219
1220 if (getInitParam("confirm_feed_catchup") != 1 || confirm(msg)) {
1221
1222 for (var i = 0; i < ids_to_mark.length; i++) {
1223 var e = $("RROW-" + ids_to_mark[i]);
1224 e.removeClassName("Unread");
1225 }
1226
1227 var query = "?op=rpc&subop=catchupSelected" +
1228 "&cmode=0" + "&ids=" + param_escape(ids_to_mark.toString());
1229
1230 new Ajax.Request("backend.php", {
1231 parameters: query,
1232 onComplete: function(transport) {
1233 handle_rpc_json(transport);
1234 } });
1235
1236 }
1237 }
1238
1239 } catch (e) {
1240 exception_error("catchupRelativeToArticle", e);
1241 }
1242 }
1243
1244 function cdmExpandArticle(id) {
1245 try {
1246
1247 hideAuxDlg();
1248
1249 var elem = $("CICD-" + active_post_id);
1250
1251 var upd_img_pic = $("FUPDPIC-" + id);
1252
1253 if (upd_img_pic && (upd_img_pic.src.match("updated.png") ||
1254 upd_img_pic.src.match("fresh_sign.png"))) {
1255
1256 upd_img_pic.src = "images/blank_icon.gif";
1257 }
1258
1259 if (id == active_post_id && Element.visible(elem))
1260 return true;
1261
1262 selectArticles("none");
1263
1264 var old_offset = $("RROW-" + id).offsetTop;
1265
1266 if (active_post_id && elem && !getInitParam("cdm_expanded")) {
1267 Element.hide(elem);
1268 Element.show("CEXC-" + active_post_id);
1269 }
1270
1271 active_post_id = id;
1272
1273 elem = $("CICD-" + id);
1274
1275 if (!Element.visible(elem)) {
1276 Element.show(elem);
1277 Element.hide("CEXC-" + id);
1278
1279 if ($("CWRAP-" + id).innerHTML == "") {
1280
1281 $("FUPDPIC-" + id).src = "images/indicator_tiny.gif";
1282
1283 $("CWRAP-" + id).innerHTML = "<div class=\"insensitive\">" +
1284 __("Loading, please wait...") + "</div>";
1285
1286 var query = "?op=rpc&subop=cdmGetArticle&id=" + param_escape(id);
1287
1288 var neighbor_ids = getRelativePostIds(id);
1289
1290 /* only request uncached articles */
1291 var cids_to_request = [];
1292
1293 for (var i = 0; i < neighbor_ids.length; i++) {
1294 if (cids_requested.indexOf(neighbor_ids[i]) == -1)
1295 if ($("CWRAP-" + neighbor_ids[i]).innerHTML == "") {
1296 cids_to_request.push(neighbor_ids[i]);
1297 cids_requested.push(neighbor_ids[i]);
1298 }
1299 }
1300
1301 console.log("additional ids: " + cids_to_request.toString());
1302
1303 query = query + "&cids=" + cids_to_request.toString();
1304
1305 console.log(query);
1306
1307 new Ajax.Request("backend.php", {
1308 parameters: query,
1309 onComplete: function(transport) {
1310
1311 $("FUPDPIC-" + id).src = 'images/blank_icon.gif';
1312
1313 handle_rpc_json(transport);
1314
1315 var reply = JSON.parse(transport.responseText);
1316
1317 reply.each(function(article) {
1318 $("CWRAP-" + article['id']).innerHTML = article['content']
1319 cids_requested.remove(article['id']);
1320 });
1321 }});
1322
1323 }
1324 }
1325
1326 var new_offset = $("RROW-" + id).offsetTop;
1327
1328 $("headlines-frame").scrollTop += (new_offset-old_offset);
1329
1330 if ($("RROW-" + id).offsetTop != old_offset)
1331 $("headlines-frame").scrollTop = new_offset;
1332
1333 toggleUnread(id, 0, true);
1334 toggleSelected(id);
1335
1336 } catch (e) {
1337 exception_error("cdmExpandArticle", e);
1338 }
1339
1340 return false;
1341 }
1342
1343 function fixHeadlinesOrder(ids) {
1344 try {
1345 for (var i = 0; i < ids.length; i++) {
1346 var e = $("RROW-" + ids[i]);
1347
1348 if (e) {
1349 if (i % 2 == 0) {
1350 e.removeClassName("even");
1351 e.addClassName("odd");
1352 } else {
1353 e.removeClassName("odd");
1354 e.addClassName("even");
1355 }
1356 }
1357 }
1358 } catch (e) {
1359 exception_error("fixHeadlinesOrder", e);
1360 }
1361 }
1362
1363 function getArticleUnderPointer() {
1364 return post_under_pointer;
1365 }
1366
1367 function zoomToArticle(event, id) {
1368 try {
1369 var cached_article = cache_get("article: " + id);
1370
1371 if (dijit.byId("ATAB-" + id))
1372 if (!event || !event.shiftKey)
1373 return dijit.byId("content-tabs").selectChild(dijit.byId("ATAB-" + id));
1374
1375 if (dijit.byId("ATSTRTIP-" + id))
1376 dijit.byId("ATSTRTIP-" + id).destroyRecursive();
1377
1378 if (cached_article) {
1379 //closeArticlePanel();
1380
1381 var article_pane = new dijit.layout.ContentPane({
1382 title: __("Loading...") , content: cached_article,
1383 style: 'padding : 0px;',
1384 id: 'ATAB-' + id,
1385 closable: true });
1386
1387 dijit.byId("content-tabs").addChild(article_pane);
1388
1389 if (!event || !event.shiftKey)
1390 dijit.byId("content-tabs").selectChild(article_pane);
1391
1392 if ($("PTITLE-" + id))
1393 article_pane.attr('title', $("PTITLE-" + id).innerHTML);
1394
1395 } else {
1396
1397 var query = "?op=rpc&subop=getArticles&ids=" + param_escape(id);
1398
1399 notify_progress("Loading, please wait...", true);
1400
1401 new Ajax.Request("backend.php", {
1402 parameters: query,
1403 onComplete: function(transport) {
1404 notify('');
1405
1406 var reply = JSON.parse(transport.responseText);
1407
1408 if (reply) {
1409 //closeArticlePanel();
1410
1411 var content = reply[0]['content'];
1412
1413 var article_pane = new dijit.layout.ContentPane({
1414 title: "article-" + id , content: content,
1415 style: 'padding : 0px;',
1416 id: 'ATAB-' + id,
1417 closable: true });
1418
1419 dijit.byId("content-tabs").addChild(article_pane);
1420
1421 if (!event || !event.shiftKey)
1422 dijit.byId("content-tabs").selectChild(article_pane);
1423
1424 if ($("PTITLE-" + id))
1425 article_pane.attr('title', $("PTITLE-" + id).innerHTML);
1426 }
1427
1428 } });
1429 }
1430
1431 } catch (e) {
1432 exception_error("zoomToArticle", e);
1433 }
1434 }
1435
1436 function scrollArticle(offset) {
1437 try {
1438 if (!isCdmMode()) {
1439 var ci = $("content-insert");
1440 if (ci) {
1441 ci.scrollTop += offset;
1442 }
1443 } else {
1444 var hi = $("headlines-frame");
1445 if (hi) {
1446 hi.scrollTop += offset;
1447 }
1448
1449 }
1450 } catch (e) {
1451 exception_error("scrollArticle", e);
1452 }
1453 }
1454
1455 function show_labels_in_headlines(transport) {
1456 try {
1457 var data = JSON.parse(transport.responseText);
1458
1459 if (data) {
1460 data['info-for-headlines'].each(function(elem) {
1461 var ctr = $("HLLCTR-" + elem.id);
1462
1463 if (ctr) ctr.innerHTML = elem.labels;
1464 });
1465
1466 cache_headlines(getActiveFeedId(), activeFeedIsCat(), null, $("headlines-frame").innerHTML);
1467
1468 }
1469 } catch (e) {
1470 exception_error("show_labels_in_headlines", e);
1471 }
1472 }
1473
1474 /* function toggleHeadlineActions() {
1475 try {
1476 var e = $("headlineActionsBody");
1477 var p = $("headlineActionsDrop");
1478
1479 if (!Element.visible(e)) {
1480 Element.show(e);
1481 } else {
1482 Element.hide(e);
1483 }
1484
1485 e.scrollTop = 0;
1486 e.style.left = (p.offsetLeft + 1) + "px";
1487 e.style.top = (p.offsetTop + p.offsetHeight + 2) + "px";
1488
1489 } catch (e) {
1490 exception_error("toggleHeadlineActions", e);
1491 }
1492 } */
1493
1494 /* function publishWithNote(id, def_note) {
1495 try {
1496 if (!def_note) def_note = '';
1497
1498 var note = prompt(__("Please enter a note for this article:"), def_note);
1499
1500 if (note != undefined) {
1501 togglePub(id, false, false, note);
1502 }
1503
1504 } catch (e) {
1505 exception_error("publishWithNote", e);
1506 }
1507 } */
1508
1509 function emailArticle(id) {
1510 try {
1511 if (!id) {
1512 var ids = getSelectedArticleIds2();
1513
1514 if (ids.length == 0) {
1515 alert(__("No articles are selected."));
1516 return;
1517 }
1518
1519 id = ids.toString();
1520 }
1521
1522 if (dijit.byId("emailArticleDlg"))
1523 dijit.byId("emailArticleDlg").destroyRecursive();
1524
1525 var query = "backend.php?op=dlg&id=emailArticle&param=" + param_escape(id);
1526
1527 dialog = new dijit.Dialog({
1528 id: "emailArticleDlg",
1529 title: __("Forward article by email"),
1530 style: "width: 600px",
1531 execute: function() {
1532 if (this.validate()) {
1533
1534 new Ajax.Request("backend.php", {
1535 parameters: dojo.objectToQuery(this.attr('value')),
1536 onComplete: function(transport) {
1537
1538 var reply = JSON.parse(transport.responseText);
1539
1540 var error = reply['error'];
1541
1542 if (error) {
1543 alert(__('Error sending email:') + ' ' + error);
1544 } else {
1545 notify_info('Your message has been sent.');
1546 dialog.hide();
1547 }
1548
1549 } });
1550 }
1551 },
1552 href: query});
1553
1554 var tmph = dojo.connect(dialog, 'onLoad', function() {
1555 dojo.disconnect(tmph);
1556
1557 new Ajax.Autocompleter('emailArticleDlg_destination', 'emailArticleDlg_dst_choices',
1558 "backend.php?op=rpc&subop=completeEmails",
1559 { tokens: '', paramName: "search" });
1560 });
1561
1562 dialog.show();
1563
1564 /* displayDlg('emailArticle', id,
1565 function () {
1566 document.forms['article_email_form'].destination.focus();
1567
1568 new Ajax.Autocompleter('destination', 'destination_choices',
1569 "backend.php?op=rpc&subop=completeEmails",
1570 { tokens: '', paramName: "search" });
1571
1572 }); */
1573
1574 } catch (e) {
1575 exception_error("emailArticle", e);
1576 }
1577 }
1578
1579 function dismissArticle(id) {
1580 try {
1581 var elem = $("RROW-" + id);
1582
1583 toggleUnread(id, 0, true);
1584
1585 new Effect.Fade(elem, {duration : 0.5});
1586
1587 active_post_id = false;
1588
1589 } catch (e) {
1590 exception_error("dismissArticle", e);
1591 }
1592 }
1593
1594 function dismissSelectedArticles() {
1595 try {
1596
1597 var ids = getVisibleArticleIds();
1598 var tmp = [];
1599 var sel = [];
1600
1601 for (var i = 0; i < ids.length; i++) {
1602 var elem = $("RROW-" + ids[i]);
1603
1604 if (elem.className && elem.hasClassName("Selected") &&
1605 ids[i] != active_post_id) {
1606 new Effect.Fade(elem, {duration : 0.5});
1607 sel.push(ids[i]);
1608 } else {
1609 tmp.push(ids[i]);
1610 }
1611 }
1612
1613 if (sel.length > 0)
1614 selectionToggleUnread(false);
1615
1616 fixHeadlinesOrder(tmp);
1617
1618 } catch (e) {
1619 exception_error("dismissSelectedArticles", e);
1620 }
1621 }
1622
1623 function dismissReadArticles() {
1624 try {
1625
1626 var ids = getVisibleArticleIds();
1627 var tmp = [];
1628
1629 for (var i = 0; i < ids.length; i++) {
1630 var elem = $("RROW-" + ids[i]);
1631
1632 if (elem.className && !elem.hasClassName("Unread") &&
1633 !elem.hasClassName("Selected")) {
1634
1635 new Effect.Fade(elem, {duration : 0.5});
1636 } else {
1637 tmp.push(ids[i]);
1638 }
1639 }
1640
1641 fixHeadlinesOrder(tmp);
1642
1643 } catch (e) {
1644 exception_error("dismissSelectedArticles", e);
1645 }
1646 }
1647
1648 function getVisibleArticleIds() {
1649 var ids = [];
1650
1651 try {
1652
1653 getLoadedArticleIds().each(function(id) {
1654 var elem = $("RROW-" + id);
1655 if (elem && Element.visible(elem))
1656 ids.push(id);
1657 });
1658
1659 } catch (e) {
1660 exception_error("getVisibleArticleIds", e);
1661 }
1662
1663 return ids;
1664 }
1665
1666 function cdmClicked(event, id) {
1667 try {
1668 var shift_key = event.shiftKey;
1669
1670 hideAuxDlg();
1671
1672 if (!event.ctrlKey) {
1673
1674 if (!getInitParam("cdm_expanded")) {
1675 return cdmExpandArticle(id);
1676 } else {
1677
1678 selectArticles("none");
1679 toggleSelected(id);
1680
1681 var elem = $("RROW-" + id);
1682
1683 if (elem)
1684 elem.removeClassName("Unread");
1685
1686 var upd_img_pic = $("FUPDPIC-" + id);
1687
1688 if (upd_img_pic && (upd_img_pic.src.match("updated.png") ||
1689 upd_img_pic.src.match("fresh_sign.png"))) {
1690
1691 upd_img_pic.src = "images/blank_icon.gif";
1692 }
1693
1694 active_post_id = id;
1695
1696 var query = "?op=rpc&subop=catchupSelected" +
1697 "&cmode=0&ids=" + param_escape(id);
1698
1699 new Ajax.Request("backend.php", {
1700 parameters: query,
1701 onComplete: function(transport) {
1702 handle_rpc_json(transport);
1703 } });
1704
1705 return true;
1706 }
1707
1708 } else {
1709 toggleSelected(id, true);
1710 toggleUnread(id, 0, false);
1711 zoomToArticle(event, id);
1712 }
1713
1714 } catch (e) {
1715 exception_error("cdmClicked");
1716 }
1717
1718 return false;
1719 }
1720
1721 function postClicked(event, id) {
1722 try {
1723
1724 if (!event.ctrlKey) {
1725 return true;
1726 } else {
1727 postOpenInNewTab(event, id);
1728 return false;
1729 }
1730
1731 } catch (e) {
1732 exception_error("postClicked");
1733 }
1734 }
1735
1736 function hlOpenInNewTab(event, id) {
1737 toggleUnread(id, 0, false);
1738 zoomToArticle(event, id);
1739 }
1740
1741 function postOpenInNewTab(event, id) {
1742 closeArticlePanel(id);
1743 zoomToArticle(event, id);
1744 }
1745
1746 function hlClicked(event, id) {
1747 try {
1748 if (event.which == 2) {
1749 view(id);
1750 return true;
1751 } else if (event.altKey) {
1752 openArticleInNewWindow(id);
1753 } else if (!event.ctrlKey) {
1754 view(id);
1755 return false;
1756 } else {
1757 toggleSelected(id);
1758 toggleUnread(id, 0, false);
1759 zoomToArticle(event, id);
1760 return false;
1761 }
1762
1763 } catch (e) {
1764 exception_error("hlClicked");
1765 }
1766 }
1767
1768 function getFirstVisibleHeadlineId() {
1769 var rows = getVisibleArticleIds();
1770 return rows[0];
1771
1772 }
1773
1774 function getLastVisibleHeadlineId() {
1775 var rows = getVisibleArticleIds();
1776 return rows[rows.length-1];
1777 }
1778
1779 function openArticleInNewWindow(id) {
1780 toggleUnread(id, 0, false);
1781 window.open("backend.php?op=la&id=" + id);
1782 }
1783
1784 function isCdmMode() {
1785 return getInitParam("combined_display_mode");
1786 }
1787
1788 function markHeadline(id) {
1789 var row = $("RROW-" + id);
1790 if (row) {
1791 var check = $("RCHK-" + id);
1792
1793 if (check) {
1794 check.checked = true;
1795 }
1796
1797 row.addClassName("Selected");
1798 }
1799 }
1800
1801 function getRelativePostIds(id, limit) {
1802
1803 var tmp = [];
1804
1805 try {
1806
1807 if (!limit) limit = 6; //3
1808
1809 var ids = getVisibleArticleIds();
1810
1811 for (var i = 0; i < ids.length; i++) {
1812 if (ids[i] == id) {
1813 for (var k = 1; k <= limit; k++) {
1814 //if (i > k-1) tmp.push(ids[i-k]);
1815 if (i < ids.length-k) tmp.push(ids[i+k]);
1816 }
1817 break;
1818 }
1819 }
1820
1821 } catch (e) {
1822 exception_error("getRelativePostIds", e);
1823 }
1824
1825 return tmp;
1826 }
1827
1828 function correctHeadlinesOffset(id) {
1829
1830 try {
1831
1832 var container = $("headlines-frame");
1833 var row = $("RROW-" + id);
1834
1835 var viewport = container.offsetHeight;
1836
1837 var rel_offset_top = row.offsetTop - container.scrollTop;
1838 var rel_offset_bottom = row.offsetTop + row.offsetHeight - container.scrollTop;
1839
1840 //console.log("Rtop: " + rel_offset_top + " Rbtm: " + rel_offset_bottom);
1841 //console.log("Vport: " + viewport);
1842
1843 if (rel_offset_top <= 0 || rel_offset_top > viewport) {
1844 container.scrollTop = row.offsetTop;
1845 } else if (rel_offset_bottom > viewport) {
1846
1847 /* doesn't properly work with Opera in some cases because
1848 Opera fucks up element scrolling */
1849
1850 container.scrollTop = row.offsetTop + row.offsetHeight - viewport;
1851 }
1852
1853 } catch (e) {
1854 exception_error("correctHeadlinesOffset", e);
1855 }
1856
1857 }
1858
1859 function headlineActionsChange(elem) {
1860 try {
1861 eval(elem.value);
1862 elem.attr('value', 'false');
1863 } catch (e) {
1864 exception_error("headlineActionsChange", e);
1865 }
1866 }
1867
1868 function closeArticlePanel() {
1869
1870 var tabs = dijit.byId("content-tabs");
1871 var child = tabs.selectedChildWidget;
1872
1873 if (child && tabs.getIndexOfChild(child) > 0) {
1874 tabs.removeChild(child);
1875 child.destroy();
1876 } else {
1877 if (dijit.byId("content-insert"))
1878 dijit.byId("headlines-wrap-inner").removeChild(
1879 dijit.byId("content-insert"));
1880 }
1881 }
1882
1883 function initHeadlinesMenu() {
1884 try {
1885 if (dijit.byId("headlinesMenu"))
1886 dijit.byId("headlinesMenu").destroyRecursive();
1887
1888 var ids = [];
1889
1890 if (!isCdmMode()) {
1891 nodes = $$("#headlines-frame > div[id*=RROW]");
1892 } else {
1893 nodes = $$("#headlines-frame span[id*=RTITLE]");
1894 }
1895
1896 nodes.each(function(node) {
1897 ids.push(node.id);
1898 });
1899
1900 var menu = new dijit.Menu({
1901 id: "headlinesMenu",
1902 targetNodeIds: ids,
1903 });
1904
1905 var tmph = dojo.connect(menu, '_openMyself', function (event) {
1906 var callerNode = event.target, match = null, tries = 0;
1907
1908 while (match == null && callerNode && tries <= 3) {
1909 match = callerNode.id.match("^[A-Z]+[-]([0-9]+)$");
1910 callerNode = callerNode.parentNode;
1911 ++tries;
1912 }
1913
1914 if (match) this.callerRowId = parseInt(match[1]);
1915
1916 });
1917
1918 /* if (!isCdmMode())
1919 menu.addChild(new dijit.MenuItem({
1920 label: __("View article"),
1921 onClick: function(event) {
1922 view(this.getParent().callerRowId);
1923 }})); */
1924
1925 menu.addChild(new dijit.MenuItem({
1926 label: __("Open original article"),
1927 onClick: function(event) {
1928 openArticleInNewWindow(this.getParent().callerRowId);
1929 }}));
1930
1931 menu.addChild(new dijit.MenuItem({
1932 label: __("View in a tt-rss tab"),
1933 onClick: function(event) {
1934 hlOpenInNewTab(event, this.getParent().callerRowId);
1935 }}));
1936
1937 // menu.addChild(new dijit.MenuSeparator());
1938
1939 var labels = dijit.byId("feedTree").model.getItemsInCategory(-2);
1940
1941 if (labels) {
1942
1943 menu.addChild(new dijit.MenuSeparator());
1944
1945 var labelAddMenu = new dijit.Menu({ownerMenu: menu});
1946 var labelDelMenu = new dijit.Menu({ownerMenu: menu});
1947
1948 labels.each(function(label) {
1949 var id = label.id[0];
1950 var bare_id = id.substr(id.indexOf(":")+1);
1951 var name = label.name[0];
1952
1953 bare_id = -11-bare_id;
1954
1955 labelAddMenu.addChild(new dijit.MenuItem({
1956 label: name,
1957 labelId: bare_id,
1958 onClick: function(event) {
1959 selectionAssignLabel(this.labelId,
1960 [this.getParent().ownerMenu.callerRowId]);
1961 }}));
1962
1963 labelDelMenu.addChild(new dijit.MenuItem({
1964 label: name,
1965 labelId: bare_id,
1966 onClick: function(event) {
1967 selectionRemoveLabel(this.labelId,
1968 [this.getParent().ownerMenu.callerRowId]);
1969 }}));
1970
1971 });
1972
1973 menu.addChild(new dijit.PopupMenuItem({
1974 label: __("Assign label"),
1975 popup: labelAddMenu,
1976 }));
1977
1978 menu.addChild(new dijit.PopupMenuItem({
1979 label: __("Remove label"),
1980 popup: labelDelMenu,
1981 }));
1982
1983 }
1984
1985 menu.startup();
1986
1987 } catch (e) {
1988 exception_error("initHeadlinesMenu", e);
1989 }
1990 }
1991
1992 function tweetArticle(id) {
1993 try {
1994 var query = "?op=rpc&subop=getTweetInfo&id=" + param_escape(id);
1995
1996 console.log(query);
1997
1998 var d = new Date();
1999 var ts = d.getTime();
2000
2001 var w = window.open('backend.php?op=loading', 'ttrss_tweet',
2002 "status=0,toolbar=0,location=0,width=500,height=400,scrollbars=1,menubar=0");
2003
2004 new Ajax.Request("backend.php", {
2005 parameters: query,
2006 onComplete: function(transport) {
2007 var ti = JSON.parse(transport.responseText);
2008
2009 var share_url = "http://twitter.com/share?_=" + ts +
2010 "&text=" + param_escape(ti.title) +
2011 "&url=" + param_escape(ti.link);
2012
2013 w.location.href = share_url;
2014
2015 } });
2016
2017
2018 } catch (e) {
2019 exception_error("tweetArticle", e);
2020 }
2021 }
2022
2023 function editArticleNote(id) {
2024 try {
2025
2026 var query = "backend.php?op=dlg&id=editArticleNote&param=" + param_escape(id);
2027
2028 if (dijit.byId("editNoteDlg"))
2029 dijit.byId("editNoteDlg").destroyRecursive();
2030
2031 dialog = new dijit.Dialog({
2032 id: "editNoteDlg",
2033 title: __("Edit article note"),
2034 style: "width: 600px",
2035 execute: function() {
2036 if (this.validate()) {
2037 var query = dojo.objectToQuery(this.attr('value'));
2038
2039 notify_progress("Saving article note...", true);
2040
2041 new Ajax.Request("backend.php", {
2042 parameters: query,
2043 onComplete: function(transport) {
2044 notify('');
2045 dialog.hide();
2046
2047 var reply = JSON.parse(transport.responseText);
2048
2049 cache_delete("article:" + id);
2050
2051 var elem = $("POSTNOTE-" + id);
2052
2053 if (elem) {
2054 Element.hide(elem);
2055 elem.innerHTML = reply.note;
2056
2057 if (reply.raw_length != 0)
2058 new Effect.Appear(elem);
2059 }
2060
2061 }});
2062 }
2063 },
2064 href: query,
2065 });
2066
2067 dialog.show();
2068
2069 } catch (e) {
2070 exception_error("editArticleNote", e);
2071 }
2072 }
2073
2074 function player(elem) {
2075 var aid = elem.getAttribute("audio-id");
2076 var status = elem.getAttribute("status");
2077
2078 var audio = $(aid);
2079
2080 if (audio) {
2081 if (status == 0) {
2082 audio.play();
2083 status = 1;
2084 elem.innerHTML = __("Playing...");
2085 elem.title = __("Click to pause");
2086 elem.addClassName("playing");
2087 } else {
2088 audio.pause();
2089 status = 0;
2090 elem.innerHTML = __("Play");
2091 elem.title = __("Click to play");
2092 elem.removeClassName("playing");
2093 }
2094
2095 elem.setAttribute("status", status);
2096 } else {
2097 alert("Your browser doesn't seem to support HTML5 audio.");
2098 }
2099 }
2100
2101 function cache_set(id, obj) {
2102 //console.log("cache_set: " + id);
2103 if (has_storage)
2104 try {
2105 sessionStorage[id] = obj;
2106 } catch (e) {
2107 sessionStorage.clear();
2108 }
2109 }
2110
2111 function cache_get(id) {
2112 if (has_storage)
2113 return sessionStorage[id];
2114 }
2115
2116 function cache_clear() {
2117 if (has_storage)
2118 sessionStorage.clear();
2119 }
2120
2121 function cache_delete(id) {
2122 if (has_storage)
2123 sessionStorage.removeItem(id);
2124 }
2125
2126 function cache_headlines(feed, is_cat, toolbar_obj, content_obj) {
2127 if (toolbar_obj && content_obj) {
2128 cache_set("feed:" + feed + ":" + is_cat,
2129 JSON.stringify({toolbar: toolbar_obj, content: content_obj}));
2130 } else {
2131 try {
2132 obj = cache_get("feed:" + feed + ":" + is_cat);
2133
2134 if (obj) {
2135 obj = JSON.parse(obj);
2136
2137 if (toolbar_obj) obj.toolbar = toolbar_obj;
2138 if (content_obj) obj.content = content_obj;
2139
2140 cache_set("feed:" + feed + ":" + is_cat, JSON.stringify(obj));
2141 }
2142
2143 } catch (e) {
2144 console.warn("cache_headlines failed: " + e);
2145 }
2146 }
2147 }
2148
2149 function render_local_headlines(feed, is_cat, obj) {
2150 try {
2151
2152 dijit.byId("headlines-toolbar").attr('content',
2153 obj.toolbar);
2154
2155 dijit.byId("headlines-frame").attr('content',
2156 obj.content);
2157
2158 dojo.parser.parse('headlines-toolbar');
2159
2160 $("headlines-frame").scrollTop = 0;
2161 selectArticles('none');
2162 setActiveFeedId(feed, is_cat);
2163 initHeadlinesMenu();
2164
2165 precache_headlines();
2166
2167 } catch (e) {
2168 exception_error("render_local_headlines", e);
2169 }
2170 }
2171
2172 function precache_headlines_idle() {
2173 try {
2174 if (!feed_precache_timeout_id) {
2175 var feeds = dijit.byId("feedTree").getVisibleUnreadFeeds();
2176 var uncached = [];
2177
2178 feeds.each(function(item) {
2179 if (parseInt(item[0]) > 0 && !cache_get("feed:" + item[0] + ":" + item[1]))
2180 uncached.push(item);
2181 });
2182
2183 if (uncached.length > 0) {
2184 var rf = uncached[Math.floor(Math.random()*uncached.length)];
2185 viewfeed(rf[0], '', rf[1], 0, true);
2186 }
2187 }
2188 precache_idle_timeout_id = setTimeout("precache_headlines_idle()", 1000*30);
2189
2190 } catch (e) {
2191 exception_error("precache_headlines_idle", e);
2192 }
2193 }
2194
2195 function precache_headlines() {
2196 try {
2197
2198 if (!feed_precache_timeout_id) {
2199 feed_precache_timeout_id = window.setTimeout(function() {
2200 var nuf = getNextUnreadFeed(getActiveFeedId(), activeFeedIsCat());
2201 var nf = dijit.byId("feedTree").getNextFeed(getActiveFeedId(), activeFeedIsCat());
2202
2203 if (nuf && !cache_get("feed:" + nuf + ":" + activeFeedIsCat()))
2204 viewfeed(nuf, '', activeFeedIsCat(), 0, true);
2205
2206 if (nf != nuf && nf && !cache_get("feed:" + nf[0] + ":" + nf[1]))
2207 viewfeed(nf[0], '', nf[1], 0, true);
2208
2209 window.setTimeout(function() {
2210 feed_precache_timeout_id = false;
2211 }, 3000);
2212 }, 1000);
2213 }
2214
2215 } catch (e) {
2216 exception_error("precache_headlines", e);
2217 }
2218 }
2219
2220 function shareArticle(id) {
2221 try {
2222 if (dijit.byId("shareArticleDlg"))
2223 dijit.byId("shareArticleDlg").destroyRecursive();
2224
2225 var query = "backend.php?op=dlg&id=shareArticle&param=" + param_escape(id);
2226
2227 dialog = new dijit.Dialog({
2228 id: "shareArticleDlg",
2229 title: __("Share article by URL"),
2230 style: "width: 600px",
2231 href: query});
2232
2233 dialog.show();
2234
2235 } catch (e) {
2236 exception_error("emailArticle", e);
2237 }
2238 }
2239
2240