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