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