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