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