settings_page.js 4.1 KB

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