settings_page.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. const Cls = (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)) { settings[layout] = app.settings.hasLayout(layout); }
  40. return settings;
  41. }
  42. getTitle() {
  43. return 'Preferences';
  44. }
  45. setTheme(value) {
  46. app.settings.set('theme', value);
  47. }
  48. toggleLayout(layout, enable) {
  49. app.settings.setLayout(layout, enable);
  50. }
  51. toggleSmoothScroll(enable) {
  52. app.settings.set('fastScroll', !enable);
  53. }
  54. toggleAnalyticsConsent(enable) {
  55. app.settings.set('analyticsConsent', enable ? '1' : '0');
  56. if (!enable) { resetAnalytics(); }
  57. }
  58. toggleSpaceScroll(enable) {
  59. app.settings.set('spaceScroll', enable ? 1 : 0);
  60. }
  61. setScrollTimeout(value) {
  62. return app.settings.set('spaceTimeout', value);
  63. }
  64. toggle(name, enable) {
  65. app.settings.set(name, enable);
  66. }
  67. export() {
  68. const data = new Blob([JSON.stringify(app.settings.export())], {type: 'application/json'});
  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 = (() => { try { return JSON.parse(reader.result); } catch (error) {} })();
  85. if (!data || (data.constructor !== Object)) {
  86. new app.views.Notif('ImportInvalid', {autoHide: false});
  87. return;
  88. }
  89. app.settings.import(data);
  90. $.trigger(input.form, 'import');
  91. };
  92. reader.readAsText(file);
  93. }
  94. onChange(event) {
  95. const input = event.target;
  96. switch (input.name) {
  97. case 'theme':
  98. this.setTheme(input.value);
  99. break;
  100. case 'layout':
  101. this.toggleLayout(input.value, input.checked);
  102. break;
  103. case 'smoothScroll':
  104. this.toggleSmoothScroll(input.checked);
  105. break;
  106. case 'import':
  107. this.import(input.files[0], input);
  108. break;
  109. case 'analyticsConsent':
  110. this.toggleAnalyticsConsent(input.checked);
  111. break;
  112. case 'spaceScroll':
  113. this.toggleSpaceScroll(input.checked);
  114. break;
  115. case 'spaceTimeout':
  116. this.setScrollTimeout(input.value);
  117. break;
  118. default:
  119. this.toggle(input.name, input.checked);
  120. }
  121. }
  122. onClick(event) {
  123. const target = $.eventTarget(event);
  124. switch (target.getAttribute('data-action')) {
  125. case 'export':
  126. $.stopEvent(event);
  127. this.export();
  128. break;
  129. }
  130. }
  131. onRoute(context) {
  132. this.render();
  133. }
  134. });
  135. Cls.initClass();