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