settings_page.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS002: Fix invalid constructor
  6. * DS101: Remove unnecessary use of Array.from
  7. * DS102: Remove unnecessary code created because of implicit returns
  8. * DS205: Consider reworking code to avoid use of IIFEs
  9. * DS206: Consider reworking classes to avoid initClass
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  11. */
  12. app.views.SettingsPage = class SettingsPage extends app.View {
  13. constructor(...args) {
  14. this.onChange = this.onChange.bind(this);
  15. this.onClick = this.onClick.bind(this);
  16. super(...args);
  17. }
  18. static initClass() {
  19. this.className = "_static";
  20. this.events = {
  21. click: "onClick",
  22. change: "onChange",
  23. };
  24. }
  25. render() {
  26. this.html(this.tmpl("settingsPage", this.currentSettings()));
  27. }
  28. currentSettings() {
  29. const settings = {};
  30. settings.theme = app.settings.get("theme");
  31. settings.smoothScroll = !app.settings.get("fastScroll");
  32. settings.arrowScroll = app.settings.get("arrowScroll");
  33. settings.noAutofocus = app.settings.get("noAutofocus");
  34. settings.autoInstall = app.settings.get("autoInstall");
  35. settings.analyticsConsent = app.settings.get("analyticsConsent");
  36. settings.spaceScroll = app.settings.get("spaceScroll");
  37. settings.spaceTimeout = app.settings.get("spaceTimeout");
  38. settings.autoSupported = app.settings.autoSupported;
  39. for (var layout of Array.from(app.settings.LAYOUTS)) {
  40. settings[layout] = app.settings.hasLayout(layout);
  41. }
  42. return settings;
  43. }
  44. getTitle() {
  45. return "Preferences";
  46. }
  47. setTheme(value) {
  48. app.settings.set("theme", value);
  49. }
  50. toggleLayout(layout, enable) {
  51. app.settings.setLayout(layout, enable);
  52. }
  53. toggleSmoothScroll(enable) {
  54. app.settings.set("fastScroll", !enable);
  55. }
  56. toggleAnalyticsConsent(enable) {
  57. app.settings.set("analyticsConsent", enable ? "1" : "0");
  58. if (!enable) {
  59. resetAnalytics();
  60. }
  61. }
  62. toggleSpaceScroll(enable) {
  63. app.settings.set("spaceScroll", enable ? 1 : 0);
  64. }
  65. setScrollTimeout(value) {
  66. return app.settings.set("spaceTimeout", value);
  67. }
  68. toggle(name, enable) {
  69. app.settings.set(name, enable);
  70. }
  71. export() {
  72. const data = new Blob([JSON.stringify(app.settings.export())], {
  73. type: "application/json",
  74. });
  75. const link = document.createElement("a");
  76. link.href = URL.createObjectURL(data);
  77. link.download = "devdocs.json";
  78. link.style.display = "none";
  79. document.body.appendChild(link);
  80. link.click();
  81. document.body.removeChild(link);
  82. }
  83. import(file, input) {
  84. if (!file || file.type !== "application/json") {
  85. new app.views.Notif("ImportInvalid", { autoHide: false });
  86. return;
  87. }
  88. const reader = new FileReader();
  89. reader.onloadend = function () {
  90. const data = (() => {
  91. try {
  92. return JSON.parse(reader.result);
  93. } catch (error) {}
  94. })();
  95. if (!data || data.constructor !== Object) {
  96. new app.views.Notif("ImportInvalid", { autoHide: false });
  97. return;
  98. }
  99. app.settings.import(data);
  100. $.trigger(input.form, "import");
  101. };
  102. reader.readAsText(file);
  103. }
  104. onChange(event) {
  105. const input = event.target;
  106. switch (input.name) {
  107. case "theme":
  108. this.setTheme(input.value);
  109. break;
  110. case "layout":
  111. this.toggleLayout(input.value, input.checked);
  112. break;
  113. case "smoothScroll":
  114. this.toggleSmoothScroll(input.checked);
  115. break;
  116. case "import":
  117. this.import(input.files[0], input);
  118. break;
  119. case "analyticsConsent":
  120. this.toggleAnalyticsConsent(input.checked);
  121. break;
  122. case "spaceScroll":
  123. this.toggleSpaceScroll(input.checked);
  124. break;
  125. case "spaceTimeout":
  126. this.setScrollTimeout(input.value);
  127. break;
  128. default:
  129. this.toggle(input.name, input.checked);
  130. }
  131. }
  132. onClick(event) {
  133. const target = $.eventTarget(event);
  134. switch (target.getAttribute("data-action")) {
  135. case "export":
  136. $.stopEvent(event);
  137. this.export();
  138. break;
  139. }
  140. }
  141. onRoute(context) {
  142. this.render();
  143. }
  144. };
  145. app.views.SettingsPage.initClass();