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