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