3 let global_unread = -1;
4 let hotkey_prefix = false;
5 let hotkey_prefix_pressed = false;
6 let hotkey_actions = {};
7 let _widescreen_mode = false;
9 let _active_feed_id = 0;
10 let _active_feed_is_cat = false;
21 function activeFeedIsCat() {
22 return _active_feed_is_cat;
25 function getActiveFeedId() {
26 return _active_feed_id;
29 function setActiveFeedId(id, is_cat) {
31 hash_set('c', is_cat ? 1 : 0);
34 _active_feed_is_cat = is_cat;
36 $("headlines-frame").setAttribute("feed-id", id);
37 $("headlines-frame").setAttribute("is-cat", is_cat ? 1 : 0);
39 selectFeed(id, is_cat);
41 PluginHost.run(PluginHost.HOOK_FEED_SET_ACTIVE, _active_article_id);
45 function updateFeedList() {
47 Element.show("feedlistLoading");
51 if (dijit.byId("feedTree")) {
52 dijit.byId("feedTree").destroyRecursive();
55 const store = new dojo.data.ItemFileWriteStore({
56 url: "backend.php?op=pref_feeds&method=getfeedtree&mode=2"
59 const treeModel = new fox.FeedStoreModel({
62 "type": getInitParam('enable_feed_cats') == 1 ? "category" : "feed"
66 childrenAttrs: ["items"]
69 const tree = new fox.FeedTree({
71 onClick: function (item, node) {
72 const id = String(item.id);
73 const is_cat = id.match("^CAT:");
74 const feed = id.substr(id.indexOf(":") + 1);
75 viewfeed({feed: feed, is_cat: is_cat});
84 var tmph = dojo.connect(dijit.byId('feedMenu'), '_openMyself', function (event) {
85 console.log(dijit.getEnclosingWidget(event.target));
86 dojo.disconnect(tmph);
89 $("feeds-holder").appendChild(tree.domNode);
91 var tmph = dojo.connect(tree, 'onLoad', function () {
92 dojo.disconnect(tmph);
93 Element.hide("feedlistLoading");
98 loading_set_progress(25);
110 function catchupAllFeeds() {
112 const str = __("Mark all articles as read?");
114 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
116 const query_str = "backend.php?op=feeds&method=catchupAll";
118 notify_progress("Marking all feeds as read...");
120 //console.log("catchupAllFeeds Q=" + query_str);
122 new Ajax.Request("backend.php", {
123 parameters: query_str,
124 onComplete: function(transport) {
125 request_counters(true);
134 function viewCurrentFeed(method) {
135 console.log("viewCurrentFeed: " + method);
137 if (getActiveFeedId() != undefined) {
138 viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat(), method: method});
140 return false; // block unneeded form submits
144 if (getInitParam("bw_limit") != "1") {
145 request_counters(true);
146 setTimeout(timeout, 60*1000);
151 const query = "backend.php?op=feeds&method=search¶m=" +
152 param_escape(getActiveFeedId() + ":" + activeFeedIsCat());
154 if (dijit.byId("searchDlg"))
155 dijit.byId("searchDlg").destroyRecursive();
157 const dialog = new dijit.Dialog({
160 style: "width: 600px",
161 execute: function() {
162 if (this.validate()) {
163 _search_query = dojo.objectToQuery(this.attr('value'));
173 function updateTitle() {
174 let tmp = "Tiny Tiny RSS";
176 if (global_unread > 0) {
177 tmp = "(" + global_unread + ") " + tmp;
180 document.title = tmp;
183 function genericSanityCheck() {
184 setCookie("ttrss_test", "TEST");
186 if (getCookie("ttrss_test") != "TEST") {
187 return fatalError(2);
196 window.onerror = function(message, filename, lineno, colno, error) {
197 report_error(message, filename, lineno, colno, error);
200 require(["dojo/_base/kernel",
207 "dijit/ColorPalette",
210 "dijit/form/ComboButton",
211 "dijit/form/CheckBox",
212 "dijit/form/DropDownButton",
213 "dijit/form/FilteringSelect",
215 "dijit/form/RadioButton",
217 "dijit/form/MultiSelect",
218 "dijit/form/SimpleTextarea",
219 "dijit/form/TextBox",
220 "dijit/form/ComboBox",
221 "dijit/form/ValidationTextBox",
222 "dijit/InlineEditBox",
223 "dijit/layout/AccordionContainer",
224 "dijit/layout/BorderContainer",
225 "dijit/layout/ContentPane",
226 "dijit/layout/TabContainer",
227 "dijit/PopupMenuItem",
231 "dijit/tree/dndSource",
232 "dijit/tree/ForestStoreModel",
233 "dojo/data/ItemFileWriteStore",
234 "fox/FeedStoreModel",
235 "fox/FeedTree" ], function (dojo, ready, parser) {
242 if (!genericSanityCheck())
245 loading_set_progress(30);
247 const a = document.createElement('audio');
249 const hasAudio = !!a.canPlayType;
250 const hasSandbox = "sandbox" in document.createElement("iframe");
251 const hasMp3 = !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));
252 const clientTzOffset = new Date().getTimezoneOffset() * 60;
254 init_hotkey_actions();
256 new Ajax.Request("backend.php", {
258 op: "rpc", method: "sanityCheck", hasAudio: hasAudio,
260 clientTzOffset: clientTzOffset,
261 hasSandbox: hasSandbox
263 onComplete: function (transport) {
264 backend_sanity_check_callback(transport);
277 function init_hotkey_actions() {
278 hotkey_actions["next_feed"] = function() {
279 const rv = dijit.byId("feedTree").getNextFeed(
280 getActiveFeedId(), activeFeedIsCat());
282 if (rv) viewfeed({feed: rv[0], is_cat: rv[1], can_wait: true})
284 hotkey_actions["prev_feed"] = function() {
285 const rv = dijit.byId("feedTree").getPreviousFeed(
286 getActiveFeedId(), activeFeedIsCat());
288 if (rv) viewfeed({feed: rv[0], is_cat: rv[1], can_wait: true})
290 hotkey_actions["next_article"] = function() {
293 hotkey_actions["prev_article"] = function() {
296 hotkey_actions["next_article_noscroll"] = function() {
297 moveToPost('next', true);
299 hotkey_actions["prev_article_noscroll"] = function() {
300 moveToPost('prev', true);
302 hotkey_actions["next_article_noexpand"] = function() {
303 moveToPost('next', true, true);
305 hotkey_actions["prev_article_noexpand"] = function() {
306 moveToPost('prev', true, true);
308 hotkey_actions["collapse_article"] = function() {
309 const id = getActiveArticleId();
310 const elem = $("CICD-"+id);
313 if (elem.visible()) {
314 cdmCollapseArticle(null, id);
317 cdmExpandArticle(id);
321 hotkey_actions["toggle_expand"] = function() {
322 const id = getActiveArticleId();
323 const elem = $("CICD-"+id);
326 if (elem.visible()) {
327 cdmCollapseArticle(null, id, false);
330 cdmExpandArticle(id);
334 hotkey_actions["search_dialog"] = function() {
337 hotkey_actions["toggle_mark"] = function() {
338 selectionToggleMarked(undefined, false, true);
340 hotkey_actions["toggle_publ"] = function() {
341 selectionTogglePublished(undefined, false, true);
343 hotkey_actions["toggle_unread"] = function() {
344 selectionToggleUnread(undefined, false, true);
346 hotkey_actions["edit_tags"] = function() {
347 const id = getActiveArticleId();
352 hotkey_actions["open_in_new_window"] = function() {
353 if (getActiveArticleId()) {
354 openArticleInNewWindow(getActiveArticleId());
357 hotkey_actions["catchup_below"] = function() {
358 catchupRelativeToArticle(1);
360 hotkey_actions["catchup_above"] = function() {
361 catchupRelativeToArticle(0);
363 hotkey_actions["article_scroll_down"] = function() {
366 hotkey_actions["article_scroll_up"] = function() {
369 hotkey_actions["close_article"] = function() {
371 if (!getInitParam("cdm_expanded")) {
372 cdmCollapseArticle(false, getActiveArticleId());
378 hotkey_actions["email_article"] = function() {
379 if (typeof emailArticle != "undefined") {
381 } else if (typeof mailtoArticle != "undefined") {
384 alert(__("Please enable mail plugin first."));
387 hotkey_actions["select_all"] = function() {
388 selectArticles('all');
390 hotkey_actions["select_unread"] = function() {
391 selectArticles('unread');
393 hotkey_actions["select_marked"] = function() {
394 selectArticles('marked');
396 hotkey_actions["select_published"] = function() {
397 selectArticles('published');
399 hotkey_actions["select_invert"] = function() {
400 selectArticles('invert');
402 hotkey_actions["select_none"] = function() {
403 selectArticles('none');
405 hotkey_actions["feed_refresh"] = function() {
406 if (getActiveFeedId() != undefined) {
407 viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat()});
411 hotkey_actions["feed_unhide_read"] = function() {
414 hotkey_actions["feed_subscribe"] = function() {
417 hotkey_actions["feed_debug_update"] = function() {
418 if (!activeFeedIsCat() && parseInt(getActiveFeedId()) > 0) {
419 window.open("backend.php?op=feeds&method=update_debugger&feed_id=" + getActiveFeedId() +
420 "&csrf_token=" + getInitParam("csrf_token"));
422 alert("You can't debug this kind of feed.");
426 hotkey_actions["feed_debug_viewfeed"] = function() {
427 viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat(), viewfeed_debug: true});
430 hotkey_actions["feed_edit"] = function() {
431 if (activeFeedIsCat())
432 alert(__("You can't edit this kind of feed."));
434 editFeed(getActiveFeedId());
436 hotkey_actions["feed_catchup"] = function() {
437 if (getActiveFeedId() != undefined) {
438 catchupCurrentFeed();
442 hotkey_actions["feed_reverse"] = function() {
443 reverseHeadlineOrder();
445 hotkey_actions["feed_toggle_vgroup"] = function() {
446 const query_str = "?op=rpc&method=togglepref&key=VFEED_GROUP_BY_FEED";
448 new Ajax.Request("backend.php", {
449 parameters: query_str,
450 onComplete: function(transport) {
455 hotkey_actions["catchup_all"] = function() {
458 hotkey_actions["cat_toggle_collapse"] = function() {
459 if (activeFeedIsCat()) {
460 dijit.byId("feedTree").collapseCat(getActiveFeedId());
464 hotkey_actions["goto_all"] = function() {
465 viewfeed({feed: -4});
467 hotkey_actions["goto_fresh"] = function() {
468 viewfeed({feed: -3});
470 hotkey_actions["goto_marked"] = function() {
471 viewfeed({feed: -1});
473 hotkey_actions["goto_published"] = function() {
474 viewfeed({feed: -2});
476 hotkey_actions["goto_tagcloud"] = function() {
477 displayDlg(__("Tag cloud"), "printTagCloud");
479 hotkey_actions["goto_prefs"] = function() {
482 hotkey_actions["select_article_cursor"] = function() {
483 const id = getArticleUnderPointer();
485 const row = $("RROW-" + id);
488 const cb = dijit.getEnclosingWidget(
489 row.getElementsByClassName("rchk")[0]);
492 cb.attr("checked", !cb.attr("checked"));
493 toggleSelectRowById(cb, "RROW-" + id);
499 hotkey_actions["create_label"] = function() {
502 hotkey_actions["create_filter"] = function() {
505 hotkey_actions["collapse_sidebar"] = function() {
508 hotkey_actions["toggle_embed_original"] = function() {
509 if (typeof embedOriginalArticle != "undefined") {
510 if (getActiveArticleId())
511 embedOriginalArticle(getActiveArticleId());
513 alert(__("Please enable embed_original plugin first."));
516 hotkey_actions["toggle_widescreen"] = function() {
518 _widescreen_mode = !_widescreen_mode;
520 // reset stored sizes because geometry changed
521 setCookie("ttrss_ci_width", 0);
522 setCookie("ttrss_ci_height", 0);
524 switchPanelMode(_widescreen_mode);
526 alert(__("Widescreen is not available in combined mode."));
529 hotkey_actions["help_dialog"] = function() {
532 hotkey_actions["toggle_combined_mode"] = function() {
533 notify_progress("Loading, please wait...");
535 const value = isCdmMode() ? "false" : "true";
536 const query = "?op=rpc&method=setpref&key=COMBINED_DISPLAY_MODE&value=" + value;
538 new Ajax.Request("backend.php", {
540 onComplete: function(transport) {
541 setInitParam("combined_display_mode",
542 !getInitParam("combined_display_mode"));
549 hotkey_actions["toggle_cdm_expanded"] = function() {
550 notify_progress("Loading, please wait...");
552 const value = getInitParam("cdm_expanded") ? "false" : "true";
553 const query = "?op=rpc&method=setpref&key=CDM_EXPANDED&value=" + value;
555 new Ajax.Request("backend.php", {
557 onComplete: function(transport) {
558 setInitParam("cdm_expanded", !getInitParam("cdm_expanded"));
564 function init_second_stage() {
568 if (parseInt(getCookie("ttrss_fh_width")) > 0) {
569 dijit.byId("feeds-holder").domNode.setStyle(
570 {width: getCookie("ttrss_fh_width") + "px" });
573 dijit.byId("main").resize();
575 var tmph = dojo.connect(dijit.byId('feeds-holder'), 'resize',
577 if (args && args.w >= 0) {
578 setCookie("ttrss_fh_width", args.w, getInitParam("cookie_lifetime"));
582 var tmph = dojo.connect(dijit.byId('content-insert'), 'resize',
584 if (args && args.w >= 0 && args.h >= 0) {
585 setCookie("ttrss_ci_width", args.w, getInitParam("cookie_lifetime"));
586 setCookie("ttrss_ci_height", args.h, getInitParam("cookie_lifetime"));
590 delCookie("ttrss_test");
592 const toolbar = document.forms["main_toolbar_form"];
594 dijit.getEnclosingWidget(toolbar.view_mode).attr('value',
595 getInitParam("default_view_mode"));
597 dijit.getEnclosingWidget(toolbar.order_by).attr('value',
598 getInitParam("default_view_order_by"));
600 const hash_feed_id = hash_get('f');
601 const hash_feed_is_cat = hash_get('c') == "1";
603 if (hash_feed_id != undefined) {
604 setActiveFeedId(hash_feed_id, hash_feed_is_cat);
607 loading_set_progress(50);
609 // can't use cache_clear() here because viewfeed might not have initialized yet
610 if ('sessionStorage' in window && window['sessionStorage'] !== null)
611 sessionStorage.clear();
613 const hotkeys = getInitParam("hotkeys");
616 for (const sequence in hotkeys[1]) {
617 const filtered = sequence.replace(/\|.*$/, "");
618 tmp[filtered] = hotkeys[1][sequence];
622 setInitParam("hotkeys", hotkeys);
624 _widescreen_mode = getInitParam("widescreen");
625 switchPanelMode(_widescreen_mode);
627 console.log("second stage ok");
629 if (getInitParam("simple_update")) {
630 console.log("scheduling simple feed updater...");
631 window.setTimeout(update_random_feed, 30*1000);
635 function quickMenuGo(opid) {
644 displayDlg(__("Tag cloud"), "printTagCloud");
653 window.location.href = "backend.php?op=digest";
656 if (activeFeedIsCat())
657 alert(__("You can't edit this kind of feed."));
659 editFeed(getActiveFeedId());
661 case "qmcRemoveFeed":
662 var actid = getActiveFeedId();
664 if (activeFeedIsCat()) {
665 alert(__("You can't unsubscribe from the category."));
670 alert(__("Please select some feed first."));
674 var fn = getFeedName(actid);
676 var pr = __("Unsubscribe from %s?").replace("%s", fn);
679 unsubscribeFeed(actid);
682 case "qmcCatchupAll":
685 case "qmcShowOnlyUnread":
694 case "qmcRescoreFeed":
695 rescoreCurrentFeed();
697 case "qmcToggleWidescreen":
699 _widescreen_mode = !_widescreen_mode;
701 // reset stored sizes because geometry changed
702 setCookie("ttrss_ci_width", 0);
703 setCookie("ttrss_ci_height", 0);
705 switchPanelMode(_widescreen_mode);
707 alert(__("Widescreen is not available in combined mode."));
714 console.log("quickMenuGo: unknown action: " + opid);
718 function toggleDispRead() {
720 const hide = !(getInitParam("hide_read_feeds") == "1");
722 hideOrShowFeeds(hide);
724 const query = "?op=rpc&method=setpref&key=HIDE_READ_FEEDS&value=" +
727 setInitParam("hide_read_feeds", hide);
729 new Ajax.Request("backend.php", {
731 onComplete: function(transport) {
736 function parse_runtime_info(data) {
738 //console.log("parsing runtime info...");
740 for (const k in data) {
743 // console.log("RI: " + k + " => " + v);
745 if (k == "dep_ts" && parseInt(getInitParam("dep_ts")) > 0) {
746 if (parseInt(getInitParam("dep_ts")) < parseInt(v) && getInitParam("reload_on_ts_change")) {
747 window.location.reload();
751 if (k == "daemon_is_running" && v != 1) {
752 notify_error("<span onclick=\"explainError(1)\">Update daemon is not running.</span>", true);
756 if (k == "update_result") {
757 const updatesIcon = dijit.byId("updatesIcon").domNode;
760 Element.show(updatesIcon);
762 Element.hide(updatesIcon);
766 if (k == "daemon_stamp_ok" && v != 1) {
767 notify_error("<span onclick=\"explainError(3)\">Update daemon is not updating feeds.</span>", true);
771 if (k == "max_feed_id" || k == "num_feeds") {
772 if (init_params[k] != v) {
773 console.log("feed count changed, need to reload feedlist.");
782 PluginHost.run(PluginHost.HOOK_RUNTIME_INFO_LOADED, data);
785 function collapse_feedlist() {
786 Element.toggle("feeds-holder");
788 const splitter = $("feeds-holder_splitter");
790 Element.visible("feeds-holder") ? splitter.show() : splitter.hide();
792 dijit.byId("main").resize();
795 function viewModeChanged() {
797 return viewCurrentFeed('');
800 function rescoreCurrentFeed() {
802 const actid = getActiveFeedId();
804 if (activeFeedIsCat() || actid < 0) {
805 alert(__("You can't rescore this kind of feed."));
810 alert(__("Please select some feed first."));
814 const fn = getFeedName(actid);
815 const pr = __("Rescore articles in %s?").replace("%s", fn);
818 notify_progress("Rescoring articles...");
820 const query = "?op=pref-feeds&method=rescore&quiet=1&ids=" + actid;
822 new Ajax.Request("backend.php", {
824 onComplete: function() {
830 function hotkey_handler(e) {
832 if (e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA") return;
834 let keycode = e.which;
836 if (keycode == 27) { // escape and drop prefix
837 hotkey_prefix = false;
840 if (keycode == 16 || keycode == 17) return; // ignore lone shift / ctrl
842 const hotkeys = getInitParam("hotkeys");
843 const keychar = String.fromCharCode(keycode).toLowerCase();
845 if (!hotkey_prefix && hotkeys[0].indexOf(keychar) != -1) {
847 const date = new Date();
848 const ts = Math.round(date.getTime() / 1000);
850 hotkey_prefix = keychar;
851 hotkey_prefix_pressed = ts;
853 $("cmdline").innerHTML = keychar;
854 Element.show("cmdline");
858 // returning false here literally disables ctrl-c in browser lol (because C is a valid prefix)
862 Element.hide("cmdline");
864 let hotkey = keychar.search(/[a-zA-Z0-9]/) != -1 ? keychar : "(" + keycode + ")";
866 // ensure ^*char notation
867 if (e.shiftKey) hotkey = "*" + hotkey;
868 if (e.ctrlKey) hotkey = "^" + hotkey;
869 if (e.altKey) hotkey = "+" + hotkey;
870 if (e.metaKey) hotkey = "%" + hotkey;
872 hotkey = hotkey_prefix ? hotkey_prefix + " " + hotkey : hotkey;
873 hotkey_prefix = false;
875 let hotkey_action = false;
877 for (const sequence in hotkeys[1]) {
878 if (sequence == hotkey) {
879 hotkey_action = hotkeys[1][sequence];
884 const action = hotkey_actions[hotkey_action];
886 if (action != null) {
893 function inPreferences() {
897 function reverseHeadlineOrder() {
899 const toolbar = document.forms["main_toolbar_form"];
900 const order_by = dijit.getEnclosingWidget(toolbar.order_by);
902 let value = order_by.attr('value');
904 if (value == "date_reverse")
907 value = "date_reverse";
909 order_by.attr('value', value);
915 function handle_rpc_json(transport, scheduled_call) {
917 const netalert_dijit = dijit.byId("net-alert");
918 let netalert = false;
920 if (netalert_dijit) netalert = netalert_dijit.domNode;
923 const reply = JSON.parse(transport.responseText);
927 const error = reply['error'];
930 const code = error['code'];
931 const msg = error['msg'];
933 console.warn("[handle_rpc_json] received fatal error " + code + "/" + msg);
936 fatalError(code, msg);
941 const seq = reply['seq'];
943 if (seq && get_seq() != seq) {
944 console.log("[handle_rpc_json] sequence mismatch: " + seq +
945 " (want: " + get_seq() + ")");
949 const message = reply['message'];
951 if (message == "UPDATE_COUNTERS") {
952 console.log("need to refresh counters...");
953 setInitParam("last_article_id", -1);
954 request_counters(true);
957 const counters = reply['counters'];
960 parse_counters(counters, scheduled_call);
962 const runtime_info = reply['runtime-info'];
965 parse_runtime_info(runtime_info);
967 if (netalert) netalert.hide();
973 notify_error("Communication problem with server.");
979 notify_error("Communication problem with server.");
987 function switchPanelMode(wide) {
988 if (isCdmMode()) return;
990 const article_id = getActiveArticleId();
993 dijit.byId("headlines-wrap-inner").attr("design", 'sidebar');
994 dijit.byId("content-insert").attr("region", "trailing");
996 dijit.byId("content-insert").domNode.setStyle({width: '50%',
998 borderTopWidth: '0px' });
1000 if (parseInt(getCookie("ttrss_ci_width")) > 0) {
1001 dijit.byId("content-insert").domNode.setStyle(
1002 {width: getCookie("ttrss_ci_width") + "px" });
1005 $("headlines-frame").setStyle({ borderBottomWidth: '0px' });
1006 $("headlines-frame").addClassName("wide");
1010 dijit.byId("content-insert").attr("region", "bottom");
1012 dijit.byId("content-insert").domNode.setStyle({width: 'auto',
1014 borderTopWidth: '0px'});
1016 if (parseInt(getCookie("ttrss_ci_height")) > 0) {
1017 dijit.byId("content-insert").domNode.setStyle(
1018 {height: getCookie("ttrss_ci_height") + "px" });
1021 $("headlines-frame").setStyle({ borderBottomWidth: '1px' });
1022 $("headlines-frame").removeClassName("wide");
1026 closeArticlePanel();
1028 if (article_id) view(article_id);
1030 new Ajax.Request("backend.php", {
1031 parameters: "op=rpc&method=setpanelmode&wide=" + (wide ? 1 : 0),
1032 onComplete: function(transport) {
1033 console.log(transport.responseText);
1037 function update_random_feed() {
1038 console.log("in update_random_feed");
1040 new Ajax.Request("backend.php", {
1041 parameters: "op=rpc&method=updateRandomFeed",
1042 onComplete: function(transport) {
1043 handle_rpc_json(transport, true);
1044 window.setTimeout(update_random_feed, 30*1000);
1048 function hash_get(key) {
1049 const kv = window.location.hash.substring(1).toQueryParams();
1052 function hash_set(key, value) {
1053 const kv = window.location.hash.substring(1).toQueryParams();
1055 window.location.hash = $H(kv).toQueryString();