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