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