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