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