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