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