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