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;
7 let _active_feed_id = 0;
8 let _active_feed_is_cat = false;
19 function activeFeedIsCat() {
20 return _active_feed_is_cat;
23 function getActiveFeedId() {
24 return _active_feed_id;
27 function setActiveFeedId(id, is_cat) {
29 hash_set('c', is_cat ? 1 : 0);
32 _active_feed_is_cat = is_cat;
34 $("headlines-frame").setAttribute("feed-id", id);
35 $("headlines-frame").setAttribute("is-cat", is_cat ? 1 : 0);
37 selectFeed(id, is_cat);
39 PluginHost.run(PluginHost.HOOK_FEED_SET_ACTIVE, _active_article_id);
43 function updateFeedList() {
45 Element.show("feedlistLoading");
49 if (dijit.byId("feedTree")) {
50 dijit.byId("feedTree").destroyRecursive();
53 const store = new dojo.data.ItemFileWriteStore({
54 url: "backend.php?op=pref_feeds&method=getfeedtree&mode=2"
57 const treeModel = new fox.FeedStoreModel({
60 "type": getInitParam('enable_feed_cats') == 1 ? "category" : "feed"
64 childrenAttrs: ["items"]
67 const tree = new fox.FeedTree({
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});
82 /* var menu = new dijit.Menu({id: 'feedMenu'});
84 menu.addChild(new dijit.MenuItem({
85 label: "Simple menu item"
88 // menu.bindDomNode(tree.domNode); */
90 var tmph = dojo.connect(dijit.byId('feedMenu'), '_openMyself', function (event) {
91 console.log(dijit.getEnclosingWidget(event.target));
92 dojo.disconnect(tmph);
95 $("feeds-holder").appendChild(tree.domNode);
97 var tmph = dojo.connect(tree, 'onLoad', function () {
98 dojo.disconnect(tmph);
99 Element.hide("feedlistLoading");
104 loading_set_progress(25);
116 function catchupAllFeeds() {
118 const str = __("Mark all articles as read?");
120 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
122 const query_str = "backend.php?op=feeds&method=catchupAll";
124 notify_progress("Marking all feeds as read...");
126 //console.log("catchupAllFeeds Q=" + query_str);
128 new Ajax.Request("backend.php", {
129 parameters: query_str,
130 onComplete: function(transport) {
131 request_counters(true);
140 function viewCurrentFeed(method) {
141 console.log("viewCurrentFeed: " + method);
143 if (getActiveFeedId() != undefined) {
144 viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat(), method: method});
146 return false; // block unneeded form submits
150 if (getInitParam("bw_limit") != "1") {
151 request_counters(true);
152 setTimeout(timeout, 60*1000);
157 const query = "backend.php?op=feeds&method=search¶m=" +
158 param_escape(getActiveFeedId() + ":" + activeFeedIsCat());
160 if (dijit.byId("searchDlg"))
161 dijit.byId("searchDlg").destroyRecursive();
163 const dialog = new dijit.Dialog({
166 style: "width: 600px",
167 execute: function() {
168 if (this.validate()) {
169 _search_query = dojo.objectToQuery(this.attr('value'));
179 function updateTitle() {
180 let tmp = "Tiny Tiny RSS";
182 if (global_unread > 0) {
183 tmp = "(" + global_unread + ") " + tmp;
186 document.title = tmp;
189 function genericSanityCheck() {
190 setCookie("ttrss_test", "TEST");
192 if (getCookie("ttrss_test") != "TEST") {
193 return fatalError(2);
202 window.onerror = function(message, filename, lineno, colno, error) {
203 report_error(message, filename, lineno, colno, error);
206 require(["dojo/_base/kernel",
213 "dijit/ColorPalette",
216 "dijit/form/ComboButton",
217 "dijit/form/CheckBox",
218 "dijit/form/DropDownButton",
219 "dijit/form/FilteringSelect",
221 "dijit/form/RadioButton",
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",
237 "dijit/tree/dndSource",
238 "dijit/tree/ForestStoreModel",
239 "dojo/data/ItemFileWriteStore",
240 "fox/FeedStoreModel",
241 "fox/FeedTree" ], function (dojo, ready, parser) {
248 if (!genericSanityCheck())
251 loading_set_progress(30);
253 const a = document.createElement('audio');
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;
260 init_hotkey_actions();
262 new Ajax.Request("backend.php", {
264 op: "rpc", method: "sanityCheck", hasAudio: hasAudio,
266 clientTzOffset: clientTzOffset,
267 hasSandbox: hasSandbox
269 onComplete: function (transport) {
270 backend_sanity_check_callback(transport);
283 function init_hotkey_actions() {
284 hotkey_actions["next_feed"] = function() {
285 const rv = dijit.byId("feedTree").getNextFeed(
286 getActiveFeedId(), activeFeedIsCat());
288 if (rv) viewfeed({feed: rv[0], is_cat: rv[1], can_wait: true})
290 hotkey_actions["prev_feed"] = function() {
291 const rv = dijit.byId("feedTree").getPreviousFeed(
292 getActiveFeedId(), activeFeedIsCat());
294 if (rv) viewfeed({feed: rv[0], is_cat: rv[1], can_wait: true})
296 hotkey_actions["next_article"] = function() {
299 hotkey_actions["prev_article"] = function() {
302 hotkey_actions["next_article_noscroll"] = function() {
303 moveToPost('next', true);
305 hotkey_actions["prev_article_noscroll"] = function() {
306 moveToPost('prev', true);
308 hotkey_actions["next_article_noexpand"] = function() {
309 moveToPost('next', true, true);
311 hotkey_actions["prev_article_noexpand"] = function() {
312 moveToPost('prev', true, true);
314 hotkey_actions["collapse_article"] = function() {
315 const id = getActiveArticleId();
316 const elem = $("CICD-"+id);
319 if (elem.visible()) {
320 cdmCollapseArticle(null, id);
323 cdmExpandArticle(id);
327 hotkey_actions["toggle_expand"] = function() {
328 const id = getActiveArticleId();
329 const elem = $("CICD-"+id);
332 if (elem.visible()) {
333 cdmCollapseArticle(null, id, false);
336 cdmExpandArticle(id);
340 hotkey_actions["search_dialog"] = function() {
343 hotkey_actions["toggle_mark"] = function() {
344 selectionToggleMarked(undefined, false, true);
346 hotkey_actions["toggle_publ"] = function() {
347 selectionTogglePublished(undefined, false, true);
349 hotkey_actions["toggle_unread"] = function() {
350 selectionToggleUnread(undefined, false, true);
352 hotkey_actions["edit_tags"] = function() {
353 const id = getActiveArticleId();
358 hotkey_actions["open_in_new_window"] = function() {
359 if (getActiveArticleId()) {
360 openArticleInNewWindow(getActiveArticleId());
363 hotkey_actions["catchup_below"] = function() {
364 catchupRelativeToArticle(1);
366 hotkey_actions["catchup_above"] = function() {
367 catchupRelativeToArticle(0);
369 hotkey_actions["article_scroll_down"] = function() {
372 hotkey_actions["article_scroll_up"] = function() {
375 hotkey_actions["close_article"] = function() {
377 if (!getInitParam("cdm_expanded")) {
378 cdmCollapseArticle(false, getActiveArticleId());
384 hotkey_actions["email_article"] = function() {
385 if (typeof emailArticle != "undefined") {
387 } else if (typeof mailtoArticle != "undefined") {
390 alert(__("Please enable mail plugin first."));
393 hotkey_actions["select_all"] = function() {
394 selectArticles('all');
396 hotkey_actions["select_unread"] = function() {
397 selectArticles('unread');
399 hotkey_actions["select_marked"] = function() {
400 selectArticles('marked');
402 hotkey_actions["select_published"] = function() {
403 selectArticles('published');
405 hotkey_actions["select_invert"] = function() {
406 selectArticles('invert');
408 hotkey_actions["select_none"] = function() {
409 selectArticles('none');
411 hotkey_actions["feed_refresh"] = function() {
412 if (getActiveFeedId() != undefined) {
413 viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat()});
417 hotkey_actions["feed_unhide_read"] = function() {
420 hotkey_actions["feed_subscribe"] = function() {
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"));
428 alert("You can't debug this kind of feed.");
432 hotkey_actions["feed_debug_viewfeed"] = function() {
433 viewfeed({feed: getActiveFeedId(), is_cat: activeFeedIsCat(), viewfeed_debug: true});
436 hotkey_actions["feed_edit"] = function() {
437 if (activeFeedIsCat())
438 alert(__("You can't edit this kind of feed."));
440 editFeed(getActiveFeedId());
442 hotkey_actions["feed_catchup"] = function() {
443 if (getActiveFeedId() != undefined) {
444 catchupCurrentFeed();
448 hotkey_actions["feed_reverse"] = function() {
449 reverseHeadlineOrder();
451 hotkey_actions["feed_toggle_vgroup"] = function() {
452 const query_str = "?op=rpc&method=togglepref&key=VFEED_GROUP_BY_FEED";
454 new Ajax.Request("backend.php", {
455 parameters: query_str,
456 onComplete: function(transport) {
461 hotkey_actions["catchup_all"] = function() {
464 hotkey_actions["cat_toggle_collapse"] = function() {
465 if (activeFeedIsCat()) {
466 dijit.byId("feedTree").collapseCat(getActiveFeedId());
470 hotkey_actions["goto_all"] = function() {
471 viewfeed({feed: -4});
473 hotkey_actions["goto_fresh"] = function() {
474 viewfeed({feed: -3});
476 hotkey_actions["goto_marked"] = function() {
477 viewfeed({feed: -1});
479 hotkey_actions["goto_published"] = function() {
480 viewfeed({feed: -2});
482 hotkey_actions["goto_tagcloud"] = function() {
483 displayDlg(__("Tag cloud"), "printTagCloud");
485 hotkey_actions["goto_prefs"] = function() {
488 hotkey_actions["select_article_cursor"] = function() {
489 const id = getArticleUnderPointer();
491 const row = $("RROW-" + id);
494 const cb = dijit.getEnclosingWidget(
495 row.getElementsByClassName("rchk")[0]);
498 cb.attr("checked", !cb.attr("checked"));
499 toggleSelectRowById(cb, "RROW-" + id);
505 hotkey_actions["create_label"] = function() {
508 hotkey_actions["create_filter"] = function() {
511 hotkey_actions["collapse_sidebar"] = function() {
514 hotkey_actions["toggle_embed_original"] = function() {
515 if (typeof embedOriginalArticle != "undefined") {
516 if (getActiveArticleId())
517 embedOriginalArticle(getActiveArticleId());
519 alert(__("Please enable embed_original plugin first."));
522 hotkey_actions["toggle_widescreen"] = function() {
524 _widescreen_mode = !_widescreen_mode;
526 // reset stored sizes because geometry changed
527 setCookie("ttrss_ci_width", 0);
528 setCookie("ttrss_ci_height", 0);
530 switchPanelMode(_widescreen_mode);
532 alert(__("Widescreen is not available in combined mode."));
535 hotkey_actions["help_dialog"] = function() {
538 hotkey_actions["toggle_combined_mode"] = function() {
539 notify_progress("Loading, please wait...");
541 const value = isCdmMode() ? "false" : "true";
542 const query = "?op=rpc&method=setpref&key=COMBINED_DISPLAY_MODE&value=" + value;
544 new Ajax.Request("backend.php", {
546 onComplete: function(transport) {
547 setInitParam("combined_display_mode",
548 !getInitParam("combined_display_mode"));
555 hotkey_actions["toggle_cdm_expanded"] = function() {
556 notify_progress("Loading, please wait...");
558 const value = getInitParam("cdm_expanded") ? "false" : "true";
559 const query = "?op=rpc&method=setpref&key=CDM_EXPANDED&value=" + value;
561 new Ajax.Request("backend.php", {
563 onComplete: function(transport) {
564 setInitParam("cdm_expanded", !getInitParam("cdm_expanded"));
570 function init_second_stage() {
574 if (parseInt(getCookie("ttrss_fh_width")) > 0) {
575 dijit.byId("feeds-holder").domNode.setStyle(
576 {width: getCookie("ttrss_fh_width") + "px" });
579 dijit.byId("main").resize();
581 var tmph = dojo.connect(dijit.byId('feeds-holder'), 'resize',
583 if (args && args.w >= 0) {
584 setCookie("ttrss_fh_width", args.w, getInitParam("cookie_lifetime"));
588 var tmph = dojo.connect(dijit.byId('content-insert'), 'resize',
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"));
596 delCookie("ttrss_test");
598 const toolbar = document.forms["main_toolbar_form"];
600 dijit.getEnclosingWidget(toolbar.view_mode).attr('value',
601 getInitParam("default_view_mode"));
603 dijit.getEnclosingWidget(toolbar.order_by).attr('value',
604 getInitParam("default_view_order_by"));
606 const feeds_sort_by_unread = getInitParam("feeds_sort_by_unread") == 1;
608 const hash_feed_id = hash_get('f');
609 const hash_feed_is_cat = hash_get('c') == "1";
611 if (hash_feed_id != undefined) {
612 setActiveFeedId(hash_feed_id, hash_feed_is_cat);
615 loading_set_progress(50);
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();
621 const hotkeys = getInitParam("hotkeys");
624 for (const sequence in hotkeys[1]) {
625 const filtered = sequence.replace(/\|.*$/, "");
626 tmp[filtered] = hotkeys[1][sequence];
630 setInitParam("hotkeys", hotkeys);
632 _widescreen_mode = getInitParam("widescreen");
633 switchPanelMode(_widescreen_mode);
635 console.log("second stage ok");
637 if (getInitParam("simple_update")) {
638 console.log("scheduling simple feed updater...");
639 window.setTimeout(update_random_feed, 30*1000);
643 function quickMenuGo(opid) {
652 displayDlg(__("Tag cloud"), "printTagCloud");
661 window.location.href = "backend.php?op=digest";
664 if (activeFeedIsCat())
665 alert(__("You can't edit this kind of feed."));
667 editFeed(getActiveFeedId());
669 case "qmcRemoveFeed":
670 var actid = getActiveFeedId();
672 if (activeFeedIsCat()) {
673 alert(__("You can't unsubscribe from the category."));
678 alert(__("Please select some feed first."));
682 var fn = getFeedName(actid);
684 var pr = __("Unsubscribe from %s?").replace("%s", fn);
687 unsubscribeFeed(actid);
690 case "qmcCatchupAll":
693 case "qmcShowOnlyUnread":
702 case "qmcRescoreFeed":
703 rescoreCurrentFeed();
705 case "qmcToggleWidescreen":
707 _widescreen_mode = !_widescreen_mode;
709 // reset stored sizes because geometry changed
710 setCookie("ttrss_ci_width", 0);
711 setCookie("ttrss_ci_height", 0);
713 switchPanelMode(_widescreen_mode);
715 alert(__("Widescreen is not available in combined mode."));
722 console.log("quickMenuGo: unknown action: " + opid);
726 function toggleDispRead() {
728 const hide = !(getInitParam("hide_read_feeds") == "1");
730 hideOrShowFeeds(hide);
732 const query = "?op=rpc&method=setpref&key=HIDE_READ_FEEDS&value=" +
735 setInitParam("hide_read_feeds", hide);
737 new Ajax.Request("backend.php", {
739 onComplete: function(transport) {
744 function parse_runtime_info(data) {
746 //console.log("parsing runtime info...");
748 for (const k in data) {
751 // console.log("RI: " + k + " => " + v);
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();
759 if (k == "daemon_is_running" && v != 1) {
760 notify_error("<span onclick=\"explainError(1)\">Update daemon is not running.</span>", true);
764 if (k == "update_result") {
765 const updatesIcon = dijit.byId("updatesIcon").domNode;
768 Element.show(updatesIcon);
770 Element.hide(updatesIcon);
774 if (k == "daemon_stamp_ok" && v != 1) {
775 notify_error("<span onclick=\"explainError(3)\">Update daemon is not updating feeds.</span>", true);
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.");
790 PluginHost.run(PluginHost.HOOK_RUNTIME_INFO_LOADED, data);
793 function collapse_feedlist() {
794 Element.toggle("feeds-holder");
796 const splitter = $("feeds-holder_splitter");
798 Element.visible("feeds-holder") ? splitter.show() : splitter.hide();
800 dijit.byId("main").resize();
803 function viewModeChanged() {
805 return viewCurrentFeed('');
808 function rescoreCurrentFeed() {
810 const actid = getActiveFeedId();
812 if (activeFeedIsCat() || actid < 0) {
813 alert(__("You can't rescore this kind of feed."));
818 alert(__("Please select some feed first."));
822 const fn = getFeedName(actid);
823 const pr = __("Rescore articles in %s?").replace("%s", fn);
826 notify_progress("Rescoring articles...");
828 const query = "?op=pref-feeds&method=rescore&quiet=1&ids=" + actid;
830 new Ajax.Request("backend.php", {
832 onComplete: function(transport) {
838 function hotkey_handler(e) {
840 if (e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA") return;
844 const cmdline = $('cmdline');
847 keycode = window.event.keyCode;
852 if (keycode == 27) { // escape
853 hotkey_prefix = false;
856 if (keycode == 16) return; // ignore lone shift
857 if (keycode == 17) return; // ignore lone ctrl
859 var hotkeys = getInitParam("hotkeys");
860 const keychar = String.fromCharCode(keycode).toLowerCase();
862 if (!hotkey_prefix && hotkeys[0].indexOf(keychar) != -1) {
864 const date = new Date();
865 const ts = Math.round(date.getTime() / 1000);
867 hotkey_prefix = keychar;
868 hotkey_prefix_pressed = ts;
870 cmdline.innerHTML = keychar;
871 Element.show(cmdline);
875 // returning false here literally disables ctrl-c in browser lol (because C is a valid prefix)
879 Element.hide(cmdline);
881 let hotkey = keychar.search(/[a-zA-Z0-9]/) != -1 ? keychar : "(" + keycode + ")";
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;
889 hotkey = hotkey_prefix ? hotkey_prefix + " " + hotkey : hotkey;
890 hotkey_prefix = false;
892 let hotkey_action = false;
893 var hotkeys = getInitParam("hotkeys");
895 for (const sequence in hotkeys[1]) {
896 if (sequence == hotkey) {
897 hotkey_action = hotkeys[1][sequence];
902 const action = hotkey_actions[hotkey_action];
904 if (action != null) {
911 function inPreferences() {
915 function reverseHeadlineOrder() {
917 const toolbar = document.forms["main_toolbar_form"];
918 const order_by = dijit.getEnclosingWidget(toolbar.order_by);
920 let value = order_by.attr('value');
922 if (value == "date_reverse")
925 value = "date_reverse";
927 order_by.attr('value', value);
933 function handle_rpc_json(transport, scheduled_call) {
935 const netalert_dijit = dijit.byId("net-alert");
936 let netalert = false;
938 if (netalert_dijit) netalert = netalert_dijit.domNode;
941 const reply = JSON.parse(transport.responseText);
945 const error = reply['error'];
948 const code = error['code'];
949 const msg = error['msg'];
951 console.warn("[handle_rpc_json] received fatal error " + code + "/" + msg);
954 fatalError(code, msg);
959 const seq = reply['seq'];
962 if (get_seq() != seq) {
963 console.log("[handle_rpc_json] sequence mismatch: " + seq +
964 " (want: " + get_seq() + ")");
969 const message = reply['message'];
972 if (message == "UPDATE_COUNTERS") {
973 console.log("need to refresh counters...");
974 setInitParam("last_article_id", -1);
975 request_counters(true);
979 const counters = reply['counters'];
982 parse_counters(counters, scheduled_call);
984 const runtime_info = reply['runtime-info'];
987 parse_runtime_info(runtime_info);
989 if (netalert) netalert.hide();
994 notify_error("Communication problem with server.");
1000 notify_error("Communication problem with server.");
1008 function switchPanelMode(wide) {
1009 if (isCdmMode()) return;
1011 const article_id = getActiveArticleId();
1014 dijit.byId("headlines-wrap-inner").attr("design", 'sidebar');
1015 dijit.byId("content-insert").attr("region", "trailing");
1017 dijit.byId("content-insert").domNode.setStyle({width: '50%',
1019 borderTopWidth: '0px' });
1021 if (parseInt(getCookie("ttrss_ci_width")) > 0) {
1022 dijit.byId("content-insert").domNode.setStyle(
1023 {width: getCookie("ttrss_ci_width") + "px" });
1026 $("headlines-frame").setStyle({ borderBottomWidth: '0px' });
1027 $("headlines-frame").addClassName("wide");
1031 dijit.byId("content-insert").attr("region", "bottom");
1033 dijit.byId("content-insert").domNode.setStyle({width: 'auto',
1035 borderTopWidth: '0px'});
1037 if (parseInt(getCookie("ttrss_ci_height")) > 0) {
1038 dijit.byId("content-insert").domNode.setStyle(
1039 {height: getCookie("ttrss_ci_height") + "px" });
1042 $("headlines-frame").setStyle({ borderBottomWidth: '1px' });
1043 $("headlines-frame").removeClassName("wide");
1047 closeArticlePanel();
1049 if (article_id) view(article_id);
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);
1058 function update_random_feed() {
1059 console.log("in update_random_feed");
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);
1069 function hash_get(key) {
1070 const kv = window.location.hash.substring(1).toQueryParams();
1073 function hash_set(key, value) {
1074 const kv = window.location.hash.substring(1).toQueryParams();
1076 window.location.hash = $H(kv).toQueryString();