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