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