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