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