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