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