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