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