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