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