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