settings.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. const Cls = (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 = [
  37. 'count',
  38. 'schema',
  39. 'version',
  40. 'news'
  41. ];
  42. this.prototype.LAYOUTS = [
  43. '_max-width',
  44. '_sidebar-hidden',
  45. '_native-scrollbars',
  46. '_text-justify-hyphenate'
  47. ];
  48. this.defaults = {
  49. count: 0,
  50. hideDisabled: false,
  51. hideIntro: false,
  52. news: 0,
  53. manualUpdate: false,
  54. schema: 1,
  55. analyticsConsent: false,
  56. theme: 'auto',
  57. spaceScroll: 1,
  58. spaceTimeout: 0.5
  59. };
  60. }
  61. constructor() {
  62. this.store = new CookiesStore;
  63. this.cache = {};
  64. this.autoSupported = window.matchMedia('(prefers-color-scheme)').media !== 'not all';
  65. if (this.autoSupported) {
  66. this.darkModeQuery = window.matchMedia('(prefers-color-scheme: dark)');
  67. this.darkModeQuery.addListener(() => this.setTheme(this.get('theme')));
  68. }
  69. }
  70. get(key) {
  71. let left;
  72. if (this.cache.hasOwnProperty(key)) { return this.cache[key]; }
  73. this.cache[key] = (left = this.store.get(key)) != null ? left : this.constructor.defaults[key];
  74. if ((key === 'theme') && (this.cache[key] === 'auto') && !this.darkModeQuery) {
  75. return this.cache[key] = 'default';
  76. } else {
  77. return this.cache[key];
  78. }
  79. }
  80. set(key, value) {
  81. this.store.set(key, value);
  82. delete this.cache[key];
  83. if (key === 'theme') { this.setTheme(value); }
  84. }
  85. del(key) {
  86. this.store.del(key);
  87. delete this.cache[key];
  88. }
  89. hasDocs() {
  90. try { return !!this.store.get('docs'); } catch (error) {}
  91. }
  92. getDocs() {
  93. return __guard__(this.store.get('docs'), x => x.split('/')) || app.config.default_docs;
  94. }
  95. setDocs(docs) {
  96. this.set('docs', docs.join('/'));
  97. }
  98. getTips() {
  99. return __guard__(this.store.get('tips'), x => x.split('/')) || [];
  100. }
  101. setTips(tips) {
  102. this.set('tips', tips.join('/'));
  103. }
  104. setLayout(name, enable) {
  105. this.toggleLayout(name, enable);
  106. const layout = (this.store.get('layout') || '').split(' ');
  107. $.arrayDelete(layout, '');
  108. if (enable) {
  109. if (layout.indexOf(name) === -1) { layout.push(name); }
  110. } else {
  111. $.arrayDelete(layout, name);
  112. }
  113. if (layout.length > 0) {
  114. this.set('layout', layout.join(' '));
  115. } else {
  116. this.del('layout');
  117. }
  118. }
  119. hasLayout(name) {
  120. const layout = (this.store.get('layout') || '').split(' ');
  121. return layout.indexOf(name) !== -1;
  122. }
  123. setSize(value) {
  124. this.set('size', value);
  125. }
  126. dump() {
  127. return this.store.dump();
  128. }
  129. export() {
  130. const data = this.dump();
  131. for (var key of Array.from(INTERNAL_KEYS)) { delete data[key]; }
  132. return data;
  133. }
  134. import(data) {
  135. let key, value;
  136. const object = this.export();
  137. for (key in object) {
  138. value = object[key];
  139. if (!data.hasOwnProperty(key)) { this.del(key); }
  140. }
  141. for (key in data) {
  142. value = data[key];
  143. if (PREFERENCE_KEYS.indexOf(key) !== -1) { this.set(key, value); }
  144. }
  145. }
  146. reset() {
  147. this.store.reset();
  148. this.cache = {};
  149. }
  150. initLayout() {
  151. if (this.get('dark') === 1) {
  152. this.set('theme', 'dark');
  153. this.del('dark');
  154. }
  155. this.setTheme(this.get('theme'));
  156. for (var layout of Array.from(this.LAYOUTS)) { this.toggleLayout(layout, this.hasLayout(layout)); }
  157. this.initSidebarWidth();
  158. }
  159. setTheme(theme) {
  160. if (theme === 'auto') {
  161. theme = this.darkModeQuery.matches ? 'dark' : 'default';
  162. }
  163. const {
  164. classList
  165. } = document.documentElement;
  166. classList.remove('_theme-default', '_theme-dark');
  167. classList.add('_theme-' + theme);
  168. this.updateColorMeta();
  169. }
  170. updateColorMeta() {
  171. const color = getComputedStyle(document.documentElement).getPropertyValue('--headerBackground').trim();
  172. $('meta[name=theme-color]').setAttribute('content', color);
  173. }
  174. toggleLayout(layout, enable) {
  175. const {
  176. classList
  177. } = document.body;
  178. // sidebar is always shown for settings; its state is updated in app.views.Settings
  179. if ((layout !== '_sidebar-hidden') || !(app.router != null ? app.router.isSettings : undefined)) { classList.toggle(layout, enable); }
  180. classList.toggle('_overlay-scrollbars', $.overlayScrollbarsEnabled());
  181. }
  182. initSidebarWidth() {
  183. const size = this.get('size');
  184. if (size) { document.documentElement.style.setProperty('--sidebarWidth', size + 'px'); }
  185. }
  186. });
  187. Cls.initClass();
  188. return Cls;
  189. })();
  190. function __guard__(value, transform) {
  191. return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined;
  192. }