TreeViewModal.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { h, on, t } from '@dom111/element';
  2. import joinPath, { leadingAndTrailingSlash } from '../lib/joinPath';
  3. import Modal from './Modal';
  4. import Tree from './Tree/Tree';
  5. import WebDAV from './Tree/WebDAV';
  6. type TreeViewModalEvents = {
  7. cancelled: [];
  8. selected: [string];
  9. };
  10. export class TreeViewModal extends Modal<TreeViewModalEvents> {
  11. #tree: Tree;
  12. constructor(title: string) {
  13. super();
  14. this.#tree = new Tree(new WebDAV());
  15. this.append(
  16. h('header', h('h2', t(title))),
  17. this.#tree,
  18. h(
  19. 'footer',
  20. h('button[type="submit"]', t('Choose')),
  21. h('button', t('Cancel'))
  22. )
  23. );
  24. this.addClass('tree-view-modal');
  25. this.bindLocalEvents();
  26. }
  27. private bindLocalEvents(): void {
  28. on(this.query('button[type="submit"]'), 'click', () => {
  29. const value = this.#tree.value();
  30. if (value === null) {
  31. this.emitCustom('selected', null);
  32. return;
  33. }
  34. this.emitCustom('selected', leadingAndTrailingSlash(joinPath(...value)));
  35. });
  36. on(this.query('button:not([type="submit"])'), 'click', () => {
  37. this.emitCustom('cancelled');
  38. });
  39. }
  40. async value(): Promise<string | null> {
  41. return new Promise<string>((resolve) => {
  42. this.on('cancelled', () => resolve(null));
  43. this.on('selected', ({ detail: [path] }) => resolve(path));
  44. });
  45. }
  46. }
  47. export default TreeViewModal;