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