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