]>
git.wh0rd.org - tt-rss.git/blob - js/tt-rss.js
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 notify_progress("Marking all feeds as read...");
118 xhrPost("backend.php", {op
: "feeds", method
: "catchupAll"}, () => {
119 request_counters(true);
128 function viewCurrentFeed(method
) {
129 console
.log("viewCurrentFeed: " + method
);
131 if (getActiveFeedId() != undefined) {
132 viewfeed({feed
: getActiveFeedId(), is_cat
: activeFeedIsCat(), method
: method
});
134 return false; // block unneeded form submits
138 if (getInitParam("bw_limit") != "1") {
139 request_counters(true);
140 setTimeout(timeout
, 60*1000);
145 const query
= "backend.php?op=feeds&method=search¶m=" +
146 param_escape(getActiveFeedId() + ":" + activeFeedIsCat());
148 if (dijit
.byId("searchDlg"))
149 dijit
.byId("searchDlg").destroyRecursive();
151 const dialog
= new dijit
.Dialog({
154 style
: "width: 600px",
155 execute: function() {
156 if (this.validate()) {
157 _search_query
= this.attr('value');
167 function updateTitle() {
168 let tmp
= "Tiny Tiny RSS";
170 if (global_unread
> 0) {
171 tmp
= "(" + global_unread
+ ") " + tmp
;
174 document
.title
= tmp
;
177 function genericSanityCheck() {
178 setCookie("ttrss_test", "TEST");
180 if (getCookie("ttrss_test") != "TEST") {
181 return fatalError(2);
190 window
.onerror = function(message
, filename
, lineno
, colno
, error
) {
191 report_error(message
, filename
, lineno
, colno
, error
);
194 require(["dojo/_base/kernel",
201 "dijit/ColorPalette",
204 "dijit/form/ComboButton",
205 "dijit/form/CheckBox",
206 "dijit/form/DropDownButton",
207 "dijit/form/FilteringSelect",
209 "dijit/form/RadioButton",
211 "dijit/form/MultiSelect",
212 "dijit/form/SimpleTextarea",
213 "dijit/form/TextBox",
214 "dijit/form/ComboBox",
215 "dijit/form/ValidationTextBox",
216 "dijit/InlineEditBox",
217 "dijit/layout/AccordionContainer",
218 "dijit/layout/BorderContainer",
219 "dijit/layout/ContentPane",
220 "dijit/layout/TabContainer",
221 "dijit/PopupMenuItem",
225 "dijit/tree/dndSource",
226 "dijit/tree/ForestStoreModel",
227 "dojo/data/ItemFileWriteStore",
228 "fox/FeedStoreModel",
229 "fox/FeedTree" ], function (dojo
, ready
, parser
) {
236 if (!genericSanityCheck())
239 loading_set_progress(30);
241 const a
= document
.createElement('audio');
243 const hasAudio
= !!a
.canPlayType
;
244 const hasSandbox
= "sandbox" in document
.createElement("iframe");
245 const hasMp3
= !!(a
.canPlayType
&& a
.canPlayType('audio/mpeg;').replace(/no
/, ''));
246 const clientTzOffset
= new Date().getTimezoneOffset() * 60;
248 init_hotkey_actions();
251 op
: "rpc", method
: "sanityCheck", hasAudio
: hasAudio
,
253 clientTzOffset
: clientTzOffset
,
254 hasSandbox
: hasSandbox
257 xhrPost("backend.php", params
, (transport
) => {
259 backend_sanity_check_callback(transport
);
275 function init_hotkey_actions() {
276 hotkey_actions
["next_feed"] = function() {
277 const rv
= dijit
.byId("feedTree").getNextFeed(
278 getActiveFeedId(), activeFeedIsCat());
280 if (rv
) viewfeed({feed
: rv
[0], is_cat
: rv
[1], can_wait
: true})
282 hotkey_actions
["prev_feed"] = function() {
283 const rv
= dijit
.byId("feedTree").getPreviousFeed(
284 getActiveFeedId(), activeFeedIsCat());
286 if (rv
) viewfeed({feed
: rv
[0], is_cat
: rv
[1], can_wait
: true})
288 hotkey_actions
["next_article"] = function() {
291 hotkey_actions
["prev_article"] = function() {
294 hotkey_actions
["next_article_noscroll"] = function() {
295 moveToPost('next', true);
297 hotkey_actions
["prev_article_noscroll"] = function() {
298 moveToPost('prev', true);
300 hotkey_actions
["next_article_noexpand"] = function() {
301 moveToPost('next', true, true);
303 hotkey_actions
["prev_article_noexpand"] = function() {
304 moveToPost('prev', true, true);
306 hotkey_actions
["collapse_article"] = function() {
307 const id
= getActiveArticleId();
308 const elem
= $("CICD-"+id
);
311 if (elem
.visible()) {
312 cdmCollapseArticle(null, id
);
315 cdmExpandArticle(id
);
319 hotkey_actions
["toggle_expand"] = function() {
320 const id
= getActiveArticleId();
321 const elem
= $("CICD-"+id
);
324 if (elem
.visible()) {
325 cdmCollapseArticle(null, id
, false);
328 cdmExpandArticle(id
);
332 hotkey_actions
["search_dialog"] = function() {
335 hotkey_actions
["toggle_mark"] = function() {
336 selectionToggleMarked(undefined, false, true);
338 hotkey_actions
["toggle_publ"] = function() {
339 selectionTogglePublished(undefined, false, true);
341 hotkey_actions
["toggle_unread"] = function() {
342 selectionToggleUnread(undefined, false, true);
344 hotkey_actions
["edit_tags"] = function() {
345 const id
= getActiveArticleId();
350 hotkey_actions
["open_in_new_window"] = function() {
351 if (getActiveArticleId()) {
352 openArticleInNewWindow(getActiveArticleId());
355 hotkey_actions
["catchup_below"] = function() {
356 catchupRelativeToArticle(1);
358 hotkey_actions
["catchup_above"] = function() {
359 catchupRelativeToArticle(0);
361 hotkey_actions
["article_scroll_down"] = function() {
364 hotkey_actions
["article_scroll_up"] = function() {
367 hotkey_actions
["close_article"] = function() {
369 if (!getInitParam("cdm_expanded")) {
370 cdmCollapseArticle(false, getActiveArticleId());
376 hotkey_actions
["email_article"] = function() {
377 if (typeof emailArticle
!= "undefined") {
379 } else if (typeof mailtoArticle
!= "undefined") {
382 alert(__("Please enable mail plugin first."));
385 hotkey_actions
["select_all"] = function() {
386 selectArticles('all');
388 hotkey_actions
["select_unread"] = function() {
389 selectArticles('unread');
391 hotkey_actions
["select_marked"] = function() {
392 selectArticles('marked');
394 hotkey_actions
["select_published"] = function() {
395 selectArticles('published');
397 hotkey_actions
["select_invert"] = function() {
398 selectArticles('invert');
400 hotkey_actions
["select_none"] = function() {
401 selectArticles('none');
403 hotkey_actions
["feed_refresh"] = function() {
404 if (getActiveFeedId() != undefined) {
405 viewfeed({feed
: getActiveFeedId(), is_cat
: activeFeedIsCat()});
409 hotkey_actions
["feed_unhide_read"] = function() {
412 hotkey_actions
["feed_subscribe"] = function() {
415 hotkey_actions
["feed_debug_update"] = function() {
416 if (!activeFeedIsCat() && parseInt(getActiveFeedId()) > 0) {
417 window
.open("backend.php?op=feeds&method=update_debugger&feed_id=" + getActiveFeedId() +
418 "&csrf_token=" + getInitParam("csrf_token"));
420 alert("You can't debug this kind of feed.");
424 hotkey_actions
["feed_debug_viewfeed"] = function() {
425 viewfeed({feed
: getActiveFeedId(), is_cat
: activeFeedIsCat(), viewfeed_debug
: true});
428 hotkey_actions
["feed_edit"] = function() {
429 if (activeFeedIsCat())
430 alert(__("You can't edit this kind of feed."));
432 editFeed(getActiveFeedId());
434 hotkey_actions
["feed_catchup"] = function() {
435 if (getActiveFeedId() != undefined) {
436 catchupCurrentFeed();
440 hotkey_actions
["feed_reverse"] = function() {
441 reverseHeadlineOrder();
443 hotkey_actions
["feed_toggle_vgroup"] = function() {
444 xhrPost("backend.php", {op
: "rpc", method
: "togglepref", key
: "VFEED_GROUP_BY_FEED"}, () => {
448 hotkey_actions
["catchup_all"] = function() {
451 hotkey_actions
["cat_toggle_collapse"] = function() {
452 if (activeFeedIsCat()) {
453 dijit
.byId("feedTree").collapseCat(getActiveFeedId());
457 hotkey_actions
["goto_all"] = function() {
458 viewfeed({feed
: -4});
460 hotkey_actions
["goto_fresh"] = function() {
461 viewfeed({feed
: -3});
463 hotkey_actions
["goto_marked"] = function() {
464 viewfeed({feed
: -1});
466 hotkey_actions
["goto_published"] = function() {
467 viewfeed({feed
: -2});
469 hotkey_actions
["goto_tagcloud"] = function() {
470 displayDlg(__("Tag cloud"), "printTagCloud");
472 hotkey_actions
["goto_prefs"] = function() {
475 hotkey_actions
["select_article_cursor"] = function() {
476 const id
= getArticleUnderPointer();
478 const row
= $("RROW-" + id
);
481 const cb
= dijit
.getEnclosingWidget(
482 row
.getElementsByClassName("rchk")[0]);
485 cb
.attr("checked", !cb
.attr("checked"));
486 toggleSelectRowById(cb
, "RROW-" + id
);
492 hotkey_actions
["create_label"] = function() {
495 hotkey_actions
["create_filter"] = function() {
498 hotkey_actions
["collapse_sidebar"] = function() {
501 hotkey_actions
["toggle_embed_original"] = function() {
502 if (typeof embedOriginalArticle
!= "undefined") {
503 if (getActiveArticleId())
504 embedOriginalArticle(getActiveArticleId());
506 alert(__("Please enable embed_original plugin first."));
509 hotkey_actions
["toggle_widescreen"] = function() {
511 _widescreen_mode
= !_widescreen_mode
;
513 // reset stored sizes because geometry changed
514 setCookie("ttrss_ci_width", 0);
515 setCookie("ttrss_ci_height", 0);
517 switchPanelMode(_widescreen_mode
);
519 alert(__("Widescreen is not available in combined mode."));
522 hotkey_actions
["help_dialog"] = function() {
525 hotkey_actions
["toggle_combined_mode"] = function() {
526 notify_progress("Loading, please wait...");
528 const value
= isCdmMode() ? "false" : "true";
530 xhrPost("backend.php", {op
: "rpc", method
: "setpref", key
: "COMBINED_DISPLAY_MODE", value
: value
}, () => {
531 setInitParam("combined_display_mode",
532 !getInitParam("combined_display_mode"));
538 hotkey_actions
["toggle_cdm_expanded"] = function() {
539 notify_progress("Loading, please wait...");
541 const value
= getInitParam("cdm_expanded") ? "false" : "true";
543 xhrPost("backend.php", {op
: "rpc", method
: "setpref", key
: "CDM_EXPANDED", value
: value
}, () => {
544 setInitParam("cdm_expanded", !getInitParam("cdm_expanded"));
550 function init_second_stage() {
554 if (parseInt(getCookie("ttrss_fh_width")) > 0) {
555 dijit
.byId("feeds-holder").domNode
.setStyle(
556 {width
: getCookie("ttrss_fh_width") + "px" });
559 dijit
.byId("main").resize();
561 var tmph
= dojo
.connect(dijit
.byId('feeds-holder'), 'resize',
563 if (args
&& args
.w
>= 0) {
564 setCookie("ttrss_fh_width", args
.w
, getInitParam("cookie_lifetime"));
568 var tmph
= dojo
.connect(dijit
.byId('content-insert'), 'resize',
570 if (args
&& args
.w
>= 0 && args
.h
>= 0) {
571 setCookie("ttrss_ci_width", args
.w
, getInitParam("cookie_lifetime"));
572 setCookie("ttrss_ci_height", args
.h
, getInitParam("cookie_lifetime"));
576 delCookie("ttrss_test");
578 const toolbar
= document
.forms
["main_toolbar_form"];
580 dijit
.getEnclosingWidget(toolbar
.view_mode
).attr('value',
581 getInitParam("default_view_mode"));
583 dijit
.getEnclosingWidget(toolbar
.order_by
).attr('value',
584 getInitParam("default_view_order_by"));
586 const hash_feed_id
= hash_get('f');
587 const hash_feed_is_cat
= hash_get('c') == "1";
589 if (hash_feed_id
!= undefined) {
590 setActiveFeedId(hash_feed_id
, hash_feed_is_cat
);
593 loading_set_progress(50);
595 // can't use cache_clear() here because viewfeed might not have initialized yet
596 if ('sessionStorage' in window
&& window
['sessionStorage'] !== null)
597 sessionStorage
.clear();
599 const hotkeys
= getInitParam("hotkeys");
602 for (const sequence
in hotkeys
[1]) {
603 const filtered
= sequence
.replace(/\|.*$/, "");
604 tmp
[filtered
] = hotkeys
[1][sequence
];
608 setInitParam("hotkeys", hotkeys
);
610 _widescreen_mode
= getInitParam("widescreen");
611 switchPanelMode(_widescreen_mode
);
613 console
.log("second stage ok");
615 if (getInitParam("simple_update")) {
616 console
.log("scheduling simple feed updater...");
617 window
.setTimeout(update_random_feed
, 30*1000);
621 function quickMenuGo(opid
) {
630 displayDlg(__("Tag cloud"), "printTagCloud");
639 window
.location
.href
= "backend.php?op=digest";
642 if (activeFeedIsCat())
643 alert(__("You can't edit this kind of feed."));
645 editFeed(getActiveFeedId());
647 case "qmcRemoveFeed":
648 var actid
= getActiveFeedId();
650 if (activeFeedIsCat()) {
651 alert(__("You can't unsubscribe from the category."));
656 alert(__("Please select some feed first."));
660 var fn
= getFeedName(actid
);
662 var pr
= __("Unsubscribe from %s?").replace("%s", fn
);
665 unsubscribeFeed(actid
);
668 case "qmcCatchupAll":
671 case "qmcShowOnlyUnread":
674 case "qmcToggleWidescreen":
676 _widescreen_mode
= !_widescreen_mode
;
678 // reset stored sizes because geometry changed
679 setCookie("ttrss_ci_width", 0);
680 setCookie("ttrss_ci_height", 0);
682 switchPanelMode(_widescreen_mode
);
684 alert(__("Widescreen is not available in combined mode."));
691 console
.log("quickMenuGo: unknown action: " + opid
);
695 function toggleDispRead() {
697 const hide
= !(getInitParam("hide_read_feeds") == "1");
699 xhrPost("backend.php", {op
: "rpc", method
: "setpref", key
: "HIDE_READ_FEEDS", value
: hide
}, () => {
700 hideOrShowFeeds(hide
);
701 setInitParam("hide_read_feeds", hide
);
705 function parse_runtime_info(data
) {
707 //console.log("parsing runtime info...");
709 for (const k
in data
) {
712 // console.log("RI: " + k + " => " + v);
714 if (k
== "dep_ts" && parseInt(getInitParam("dep_ts")) > 0) {
715 if (parseInt(getInitParam("dep_ts")) < parseInt(v
) && getInitParam("reload_on_ts_change")) {
716 window
.location
.reload();
720 if (k
== "daemon_is_running" && v
!= 1) {
721 notify_error("<span onclick=\"explainError(1)\">Update daemon is not running.</span>", true);
725 if (k
== "update_result") {
726 const updatesIcon
= dijit
.byId("updatesIcon").domNode
;
729 Element
.show(updatesIcon
);
731 Element
.hide(updatesIcon
);
735 if (k
== "daemon_stamp_ok" && v
!= 1) {
736 notify_error("<span onclick=\"explainError(3)\">Update daemon is not updating feeds.</span>", true);
740 if (k
== "max_feed_id" || k
== "num_feeds") {
741 if (init_params
[k
] != v
) {
742 console
.log("feed count changed, need to reload feedlist.");
751 PluginHost
.run(PluginHost
.HOOK_RUNTIME_INFO_LOADED
, data
);
754 function collapse_feedlist() {
755 Element
.toggle("feeds-holder");
757 const splitter
= $("feeds-holder_splitter");
759 Element
.visible("feeds-holder") ? splitter
.show() : splitter
.hide();
761 dijit
.byId("main").resize();
764 function viewModeChanged() {
766 return viewCurrentFeed('');
769 function hotkey_handler(e
) {
771 if (e
.target
.nodeName
== "INPUT" || e
.target
.nodeName
== "TEXTAREA") return;
773 let keycode
= e
.which
;
775 if (keycode
== 27) { // escape and drop prefix
776 hotkey_prefix
= false;
779 if (keycode
== 16 || keycode
== 17) return; // ignore lone shift / ctrl
781 const hotkeys
= getInitParam("hotkeys");
782 const keychar
= String
.fromCharCode(keycode
).toLowerCase();
784 if (!hotkey_prefix
&& hotkeys
[0].indexOf(keychar
) != -1) {
786 const date
= new Date();
787 const ts
= Math
.round(date
.getTime() / 1000);
789 hotkey_prefix
= keychar
;
790 hotkey_prefix_pressed
= ts
;
792 $("cmdline").innerHTML
= keychar
;
793 Element
.show("cmdline");
797 // returning false here literally disables ctrl-c in browser lol (because C is a valid prefix)
801 Element
.hide("cmdline");
803 let hotkey
= keychar
.search(/[a-zA-Z0-9]/) != -1 ? keychar
: "(" + keycode
+ ")";
805 // ensure ^*char notation
806 if (e
.shiftKey
) hotkey
= "*" + hotkey
;
807 if (e
.ctrlKey
) hotkey
= "^" + hotkey
;
808 if (e
.altKey
) hotkey
= "+" + hotkey
;
809 if (e
.metaKey
) hotkey
= "%" + hotkey
;
811 hotkey
= hotkey_prefix
? hotkey_prefix
+ " " + hotkey
: hotkey
;
812 hotkey_prefix
= false;
814 let hotkey_action
= false;
816 for (const sequence
in hotkeys
[1]) {
817 if (sequence
== hotkey
) {
818 hotkey_action
= hotkeys
[1][sequence
];
823 const action
= hotkey_actions
[hotkey_action
];
825 if (action
!= null) {
832 function inPreferences() {
836 function reverseHeadlineOrder() {
838 const toolbar
= document
.forms
["main_toolbar_form"];
839 const order_by
= dijit
.getEnclosingWidget(toolbar
.order_by
);
841 let value
= order_by
.attr('value');
843 if (value
== "date_reverse")
846 value
= "date_reverse";
848 order_by
.attr('value', value
);
854 function handle_rpc_json(transport
, scheduled_call
) {
856 const netalert_dijit
= dijit
.byId("net-alert");
857 let netalert
= false;
859 if (netalert_dijit
) netalert
= netalert_dijit
.domNode
;
862 const reply
= JSON
.parse(transport
.responseText
);
866 const error
= reply
['error'];
869 const code
= error
['code'];
870 const msg
= error
['msg'];
872 console
.warn("[handle_rpc_json] received fatal error " + code
+ "/" + msg
);
875 fatalError(code
, msg
);
880 const seq
= reply
['seq'];
882 if (seq
&& get_seq() != seq
) {
883 console
.log("[handle_rpc_json] sequence mismatch: " + seq
+
884 " (want: " + get_seq() + ")");
888 const message
= reply
['message'];
890 if (message
== "UPDATE_COUNTERS") {
891 console
.log("need to refresh counters...");
892 setInitParam("last_article_id", -1);
893 request_counters(true);
896 const counters
= reply
['counters'];
899 parse_counters(counters
, scheduled_call
);
901 const runtime_info
= reply
['runtime-info'];
904 parse_runtime_info(runtime_info
);
906 if (netalert
) netalert
.hide();
914 notify_error("Communication problem with server.");
921 notify_error("Communication problem with server.");
929 function switchPanelMode(wide
) {
930 if (isCdmMode()) return;
932 const article_id
= getActiveArticleId();
935 dijit
.byId("headlines-wrap-inner").attr("design", 'sidebar');
936 dijit
.byId("content-insert").attr("region", "trailing");
938 dijit
.byId("content-insert").domNode
.setStyle({width
: '50%',
940 borderTopWidth
: '0px' });
942 if (parseInt(getCookie("ttrss_ci_width")) > 0) {
943 dijit
.byId("content-insert").domNode
.setStyle(
944 {width
: getCookie("ttrss_ci_width") + "px" });
947 $("headlines-frame").setStyle({ borderBottomWidth
: '0px' });
948 $("headlines-frame").addClassName("wide");
952 dijit
.byId("content-insert").attr("region", "bottom");
954 dijit
.byId("content-insert").domNode
.setStyle({width
: 'auto',
956 borderTopWidth
: '0px'});
958 if (parseInt(getCookie("ttrss_ci_height")) > 0) {
959 dijit
.byId("content-insert").domNode
.setStyle(
960 {height
: getCookie("ttrss_ci_height") + "px" });
963 $("headlines-frame").setStyle({ borderBottomWidth
: '1px' });
964 $("headlines-frame").removeClassName("wide");
970 if (article_id
) view(article_id
);
972 xhrPost("backend.php", {op
: "rpc", method
: "setpanelmode", wide
: wide
? 1 : 0});
975 function update_random_feed() {
976 console
.log("in update_random_feed");
978 xhrPost("backend.php", { op
: "rpc", method
: "updateRandomFeed" }, (transport
) => {
979 handle_rpc_json(transport
, true);
980 window
.setTimeout(update_random_feed
, 30*1000);
984 function hash_get(key
) {
985 const kv
= window
.location
.hash
.substring(1).toQueryParams();
988 function hash_set(key
, value
) {
989 const kv
= window
.location
.hash
.substring(1).toQueryParams();
991 window
.location
.hash
= $H(kv
).toQueryString();