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