settings_page.js 3.6 KB

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