1
0

settings.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. app.views.Settings = class Settings extends app.View {
  2. static SIDEBAR_HIDDEN_LAYOUT = "_sidebar-hidden";
  3. static el = "._settings";
  4. static elements = {
  5. sidebar: "._sidebar",
  6. saveBtn: 'button[type="submit"]',
  7. backBtn: "button[data-back]",
  8. };
  9. static events = {
  10. import: "onImport",
  11. change: "onChange",
  12. submit: "onSubmit",
  13. click: "onClick",
  14. };
  15. static shortcuts = { enter: "onEnter" };
  16. init() {
  17. this.addSubview((this.docPicker = new app.views.DocPicker()));
  18. }
  19. activate() {
  20. if (super.activate(...arguments)) {
  21. this.render();
  22. document.body.classList.remove(Settings.SIDEBAR_HIDDEN_LAYOUT);
  23. }
  24. }
  25. deactivate() {
  26. if (super.deactivate(...arguments)) {
  27. this.resetClass();
  28. this.docPicker.detach();
  29. if (app.settings.hasLayout(Settings.SIDEBAR_HIDDEN_LAYOUT)) {
  30. document.body.classList.add(Settings.SIDEBAR_HIDDEN_LAYOUT);
  31. }
  32. }
  33. }
  34. render() {
  35. this.docPicker.appendTo(this.sidebar);
  36. this.refreshElements();
  37. this.addClass("_in");
  38. }
  39. save(options) {
  40. if (options == null) {
  41. options = {};
  42. }
  43. if (!this.saving) {
  44. let docs;
  45. this.saving = true;
  46. if (options.import) {
  47. docs = app.settings.getDocs();
  48. } else {
  49. docs = this.docPicker.getSelectedDocs();
  50. app.settings.setDocs(docs);
  51. }
  52. this.saveBtn.textContent = "Saving\u2026";
  53. const disabledDocs = new app.collections.Docs(
  54. (() => {
  55. const result = [];
  56. for (var doc of app.docs.all()) {
  57. if (!docs.includes(doc.slug)) {
  58. result.push(doc);
  59. }
  60. }
  61. return result;
  62. })(),
  63. );
  64. disabledDocs.uninstall(function () {
  65. app.db.migrate();
  66. return app.reload();
  67. });
  68. }
  69. }
  70. onChange() {
  71. this.addClass("_dirty");
  72. }
  73. onEnter() {
  74. this.save();
  75. }
  76. onSubmit(event) {
  77. event.preventDefault();
  78. this.save();
  79. }
  80. onImport() {
  81. this.addClass("_dirty");
  82. this.save({ import: true });
  83. }
  84. onClick(event) {
  85. if (event.which !== 1) {
  86. return;
  87. }
  88. if (event.target === this.backBtn) {
  89. $.stopEvent(event);
  90. app.router.show("/");
  91. }
  92. }
  93. };