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