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