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