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