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