]> git.wh0rd.org Git - tt-rss.git/blob - js/tt-rss.js
b2b6da3c0d4cfb3e8b906bcd17fdb034bb88057c
[tt-rss.git] / js / tt-rss.js
1 let global_unread = -1;
2 let hotkey_prefix = false;
3 let hotkey_prefix_pressed = false;
4 let hotkey_actions = {};
5 let _widescreen_mode = false;
6 let _rpc_seq = 0;
7 let _active_feed_id = 0;
8 let _active_feed_is_cat = false;
9
10 function next_seq() {
11         _rpc_seq += 1;
12         return _rpc_seq;
13 }
14
15 function get_seq() {
16         return _rpc_seq;
17 }
18
19 function activeFeedIsCat() {
20         return _active_feed_is_cat;
21 }
22
23 function getActiveFeedId() {
24         return _active_feed_id;
25 }
26
27 function setActiveFeedId(id, is_cat) {
28         hash_set('f', id);
29         hash_set('c', is_cat ? 1 : 0);
30
31         _active_feed_id = id;
32         _active_feed_is_cat = is_cat;
33
34         $("headlines-frame").setAttribute("feed-id", id);
35         $("headlines-frame").setAttribute("is-cat", is_cat ? 1 : 0);
36
37         selectFeed(id, is_cat);
38
39         PluginHost.run(PluginHost.HOOK_FEED_SET_ACTIVE, _active_article_id);
40 }
41
42
43 function updateFeedList() {
44         try {
45                 Element.show("feedlistLoading");
46
47                 resetCounterCache();
48
49                 if (dijit.byId("feedTree")) {
50                         dijit.byId("feedTree").destroyRecursive();
51                 }
52
53                 const store = new dojo.data.ItemFileWriteStore({
54                         url: "backend.php?op=pref_feeds&method=getfeedtree&mode=2"
55                 });
56
57                 const treeModel = new fox.FeedStoreModel({
58                         store: store,
59                         query: {
60                                 "type": getInitParam('enable_feed_cats') == 1 ? "category" : "feed"
61                         },
62                         rootId: "root",
63                         rootLabel: "Feeds",
64                         childrenAttrs: ["items"]
65                 });
66
67                 const tree = new fox.FeedTree({
68                         model: treeModel,
69                         onClick: function (item, node) {
70                                 const id = String(item.id);
71                                 const is_cat = id.match("^CAT:");
72                                 const feed = id.substr(id.indexOf(":") + 1);
73                                 viewfeed({feed: feed, is_cat: is_cat});
74                                 return false;
75                         },
76                         openOnClick: false,
77                         showRoot: false,
78                         persist: true,
79                         id: "feedTree",
80                 }, "feedTree");
81
82                 /*              var menu = new dijit.Menu({id: 'feedMenu'});
83
84                  menu.addChild(new dijit.MenuItem({
85                  label: "Simple menu item"
86                  }));
87
88                  //             menu.bindDomNode(tree.domNode); */
89
90                 var tmph = dojo.connect(dijit.byId('feedMenu'), '_openMyself', function (event) {
91                         console.log(dijit.getEnclosingWidget(event.target));
92                         dojo.disconnect(tmph);
93                 });
94
95                 $("feeds-holder").appendChild(tree.domNode);
96
97                 var tmph = dojo.connect(tree, 'onLoad', function () {
98                         dojo.disconnect(tmph);
99                         Element.hide("feedlistLoading");
100
101                         try {
102                                 feedlist_init();
103
104                                 loading_set_progress(25);
105                         } catch (e) {
106                                 exception_error(e);
107                         }
108                 });
109
110                 tree.startup();
111         } catch (e) {
112                 exception_error(e);
113         }
114 }
115
116 function catchupAllFeeds() {
117
118         const str = __("Mark all articles as read?");
119
120         if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
121
122                 const query_str = "backend.php?op=feeds&method=catchupAll";
123
124                 notify_progress("Marking all feeds as read...");
125
126                 //console.log("catchupAllFeeds Q=" + query_str);
127
128                 new Ajax.Request("backend.php", {
129                         parameters: query_str,
130                         onComplete: function(transport) {
131                                 request_counters(true);
132                                 viewCurrentFeed();
133                         } });
134
135                 global_unread = 0;
136                 updateTitle("");
137         }
138 }
139
140 function viewCurrentFeed(method) {
141         console.log("viewCurrentFeed: " + method);
142
143         if (getActiveFeedId() != undefined) {
144                 viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat(), method: method});
145         }
146         return false; // block unneeded form submits
147 }
148
149 function timeout() {
150         if (getInitParam("bw_limit") != "1") {
151                 request_counters(true);
152                 setTimeout(timeout, 60*1000);
153         }
154 }
155
156 function search() {
157         const query = "backend.php?op=feeds&method=search&param=" +
158                 param_escape(getActiveFeedId() + ":" + activeFeedIsCat());
159
160         if (dijit.byId("searchDlg"))
161                 dijit.byId("searchDlg").destroyRecursive();
162
163         const dialog = new dijit.Dialog({
164                 id: "searchDlg",
165                 title: __("Search"),
166                 style: "width: 600px",
167                 execute: function() {
168                         if (this.validate()) {
169                                 _search_query = dojo.objectToQuery(this.attr('value'));
170                                 this.hide();
171                                 viewCurrentFeed();
172                         }
173                 },
174                 href: query});
175
176         dialog.show();
177 }
178
179 function updateTitle() {
180         let tmp = "Tiny Tiny RSS";
181
182         if (global_unread > 0) {
183                 tmp = "(" + global_unread + ") " + tmp;
184         }
185
186         document.title = tmp;
187 }
188
189 function genericSanityCheck() {
190         setCookie("ttrss_test", "TEST");
191
192         if (getCookie("ttrss_test") != "TEST") {
193                 return fatalError(2);
194         }
195
196         return true;
197 }
198
199
200 function init() {
201
202         window.onerror = function(message, filename, lineno, colno, error) {
203                 report_error(message, filename, lineno, colno, error);
204         };
205
206         require(["dojo/_base/kernel",
207                         "dojo/ready",
208                         "dojo/parser",
209                         "dojo/_base/loader",
210                         "dojo/_base/html",
211                         "dojo/query",
212                         "dijit/ProgressBar",
213                         "dijit/ColorPalette",
214                         "dijit/Dialog",
215                         "dijit/form/Button",
216                         "dijit/form/ComboButton",
217                         "dijit/form/CheckBox",
218                         "dijit/form/DropDownButton",
219                         "dijit/form/FilteringSelect",
220                         "dijit/form/Form",
221                         "dijit/form/RadioButton",
222                         "dijit/form/Select",
223                 "dijit/form/MultiSelect",
224                         "dijit/form/SimpleTextarea",
225                         "dijit/form/TextBox",
226                         "dijit/form/ComboBox",
227                         "dijit/form/ValidationTextBox",
228                         "dijit/InlineEditBox",
229                         "dijit/layout/AccordionContainer",
230                         "dijit/layout/BorderContainer",
231                         "dijit/layout/ContentPane",
232                         "dijit/layout/TabContainer",
233                         "dijit/PopupMenuItem",
234                         "dijit/Menu",
235                         "dijit/Toolbar",
236                         "dijit/Tree",
237                         "dijit/tree/dndSource",
238                         "dijit/tree/ForestStoreModel",
239                         "dojo/data/ItemFileWriteStore",
240                         "fox/FeedStoreModel",
241                         "fox/FeedTree" ], function (dojo, ready, parser) {
242
243                         ready(function() {
244
245                                 try {
246                                         parser.parse();
247
248                                         if (!genericSanityCheck())
249                                                 return false;
250
251                                         loading_set_progress(30);
252
253                                         const a = document.createElement('audio');
254
255                                         const hasAudio = !!a.canPlayType;
256                                         const hasSandbox = "sandbox" in document.createElement("iframe");
257                                         const hasMp3 = !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));
258                                         const clientTzOffset = new Date().getTimezoneOffset() * 60;
259
260                                         init_hotkey_actions();
261
262                                         new Ajax.Request("backend.php", {
263                                                 parameters: {
264                                                         op: "rpc", method: "sanityCheck", hasAudio: hasAudio,
265                                                         hasMp3: hasMp3,
266                                                         clientTzOffset: clientTzOffset,
267                                                         hasSandbox: hasSandbox
268                                                 },
269                                                 onComplete: function (transport) {
270                                                         backend_sanity_check_callback(transport);
271                                                 }
272                                         });
273                                 } catch (e) {
274                                         exception_error(e);
275                                 }
276
277                         });
278
279
280         });
281 }
282
283 function init_hotkey_actions() {
284         hotkey_actions["next_feed"] = function() {
285                 const rv = dijit.byId("feedTree").getNextFeed(
286                         getActiveFeedId(), activeFeedIsCat());
287
288                 if (rv) viewfeed({feed: rv[0], is_cat: rv[1], can_wait: true})
289         };
290         hotkey_actions["prev_feed"] = function() {
291                 const rv = dijit.byId("feedTree").getPreviousFeed(
292                         getActiveFeedId(), activeFeedIsCat());
293
294                 if (rv) viewfeed({feed: rv[0], is_cat: rv[1], can_wait: true})
295         };
296         hotkey_actions["next_article"] = function() {
297                 moveToPost('next');
298         };
299         hotkey_actions["prev_article"] = function() {
300                 moveToPost('prev');
301         };
302         hotkey_actions["next_article_noscroll"] = function() {
303                 moveToPost('next', true);
304         };
305         hotkey_actions["prev_article_noscroll"] = function() {
306                 moveToPost('prev', true);
307         };
308         hotkey_actions["next_article_noexpand"] = function() {
309                 moveToPost('next', true, true);
310         };
311         hotkey_actions["prev_article_noexpand"] = function() {
312                 moveToPost('prev', true, true);
313         };
314         hotkey_actions["collapse_article"] = function() {
315                 const id = getActiveArticleId();
316                 const elem = $("CICD-"+id);
317
318                 if (elem) {
319                         if (elem.visible()) {
320                                 cdmCollapseArticle(null, id);
321                         }
322                         else {
323                                 cdmExpandArticle(id);
324                         }
325                 }
326         };
327         hotkey_actions["toggle_expand"] = function() {
328                 const id = getActiveArticleId();
329                 const elem = $("CICD-"+id);
330
331                 if (elem) {
332                         if (elem.visible()) {
333                                 cdmCollapseArticle(null, id, false);
334                         }
335                         else {
336                                 cdmExpandArticle(id);
337                         }
338                 }
339         };
340         hotkey_actions["search_dialog"] = function() {
341                 search();
342         };
343         hotkey_actions["toggle_mark"] = function() {
344                 selectionToggleMarked(undefined, false, true);
345         };
346         hotkey_actions["toggle_publ"] = function() {
347                 selectionTogglePublished(undefined, false, true);
348         };
349         hotkey_actions["toggle_unread"] = function() {
350                 selectionToggleUnread(undefined, false, true);
351         };
352         hotkey_actions["edit_tags"] = function() {
353                 const id = getActiveArticleId();
354                 if (id) {
355                         editArticleTags(id);
356                 }
357         }
358         hotkey_actions["open_in_new_window"] = function() {
359                 if (getActiveArticleId()) {
360                         openArticleInNewWindow(getActiveArticleId());
361                 }
362         };
363         hotkey_actions["catchup_below"] = function() {
364                 catchupRelativeToArticle(1);
365         };
366         hotkey_actions["catchup_above"] = function() {
367                 catchupRelativeToArticle(0);
368         };
369         hotkey_actions["article_scroll_down"] = function() {
370                 scrollArticle(40);
371         };
372         hotkey_actions["article_scroll_up"] = function() {
373                 scrollArticle(-40);
374         };
375         hotkey_actions["close_article"] = function() {
376                 if (isCdmMode()) {
377                         if (!getInitParam("cdm_expanded")) {
378                                 cdmCollapseArticle(false, getActiveArticleId());
379                         }
380                 } else {
381                         closeArticlePanel();
382                 }
383         };
384         hotkey_actions["email_article"] = function() {
385                 if (typeof emailArticle != "undefined") {
386                         emailArticle();
387                 } else if (typeof mailtoArticle != "undefined") {
388                         mailtoArticle();
389                 } else {
390                         alert(__("Please enable mail plugin first."));
391                 }
392         };
393         hotkey_actions["select_all"] = function() {
394                 selectArticles('all');
395         };
396         hotkey_actions["select_unread"] = function() {
397                 selectArticles('unread');
398         };
399         hotkey_actions["select_marked"] = function() {
400                 selectArticles('marked');
401         };
402         hotkey_actions["select_published"] = function() {
403                 selectArticles('published');
404         };
405         hotkey_actions["select_invert"] = function() {
406                 selectArticles('invert');
407         };
408         hotkey_actions["select_none"] = function() {
409                 selectArticles('none');
410         };
411         hotkey_actions["feed_refresh"] = function() {
412                 if (getActiveFeedId() != undefined) {
413                         viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat()});
414                         return;
415                 }
416         };
417         hotkey_actions["feed_unhide_read"] = function() {
418                 toggleDispRead();
419         };
420         hotkey_actions["feed_subscribe"] = function() {
421                 quickAddFeed();
422         };
423         hotkey_actions["feed_debug_update"] = function() {
424                 if (!activeFeedIsCat() && parseInt(getActiveFeedId()) > 0) {
425                         window.open("backend.php?op=feeds&method=update_debugger&feed_id=" + getActiveFeedId() +
426                                 "&csrf_token=" + getInitParam("csrf_token"));
427                 } else {
428                         alert("You can't debug this kind of feed.");
429                 }
430         };
431
432         hotkey_actions["feed_debug_viewfeed"] = function() {
433                 viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat(), viewfeed_debug: true});
434         };
435
436         hotkey_actions["feed_edit"] = function() {
437                 if (activeFeedIsCat())
438                         alert(__("You can't edit this kind of feed."));
439                 else
440                         editFeed(getActiveFeedId());
441         };
442         hotkey_actions["feed_catchup"] = function() {
443                 if (getActiveFeedId() != undefined) {
444                         catchupCurrentFeed();
445                         return;
446                 }
447         };
448         hotkey_actions["feed_reverse"] = function() {
449                 reverseHeadlineOrder();
450         };
451         hotkey_actions["feed_toggle_vgroup"] = function() {
452                 const query_str = "?op=rpc&method=togglepref&key=VFEED_GROUP_BY_FEED";
453
454                 new Ajax.Request("backend.php", {
455                         parameters: query_str,
456                         onComplete: function(transport) {
457                                 viewCurrentFeed();
458                         } });
459
460         };
461         hotkey_actions["catchup_all"] = function() {
462                 catchupAllFeeds();
463         };
464         hotkey_actions["cat_toggle_collapse"] = function() {
465                 if (activeFeedIsCat()) {
466                         dijit.byId("feedTree").collapseCat(getActiveFeedId());
467                         return;
468                 }
469         };
470         hotkey_actions["goto_all"] = function() {
471                 viewfeed({feed: -4});
472         };
473         hotkey_actions["goto_fresh"] = function() {
474                 viewfeed({feed: -3});
475         };
476         hotkey_actions["goto_marked"] = function() {
477                 viewfeed({feed: -1});
478         };
479         hotkey_actions["goto_published"] = function() {
480                 viewfeed({feed: -2});
481         };
482         hotkey_actions["goto_tagcloud"] = function() {
483                 displayDlg(__("Tag cloud"), "printTagCloud");
484         };
485         hotkey_actions["goto_prefs"] = function() {
486                 gotoPreferences();
487         };
488         hotkey_actions["select_article_cursor"] = function() {
489                 const id = getArticleUnderPointer();
490                 if (id) {
491                         const row = $("RROW-" + id);
492
493                         if (row) {
494                                 const cb = dijit.getEnclosingWidget(
495                                         row.getElementsByClassName("rchk")[0]);
496
497                                 if (cb) {
498                                         cb.attr("checked", !cb.attr("checked"));
499                                         toggleSelectRowById(cb, "RROW-" + id);
500                                         return false;
501                                 }
502                         }
503                 }
504         };
505         hotkey_actions["create_label"] = function() {
506                 addLabel();
507         };
508         hotkey_actions["create_filter"] = function() {
509                 quickAddFilter();
510         };
511         hotkey_actions["collapse_sidebar"] = function() {
512                 collapse_feedlist();
513         };
514         hotkey_actions["toggle_embed_original"] = function() {
515                 if (typeof embedOriginalArticle != "undefined") {
516                         if (getActiveArticleId())
517                                 embedOriginalArticle(getActiveArticleId());
518                 } else {
519                         alert(__("Please enable embed_original plugin first."));
520                 }
521         };
522         hotkey_actions["toggle_widescreen"] = function() {
523                 if (!isCdmMode()) {
524                         _widescreen_mode = !_widescreen_mode;
525
526                         // reset stored sizes because geometry changed
527                         setCookie("ttrss_ci_width", 0);
528                         setCookie("ttrss_ci_height", 0);
529
530                         switchPanelMode(_widescreen_mode);
531                 } else {
532                         alert(__("Widescreen is not available in combined mode."));
533                 }
534         };
535         hotkey_actions["help_dialog"] = function() {
536                 helpDialog("main");
537         };
538         hotkey_actions["toggle_combined_mode"] = function() {
539                 notify_progress("Loading, please wait...");
540
541                 const value = isCdmMode() ? "false" : "true";
542                 const query = "?op=rpc&method=setpref&key=COMBINED_DISPLAY_MODE&value=" + value;
543
544                 new Ajax.Request("backend.php", {
545                         parameters: query,
546                         onComplete: function(transport) {
547                                 setInitParam("combined_display_mode",
548                                         !getInitParam("combined_display_mode"));
549
550                                 closeArticlePanel();
551                                 viewCurrentFeed();
552
553                         } });
554         };
555         hotkey_actions["toggle_cdm_expanded"] = function() {
556                 notify_progress("Loading, please wait...");
557
558                 const value = getInitParam("cdm_expanded") ? "false" : "true";
559                 const query = "?op=rpc&method=setpref&key=CDM_EXPANDED&value=" + value;
560
561                 new Ajax.Request("backend.php", {
562                         parameters: query,
563                         onComplete: function(transport) {
564                                 setInitParam("cdm_expanded", !getInitParam("cdm_expanded"));
565                                 viewCurrentFeed();
566                         } });
567         };
568 }
569
570 function init_second_stage() {
571         updateFeedList();
572         closeArticlePanel();
573
574         if (parseInt(getCookie("ttrss_fh_width")) > 0) {
575                 dijit.byId("feeds-holder").domNode.setStyle(
576                         {width: getCookie("ttrss_fh_width") + "px" });
577         }
578
579         dijit.byId("main").resize();
580
581         var tmph = dojo.connect(dijit.byId('feeds-holder'), 'resize',
582                 function (args) {
583                         if (args && args.w >= 0) {
584                                 setCookie("ttrss_fh_width", args.w, getInitParam("cookie_lifetime"));
585                         }
586         });
587
588         var tmph = dojo.connect(dijit.byId('content-insert'), 'resize',
589                 function (args) {
590                         if (args && args.w >= 0 && args.h >= 0) {
591                                 setCookie("ttrss_ci_width", args.w, getInitParam("cookie_lifetime"));
592                                 setCookie("ttrss_ci_height", args.h, getInitParam("cookie_lifetime"));
593                         }
594         });
595
596         delCookie("ttrss_test");
597
598         const toolbar = document.forms["main_toolbar_form"];
599
600         dijit.getEnclosingWidget(toolbar.view_mode).attr('value',
601                 getInitParam("default_view_mode"));
602
603         dijit.getEnclosingWidget(toolbar.order_by).attr('value',
604                 getInitParam("default_view_order_by"));
605
606         const feeds_sort_by_unread = getInitParam("feeds_sort_by_unread") == 1;
607
608         const hash_feed_id = hash_get('f');
609         const hash_feed_is_cat = hash_get('c') == "1";
610
611         if (hash_feed_id != undefined) {
612                 setActiveFeedId(hash_feed_id, hash_feed_is_cat);
613         }
614
615         loading_set_progress(50);
616
617         // can't use cache_clear() here because viewfeed might not have initialized yet
618         if ('sessionStorage' in window && window['sessionStorage'] !== null)
619                 sessionStorage.clear();
620
621         const hotkeys = getInitParam("hotkeys");
622         const tmp = [];
623
624         for (const sequence in hotkeys[1]) {
625                 const filtered = sequence.replace(/\|.*$/, "");
626                 tmp[filtered] = hotkeys[1][sequence];
627         }
628
629         hotkeys[1] = tmp;
630         setInitParam("hotkeys", hotkeys);
631
632         _widescreen_mode = getInitParam("widescreen");
633         switchPanelMode(_widescreen_mode);
634
635         console.log("second stage ok");
636
637         if (getInitParam("simple_update")) {
638                 console.log("scheduling simple feed updater...");
639                 window.setTimeout(update_random_feed, 30*1000);
640         }
641 }
642
643 function quickMenuGo(opid) {
644         switch (opid) {
645         case "qmcPrefs":
646                 gotoPreferences();
647                 break;
648         case "qmcLogout":
649                 gotoLogout();
650                 break;
651         case "qmcTagCloud":
652                 displayDlg(__("Tag cloud"), "printTagCloud");
653                 break;
654         case "qmcSearch":
655                 search();
656                 break;
657         case "qmcAddFeed":
658                 quickAddFeed();
659                 break;
660         case "qmcDigest":
661                 window.location.href = "backend.php?op=digest";
662                 break;
663         case "qmcEditFeed":
664                 if (activeFeedIsCat())
665                         alert(__("You can't edit this kind of feed."));
666                 else
667                         editFeed(getActiveFeedId());
668                 break;
669         case "qmcRemoveFeed":
670                 var actid = getActiveFeedId();
671
672                 if (activeFeedIsCat()) {
673                         alert(__("You can't unsubscribe from the category."));
674                         return;
675                 }
676
677                 if (!actid) {
678                         alert(__("Please select some feed first."));
679                         return;
680                 }
681
682                 var fn = getFeedName(actid);
683
684                 var pr = __("Unsubscribe from %s?").replace("%s", fn);
685
686                 if (confirm(pr)) {
687                         unsubscribeFeed(actid);
688                 }
689                 break;
690         case "qmcCatchupAll":
691                 catchupAllFeeds();
692                 break;
693         case "qmcShowOnlyUnread":
694                 toggleDispRead();
695                 break;
696         case "qmcAddFilter":
697                 quickAddFilter();
698                 break;
699         case "qmcAddLabel":
700                 addLabel();
701                 break;
702         case "qmcRescoreFeed":
703                 rescoreCurrentFeed();
704                 break;
705         case "qmcToggleWidescreen":
706                 if (!isCdmMode()) {
707                         _widescreen_mode = !_widescreen_mode;
708
709                         // reset stored sizes because geometry changed
710                         setCookie("ttrss_ci_width", 0);
711                         setCookie("ttrss_ci_height", 0);
712
713                         switchPanelMode(_widescreen_mode);
714                 } else {
715                         alert(__("Widescreen is not available in combined mode."));
716                 }
717                 break;
718         case "qmcHKhelp":
719                 helpDialog("main");
720                 break;
721         default:
722                 console.log("quickMenuGo: unknown action: " + opid);
723         }
724 }
725
726 function toggleDispRead() {
727
728         const hide = !(getInitParam("hide_read_feeds") == "1");
729
730         hideOrShowFeeds(hide);
731
732         const query = "?op=rpc&method=setpref&key=HIDE_READ_FEEDS&value=" +
733                 param_escape(hide);
734
735         setInitParam("hide_read_feeds", hide);
736
737         new Ajax.Request("backend.php", {
738                 parameters: query,
739                 onComplete: function(transport) {
740                 } });
741
742 }
743
744 function parse_runtime_info(data) {
745
746         //console.log("parsing runtime info...");
747
748         for (const k in data) {
749                 const v = data[k];
750
751 //              console.log("RI: " + k + " => " + v);
752
753                 if (k == "dep_ts" && parseInt(getInitParam("dep_ts")) > 0) {
754                         if (parseInt(getInitParam("dep_ts")) < parseInt(v) && getInitParam("reload_on_ts_change")) {
755                                 window.location.reload();
756                         }
757                 }
758
759                 if (k == "daemon_is_running" && v != 1) {
760                         notify_error("<span onclick=\"explainError(1)\">Update daemon is not running.</span>", true);
761                         return;
762                 }
763
764                 if (k == "update_result") {
765                         const updatesIcon = dijit.byId("updatesIcon").domNode;
766
767                         if (v) {
768                                 Element.show(updatesIcon);
769                         } else {
770                                 Element.hide(updatesIcon);
771                         }
772                 }
773
774                 if (k == "daemon_stamp_ok" && v != 1) {
775                         notify_error("<span onclick=\"explainError(3)\">Update daemon is not updating feeds.</span>", true);
776                         return;
777                 }
778
779                 if (k == "max_feed_id" || k == "num_feeds") {
780                         if (init_params[k] != v) {
781                                 console.log("feed count changed, need to reload feedlist.");
782                                 updateFeedList();
783                         }
784                 }
785
786                 init_params[k] = v;
787                 notify('');
788         }
789
790         PluginHost.run(PluginHost.HOOK_RUNTIME_INFO_LOADED, data);
791 }
792
793 function collapse_feedlist() {
794         Element.toggle("feeds-holder");
795
796         const splitter = $("feeds-holder_splitter");
797
798         Element.visible("feeds-holder") ? splitter.show() : splitter.hide();
799
800         dijit.byId("main").resize();
801 }
802
803 function viewModeChanged() {
804         cache_clear();
805         return viewCurrentFeed('');
806 }
807
808 function rescoreCurrentFeed() {
809
810         const actid = getActiveFeedId();
811
812         if (activeFeedIsCat() || actid < 0) {
813                 alert(__("You can't rescore this kind of feed."));
814                 return;
815         }
816
817         if (!actid) {
818                 alert(__("Please select some feed first."));
819                 return;
820         }
821
822         const fn = getFeedName(actid);
823         const pr = __("Rescore articles in %s?").replace("%s", fn);
824
825         if (confirm(pr)) {
826                 notify_progress("Rescoring articles...");
827
828                 const query = "?op=pref-feeds&method=rescore&quiet=1&ids=" + actid;
829
830                 new Ajax.Request("backend.php", {
831                         parameters: query,
832                         onComplete: function(transport) {
833                                 viewCurrentFeed();
834                         } });
835         }
836 }
837
838 function hotkey_handler(e) {
839
840         if (e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA") return;
841
842         let keycode = false;
843
844         const cmdline = $('cmdline');
845
846         if (window.event) {
847                 keycode = window.event.keyCode;
848         } else if (e) {
849                 keycode = e.which;
850         }
851
852         if (keycode == 27) { // escape
853                 hotkey_prefix = false;
854         }
855
856         if (keycode == 16) return; // ignore lone shift
857         if (keycode == 17) return; // ignore lone ctrl
858
859         var hotkeys = getInitParam("hotkeys");
860         const keychar = String.fromCharCode(keycode).toLowerCase();
861
862         if (!hotkey_prefix && hotkeys[0].indexOf(keychar) != -1) {
863
864                 const date = new Date();
865                 const ts = Math.round(date.getTime() / 1000);
866
867                 hotkey_prefix = keychar;
868                 hotkey_prefix_pressed = ts;
869
870                 cmdline.innerHTML = keychar;
871                 Element.show(cmdline);
872
873                 e.stopPropagation();
874
875                 // returning false here literally disables ctrl-c in browser lol (because C is a valid prefix)
876                 return true;
877         }
878
879         Element.hide(cmdline);
880
881         let hotkey = keychar.search(/[a-zA-Z0-9]/) != -1 ? keychar : "(" + keycode + ")";
882
883         // ensure ^*char notation
884         if (e.shiftKey) hotkey = "*" + hotkey;
885         if (e.ctrlKey) hotkey = "^" + hotkey;
886         if (e.altKey) hotkey = "+" + hotkey;
887         if (e.metaKey) hotkey = "%" + hotkey;
888
889         hotkey = hotkey_prefix ? hotkey_prefix + " " + hotkey : hotkey;
890         hotkey_prefix = false;
891
892         let hotkey_action = false;
893         var hotkeys = getInitParam("hotkeys");
894
895         for (const sequence in hotkeys[1]) {
896                 if (sequence == hotkey) {
897                         hotkey_action = hotkeys[1][sequence];
898                         break;
899                 }
900         }
901
902         const action = hotkey_actions[hotkey_action];
903
904         if (action != null) {
905                 action();
906                 e.stopPropagation();
907                 return false;
908         }
909 }
910
911 function inPreferences() {
912         return false;
913 }
914
915 function reverseHeadlineOrder() {
916
917         const toolbar = document.forms["main_toolbar_form"];
918         const order_by = dijit.getEnclosingWidget(toolbar.order_by);
919
920         let value = order_by.attr('value');
921
922         if (value == "date_reverse")
923                 value = "default";
924         else
925                 value = "date_reverse";
926
927         order_by.attr('value', value);
928
929         viewCurrentFeed();
930
931 }
932
933 function handle_rpc_json(transport, scheduled_call) {
934
935         const netalert_dijit = dijit.byId("net-alert");
936         let netalert = false;
937
938         if (netalert_dijit) netalert = netalert_dijit.domNode;
939
940         try {
941                 const reply = JSON.parse(transport.responseText);
942
943                 if (reply) {
944
945                         const error = reply['error'];
946
947                         if (error) {
948                                 const code = error['code'];
949                                 const msg = error['msg'];
950
951                                 console.warn("[handle_rpc_json] received fatal error " + code + "/" + msg);
952
953                                 if (code != 0) {
954                                         fatalError(code, msg);
955                                         return false;
956                                 }
957                         }
958
959                         const seq = reply['seq'];
960
961                         if (seq) {
962                                 if (get_seq() != seq) {
963                                         console.log("[handle_rpc_json] sequence mismatch: " + seq +
964                                                 " (want: " + get_seq() + ")");
965                                         return true;
966                                 }
967                         }
968
969                         const message = reply['message'];
970
971                         if (message) {
972                                 if (message == "UPDATE_COUNTERS") {
973                                         console.log("need to refresh counters...");
974                                         setInitParam("last_article_id", -1);
975                                         request_counters(true);
976                                 }
977                         }
978
979                         const counters = reply['counters'];
980
981                         if (counters)
982                                 parse_counters(counters, scheduled_call);
983
984                         const runtime_info = reply['runtime-info'];
985
986                         if (runtime_info)
987                                 parse_runtime_info(runtime_info);
988
989                         if (netalert) netalert.hide();
990
991                 } else if (netalert)
992                                 netalert.show();
993                         else
994                                 notify_error("Communication problem with server.");
995
996         } catch (e) {
997                 if (netalert)
998                         netalert.show();
999                 else
1000                         notify_error("Communication problem with server.");
1001
1002                 console.error(e);
1003         }
1004
1005         return true;
1006 }
1007
1008 function switchPanelMode(wide) {
1009         if (isCdmMode()) return;
1010
1011         const article_id = getActiveArticleId();
1012
1013         if (wide) {
1014                 dijit.byId("headlines-wrap-inner").attr("design", 'sidebar');
1015                 dijit.byId("content-insert").attr("region", "trailing");
1016
1017                 dijit.byId("content-insert").domNode.setStyle({width: '50%',
1018                         height: 'auto',
1019                         borderTopWidth: '0px' });
1020
1021                 if (parseInt(getCookie("ttrss_ci_width")) > 0) {
1022                         dijit.byId("content-insert").domNode.setStyle(
1023                                 {width: getCookie("ttrss_ci_width") + "px" });
1024                 }
1025
1026                 $("headlines-frame").setStyle({ borderBottomWidth: '0px' });
1027                 $("headlines-frame").addClassName("wide");
1028
1029         } else {
1030
1031                 dijit.byId("content-insert").attr("region", "bottom");
1032
1033                 dijit.byId("content-insert").domNode.setStyle({width: 'auto',
1034                         height: '50%',
1035                         borderTopWidth: '0px'});
1036
1037                 if (parseInt(getCookie("ttrss_ci_height")) > 0) {
1038                         dijit.byId("content-insert").domNode.setStyle(
1039                                 {height: getCookie("ttrss_ci_height") + "px" });
1040                 }
1041
1042                 $("headlines-frame").setStyle({ borderBottomWidth: '1px' });
1043                 $("headlines-frame").removeClassName("wide");
1044
1045         }
1046
1047         closeArticlePanel();
1048
1049         if (article_id) view(article_id);
1050
1051         new Ajax.Request("backend.php", {
1052                 parameters: "op=rpc&method=setpanelmode&wide=" + (wide ? 1 : 0),
1053                 onComplete: function(transport) {
1054                         console.log(transport.responseText);
1055                 } });
1056 }
1057
1058 function update_random_feed() {
1059         console.log("in update_random_feed");
1060
1061         new Ajax.Request("backend.php", {
1062                 parameters: "op=rpc&method=updateRandomFeed",
1063                 onComplete: function(transport) {
1064                         handle_rpc_json(transport, true);
1065                         window.setTimeout(update_random_feed, 30*1000);
1066                 } });
1067 }
1068
1069 function hash_get(key) {
1070         const kv = window.location.hash.substring(1).toQueryParams();
1071         return kv[key];
1072 }
1073 function hash_set(key, value) {
1074         const kv = window.location.hash.substring(1).toQueryParams();
1075         kv[key] = value;
1076         window.location.hash = $H(kv).toQueryString();
1077 }