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