settings.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  12. */
  13. (function () {
  14. let SIDEBAR_HIDDEN_LAYOUT = undefined;
  15. app.views.Settings = class Settings extends app.View {
  16. constructor(...args) {
  17. this.onChange = this.onChange.bind(this);
  18. this.onEnter = this.onEnter.bind(this);
  19. this.onSubmit = this.onSubmit.bind(this);
  20. this.onImport = this.onImport.bind(this);
  21. this.onClick = this.onClick.bind(this);
  22. super(...args);
  23. }
  24. static initClass() {
  25. SIDEBAR_HIDDEN_LAYOUT = "_sidebar-hidden";
  26. this.el = "._settings";
  27. this.elements = {
  28. sidebar: "._sidebar",
  29. saveBtn: 'button[type="submit"]',
  30. backBtn: "button[data-back]",
  31. };
  32. this.events = {
  33. import: "onImport",
  34. change: "onChange",
  35. submit: "onSubmit",
  36. click: "onClick",
  37. };
  38. this.shortcuts = { enter: "onEnter" };
  39. }
  40. init() {
  41. this.addSubview((this.docPicker = new app.views.DocPicker()));
  42. }
  43. activate() {
  44. if (super.activate(...arguments)) {
  45. this.render();
  46. document.body.classList.remove(SIDEBAR_HIDDEN_LAYOUT);
  47. }
  48. }
  49. deactivate() {
  50. if (super.deactivate(...arguments)) {
  51. this.resetClass();
  52. this.docPicker.detach();
  53. if (app.settings.hasLayout(SIDEBAR_HIDDEN_LAYOUT)) {
  54. document.body.classList.add(SIDEBAR_HIDDEN_LAYOUT);
  55. }
  56. }
  57. }
  58. render() {
  59. this.docPicker.appendTo(this.sidebar);
  60. this.refreshElements();
  61. this.addClass("_in");
  62. }
  63. save(options) {
  64. if (options == null) {
  65. options = {};
  66. }
  67. if (!this.saving) {
  68. let docs;
  69. this.saving = true;
  70. if (options.import) {
  71. docs = app.settings.getDocs();
  72. } else {
  73. docs = this.docPicker.getSelectedDocs();
  74. app.settings.setDocs(docs);
  75. }
  76. this.saveBtn.textContent = "Saving\u2026";
  77. const disabledDocs = new app.collections.Docs(
  78. (() => {
  79. const result = [];
  80. for (var doc of Array.from(app.docs.all())) {
  81. if (docs.indexOf(doc.slug) === -1) {
  82. result.push(doc);
  83. }
  84. }
  85. return result;
  86. })(),
  87. );
  88. disabledDocs.uninstall(function () {
  89. app.db.migrate();
  90. return app.reload();
  91. });
  92. }
  93. }
  94. onChange() {
  95. this.addClass("_dirty");
  96. }
  97. onEnter() {
  98. this.save();
  99. }
  100. onSubmit(event) {
  101. event.preventDefault();
  102. this.save();
  103. }
  104. onImport() {
  105. this.addClass("_dirty");
  106. this.save({ import: true });
  107. }
  108. onClick(event) {
  109. if (event.which !== 1) {
  110. return;
  111. }
  112. if (event.target === this.backBtn) {
  113. $.stopEvent(event);
  114. app.router.show("/");
  115. }
  116. }
  117. };
  118. app.views.Settings.initClass();
  119. return app.views.Settings;
  120. })();