]> BookStack Code Mirror - bookstack/blob - resources/js/components/confirm-dialog.js
215c0b94e29fd7fe5b3da1f85cd1cc347a8bfe3c
[bookstack] / resources / js / components / confirm-dialog.js
1 import {onSelect} from "../services/dom";
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, rej) => {
29            this.res = res;
30         });
31     }
32
33     /**
34      * @returns {Popup}
35      */
36     getPopup() {
37         return this.container.components.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 }