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