settings.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. const Cls = (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 =
  39. {enter: 'onEnter'};
  40. }
  41. init() {
  42. this.addSubview(this.docPicker = new app.views.DocPicker);
  43. }
  44. activate() {
  45. if (super.activate(...arguments)) {
  46. this.render();
  47. document.body.classList.remove(SIDEBAR_HIDDEN_LAYOUT);
  48. }
  49. }
  50. deactivate() {
  51. if (super.deactivate(...arguments)) {
  52. this.resetClass();
  53. this.docPicker.detach();
  54. if (app.settings.hasLayout(SIDEBAR_HIDDEN_LAYOUT)) { document.body.classList.add(SIDEBAR_HIDDEN_LAYOUT); }
  55. }
  56. }
  57. render() {
  58. this.docPicker.appendTo(this.sidebar);
  59. this.refreshElements();
  60. this.addClass('_in');
  61. }
  62. save(options) {
  63. if (options == null) { options = {}; }
  64. if (!this.saving) {
  65. let docs;
  66. this.saving = true;
  67. if (options.import) {
  68. docs = app.settings.getDocs();
  69. } else {
  70. docs = this.docPicker.getSelectedDocs();
  71. app.settings.setDocs(docs);
  72. }
  73. this.saveBtn.textContent = 'Saving\u2026';
  74. const disabledDocs = new app.collections.Docs((() => {
  75. const result = [];
  76. for (var doc of Array.from(app.docs.all())) { if (docs.indexOf(doc.slug) === -1) {
  77. result.push(doc);
  78. }
  79. }
  80. return result;
  81. })());
  82. disabledDocs.uninstall(function() {
  83. app.db.migrate();
  84. return app.reload();
  85. });
  86. }
  87. }
  88. onChange() {
  89. this.addClass('_dirty');
  90. }
  91. onEnter() {
  92. this.save();
  93. }
  94. onSubmit(event) {
  95. event.preventDefault();
  96. this.save();
  97. }
  98. onImport() {
  99. this.addClass('_dirty');
  100. this.save({import: true});
  101. }
  102. onClick(event) {
  103. if (event.which !== 1) { return; }
  104. if (event.target === this.backBtn) {
  105. $.stopEvent(event);
  106. app.router.show('/');
  107. }
  108. }
  109. });
  110. Cls.initClass();
  111. return Cls;
  112. })();