settings.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS103: Rewrite code to no longer use __guard__, or convert again using --optional-chaining
  8. * DS104: Avoid inline assignments
  9. * DS206: Consider reworking classes to avoid initClass
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  12. */
  13. (function () {
  14. let PREFERENCE_KEYS = undefined;
  15. let INTERNAL_KEYS = undefined;
  16. app.Settings = class Settings {
  17. static initClass() {
  18. PREFERENCE_KEYS = [
  19. "hideDisabled",
  20. "hideIntro",
  21. "manualUpdate",
  22. "fastScroll",
  23. "arrowScroll",
  24. "analyticsConsent",
  25. "docs",
  26. "dark", // legacy
  27. "theme",
  28. "layout",
  29. "size",
  30. "tips",
  31. "noAutofocus",
  32. "autoInstall",
  33. "spaceScroll",
  34. "spaceTimeout",
  35. ];
  36. INTERNAL_KEYS = ["count", "schema", "version", "news"];
  37. this.prototype.LAYOUTS = [
  38. "_max-width",
  39. "_sidebar-hidden",
  40. "_native-scrollbars",
  41. "_text-justify-hyphenate",
  42. ];
  43. this.defaults = {
  44. count: 0,
  45. hideDisabled: false,
  46. hideIntro: false,
  47. news: 0,
  48. manualUpdate: false,
  49. schema: 1,
  50. analyticsConsent: false,
  51. theme: "auto",
  52. spaceScroll: 1,
  53. spaceTimeout: 0.5,
  54. };
  55. }
  56. constructor() {
  57. this.store = new CookiesStore();
  58. this.cache = {};
  59. this.autoSupported =
  60. window.matchMedia("(prefers-color-scheme)").media !== "not all";
  61. if (this.autoSupported) {
  62. this.darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)");
  63. this.darkModeQuery.addListener(() => this.setTheme(this.get("theme")));
  64. }
  65. }
  66. get(key) {
  67. let left;
  68. if (this.cache.hasOwnProperty(key)) {
  69. return this.cache[key];
  70. }
  71. this.cache[key] =
  72. (left = this.store.get(key)) != null
  73. ? left
  74. : this.constructor.defaults[key];
  75. if (
  76. key === "theme" &&
  77. this.cache[key] === "auto" &&
  78. !this.darkModeQuery
  79. ) {
  80. return (this.cache[key] = "default");
  81. } else {
  82. return this.cache[key];
  83. }
  84. }
  85. set(key, value) {
  86. this.store.set(key, value);
  87. delete this.cache[key];
  88. if (key === "theme") {
  89. this.setTheme(value);
  90. }
  91. }
  92. del(key) {
  93. this.store.del(key);
  94. delete this.cache[key];
  95. }
  96. hasDocs() {
  97. try {
  98. return !!this.store.get("docs");
  99. } catch (error) {}
  100. }
  101. getDocs() {
  102. return (
  103. __guard__(this.store.get("docs"), (x) => x.split("/")) ||
  104. app.config.default_docs
  105. );
  106. }
  107. setDocs(docs) {
  108. this.set("docs", docs.join("/"));
  109. }
  110. getTips() {
  111. return __guard__(this.store.get("tips"), (x) => x.split("/")) || [];
  112. }
  113. setTips(tips) {
  114. this.set("tips", tips.join("/"));
  115. }
  116. setLayout(name, enable) {
  117. this.toggleLayout(name, enable);
  118. const layout = (this.store.get("layout") || "").split(" ");
  119. $.arrayDelete(layout, "");
  120. if (enable) {
  121. if (layout.indexOf(name) === -1) {
  122. layout.push(name);
  123. }
  124. } else {
  125. $.arrayDelete(layout, name);
  126. }
  127. if (layout.length > 0) {
  128. this.set("layout", layout.join(" "));
  129. } else {
  130. this.del("layout");
  131. }
  132. }
  133. hasLayout(name) {
  134. const layout = (this.store.get("layout") || "").split(" ");
  135. return layout.indexOf(name) !== -1;
  136. }
  137. setSize(value) {
  138. this.set("size", value);
  139. }
  140. dump() {
  141. return this.store.dump();
  142. }
  143. export() {
  144. const data = this.dump();
  145. for (var key of Array.from(INTERNAL_KEYS)) {
  146. delete data[key];
  147. }
  148. return data;
  149. }
  150. import(data) {
  151. let key, value;
  152. const object = this.export();
  153. for (key in object) {
  154. value = object[key];
  155. if (!data.hasOwnProperty(key)) {
  156. this.del(key);
  157. }
  158. }
  159. for (key in data) {
  160. value = data[key];
  161. if (PREFERENCE_KEYS.indexOf(key) !== -1) {
  162. this.set(key, value);
  163. }
  164. }
  165. }
  166. reset() {
  167. this.store.reset();
  168. this.cache = {};
  169. }
  170. initLayout() {
  171. if (this.get("dark") === 1) {
  172. this.set("theme", "dark");
  173. this.del("dark");
  174. }
  175. this.setTheme(this.get("theme"));
  176. for (var layout of Array.from(this.LAYOUTS)) {
  177. this.toggleLayout(layout, this.hasLayout(layout));
  178. }
  179. this.initSidebarWidth();
  180. }
  181. setTheme(theme) {
  182. if (theme === "auto") {
  183. theme = this.darkModeQuery.matches ? "dark" : "default";
  184. }
  185. const { classList } = document.documentElement;
  186. classList.remove("_theme-default", "_theme-dark");
  187. classList.add("_theme-" + theme);
  188. this.updateColorMeta();
  189. }
  190. updateColorMeta() {
  191. const color = getComputedStyle(document.documentElement)
  192. .getPropertyValue("--headerBackground")
  193. .trim();
  194. $("meta[name=theme-color]").setAttribute("content", color);
  195. }
  196. toggleLayout(layout, enable) {
  197. const { classList } = document.body;
  198. // sidebar is always shown for settings; its state is updated in app.views.Settings
  199. if (
  200. layout !== "_sidebar-hidden" ||
  201. !(app.router != null ? app.router.isSettings : undefined)
  202. ) {
  203. classList.toggle(layout, enable);
  204. }
  205. classList.toggle("_overlay-scrollbars", $.overlayScrollbarsEnabled());
  206. }
  207. initSidebarWidth() {
  208. const size = this.get("size");
  209. if (size) {
  210. document.documentElement.style.setProperty(
  211. "--sidebarWidth",
  212. size + "px",
  213. );
  214. }
  215. }
  216. };
  217. app.Settings.initClass();
  218. return app.Settings;
  219. })();
  220. function __guard__(value, transform) {
  221. return typeof value !== "undefined" && value !== null
  222. ? transform(value)
  223. : undefined;
  224. }