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