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