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