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