1
0

settings_page.js 3.6 KB

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