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