]> BookStack Code Mirror - bookstack/blob - resources/js/components/confirm-dialog.js
TS: Converted dom and keyboard nav services
[bookstack] / resources / js / components / confirm-dialog.js
1 import {onSelect} from '../services/dom.ts';
2 import {Component} from './component';
3
4 /**
5  * Custom equivalent of window.confirm() using our popup component.
6  * Is promise based so can be used like so:
7  * `const result = await dialog.show()`
8  */
9 export class ConfirmDialog extends Component {
10
11     setup() {
12         this.container = this.$el;
13         this.confirmButton = this.$refs.confirm;
14
15         this.res = null;
16
17         onSelect(this.confirmButton, () => {
18             this.sendResult(true);
19             this.getPopup().hide();
20         });
21     }
22
23     show() {
24         this.getPopup().show(null, () => {
25             this.sendResult(false);
26         });
27
28         return new Promise(res => {
29             this.res = res;
30         });
31     }
32
33     /**
34      * @returns {Popup}
35      */
36     getPopup() {
37         return window.$components.firstOnElement(this.container, 'popup');
38     }
39
40     /**
41      * @param {Boolean} result
42      */
43     sendResult(result) {
44         if (this.res) {
45             this.res(result);
46             this.res = null;
47         }
48     }
49
50 }