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