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