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