]> git.wh0rd.org Git - tt-rss.git/blob - js/viewfeed.js
showArticleInHeadlines: remove unused code
[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("rowid", 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("rowid") == "RROW-" + 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("rowid") == "RROW-" + 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.id.replace("RROW-", ""));
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                         rv.push(child.id.replace("RROW-", ""));
932                 });
933
934         return rv;
935
936 }
937
938 // mode = all,none,unread,invert,marked,published
939 function selectArticles(mode, query) {
940         try {
941
942                 if (!query) query = "#headlines-frame > div[id*=RROW]";
943
944                 var children = $$(query);
945
946                 children.each(function(child) {
947                         var id = child.id.replace("RROW-", "");
948
949                         var cb = dijit.getEnclosingWidget(
950                                         child.getElementsByClassName("rchk")[0]);
951
952                         if (mode == "all") {
953                                 child.addClassName("Selected");
954                                 if (cb) cb.attr("checked", true);
955                         } else if (mode == "unread") {
956                                 if (child.hasClassName("Unread")) {
957                                         child.addClassName("Selected");
958                                         if (cb) cb.attr("checked", true);
959                                 } else {
960                                         child.removeClassName("Selected");
961                                         if (cb) cb.attr("checked", false);
962                                 }
963                         } else if (mode == "marked") {
964                                 if (child.hasClassName("marked")) {
965                                         child.addClassName("Selected");
966                                         if (cb) cb.attr("checked", true);
967                                 } else {
968                                         child.removeClassName("Selected");
969                                         if (cb) cb.attr("checked", false);
970                                 }
971                         } else if (mode == "published") {
972                                 if (child.hasClassName("published")) {
973                                         child.addClassName("Selected");
974                                         if (cb) cb.attr("checked", true);
975                                 } else {
976                                         child.removeClassName("Selected");
977                                         if (cb) cb.attr("checked", false);
978                                 }
979
980                         } else if (mode == "invert") {
981                                 if (child.hasClassName("Selected")) {
982                                         child.removeClassName("Selected");
983                                         if (cb) cb.attr("checked", false);
984                                 } else {
985                                         child.addClassName("Selected");
986                                         if (cb) cb.attr("checked", true);
987                                 }
988
989                         } else {
990                                 child.removeClassName("Selected");
991                                 if (cb) cb.attr("checked", false);
992                         }
993                 });
994
995                 updateSelectedPrompt();
996
997         } catch (e) {
998                 exception_error("selectArticles", e);
999         }
1000 }
1001
1002 function deleteSelection() {
1003
1004         try {
1005
1006                 var rows = getSelectedArticleIds2();
1007
1008                 if (rows.length == 0) {
1009                         alert(__("No articles are selected."));
1010                         return;
1011                 }
1012
1013                 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1014                 var str;
1015
1016                 if (getActiveFeedId() != 0) {
1017                         str = ngettext("Delete %d selected article in %s?", "Delete %d selected articles in %s?" , rows.length);
1018                 } else {
1019                         str = ngettext("Delete %d selected article?", "Delete %d selected articles?", rows.length);
1020                 }
1021
1022                 str = str.replace("%d", rows.length);
1023                 str = str.replace("%s", fn);
1024
1025                 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1026                         return;
1027                 }
1028
1029                 query = "?op=rpc&method=delete&ids=" + param_escape(rows);
1030
1031                 console.log(query);
1032
1033                 new Ajax.Request("backend.php", {
1034                         parameters: query,
1035                         onComplete: function(transport) {
1036                                         handle_rpc_json(transport);
1037                                         viewCurrentFeed();
1038                                 } });
1039
1040         } catch (e) {
1041                 exception_error("deleteSelection", e);
1042         }
1043 }
1044
1045 function archiveSelection() {
1046
1047         try {
1048
1049                 var rows = getSelectedArticleIds2();
1050
1051                 if (rows.length == 0) {
1052                         alert(__("No articles are selected."));
1053                         return;
1054                 }
1055
1056                 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1057                 var str;
1058                 var op;
1059
1060                 if (getActiveFeedId() != 0) {
1061                         str = ngettext("Archive %d selected article in %s?", "Archive %d selected articles in %s?", rows.length);
1062                         op = "archive";
1063                 } else {
1064                         str = ngettext("Move %d archived article back?", "Move %d archived articles back?", rows.length);
1065
1066                         str += " " + __("Please note that unstarred articles might get purged on next feed update.");
1067
1068                         op = "unarchive";
1069                 }
1070
1071                 str = str.replace("%d", rows.length);
1072                 str = str.replace("%s", fn);
1073
1074                 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1075                         return;
1076                 }
1077
1078                 query = "?op=rpc&method="+op+"&ids=" + param_escape(rows);
1079
1080                 console.log(query);
1081
1082                 for (var i = 0; i < rows.length; i++) {
1083                         cache_delete("article:" + rows[i]);
1084                 }
1085
1086                 new Ajax.Request("backend.php", {
1087                         parameters: query,
1088                         onComplete: function(transport) {
1089                                         handle_rpc_json(transport);
1090                                         viewCurrentFeed();
1091                                 } });
1092
1093         } catch (e) {
1094                 exception_error("archiveSelection", e);
1095         }
1096 }
1097
1098 function catchupSelection() {
1099
1100         try {
1101
1102                 var rows = getSelectedArticleIds2();
1103
1104                 if (rows.length == 0) {
1105                         alert(__("No articles are selected."));
1106                         return;
1107                 }
1108
1109                 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1110
1111                 var str = ngettext("Mark %d selected article in %s as read?", "Mark %d selected articles in %s as read?", rows.length);
1112
1113                 str = str.replace("%d", rows.length);
1114                 str = str.replace("%s", fn);
1115
1116                 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1117                         return;
1118                 }
1119
1120                 selectionToggleUnread(false, 'viewCurrentFeed()', true);
1121
1122         } catch (e) {
1123                 exception_error("catchupSelection", e);
1124         }
1125 }
1126
1127 function editArticleTags(id) {
1128                 var query = "backend.php?op=article&method=editArticleTags&param=" + param_escape(id);
1129
1130                 if (dijit.byId("editTagsDlg"))
1131                         dijit.byId("editTagsDlg").destroyRecursive();
1132
1133                 dialog = new dijit.Dialog({
1134                         id: "editTagsDlg",
1135                         title: __("Edit article Tags"),
1136                         style: "width: 600px",
1137                         execute: function() {
1138                                 if (this.validate()) {
1139                                         var query = dojo.objectToQuery(this.attr('value'));
1140
1141                                         notify_progress("Saving article tags...", true);
1142
1143                                         new Ajax.Request("backend.php", {
1144                                         parameters: query,
1145                                         onComplete: function(transport) {
1146                                                 try {
1147                                                         notify('');
1148                                                         dialog.hide();
1149
1150                                                         var data = JSON.parse(transport.responseText);
1151
1152                                                         if (data) {
1153                                                                 var id = data.id;
1154
1155                                                                 console.log(id);
1156
1157                                                                 var tags = $("ATSTR-" + id);
1158                                                                 var tooltip = dijit.byId("ATSTRTIP-" + id);
1159
1160                                                                 if (tags) tags.innerHTML = data.content;
1161                                                                 if (tooltip) tooltip.attr('label', data.content_full);
1162                                                         }
1163                                                 } catch (e) {
1164                                                         exception_error("editArticleTags/inner", e);
1165                                                 }
1166
1167                                         }});
1168                                 }
1169                         },
1170                         href: query
1171                 });
1172
1173                 var tmph = dojo.connect(dialog, 'onLoad', function() {
1174                 dojo.disconnect(tmph);
1175
1176                         new Ajax.Autocompleter('tags_str', 'tags_choices',
1177                            "backend.php?op=article&method=completeTags",
1178                            { tokens: ',', paramName: "search" });
1179                 });
1180
1181                 dialog.show();
1182
1183 }
1184
1185 function cdmScrollToArticleId(id, force) {
1186         try {
1187                 var ctr = $("headlines-frame");
1188                 var e = $("RROW-" + id);
1189
1190                 if (!e || !ctr) return;
1191
1192                 if (force || e.offsetTop+e.offsetHeight > (ctr.scrollTop+ctr.offsetHeight) ||
1193                                 e.offsetTop < ctr.scrollTop) {
1194
1195                         // expanded cdm has a 4px margin now
1196                         ctr.scrollTop = parseInt(e.offsetTop) - 4;
1197                 }
1198
1199         } catch (e) {
1200                 exception_error("cdmScrollToArticleId", e);
1201         }
1202 }
1203
1204 function setActiveArticleId(id) {
1205         console.log("setActiveArticleId:" + id);
1206
1207         _active_article_id = id;
1208         PluginHost.run(PluginHost.HOOK_ARTICLE_SET_ACTIVE, _active_article_id);
1209 }
1210
1211 function getActiveArticleId() {
1212         return _active_article_id;
1213 }
1214
1215 function postMouseIn(e, id) {
1216         post_under_pointer = id;
1217 }
1218
1219 function postMouseOut(id) {
1220         post_under_pointer = false;
1221 }
1222
1223 function unpackVisibleHeadlines() {
1224         try {
1225                 if (!isCdmMode() || !getInitParam("cdm_expanded")) return;
1226
1227                 $$("#headlines-frame > div[id*=RROW]").each(
1228                         function(child) {
1229                                 if (child.offsetTop <= $("headlines-frame").scrollTop +
1230                                         $("headlines-frame").offsetHeight) {
1231
1232                                         var cencw = $("CENCW-" + child.id.replace("RROW-", ""));
1233
1234                                         if (cencw) {
1235                                                 cencw.innerHTML = htmlspecialchars_decode(cencw.innerHTML);
1236                                                 cencw.setAttribute('id', '');
1237
1238                                                 PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED_CDM, child);
1239
1240                                                 Element.show(cencw);
1241                                         }
1242                                 }
1243                         }
1244                 );
1245
1246         } catch (e) {
1247                 exception_error("unpackVisibleHeadlines", e);
1248         }
1249 }
1250
1251 function headlines_scroll_handler(e) {
1252         try {
1253
1254                 // rate-limit in case of smooth scrolling and similar abominations
1255                 if (Math.max(e.scrollTop, _headlines_scroll_offset) - Math.min(e.scrollTop, _headlines_scroll_offset) < 25) {
1256                         return;
1257                 }
1258
1259                 _headlines_scroll_offset = e.scrollTop;
1260
1261                 var hsp = $("headlines-spacer");
1262
1263                 unpackVisibleHeadlines();
1264
1265                 // set topmost child in the buffer as active
1266                 if (isCdmMode() && getInitParam("cdm_auto_catchup") == 1 &&
1267                                 getSelectedArticleIds2().length <= 1 &&
1268                                 getInitParam("cdm_expanded")) {
1269
1270                         var rows = $$("#headlines-frame > div[id*=RROW]");
1271
1272                         for (var i = 0; i < rows.length; i++) {
1273                                 var child = rows[i];
1274
1275                                 if ($("headlines-frame").scrollTop <= child.offsetTop &&
1276                                         child.offsetTop - $("headlines-frame").scrollTop < 100 &&
1277                                         child.id.replace("RROW-", "") != _active_article_id) {
1278
1279                                         if (_active_article_id) {
1280                                                 var row = $("RROW-" + _active_article_id);
1281                                                 if (row) row.removeClassName("active");
1282                                         }
1283
1284                                         _active_article_id = child.id.replace("RROW-", "");
1285                                         showArticleInHeadlines(_active_article_id, true);
1286                                         updateSelectedPrompt();
1287                                         break;
1288                                 }
1289                         }
1290                 }
1291
1292                 if (!_infscroll_disable) {
1293                         if (hsp && hsp.offsetTop - 250 <= e.scrollTop + e.offsetHeight) {
1294
1295                                 hsp.innerHTML = "<span class='loading'><img src='images/indicator_tiny.gif'> " +
1296                                         __("Loading, please wait...") + "</span>";
1297
1298                                 loadMoreHeadlines();
1299                                 return;
1300
1301                         }
1302                 }
1303
1304                 if (isCdmMode()) {
1305                         updateFloatingTitle();
1306                 }
1307
1308                 catchupCurrentBatchIfNeeded();
1309
1310                 if (getInitParam("cdm_auto_catchup") == 1) {
1311
1312                         // let's get DOM some time to settle down
1313                         var ts = new Date().getTime();
1314                         if (ts - _last_headlines_update < 100) return;
1315
1316                         $$("#headlines-frame > div[id*=RROW][class*=Unread]").each(
1317                                 function(child) {
1318                                         if (child.hasClassName("Unread") && $("headlines-frame").scrollTop >
1319                                                         (child.offsetTop + child.offsetHeight/2)) {
1320
1321                                                 var id = child.id.replace("RROW-", "");
1322
1323                                                 if (catchup_id_batch.indexOf(id) == -1)
1324                                                         catchup_id_batch.push(id);
1325
1326                                                 //console.log("auto_catchup_batch: " + catchup_id_batch.toString());
1327                                         }
1328
1329                                 });
1330
1331                         if (_infscroll_disable) {
1332                                 var child = $$("#headlines-frame div[id*=RROW]").last();
1333
1334                                 if (child && $("headlines-frame").scrollTop >
1335                                                 (child.offsetTop + child.offsetHeight - 50)) {
1336
1337                                         console.log("we seem to be at an end");
1338
1339                                         if (getInitParam("on_catchup_show_next_feed") == "1") {
1340                                                 openNextUnreadFeed();
1341                                         }
1342                                 }
1343                         }
1344                 }
1345
1346         } catch (e) {
1347                 console.warn("headlines_scroll_handler: " + e);
1348         }
1349 }
1350
1351 function openNextUnreadFeed() {
1352         try {
1353                 var is_cat = activeFeedIsCat();
1354                 var nuf = getNextUnreadFeed(getActiveFeedId(), is_cat);
1355                 if (nuf) viewfeed({feed: nuf, is_cat: is_cat});
1356         } catch (e) {
1357                 exception_error("openNextUnreadFeed", e);
1358         }
1359 }
1360
1361 function catchupBatchedArticles() {
1362         try {
1363                 if (catchup_id_batch.length > 0 && !_infscroll_request_sent && !_catchup_request_sent) {
1364
1365                         console.log("catchupBatchedArticles: working");
1366
1367                         // make a copy of the array
1368                         var batch = catchup_id_batch.slice();
1369                         var query = "?op=rpc&method=catchupSelected" +
1370                                 "&cmode=0&ids=" + param_escape(batch.toString());
1371
1372                         console.log(query);
1373
1374                         _catchup_request_sent = true;
1375
1376                         new Ajax.Request("backend.php", {
1377                                 parameters: query,
1378                                 onComplete: function(transport) {
1379                                         handle_rpc_json(transport);
1380
1381                                         _catchup_request_sent = false;
1382
1383                                         reply = JSON.parse(transport.responseText);
1384                                         var batch = reply.ids;
1385
1386                                         batch.each(function(id) {
1387                                                 console.log(id);
1388                                                 var elem = $("RROW-" + id);
1389                                                 if (elem) elem.removeClassName("Unread");
1390                                                 catchup_id_batch.remove(id);
1391                                         });
1392
1393                                         updateFloatingTitle(true);
1394
1395                                 } });
1396                 }
1397
1398         } catch (e) {
1399                 exception_error("catchupBatchedArticles", e);
1400         }
1401 }
1402
1403 function catchupRelativeToArticle(below, id) {
1404
1405         try {
1406
1407                 if (!id) id = getActiveArticleId();
1408
1409                 if (!id) {
1410                         alert(__("No article is selected."));
1411                         return;
1412                 }
1413
1414                 var visible_ids = getLoadedArticleIds();
1415
1416                 var ids_to_mark = new Array();
1417
1418                 if (!below) {
1419                         for (var i = 0; i < visible_ids.length; i++) {
1420                                 if (visible_ids[i] != id) {
1421                                         var e = $("RROW-" + visible_ids[i]);
1422
1423                                         if (e && e.hasClassName("Unread")) {
1424                                                 ids_to_mark.push(visible_ids[i]);
1425                                         }
1426                                 } else {
1427                                         break;
1428                                 }
1429                         }
1430                 } else {
1431                         for (var i = visible_ids.length-1; i >= 0; i--) {
1432                                 if (visible_ids[i] != id) {
1433                                         var e = $("RROW-" + visible_ids[i]);
1434
1435                                         if (e && e.hasClassName("Unread")) {
1436                                                 ids_to_mark.push(visible_ids[i]);
1437                                         }
1438                                 } else {
1439                                         break;
1440                                 }
1441                         }
1442                 }
1443
1444                 if (ids_to_mark.length == 0) {
1445                         alert(__("No articles found to mark"));
1446                 } else {
1447                         var msg = ngettext("Mark %d article as read?", "Mark %d articles as read?", ids_to_mark.length).replace("%d", ids_to_mark.length);
1448
1449                         if (getInitParam("confirm_feed_catchup") != 1 || confirm(msg)) {
1450
1451                                 for (var i = 0; i < ids_to_mark.length; i++) {
1452                                         var e = $("RROW-" + ids_to_mark[i]);
1453                                         e.removeClassName("Unread");
1454                                 }
1455
1456                                 var query = "?op=rpc&method=catchupSelected" +
1457                                         "&cmode=0" + "&ids=" + param_escape(ids_to_mark.toString());
1458
1459                                 new Ajax.Request("backend.php", {
1460                                         parameters: query,
1461                                         onComplete: function(transport) {
1462                                                 handle_rpc_json(transport);
1463                                         } });
1464
1465                         }
1466                 }
1467
1468         } catch (e) {
1469                 exception_error("catchupRelativeToArticle", e);
1470         }
1471 }
1472
1473 function cdmCollapseArticle(event, id, unmark) {
1474         try {
1475                 if (unmark == undefined) unmark = true;
1476
1477                 var row = $("RROW-" + id);
1478                 var elem = $("CICD-" + id);
1479
1480                 if (elem && row) {
1481                         var collapse = $$("div#RROW-" + id +
1482                                 " span[class='collapseBtn']")[0];
1483
1484                         Element.hide(elem);
1485                         Element.show("CEXC-" + id);
1486                         Element.hide(collapse);
1487
1488                         if (unmark) {
1489                                 row.removeClassName("active");
1490
1491                                 markHeadline(id, false);
1492
1493                                 if (id == getActiveArticleId()) {
1494                                         setActiveArticleId(0);
1495                                 }
1496
1497                                 updateSelectedPrompt();
1498                         }
1499
1500                         if (event) Event.stop(event);
1501
1502                         PluginHost.run(PluginHost.HOOK_ARTICLE_COLLAPSED, id);
1503
1504                         if (row.offsetTop < $("headlines-frame").scrollTop)
1505                                 scrollToRowId(row.id);
1506
1507                         $("floatingTitle").style.visibility = "hidden";
1508                         $("floatingTitle").setAttribute("rowid", false);
1509                 }
1510
1511         } catch (e) {
1512                 exception_error("cdmCollapseArticle", e);
1513         }
1514 }
1515
1516 function cdmExpandArticle(id, noexpand) {
1517         try {
1518                 console.log("cdmExpandArticle " + id);
1519
1520                 if (!$("RROW-" + id)) return false;
1521
1522                 var oldrow = $("RROW-" + getActiveArticleId());
1523
1524                 var elem = $("CICD-" + getActiveArticleId());
1525
1526                 if (id == getActiveArticleId() && Element.visible(elem))
1527                         return true;
1528
1529                 selectArticles("none");
1530
1531                 var old_offset = $("RROW-" + id).offsetTop;
1532
1533                 if (getActiveArticleId() && elem && !getInitParam("cdm_expanded")) {
1534                         var collapse = $$("div#RROW-" + getActiveArticleId() +
1535                                 " span[class='collapseBtn']")[0];
1536
1537                         Element.hide(elem);
1538                         Element.show("CEXC-" + getActiveArticleId());
1539                         Element.hide(collapse);
1540                 }
1541
1542                 if (oldrow) oldrow.removeClassName("active");
1543
1544                 setActiveArticleId(id);
1545
1546                 elem = $("CICD-" + id);
1547
1548                 var collapse = $$("div#RROW-" + id +
1549                                 " span[class='collapseBtn']")[0];
1550
1551                 var cencw = $("CENCW-" + id);
1552
1553                 if (!Element.visible(elem) && !noexpand) {
1554                         if (cencw) {
1555                                 cencw.innerHTML = htmlspecialchars_decode(cencw.innerHTML);
1556                                 cencw.setAttribute('id', '');
1557                                 Element.show(cencw);
1558                         }
1559
1560                         Element.show(elem);
1561                         Element.hide("CEXC-" + id);
1562                         Element.show(collapse);
1563                 }
1564
1565                 var new_offset = $("RROW-" + id).offsetTop;
1566
1567                 if (old_offset > new_offset)
1568                         $("headlines-frame").scrollTop -= (old_offset-new_offset);
1569
1570                 if (!noexpand) {
1571                         if (catchup_id_batch.indexOf(id) == -1)
1572                                 catchup_id_batch.push(id);
1573
1574                         catchupCurrentBatchIfNeeded();
1575                 }
1576
1577                 toggleSelected(id);
1578                 $("RROW-" + id).addClassName("active");
1579
1580                 PluginHost.run(PluginHost.HOOK_ARTICLE_EXPANDED, id);
1581
1582         } catch (e) {
1583                 exception_error("cdmExpandArticle", e);
1584         }
1585
1586         return false;
1587 }
1588
1589 function getArticleUnderPointer() {
1590         return post_under_pointer;
1591 }
1592
1593 function scrollArticle(offset) {
1594         try {
1595                 if (!isCdmMode()) {
1596                         var ci = $("content-insert");
1597                         if (ci) {
1598                                 ci.scrollTop += offset;
1599                         }
1600                 } else {
1601                         var hi = $("headlines-frame");
1602                         if (hi) {
1603                                 hi.scrollTop += offset;
1604                         }
1605
1606                 }
1607         } catch (e) {
1608                 exception_error("scrollArticle", e);
1609         }
1610 }
1611
1612 function show_labels_in_headlines(transport) {
1613         try {
1614                 var data = JSON.parse(transport.responseText);
1615
1616                 if (data) {
1617                         data['info-for-headlines'].each(function(elem) {
1618                                 $$(".HLLCTR-" + elem.id).each(function(ctr) {
1619                                         ctr.innerHTML = elem.labels;
1620                                 });
1621                         });
1622                 }
1623         } catch (e) {
1624                 exception_error("show_labels_in_headlines", e);
1625         }
1626 }
1627
1628 function cdmClicked(event, id) {
1629         try {
1630                 //var shift_key = event.shiftKey;
1631
1632                 if (!event.ctrlKey && !event.metaKey) {
1633
1634                         if (!getInitParam("cdm_expanded")) {
1635                                 return cdmExpandArticle(id);
1636                         } else {
1637
1638                                 var elem = $("RROW-" + getActiveArticleId());
1639
1640                                 if (elem) elem.removeClassName("active");
1641
1642                                 selectArticles("none");
1643                                 toggleSelected(id);
1644
1645                                 var elem = $("RROW-" + id);
1646                                 var article_is_unread = elem.hasClassName("Unread");
1647
1648                                 elem.removeClassName("Unread");
1649                                 elem.addClassName("active");
1650
1651                                 setActiveArticleId(id);
1652
1653                                 if (article_is_unread) {
1654                                         decrementFeedCounter(getActiveFeedId(), activeFeedIsCat());
1655                                         updateFloatingTitle(true);
1656                                 }
1657
1658                                 var query = "?op=rpc&method=catchupSelected" +
1659                                         "&cmode=0&ids=" + param_escape(id);
1660
1661                                 new Ajax.Request("backend.php", {
1662                                         parameters: query,
1663                                         onComplete: function(transport) {
1664                                                 handle_rpc_json(transport);
1665                                         } });
1666
1667                                 return !event.shiftKey;
1668                         }
1669
1670                 } else if (event.target.parents(".cdmHeader").length > 0) {
1671
1672                         toggleSelected(id, true);
1673
1674                         var elem = $("RROW-" + id);
1675                         var article_is_unread = elem.hasClassName("Unread");
1676
1677                         if (article_is_unread) {
1678                                 decrementFeedCounter(getActiveFeedId(), activeFeedIsCat());
1679                         }
1680
1681                         toggleUnread(id, 0, false);
1682
1683                         openArticleInNewWindow(id);
1684                 }
1685
1686                 var unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length
1687                 request_counters(unread_in_buffer == 0);
1688
1689         } catch (e) {
1690                 exception_error("cdmClicked");
1691         }
1692
1693         return false;
1694 }
1695
1696 function hlClicked(event, id) {
1697         try {
1698                 if (event.which == 2) {
1699                         view(id);
1700                         return true;
1701                 } else if (event.ctrlKey || event.metaKey) {
1702                         toggleSelected(id, true);
1703                         toggleUnread(id, 0, false);
1704                         openArticleInNewWindow(id);
1705                         return false;
1706                 } else {
1707                         view(id);
1708                         return false;
1709                 }
1710
1711         } catch (e) {
1712                 exception_error("hlClicked");
1713         }
1714 }
1715
1716 function openArticleInNewWindow(id) {
1717         toggleUnread(id, 0, false);
1718         window.open("backend.php?op=article&method=redirect&id=" + id);
1719 }
1720
1721 function isCdmMode() {
1722         return getInitParam("combined_display_mode");
1723 }
1724
1725 function markHeadline(id, marked) {
1726         if (marked == undefined) marked = true;
1727
1728         var row = $("RROW-" + id);
1729         if (row) {
1730                 var check = dijit.getEnclosingWidget(
1731                                 row.getElementsByClassName("rchk")[0]);
1732
1733                 if (check) {
1734                         check.attr("checked", marked);
1735                 }
1736
1737                 if (marked)
1738                         row.addClassName("Selected");
1739                 else
1740                         row.removeClassName("Selected");
1741         }
1742 }
1743
1744 function getRelativePostIds(id, limit) {
1745
1746         var tmp = [];
1747
1748         try {
1749
1750                 if (!limit) limit = 6; //3
1751
1752                 var ids = getLoadedArticleIds();
1753
1754                 for (var i = 0; i < ids.length; i++) {
1755                         if (ids[i] == id) {
1756                                 for (var k = 1; k <= limit; k++) {
1757                                         //if (i > k-1) tmp.push(ids[i-k]);
1758                                         if (i < ids.length-k) tmp.push(ids[i+k]);
1759                                 }
1760                                 break;
1761                         }
1762                 }
1763
1764         } catch (e) {
1765                 exception_error("getRelativePostIds", e);
1766         }
1767
1768         return tmp;
1769 }
1770
1771 function correctHeadlinesOffset(id) {
1772
1773         try {
1774
1775                 var container = $("headlines-frame");
1776                 var row = $("RROW-" + id);
1777
1778                 if (!container || !row) return;
1779
1780                 var viewport = container.offsetHeight;
1781
1782                 var rel_offset_top = row.offsetTop - container.scrollTop;
1783                 var rel_offset_bottom = row.offsetTop + row.offsetHeight - container.scrollTop;
1784
1785                 //console.log("Rtop: " + rel_offset_top + " Rbtm: " + rel_offset_bottom);
1786                 //console.log("Vport: " + viewport);
1787
1788                 if (rel_offset_top <= 0 || rel_offset_top > viewport) {
1789                         container.scrollTop = row.offsetTop;
1790                 } else if (rel_offset_bottom > viewport) {
1791
1792                         /* doesn't properly work with Opera in some cases because
1793                                 Opera fucks up element scrolling */
1794
1795                         container.scrollTop = row.offsetTop + row.offsetHeight - viewport;
1796                 }
1797
1798         } catch (e) {
1799                 exception_error("correctHeadlinesOffset", e);
1800         }
1801
1802 }
1803
1804 function headlineActionsChange(elem) {
1805         try {
1806                 eval(elem.value);
1807                 elem.attr('value', 'false');
1808         } catch (e) {
1809                 exception_error("headlineActionsChange", e);
1810         }
1811 }
1812
1813 function closeArticlePanel() {
1814
1815         if (dijit.byId("content-insert"))
1816                 dijit.byId("headlines-wrap-inner").removeChild(
1817                         dijit.byId("content-insert"));
1818 }
1819
1820 function initFloatingMenu() {
1821         try {
1822                 if (!dijit.byId("floatingMenu")) {
1823
1824                         var menu = new dijit.Menu({
1825                                 id: "floatingMenu",
1826                                 targetNodeIds: ["floatingTitle"]
1827                         });
1828
1829                         var tmph = dojo.connect(menu, '_openMyself', function (event) {
1830                                 var callerNode = event.target, match = null, tries = 0;
1831
1832                                 while (match == null && callerNode && tries <= 3) {
1833                                         match = callerNode.getAttribute("rowid").match("^[A-Z]+[-]([0-9]+)$");
1834                                         callerNode = callerNode.parentNode;
1835                                         ++tries;
1836                                 }
1837
1838                                 if (match) this.callerRowId = parseInt(match[1]);
1839
1840                         });
1841
1842                         headlinesMenuCommon(menu);
1843
1844                         menu.startup();
1845                 }
1846
1847         } catch (e) {
1848                 exception_error("initFloatingMenu", e);
1849         }
1850 }
1851
1852 function headlinesMenuCommon(menu) {
1853         try {
1854
1855                 menu.addChild(new dijit.MenuItem({
1856                         label: __("Open original article"),
1857                         onClick: function(event) {
1858                                 openArticleInNewWindow(this.getParent().callerRowId);
1859                         }}));
1860
1861                 menu.addChild(new dijit.MenuItem({
1862                         label: __("Display article URL"),
1863                         onClick: function(event) {
1864                                 displayArticleUrl(this.getParent().callerRowId);
1865                         }}));
1866
1867                 menu.addChild(new dijit.MenuSeparator());
1868
1869                 menu.addChild(new dijit.MenuItem({
1870                         label: __("Toggle unread"),
1871                         onClick: function(event) {
1872                                 var ids = getSelectedArticleIds2();
1873                                 // cast to string
1874                                 var id = (this.getParent().callerRowId) + "";
1875                                 ids = ids.size() != 0 && ids.indexOf(id) != -1 ? ids : [id];
1876
1877                                 selectionToggleUnread(undefined, false, true, ids);
1878                                 }}));
1879
1880                 menu.addChild(new dijit.MenuItem({
1881                         label: __("Toggle starred"),
1882                         onClick: function(event) {
1883                                 var ids = getSelectedArticleIds2();
1884                                 // cast to string
1885                                 var id = (this.getParent().callerRowId) + "";
1886                                 ids = ids.size() != 0 && ids.indexOf(id) != -1 ? ids : [id];
1887
1888                                 selectionToggleMarked(undefined, false, true, ids);
1889                                 }}));
1890
1891                 menu.addChild(new dijit.MenuItem({
1892                         label: __("Toggle published"),
1893                         onClick: function(event) {
1894                                 var ids = getSelectedArticleIds2();
1895                                 // cast to string
1896                                 var id = (this.getParent().callerRowId) + "";
1897                                 ids = ids.size() != 0 && ids.indexOf(id) != -1 ? ids : [id];
1898
1899                                 selectionTogglePublished(undefined, false, true, ids);
1900                                 }}));
1901
1902                 menu.addChild(new dijit.MenuSeparator());
1903
1904                 menu.addChild(new dijit.MenuItem({
1905                         label: __("Mark above as read"),
1906                         onClick: function(event) {
1907                                 catchupRelativeToArticle(0, this.getParent().callerRowId);
1908                                 }}));
1909
1910                 menu.addChild(new dijit.MenuItem({
1911                         label: __("Mark below as read"),
1912                         onClick: function(event) {
1913                                 catchupRelativeToArticle(1, this.getParent().callerRowId);
1914                                 }}));
1915
1916
1917                 var labels = dijit.byId("feedTree").model.getItemsInCategory(-2);
1918
1919                 if (labels) {
1920
1921                         menu.addChild(new dijit.MenuSeparator());
1922
1923                         var labelAddMenu = new dijit.Menu({ownerMenu: menu});
1924                         var labelDelMenu = new dijit.Menu({ownerMenu: menu});
1925
1926                         labels.each(function(label) {
1927                                 var id = label.id[0];
1928                                 var bare_id = id.substr(id.indexOf(":")+1);
1929                                 var name = label.name[0];
1930
1931                                 bare_id = feed_to_label_id(bare_id);
1932
1933                                 labelAddMenu.addChild(new dijit.MenuItem({
1934                                         label: name,
1935                                         labelId: bare_id,
1936                                         onClick: function(event) {
1937                                                 var ids = getSelectedArticleIds2();
1938                                                 // cast to string
1939                                                 var id = (this.getParent().ownerMenu.callerRowId) + "";
1940
1941                                                 ids = ids.size() != 0 && ids.indexOf(id) != -1 ? ids : [id];
1942
1943                                                 selectionAssignLabel(this.labelId, ids);
1944                                 }}));
1945
1946                                 labelDelMenu.addChild(new dijit.MenuItem({
1947                                         label: name,
1948                                         labelId: bare_id,
1949                                         onClick: function(event) {
1950                                                 var ids = getSelectedArticleIds2();
1951                                                 // cast to string
1952                                                 var id = (this.getParent().ownerMenu.callerRowId) + "";
1953
1954                                                 ids = ids.size() != 0 && ids.indexOf(id) != -1 ? ids : [id];
1955
1956                                                 selectionRemoveLabel(this.labelId, ids);
1957                                 }}));
1958
1959                         });
1960
1961                         menu.addChild(new dijit.PopupMenuItem({
1962                                 label: __("Assign label"),
1963                                 popup: labelAddMenu
1964                         }));
1965
1966                         menu.addChild(new dijit.PopupMenuItem({
1967                                 label: __("Remove label"),
1968                                 popup: labelDelMenu
1969                         }));
1970
1971                 }
1972
1973
1974         } catch (e) {
1975                 exception_error("headlinesMenuCommon", e);
1976         }
1977 }
1978
1979 function initHeadlinesMenu() {
1980         try {
1981                 if (dijit.byId("headlinesMenu"))
1982                         dijit.byId("headlinesMenu").destroyRecursive();
1983
1984                 var ids = [];
1985
1986                 if (!isCdmMode()) {
1987                         nodes = $$("#headlines-frame > div[id*=RROW]");
1988                 } else {
1989                         nodes = $$("#headlines-frame span[id*=RTITLE]");
1990                 }
1991
1992                 nodes.each(function(node) {
1993                         ids.push(node.id);
1994                 });
1995
1996                 var menu = new dijit.Menu({
1997                         id: "headlinesMenu",
1998                         targetNodeIds: ids
1999                 });
2000
2001                 var tmph = dojo.connect(menu, '_openMyself', function (event) {
2002                         var callerNode = event.target, match = null, tries = 0;
2003
2004                         while (match == null && callerNode && tries <= 3) {
2005                                 match = callerNode.id.match("^[A-Z]+[-]([0-9]+)$");
2006                                 callerNode = callerNode.parentNode;
2007                                 ++tries;
2008                         }
2009
2010                         if (match) this.callerRowId = parseInt(match[1]);
2011
2012                 });
2013
2014                 headlinesMenuCommon(menu);
2015
2016                 menu.startup();
2017
2018                 /* vgroup feed title menu */
2019
2020                 var nodes = $$("#headlines-frame > div[class='cdmFeedTitle']");
2021                 var ids = [];
2022
2023                 nodes.each(function(node) {
2024                         ids.push(node.id);
2025                 });
2026
2027                 if (ids.length > 0) {
2028                         if (dijit.byId("headlinesFeedTitleMenu"))
2029                                 dijit.byId("headlinesFeedTitleMenu").destroyRecursive();
2030
2031                         var menu = new dijit.Menu({
2032                                 id: "headlinesFeedTitleMenu",
2033                                 targetNodeIds: ids
2034                         });
2035
2036                         var tmph = dojo.connect(menu, '_openMyself', function (event) {
2037                                 var callerNode = event.target, match = null, tries = 0;
2038
2039                                 while (match == null && callerNode && tries <= 3) {
2040                                         match = callerNode.id.match("^[A-Z]+[-]([0-9]+)$");
2041                                         callerNode = callerNode.parentNode;
2042                                         ++tries;
2043                                 }
2044
2045                                 if (match) this.callerRowId = parseInt(match[1]);
2046
2047                         });
2048
2049                         menu.addChild(new dijit.MenuItem({
2050                                 label: __("Select articles in group"),
2051                                 onClick: function(event) {
2052                                         selectArticles("all",
2053                                                 "#headlines-frame > div[id*=RROW]"+
2054                                                 "[orig-feed-id='"+menu.callerRowId+"']");
2055
2056                                 }}));
2057
2058                         menu.addChild(new dijit.MenuItem({
2059                                 label: __("Mark group as read"),
2060                                 onClick: function(event) {
2061                                         selectArticles("none");
2062                                         selectArticles("all",
2063                                                 "#headlines-frame > div[id*=RROW]"+
2064                                                 "[orig-feed-id='"+menu.callerRowId+"']");
2065
2066                                         catchupSelection();
2067                                 }}));
2068
2069
2070                         menu.addChild(new dijit.MenuItem({
2071                                 label: __("Mark feed as read"),
2072                                 onClick: function(event) {
2073                                         catchupFeedInGroup(menu.callerRowId);
2074                                 }}));
2075
2076                         menu.addChild(new dijit.MenuItem({
2077                                 label: __("Edit feed"),
2078                                 onClick: function(event) {
2079                                         editFeed(menu.callerRowId);
2080                                 }}));
2081
2082                         menu.startup();
2083
2084                 }
2085
2086         } catch (e) {
2087                 exception_error("initHeadlinesMenu", e);
2088         }
2089 }
2090
2091 function cache_set(id, obj) {
2092         //console.log("cache_set: " + id);
2093         if (has_storage)
2094                 try {
2095                         sessionStorage[id] = obj;
2096                 } catch (e) {
2097                         sessionStorage.clear();
2098                 }
2099 }
2100
2101 function cache_get(id) {
2102         if (has_storage)
2103                 return sessionStorage[id];
2104 }
2105
2106 function cache_clear() {
2107         if (has_storage)
2108                 sessionStorage.clear();
2109 }
2110
2111 function cache_delete(id) {
2112         if (has_storage)
2113                 sessionStorage.removeItem(id);
2114 }
2115
2116 function cancelSearch() {
2117         try {
2118                 _search_query = "";
2119                 viewCurrentFeed();
2120         } catch (e) {
2121                 exception_error("cancelSearch", e);
2122         }
2123 }
2124
2125 function setSelectionScore() {
2126         try {
2127                 var ids = getSelectedArticleIds2();
2128
2129                 if (ids.length > 0) {
2130                         console.log(ids);
2131
2132                         var score = prompt(__("Please enter new score for selected articles:"), score);
2133
2134                         if (score != undefined) {
2135                                 var query = "op=article&method=setScore&id=" + param_escape(ids.toString()) +
2136                                         "&score=" + param_escape(score);
2137
2138                                 new Ajax.Request("backend.php", {
2139                                         parameters: query,
2140                                         onComplete: function(transport) {
2141                                                 var reply = JSON.parse(transport.responseText);
2142                                                 if (reply) {
2143                                                         console.log(ids);
2144
2145                                                         ids.each(function(id) {
2146                                                                 var row = $("RROW-" + id);
2147
2148                                                                 if (row) {
2149                                                                         var pic = row.getElementsByClassName("hlScorePic")[0];
2150
2151                                                                         if (pic) {
2152                                                                                 pic.src = pic.src.replace(/score_.*?\.png/,
2153                                                                                         reply["score_pic"]);
2154                                                                                 pic.setAttribute("score", score);
2155                                                                         }
2156                                                                 }
2157                                                         });
2158                                                 }
2159                                         } });
2160                         }
2161
2162                 } else {
2163                         alert(__("No articles are selected."));
2164                 }
2165         } catch (e) {
2166                 exception_error("setSelectionScore", e);
2167         }
2168 }
2169
2170 function updateScore(id) {
2171         try {
2172                 var pic = $$("#RROW-" + id + " .hlScorePic")[0];
2173
2174                 if (pic) {
2175
2176                         var query = "op=article&method=getScore&id=" + param_escape(id);
2177
2178                         new Ajax.Request("backend.php", {
2179                                 parameters: query,
2180                                 onComplete: function(transport) {
2181                                         console.log(transport.responseText);
2182
2183                                         var reply = JSON.parse(transport.responseText);
2184
2185                                         if (reply) {
2186                                                 pic.src = pic.src.replace(/score_.*?\.png/, reply["score_pic"]);
2187                                                 pic.setAttribute("score", reply["score"]);
2188                                                 pic.setAttribute("title", reply["score"]);
2189                                         }
2190                                 } });
2191                 }
2192
2193         } catch (e) {
2194                 exception_error("updateScore", e);
2195         }
2196 }
2197
2198 function changeScore(id, pic) {
2199         try {
2200                 var score = pic.getAttribute("score");
2201
2202                 var new_score = prompt(__("Please enter new score for this article:"), score);
2203
2204                 if (new_score != undefined) {
2205
2206                         var query = "op=article&method=setScore&id=" + param_escape(id) +
2207                                 "&score=" + param_escape(new_score);
2208
2209                         new Ajax.Request("backend.php", {
2210                                 parameters: query,
2211                                 onComplete: function(transport) {
2212                                         var reply = JSON.parse(transport.responseText);
2213
2214                                         if (reply) {
2215                                                 pic.src = pic.src.replace(/score_.*?\.png/, reply["score_pic"]);
2216                                                 pic.setAttribute("score", new_score);
2217                                                 pic.setAttribute("title", new_score);
2218                                         }
2219                                 } });
2220                 }
2221         } catch (e) {
2222                 exception_error("changeScore", e);
2223         }
2224 }
2225
2226 function displayArticleUrl(id) {
2227         try {
2228                 var query = "op=rpc&method=getlinktitlebyid&id=" + param_escape(id);
2229
2230                         new Ajax.Request("backend.php", {
2231                                 parameters: query,
2232                                 onComplete: function(transport) {
2233                                         var reply = JSON.parse(transport.responseText);
2234
2235                                         if (reply && reply.link) {
2236                                                 prompt(__("Article URL:"), reply.link);
2237                                         }
2238                                 } });
2239         } catch (e) {
2240                 exception_error("changeScore", e);
2241         }
2242 }
2243
2244 function scrollToRowId(id) {
2245         try {
2246                 var row = $(id);
2247
2248                 if (row)
2249                         $("headlines-frame").scrollTop = row.offsetTop - 4;
2250
2251         } catch (e) {
2252                 exception_error("scrollToRowId", e);
2253         }
2254 }
2255
2256 function updateFloatingTitle(unread_only) {
2257         try {
2258                 if (!isCdmMode()) return;
2259
2260                 var hf = $("headlines-frame");
2261
2262                 var elems = $$("#headlines-frame > div[id*=RROW]");
2263
2264                 for (var i = 0; i < elems.length; i++) {
2265
2266                         var child = elems[i];
2267
2268                         if (child && child.offsetTop + child.offsetHeight > hf.scrollTop) {
2269
2270                                 var header = child.getElementsByClassName("cdmHeader")[0];
2271
2272                                 if (unread_only || child.id != $("floatingTitle").getAttribute("rowid")) {
2273                                         if (child.id != $("floatingTitle").getAttribute("rowid")) {
2274                                                 $("floatingTitle").setAttribute("rowid", child.id);
2275                                                 $("floatingTitle").innerHTML = header.innerHTML;
2276                                                 $("floatingTitle").firstChild.innerHTML = "<img class='anchor markedPic' src='images/page_white_go.png' onclick=\"scrollToRowId('"+child.id+"')\">" + $("floatingTitle").firstChild.innerHTML;
2277
2278                                                 initFloatingMenu();
2279
2280                                                 var cb = $$("#floatingTitle .dijitCheckBox")[0];
2281
2282                                                 if (cb)
2283                                                         cb.parentNode.removeChild(cb);
2284                                         }
2285
2286                                         if (child.hasClassName("Unread"))
2287                                                 $("floatingTitle").addClassName("Unread");
2288                                         else
2289                                                 $("floatingTitle").removeClassName("Unread");
2290
2291                                         PluginHost.run(PluginHost.HOOK_FLOATING_TITLE, child);
2292                                 }
2293
2294                                 $("floatingTitle").style.marginRight = hf.offsetWidth - child.offsetWidth + "px";
2295                                 if (header.offsetTop + header.offsetHeight < hf.scrollTop + $("floatingTitle").offsetHeight - 5 &&
2296                                     child.offsetTop + child.offsetHeight >= hf.scrollTop + $("floatingTitle").offsetHeight - 5)
2297                                         $("floatingTitle").style.visibility = "visible";
2298                                 else
2299                                         $("floatingTitle").style.visibility = "hidden";
2300
2301                                 return;
2302
2303                         }
2304                 }
2305
2306         } catch (e) {
2307                 exception_error("updateFloatingTitle", e);
2308         }
2309 }
2310
2311 function catchupCurrentBatchIfNeeded() {
2312         if (catchup_id_batch.length > 0) {
2313                 window.clearTimeout(catchup_timeout_id);
2314                 catchup_timeout_id = window.setTimeout('catchupBatchedArticles()', 1000);
2315
2316                 if (catchup_id_batch.length >= 10) {
2317                         catchupBatchedArticles();
2318                 }
2319         }
2320 }
2321
2322 function cdmFooterClick(event) {
2323         event.stopPropagation();
2324 }