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