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