]> git.wh0rd.org Git - tt-rss.git/blob - tt-rss.js
display active hotkey prefix on screen
[tt-rss.git] / tt-rss.js
1
2 var total_unread = 0;
3 var first_run = true;
4 var display_tags = false;
5 var global_unread = -1;
6 var active_title_text = "";
7 var current_subtitle = "";
8 var daemon_enabled = false;
9 var daemon_refresh_only = false;
10 //var _qfd_deleted_feed = 0;
11 var firsttime_update = true;
12 var _active_feed_id = 0;
13 var _active_feed_is_cat = false;
14 var number_of_feeds = 0;
15 var sanity_check_done = false;
16 var _hfd_scrolltop = 0;
17 var hotkey_prefix = false;
18 var hotkey_prefix_pressed = false;
19 var init_params = new Object();
20 var ver_offset = 0;
21 var hor_offset = 0;
22 var feeds_sort_by_unread = false;
23 var feedlist_sortable_enabled = false;
24
25 function activeFeedIsCat() {
26         return _active_feed_is_cat;
27 }
28
29 function getActiveFeedId() {
30 //      return getCookie("ttrss_vf_actfeed");
31         try {
32                 debug("gAFID: " + _active_feed_id);
33                 return _active_feed_id;
34         } catch (e) {
35                 exception_error("getActiveFeedId", e);
36         }
37 }
38
39 function setActiveFeedId(id, is_cat) {
40 //      return setCookie("ttrss_vf_actfeed", id);
41         try {
42                 debug("sAFID(" + id + ", " + is_cat + ")");
43                 _active_feed_id = id;
44
45                 if (is_cat != undefined) {
46                         _active_feed_is_cat = is_cat;
47                 }
48
49         } catch (e) {
50                 exception_error("setActiveFeedId", e);
51         }
52 }
53
54
55 function isFeedlistSortable() {
56         return feedlist_sortable_enabled;
57 }
58
59 function tagsAreDisplayed() {
60         return display_tags;
61 }
62
63 function toggleTags(show_all) {
64
65         try {
66
67         debug("toggleTags: " + show_all + "; " + display_tags);
68
69         var p = $("dispSwitchPrompt");
70
71         if (!show_all && !display_tags) {
72                 displayDlg("printTagCloud");
73         } else if (show_all) {
74                 closeInfoBox();
75                 display_tags = true;
76                 p.innerHTML = __("display feeds");
77                 notify_progress("Loading, please wait...", true);
78                 updateFeedList();
79         } else if (display_tags) {
80                 display_tags = false;
81                 p.innerHTML = __("tag cloud");
82                 notify_progress("Loading, please wait...", true);
83                 updateFeedList();
84         }
85
86         } catch (e) {
87                 exception_error("toggleTags", e);
88         }
89 }
90
91 function dlg_frefresh_callback(transport, deleted_feed) {
92         if (getActiveFeedId() == deleted_feed) {
93                 var h = $("headlines-frame");
94                 if (h) {
95                         h.innerHTML = "<div class='whiteBox'>" + __('No feed selected.') + "</div>";
96                 }
97         }
98
99         setTimeout('updateFeedList(false, false)', 50);
100         closeInfoBox();
101 }
102
103 function refetch_callback2(transport) {
104         try {
105
106                 var date = new Date();
107
108                 parse_counters_reply(transport, true);
109
110                 debug("refetch_callback2: done");
111
112 /*              if (!daemon_enabled && !daemon_refresh_only) {
113                         notify_info("All feeds updated.");
114                         updateTitle("");
115                 } else {
116                         //notify("");
117                 } */
118         } catch (e) {
119                 exception_error("refetch_callback", e);
120                 updateTitle("");
121         }
122 }
123
124 function backend_sanity_check_callback(transport) {
125
126         try {
127
128                 if (sanity_check_done) {
129                         fatalError(11, "Sanity check request received twice. This can indicate "+
130                       "presence of Firebug or some other disrupting extension. "+
131                                 "Please disable it and try again.");
132                         return;
133                 }
134
135                 if (!transport.responseXML) {
136                         if (!store) {
137                                 fatalError(3, "Sanity check: Received reply is not XML", 
138                                         transport.responseText);
139                                 return;
140                         } else {
141                                 init_offline();
142                                 return;
143                         }
144                 }
145
146                 if (getURLParam("offline")) {
147                         return init_offline();
148                 }
149
150                 var reply = transport.responseXML.firstChild.firstChild;
151
152                 if (!reply) {
153                         fatalError(3, "Sanity check: invalid RPC reply", transport.responseText);
154                         return;
155                 }
156
157                 var error_code = reply.getAttribute("error-code");
158         
159                 if (error_code && error_code != 0) {
160                         return fatalError(error_code, reply.getAttribute("error-msg"));
161                 }
162
163                 debug("sanity check ok");
164
165                 var params = reply.nextSibling;
166
167                 if (params) {
168                         debug('reading init-params...');
169                         var param = params.firstChild;
170
171                         while (param) {
172                                 var k = param.getAttribute("key");
173                                 var v = param.getAttribute("value");
174                                 debug(k + " => " + v);
175                                 init_params[k] = v;                                     
176
177                                 if (db) {
178                                         db.execute("DELETE FROM init_params WHERE key = ?", [k]);
179                                         db.execute("INSERT INTO init_params (key,value) VALUES (?, ?)",
180                                                 [k, v]);
181                                 }
182
183                                 param = param.nextSibling;
184                         }
185                 }
186
187                 sanity_check_done = true;
188
189                 init_second_stage();
190
191         } catch (e) {
192                 exception_error("backend_sanity_check_callback", e, transport); 
193         } 
194 }
195
196 function scheduleFeedUpdate(force) {
197
198         debug("in scheduleFeedUpdate");
199
200 /*      if (!daemon_enabled && !daemon_refresh_only) {
201                 notify_progress("Updating feeds...", true);
202         } */
203
204         var query_str = "backend.php?op=rpc&subop=";
205
206         if (force) {
207                 query_str = query_str + "forceUpdateAllFeeds";
208         } else {
209                 query_str = query_str + "updateAllFeeds";
210         }
211
212         var omode;
213
214         if (firsttime_update && !navigator.userAgent.match("Opera")) {
215                 firsttime_update = false;
216                 omode = "T";
217         } else {
218                 if (display_tags) {
219                         omode = "tl";
220                 } else {
221                         omode = "flc";
222                 }
223         }
224         
225         query_str = query_str + "&omode=" + omode;
226         query_str = query_str + "&uctr=" + global_unread;
227
228         var date = new Date();
229         var timestamp = Math.round(date.getTime() / 1000);
230         query_str = query_str + "&ts=" + timestamp
231
232         debug("REFETCH query: " + query_str);
233
234         new Ajax.Request(query_str, {
235                 onComplete: function(transport) { 
236                                 refetch_callback2(transport); 
237                         } });
238 }
239
240 function updateFeedList(silent, fetch) {
241
242 //      if (silent != true) {
243 //              notify("Loading feed list...");
244 //      }
245
246         debug("<b>updateFeedList</b>");
247
248         if (offline_mode) return render_offline_feedlist();
249
250         var query_str = "backend.php?op=feeds";
251
252         if (display_tags) {
253                 query_str = query_str + "&tags=1";
254         }
255
256         if (getActiveFeedId() && !activeFeedIsCat()) {
257                 query_str = query_str + "&actid=" + getActiveFeedId();
258         }
259
260         var date = new Date();
261         var timestamp = Math.round(date.getTime() / 1000);
262         query_str = query_str + "&ts=" + timestamp
263         
264         if (fetch) query_str = query_str + "&fetch=yes";
265
266 //      var feeds_frame = $("feeds-frame");
267 //      feeds_frame.src = query_str;
268
269         debug("updateFeedList Q=" + query_str);
270
271         new Ajax.Request(query_str, {
272                 onComplete: function(transport) { 
273                         feedlist_callback2(transport); 
274                 } });
275
276 }
277
278 function catchupAllFeeds() {
279
280         var str = __("Mark all articles as read?");
281
282         if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
283
284                 var query_str = "backend.php?op=feeds&subop=catchupAll";
285
286                 notify_progress("Marking all feeds as read...");
287
288                 debug("catchupAllFeeds Q=" + query_str);
289
290                 new Ajax.Request(query_str, {
291                         onComplete: function(transport) { 
292                                 feedlist_callback2(transport); 
293                         } });
294
295                 global_unread = 0;
296                 updateTitle("");
297         }
298 }
299
300 function viewCurrentFeed(subop) {
301
302 //      if (getActiveFeedId()) {
303         if (getActiveFeedId() != undefined) {
304                 viewfeed(getActiveFeedId(), subop, activeFeedIsCat());
305         } else {
306                 disableContainerChildren("headlinesToolbar", false, document);
307 //              viewfeed(-1, subop); // FIXME
308         }
309         return false; // block unneeded form submits
310 }
311
312 function viewfeed(feed, subop) {
313         var f = window.frames["feeds-frame"];
314         f.viewfeed(feed, subop);
315 }
316
317 function timeout() {
318         if (getInitParam("bw_limit") == "1") return;
319
320         scheduleFeedUpdate(false);
321
322         var refresh_time = getInitParam("feeds_frame_refresh");
323
324         if (!refresh_time) refresh_time = 600; 
325
326         setTimeout("timeout()", refresh_time*1000);
327 }
328
329 function resetSearch() {
330         var searchbox = $("searchbox")
331
332         if (searchbox.value != "" && getActiveFeedId()) {       
333                 searchbox.value = "";
334                 viewfeed(getActiveFeedId(), "");
335         }
336 }
337
338 function searchCancel() {
339         closeInfoBox(true);
340 }
341
342 function search() {
343         closeInfoBox(); 
344         viewCurrentFeed(0, "");
345 }
346
347 // if argument is undefined, current subtitle is not updated
348 // use blank string to clear subtitle
349 function updateTitle(s) {
350         var tmp = "Tiny Tiny RSS";
351
352         if (s != undefined) {
353                 current_subtitle = s;
354         }
355
356         if (global_unread > 0) {
357                 tmp = tmp + " (" + global_unread + ")";
358         }
359
360         if (current_subtitle) {
361                 tmp = tmp + " - " + current_subtitle;
362         }
363
364         if (active_title_text.length > 0) {
365                 tmp = tmp + " > " + active_title_text;
366         }
367
368         document.title = tmp;
369 }
370
371 function genericSanityCheck() {
372
373 //      if (!Ajax.getTransport()) fatalError(1);
374
375         setCookie("ttrss_vf_test", "TEST");
376         
377         if (getCookie("ttrss_vf_test") != "TEST") {
378                 fatalError(2);
379         }
380
381         return true;
382 }
383
384 function init() {
385
386         try {
387
388                 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
389
390                 if (arguments.callee.done) return;
391                 arguments.callee.done = true;           
392
393                 init_gears();
394
395                 disableContainerChildren("headlinesToolbar", true);
396
397                 Form.disable("main_toolbar_form");
398
399                 if (!genericSanityCheck()) 
400                         return;
401
402                 if (getURLParam('debug')) {
403                         Element.show("debug_output");
404                         debug('debug mode activated');
405                 }
406
407                 var params = "&ua=" + param_escape(navigator.userAgent);
408
409                 loading_set_progress(30);
410
411                 new Ajax.Request("backend.php?op=rpc&subop=sanityCheck" + params,       {
412                         onComplete: function(transport) {
413                                         backend_sanity_check_callback(transport);
414                                 } });
415
416         } catch (e) {
417                 exception_error("init", e);
418         }
419 }
420
421 function resize_headlines(delta_x, delta_y) {
422
423         try {
424
425                 debug("resize_headlines: " + delta_x + ":" + delta_y);
426         
427                 var h_frame = $("headlines-frame");
428                 var c_frame = $("content-frame");
429                 var f_frame = $("footer");
430                 var feeds_frame = $("feeds-holder");
431                 var resize_grab = $("resize-grabber");
432                 var resize_handle = $("resize-handle");
433
434                 if (!c_frame || !h_frame) return;
435         
436                 if (feeds_frame && getInitParam("theme") == "compat") {
437                                 feeds_frame.style.bottom = f_frame.offsetHeight + "px";         
438                 }
439         
440                 if (getInitParam("theme") == "3pane") {
441         
442                         if (delta_x != undefined) {
443                                 if (c_frame.offsetLeft - delta_x > feeds_frame.offsetWidth + feeds_frame.offsetLeft + 100 && c_frame.offsetWidth + delta_x > 100) {
444                                         hor_offset = hor_offset + delta_x;
445                                 }
446                         }
447         
448                         debug("resize_headlines: HOR-mode: " + hor_offset);
449         
450                         c_frame.style.width = (400 + hor_offset) + "px";
451                         h_frame.style.right = c_frame.offsetWidth - 1 + "px";
452         
453                         resize_grab.style.top = (h_frame.offsetTop + h_frame.offsetHeight - 60) + "px";
454                         resize_grab.style.left = (h_frame.offsetLeft + h_frame.offsetWidth - 
455                                 4) + "px";
456                         resize_grab.style.display = "block";
457
458                         resize_handle.src = "themes/3pane/images/resize_handle_vert.png";
459                         resize_handle.style.paddingTop = (resize_grab.offsetHeight / 2 - 7) + "px";
460         
461                 } else {
462         
463                         if (delta_y != undefined) {
464                                 if (c_frame.offsetHeight + delta_y > 100 && h_frame.offsetHeight - delta_y > 100) {
465                                         ver_offset = ver_offset + delta_y;
466                                 }
467                         }
468         
469                         debug("resize_headlines: VER-mode: " + ver_offset);
470         
471                         h_frame.style.height = (300 - ver_offset) + "px";
472         
473                         c_frame.style.top = (h_frame.offsetTop + h_frame.offsetHeight + 0) + "px";
474                         h_frame.style.height = h_frame.offsetHeight + "px";
475         
476                         var theme_c = 0;
477         
478                         if (getInitParam("theme") == "graycube") {
479                                 theme_c = 1;
480                         }
481
482                         if (getInitParam("theme") == "graycube" || getInitParam("theme") == "compat") {
483                                 resize_handle.src = "themes/graycube/images/resize_handle_horiz.png";
484                         }
485         
486 /*                      resize_grab.style.top = (h_frame.offsetTop + h_frame.offsetHeight - 
487                                 4 - theme_c) + "px";
488                         resize_grab.style.display = "block"; */
489         
490                 }
491         
492                 if (getInitParam("cookie_lifetime") != 0) {
493                         setCookie("ttrss_offset_ver", ver_offset, 
494                                 getInitParam("cookie_lifetime"));
495                         setCookie("ttrss_offset_hor", hor_offset, 
496                                 getInitParam("cookie_lifetime"));
497                 } else {
498                         setCookie("ttrss_offset_ver", ver_offset);
499                         setCookie("ttrss_offset_hor", hor_offset);
500                 }
501
502         } catch (e) {
503                 exception_error("resize_headlines", e);
504         }
505
506 }
507
508 function init_second_stage() {
509
510         try {
511
512                 delCookie("ttrss_vf_test");
513
514 //              document.onresize = resize_headlines;
515
516                 var toolbar = document.forms["main_toolbar_form"];
517
518                 dropboxSelect(toolbar.view_mode, getInitParam("default_view_mode"));
519                 dropboxSelect(toolbar.limit, getInitParam("default_view_limit"));
520                 dropboxSelect(toolbar.order_by, getInitParam("default_view_order_by"));
521
522                 daemon_enabled = getInitParam("daemon_enabled") == 1;
523                 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
524                 feeds_sort_by_unread = getInitParam("feeds_sort_by_unread") == 1;
525
526 /*              var fl = cache_find_param("FEEDLIST", getInitParam("num_feeds"));
527
528                 if (fl) {
529                         render_feedlist(fl);
530                         if ($("feedList")) {
531                                 request_counters();
532                         } else {
533                                 setTimeout('updateFeedList(false, false)', 50);
534                         }
535                 } else {
536                         setTimeout('updateFeedList(false, false)', 50);
537                 } */
538
539                 setTimeout('updateFeedList(false, false)', 50);
540
541                 debug("second stage ok");
542
543                 loading_set_progress(60);
544
545                 ver_offset = parseInt(getCookie("ttrss_offset_ver"));
546                 hor_offset = parseInt(getCookie("ttrss_offset_hor"));
547
548                 debug("got offsets from cookies: ver " + ver_offset + " hor " + hor_offset);
549
550                 /* fuck IE */
551
552                 if (isNaN(hor_offset)) hor_offset = 0;
553                 if (isNaN(ver_offset)) ver_offset = 0;
554
555                 debug("offsets from cookies [x:y]: " + hor_offset + ":" + ver_offset);
556
557                 resize_headlines();
558
559                 enable_offline_reading();
560
561         } catch (e) {
562                 exception_error("init_second_stage", e);
563         }
564 }
565
566 function quickMenuChange() {
567         var chooser = $("quickMenuChooser");
568         var opid = chooser[chooser.selectedIndex].value;
569
570         chooser.selectedIndex = 0;
571         quickMenuGo(opid);
572 }
573
574 function quickMenuGo(opid) {
575         try {
576
577                 if (opid == "qmcPrefs") {
578                         gotoPreferences();
579                 }
580         
581                 if (opid == "qmcSearch") {
582                         displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
583                         return;
584                 }
585         
586                 if (opid == "qmcAddFeed") {
587                         displayDlg("quickAddFeed");
588                         return;
589                 }
590
591                 if (opid == "qmcEditFeed") {
592                         editFeedDlg(getActiveFeedId());
593                 }
594         
595                 if (opid == "qmcRemoveFeed") {
596                         var actid = getActiveFeedId();
597
598                         if (activeFeedIsCat()) {
599                                 alert(__("You can't unsubscribe from the category."));
600                                 return;
601                         }       
602
603                         if (!actid) {
604                                 alert(__("Please select some feed first."));
605                                 return;
606                         }
607
608                         var fn = getFeedName(actid);
609
610                         var pr = __("Unsubscribe from %s?").replace("%s", fn);
611
612                         if (confirm(pr)) {
613                                 unsubscribeFeed(actid);
614                         }
615                 
616                         return;
617                 }
618
619                 if (opid == "qmcClearFeed") {
620                         var actid = getActiveFeedId();
621
622                         if (!actid) {
623                                 alert(__("Please select some feed first."));
624                                 return;
625                         }
626
627                         if (activeFeedIsCat() || actid < 0) {
628                                 alert(__("You can't clear this type of feed."));
629                                 return;
630                         }       
631
632                         var fn = getFeedName(actid);
633
634                         var pr = __("Erase all non-starred articles in %s?").replace("%s", fn);
635
636                         if (confirm(pr)) {
637                                 clearFeedArticles(actid);
638                         }
639                 
640                         return;
641                 }
642         
643
644                 if (opid == "qmcUpdateFeeds") {
645                         scheduleFeedUpdate(true);
646                         return;
647                 }
648         
649                 if (opid == "qmcCatchupAll") {
650                         catchupAllFeeds();
651                         return;
652                 }
653         
654                 if (opid == "qmcShowOnlyUnread") {
655                         toggleDispRead();
656                         return;
657                 }
658         
659                 if (opid == "qmcAddFilter") {
660                         displayDlg("quickAddFilter", getActiveFeedId());
661                 }
662
663                 if (opid == "qmcAddLabel") {
664                         addLabel();
665                 }
666
667                 if (opid == "qmcRescoreFeed") {
668                         rescoreCurrentFeed();
669                 }
670
671                 if (opid == "qmcHKhelp") {
672                         //Element.show("hotkey_help_overlay");
673                         Effect.Appear("hotkey_help_overlay", {duration : 0.3});
674                 }
675
676                 if (opid == "qmcResetUI") {
677                         hor_offset = 0;
678                         ver_offset = 0;
679                         resize_headlines();
680                 }
681
682                 if (opid == "qmcResetCats") {
683
684                         if (confirm(__("Reset category order?"))) {
685
686                                 var query = "backend.php?op=feeds&subop=catsortreset";
687
688                                 notify_progress("Loading, please wait...", true);
689
690                                 new Ajax.Request(query, {
691                                         onComplete: function(transport) { 
692                                                 window.setTimeout('updateFeedList(false, false)', 50);
693                                         } });
694                         }
695                 }
696
697         } catch (e) {
698                 exception_error("quickMenuGo", e);
699         }
700 }
701
702 function unsubscribeFeed(feed_id, title) {
703
704
705         var msg = __("Unsubscribe from %s?").replace("%s", title);
706
707         if (title == undefined || confirm(msg)) {
708                 notify_progress("Removing feed...");
709
710                 var query = "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id;
711
712                 new Ajax.Request(query, {
713                         onComplete: function(transport) {
714                                         dlg_frefresh_callback(transport, feed_id);
715                                 } });
716         }
717
718         return false;
719 }
720
721
722 function updateFeedTitle(t) {
723         active_title_text = t;
724         updateTitle();
725 }
726
727 function toggleDispRead() {
728         try {
729
730                 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
731
732                 hide_read_feeds = !hide_read_feeds;
733
734                 debug("toggle_disp_read => " + hide_read_feeds);
735
736                 hideOrShowFeeds(hide_read_feeds);
737
738                 storeInitParam("hide_read_feeds", hide_read_feeds, true);
739                                 
740         } catch (e) {
741                 exception_error("toggleDispRead", e);
742         }
743 }
744
745 function parse_runtime_info(elem) {
746         if (!elem) {
747                 debug("parse_runtime_info: elem is null, aborting");
748                 return;
749         }
750
751         var param = elem.firstChild;
752
753         debug("parse_runtime_info: " + param);
754
755         while (param) {
756                 var k = param.getAttribute("key");
757                 var v = param.getAttribute("value");
758
759                 debug("RI: " + k + " => " + v);
760
761                 if (k == "num_feeds") {
762                         init_params[k] = v;                                     
763                 }
764
765                 if (k == "new_version_available") {
766                         var icon = $("newVersionIcon");
767                         if (icon) {
768                                 if (v == "1") {
769                                         icon.style.display = "inline";
770                                 } else {
771                                         icon.style.display = "none";
772                                 }
773                         }
774                 }
775
776                 var error_flag;
777
778                 if (k == "daemon_is_running" && v != 1) {
779                         notify_error("<span onclick=\"javascript:explainError(1)\">Update daemon is not running.</span>", true);
780                         error_flag = true;
781                 }
782
783                 if (k == "daemon_stamp_ok" && v != 1) {
784                         notify_error("<span onclick=\"javascript:explainError(3)\">Update daemon is not updating feeds.</span>", true);
785                         error_flag = true;
786                 }
787
788                 if (!error_flag) {
789                         notify('');
790                 }
791
792 /*              var w = $("noDaemonWarning");
793                 
794                 if (w) {
795                         if (k == "daemon_is_running" && v != 1) {
796                                 w.style.display = "block";
797                         } else {
798                                 w.style.display = "none";
799                         }
800                 } */
801                 param = param.nextSibling;
802         }
803 }
804
805 function catchupCurrentFeed() {
806
807         var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
808         
809         var str = __("Mark all articles in %s as read?").replace("%s", fn);
810
811         if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
812                 return viewCurrentFeed('MarkAllRead')
813         }
814 }
815
816 function catchupFeedInGroup(id) {
817
818         try {
819
820                 var title = getFeedName(id);
821
822                 var str = __("Mark all articles in %s as read?").replace("%s", title);
823
824                 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
825                         return viewCurrentFeed('MarkAllReadGR:' + id)
826                 }
827
828         } catch (e) {
829                 exception_error("catchupFeedInGroup", e);
830         }
831 }
832
833 function editFeedDlg(feed) {
834         try {
835
836                 if (!feed) {
837                         alert(__("Please select some feed first."));
838                         return;
839                 }
840         
841                 if ((feed <= 0) || activeFeedIsCat() || tagsAreDisplayed()) {
842                         alert(__("You can't edit this kind of feed."));
843                         return;
844                 }
845         
846                 var query = "";
847         
848                 if (feed > 0) {
849                         query = "backend.php?op=pref-feeds&subop=editfeed&id=" +        param_escape(feed);
850                 } else {
851                         query = "backend.php?op=pref-labels&subop=edit&id=" +   param_escape(-feed-11);
852                 }
853
854                 disableHotkeys();
855
856                 notify_progress("Loading, please wait...", true);
857
858                 new Ajax.Request(query, {
859                         onComplete: function(transport) { 
860                                 infobox_callback2(transport); 
861                         } });
862
863         } catch (e) {
864                 exception_error("editFeedDlg", e);
865         }
866 }
867
868 /* this functions duplicate those of prefs.js feed editor, with
869         some differences because there is no feedlist */
870
871 function feedEditCancel() {
872         closeInfoBox();
873         return false;
874 }
875
876 function feedEditSave() {
877
878         try {
879         
880                 // FIXME: add parameter validation
881
882                 var query = Form.serialize("edit_feed_form");
883
884                 notify_progress("Saving feed...");
885
886                 new Ajax.Request("backend.php", {
887                         parameters: query,
888                         onComplete: function(transport) { 
889                                 dlg_frefresh_callback(transport); 
890                         } });
891
892
893                 closeInfoBox();
894
895                 return false;
896
897         } catch (e) {
898                 exception_error("feedEditSave (main)", e);
899         } 
900 }
901
902 function clearFeedArticles(feed_id) {
903
904         notify_progress("Clearing feed...");
905
906         var query = "backend.php?op=pref-feeds&quiet=1&subop=clear&id=" + feed_id;
907
908         new Ajax.Request(query, {
909                 onComplete: function(transport) {
910                                 dlg_frefresh_callback(transport, feed_id);
911                         } });
912
913         return false;
914 }
915
916 function collapse_feedlist() {
917         try {
918                 debug("toggle_feedlist");
919                 
920                 var theme = getInitParam("theme");
921                 if (theme != "" && theme != "compact" && theme != "graycube" &&
922                                 theme != "compat") return;
923
924                 var fl = $("feeds-holder");
925                 var fh = $("headlines-frame");
926                 var fc = $("content-frame");
927                 var ft = $("toolbar");
928                 var ff = $("footer");
929                 var fhdr = $("header");
930                 var fbtn = $("collapse_feeds_btn");
931
932                 if (!Element.visible(fl)) {
933                         Element.show(fl);
934                         fbtn.value = "<<";
935
936                         if (theme != "graycube") {
937
938                                 fh.style.left = fl.offsetWidth + "px";
939                                 ft.style.left = fl.offsetWidth + "px";
940                                 if (fc) fc.style.left = fl.offsetWidth + "px";
941                                 if (ff && theme != "compat") ff.style.left = (fl.offsetWidth-1) + "px";
942
943                                 if (theme == "compact") fhdr.style.left = (fl.offsetWidth + 10) + "px";
944                         } else {
945                                 fh.style.left = fl.offsetWidth + 40 + "px";
946                                 ft.style.left = fl.offsetWidth + 40 +"px";
947                                 if (fc) fc.style.left = fl.offsetWidth + 40 + "px";
948                         }
949
950                         setCookie("ttrss_vf_fclps", "0");
951
952                 } else {
953                         Element.hide(fl);
954                         fbtn.value = ">>";
955
956                         if (theme != "graycube") {
957
958                                 fh.style.left = "0px";
959                                 ft.style.left = "0px";
960                                 if (fc) fc.style.left = "0px";
961                                 if (ff) ff.style.left = "0px";
962
963                                 if (theme == "compact") fhdr.style.left = "10px";
964
965                         } else {
966                                 fh.style.left = "20px";
967                                 ft.style.left = "20px";
968                                 if (fc) fc.style.left = "20px";
969
970                         }
971
972                         setCookie("ttrss_vf_fclps", "1");
973                 }
974         } catch (e) {
975                 exception_error("toggle_feedlist", e);
976         }
977 }
978
979 function viewModeChanged() {
980         cache_empty();
981         return viewCurrentFeed(0, '')
982 }
983
984 function viewLimitChanged() {
985         cache_empty();
986         return viewCurrentFeed(0, '')
987 }
988
989 /* function adjustArticleScore(id, score) {
990         try {
991
992                 var pr = prompt(__("Assign score to article:"), score);
993
994                 if (pr != undefined) {
995                         var query = "backend.php?op=rpc&subop=setScore&id=" + id + "&score=" + pr;
996
997                         new Ajax.Request(query, {
998                         onComplete: function(transport) {
999                                         viewCurrentFeed();
1000                                 } });
1001
1002                 }
1003         } catch (e) {
1004                 exception_error("adjustArticleScore", e);
1005         }
1006 } */
1007
1008 function rescoreCurrentFeed() {
1009
1010         var actid = getActiveFeedId();
1011
1012         if (activeFeedIsCat() || actid < 0 || tagsAreDisplayed()) {
1013                 alert(__("You can't rescore this kind of feed."));
1014                 return;
1015         }       
1016
1017         if (!actid) {
1018                 alert(__("Please select some feed first."));
1019                 return;
1020         }
1021
1022         var fn = getFeedName(actid);
1023         var pr = __("Rescore articles in %s?").replace("%s", fn);
1024
1025         if (confirm(pr)) {
1026                 notify_progress("Rescoring articles...");
1027
1028                 var query = "backend.php?op=pref-feeds&subop=rescore&quiet=1&ids=" + actid;
1029
1030                 new Ajax.Request(query, {
1031                 onComplete: function(transport) {
1032                         viewCurrentFeed();
1033                 } });
1034         }
1035 }
1036
1037 function hotkey_handler(e) {
1038
1039         try {
1040
1041                 var keycode;
1042                 var shift_key = false;
1043
1044                 var cmdline = $('cmdline');
1045                 var feedlist = $('feedList');
1046
1047                 try {
1048                         shift_key = e.shiftKey;
1049                 } catch (e) {
1050
1051                 }
1052         
1053                 if (window.event) {
1054                         keycode = window.event.keyCode;
1055                 } else if (e) {
1056                         keycode = e.which;
1057                 }
1058
1059                 var keychar = String.fromCharCode(keycode);
1060
1061                 if (keycode == 27) { // escape
1062                         if (Element.visible("hotkey_help_overlay")) {
1063                                 Element.hide("hotkey_help_overlay");
1064                         }
1065                         hotkey_prefix = false;
1066                         closeInfoBox();
1067                 } 
1068
1069                 if (!hotkeys_enabled) {
1070                         debug("hotkeys disabled");
1071                         return;
1072                 }
1073
1074                 if (keycode == 16) return; // ignore lone shift
1075
1076                 if ((keycode == 70 || keycode == 67 || keycode == 71) 
1077                                 && !hotkey_prefix) {
1078
1079                         var date = new Date();
1080                         var ts = Math.round(date.getTime() / 1000);
1081
1082                         hotkey_prefix = keycode;
1083                         hotkey_prefix_pressed = ts;
1084
1085                         cmdline.innerHTML = keychar;
1086                         Element.show(cmdline);
1087
1088                         debug("KP: PREFIX=" + keycode + " CHAR=" + keychar + " TS=" + ts);
1089                         return true;
1090                 }
1091
1092                 if (Element.visible("hotkey_help_overlay")) {
1093                         Element.hide("hotkey_help_overlay");
1094                 }
1095
1096                 /* Global hotkeys */
1097
1098                 Element.hide(cmdline);
1099
1100                 if (!hotkey_prefix) {
1101
1102                         if (keycode == 68 && shift_key) { // d
1103                                 if (!Element.visible("debug_output")) {
1104                                         Element.show("debug_output");
1105                                         debug('debug mode activated');
1106                                 } else {
1107                                         Element.hide("debug_output");
1108                                 }
1109         
1110                                 return;
1111                         }
1112         
1113                         if ((keycode == 191 || keychar == '?') && shift_key) { // ?
1114                                 if (!Element.visible("hotkey_help_overlay")) {
1115                                         //Element.show("hotkey_help_overlay");
1116                                         Effect.Appear("hotkey_help_overlay", {duration : 0.3});
1117                                 } else {
1118                                         Element.hide("hotkey_help_overlay");
1119                                 }
1120                                 return false;
1121                         }
1122
1123                         if (keycode == 191 || keychar == '/') { // /
1124                                 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
1125                                 return false;
1126                         }
1127
1128                         if (keycode == 82 && shift_key) { // R
1129                                 scheduleFeedUpdate(true);
1130                                 return;
1131                         }
1132
1133                         if (keycode == 74) { // j
1134                                 var feed = getActiveFeedId();
1135                                 var new_feed = getRelativeFeedId2(feed, activeFeedIsCat(), 'prev');
1136 //                              alert(feed + " IC: " + activeFeedIsCat() + " => " + new_feed);
1137                                 if (new_feed) {
1138                                         var is_cat = new_feed.match("CAT:");
1139                                         if (is_cat) {
1140                                                 new_feed = new_feed.replace("CAT:", "");
1141                                                 viewCategory(new_feed);
1142                                         } else {
1143                                                 viewfeed(new_feed, '', false);
1144                                         }
1145                                 }
1146                                 return;
1147                         }
1148         
1149                         if (keycode == 75) { // k
1150                                 var feed = getActiveFeedId();
1151                                 var new_feed = getRelativeFeedId2(feed, activeFeedIsCat(), 'next');
1152 //                              alert(feed + " IC: " + activeFeedIsCat() + " => " + new_feed);
1153                                 if (new_feed) {
1154                                         var is_cat = new_feed.match("CAT:");
1155                                         if (is_cat == "CAT:") {
1156                                                 new_feed = new_feed.replace("CAT:", "");
1157                                                 viewCategory(new_feed);
1158                                         } else {
1159                                                 viewfeed(new_feed, '', false);
1160                                         }
1161                                 }
1162                                 return;
1163                         }
1164
1165                         if (shift_key && keycode == 40) { // shift-down
1166                                 catchupRelativeToArticle(1);
1167                                 return;
1168                         }
1169
1170                         if (shift_key && keycode == 38) { // shift-up
1171                                 catchupRelativeToArticle(0);
1172                                 return;
1173                         }
1174
1175                         if (shift_key && keycode == 78) { // N
1176                                 scrollArticle(50);      
1177                                 return;
1178                         }
1179
1180                         if (shift_key && keycode == 80) { // P
1181                                 scrollArticle(-50);     
1182                                 return;
1183                         }
1184
1185
1186                         if (keycode == 78 || keycode == 40) { // n, down
1187                                 if (typeof moveToPost != 'undefined') {
1188                                         moveToPost('next');
1189                                         return;
1190                                 }
1191                         }
1192         
1193                         if (keycode == 80 || keycode == 38) { // p, up
1194                                 if (typeof moveToPost != 'undefined') {
1195                                         moveToPost('prev');
1196                                         return;
1197                                 }
1198                         }
1199
1200                         if (keycode == 83 && shift_key) { // S
1201                                 var id = getActiveArticleId();
1202                                 if (id) {                               
1203                                         togglePub(id);
1204                                 }
1205                                 return;
1206                         }
1207
1208                         if (keycode == 83) { // s
1209                                 var id = getActiveArticleId();
1210                                 if (id) {                               
1211                                         toggleMark(id);
1212                                 }
1213                                 return;
1214                         }
1215
1216
1217                         if (keycode == 85) { // u
1218                                 var id = getActiveArticleId();
1219                                 if (id) {                               
1220                                         toggleUnread(id);
1221                                 }
1222                                 return;
1223                         }
1224
1225                         if (keycode == 84 && shift_key) { // T
1226                                 var id = getActiveArticleId();
1227                                 if (id) {
1228                                         editArticleTags(id, getActiveFeedId(), isCdmMode());
1229                                         return;
1230                                 }
1231                         }
1232
1233                         if (keycode == 9) { // tab
1234                                 var id = getArticleUnderPointer();
1235                                 if (id) {                               
1236                                         var cb = $("RCHK-" + id);
1237
1238                                         if (cb) {
1239                                                 cb.checked = !cb.checked;
1240                                                 toggleSelectRowById(cb, "RROW-" + id);
1241                                                 return false;
1242                                         }
1243                                 }
1244                         }
1245
1246                         if (keycode == 79) { // o
1247                                 if (getActiveArticleId()) {
1248                                         openArticleInNewWindow(getActiveArticleId());
1249                                         return;
1250                                 }
1251                         }
1252
1253                         if (keycode == 81 && shift_key) { // Q
1254                                 if (typeof catchupAllFeeds != 'undefined') {
1255                                         catchupAllFeeds();
1256                                         return;
1257                                 }
1258                         }
1259
1260                         if (keycode == 88) { // x
1261                                 if (activeFeedIsCat()) {
1262                                         toggleCollapseCat(getActiveFeedId());
1263                                 }
1264                         }
1265                 }
1266
1267                 /* Prefix f */
1268
1269                 if (hotkey_prefix == 70) { // f 
1270
1271                         hotkey_prefix = false;
1272
1273                         if (keycode == 81) { // q
1274                                 if (getActiveFeedId()) {
1275                                         catchupCurrentFeed();
1276                                         return;
1277                                 }
1278                         }
1279
1280                         if (keycode == 82) { // r
1281                                 if (getActiveFeedId()) {
1282                                         viewfeed(getActiveFeedId(), "ForceUpdate", activeFeedIsCat());
1283                                         return;
1284                                 }
1285                         }
1286
1287                         if (keycode == 65) { // a
1288                                 toggleDispRead();
1289                                 return false;
1290                         }
1291
1292                         if (keycode == 85 && shift_key) { // U
1293                                 scheduleFeedUpdate(true);
1294                                 return false;
1295                         }
1296
1297                         if (keycode == 85) { // u
1298                                 if (getActiveFeedId()) {
1299                                         viewfeed(getActiveFeedId(), "ForceUpdate");
1300                                         return false;
1301                                 }
1302                         }
1303
1304                         if (keycode == 69) { // e
1305                                 editFeedDlg(getActiveFeedId());
1306                                 return false;
1307                         }
1308
1309                         if (keycode == 83) { // s
1310                                 displayDlg("quickAddFeed");
1311                                 return false;
1312                         }
1313
1314                         if (keycode == 67 && shift_key) { // C
1315                                 if (typeof catchupAllFeeds != 'undefined') {
1316                                         catchupAllFeeds();
1317                                         return false;
1318                                 }
1319                         }
1320
1321                         if (keycode == 67) { // c
1322                                 if (getActiveFeedId()) {
1323                                         catchupCurrentFeed();
1324                                         return false;
1325                                 }
1326                         }
1327
1328                         if (keycode == 87) { // w
1329                                 feeds_sort_by_unread = !feeds_sort_by_unread;
1330                                 return resort_feedlist();
1331                         }
1332
1333                         if (keycode == 72) { // h
1334                                 hideReadHeadlines();
1335                                 return;
1336                         }
1337
1338                 }
1339
1340                 /* Prefix c */
1341
1342                 if (hotkey_prefix == 67) { // c
1343                         hotkey_prefix = false;
1344
1345                         if (keycode == 70) { // f
1346                                 displayDlg("quickAddFilter", getActiveFeedId());
1347                                 return false;
1348                         }
1349
1350                         if (keycode == 76) { // l
1351                                 addLabel();
1352                                 return false;
1353                         }
1354
1355                         if (keycode == 83) { // s
1356                                 if (typeof collapse_feedlist != 'undefined') {
1357                                         collapse_feedlist();
1358                                         return false;
1359                                 }
1360                         }
1361
1362                         if (keycode == 77) { // m
1363                                 feedlist_sortable_enabled = !feedlist_sortable_enabled;
1364                                 if (feedlist_sortable_enabled) {
1365                                         notify_info("Category reordering enabled");
1366                                         toggle_sortable_feedlist(true);
1367                                 } else {
1368                                         notify_info("Category reordering disabled");
1369                                         toggle_sortable_feedlist(false);
1370                                 }
1371                         }
1372
1373                         if (keycode == 78) { // n
1374                                 catchupRelativeToArticle(1);
1375                                 return;
1376                         }
1377
1378                         if (keycode == 80) { // p
1379                                 catchupRelativeToArticle(0);
1380                                 return;
1381                         }
1382
1383
1384                 }
1385
1386                 /* Prefix g */
1387
1388                 if (hotkey_prefix == 71) { // g
1389
1390                         hotkey_prefix = false;
1391
1392
1393                         if (keycode == 65) { // a
1394                                 viewfeed(-4);
1395                                 return false;
1396                         }
1397
1398                         if (keycode == 83) { // s
1399                                 viewfeed(-1);
1400                                 return false;
1401                         }
1402
1403                         if (keycode == 80 && shift_key) { // P
1404                                 gotoPreferences();
1405                                 return false;
1406                         }
1407
1408                         if (keycode == 80) { // p
1409                                 viewfeed(-2);
1410                                 return false;
1411                         }
1412
1413                         if (keycode == 70) { // f
1414                                 viewfeed(-3);
1415                                 return false;
1416                         }
1417
1418                         if (keycode == 84 && shift_key) { // T
1419                                 toggleTags();
1420                                 return false;
1421                         }
1422                 }
1423
1424                 /* Cmd */
1425
1426                 if (hotkey_prefix == 224 || hotkey_prefix == 91) { // f 
1427                         hotkey_prefix = false;
1428                         return;
1429                 }
1430
1431                 if (hotkey_prefix) {
1432                         debug("KP: PREFIX=" + hotkey_prefix + " CODE=" + keycode + " CHAR=" + keychar);
1433                 } else {
1434                         debug("KP: CODE=" + keycode + " CHAR=" + keychar);
1435                 }
1436
1437
1438         } catch (e) {
1439                 exception_error("hotkey_handler", e);
1440         }
1441 }
1442
1443 function feedsSortByUnread() {
1444         return feeds_sort_by_unread;
1445 }
1446
1447 function addLabel() {
1448
1449         try {
1450
1451                 var caption = prompt(__("Please enter label caption:"), "");
1452
1453                 if (caption != undefined) {
1454         
1455                         if (caption == "") {
1456                                 alert(__("Can't create label: missing caption."));
1457                                 return false;
1458                         }
1459
1460                         var query = "backend.php?op=pref-labels&subop=add&caption=" + 
1461                                 param_escape(caption);
1462
1463                         notify_progress("Loading, please wait...", true);
1464
1465                         new Ajax.Request(query, {
1466                                 onComplete: function(transport) { 
1467                                         updateFeedList();
1468                         } });
1469
1470                 }
1471
1472         } catch (e) {
1473                 exception_error("addLabel", e);
1474         }
1475 }
1476
1477 function visitOfficialSite() {
1478         window.open("http://tt-rss.org/");
1479 }
1480
1481
1482 function feedBrowserSubscribe() {
1483         try {
1484
1485                 var selected = getSelectedFeedsFromBrowser();
1486
1487                 if (selected.length > 0) {
1488                         closeInfoBox();
1489
1490                         notify_progress("Loading, please wait...", true);
1491
1492                         var query =  "backend.php?op=pref-feeds&subop=massSubscribe&ids="+
1493                                 param_escape(selected.toString());
1494
1495                         new Ajax.Request(query, {
1496                                 onComplete: function(transport) { 
1497                                         updateFeedList();
1498                                 } });
1499
1500                 } else {
1501                         alert(__("No feeds are selected."));
1502                 }
1503
1504         } catch (e) {
1505                 exception_error("feedBrowserSubscribe", e);
1506         }
1507 }
1508
1509