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