-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDialog.js
119 lines (107 loc) · 3 KB
/
Dialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { uuidv4 } from '../../../lib/utils/string';
class Dialog {
constructor({ closeOnOutsideClick, onSubmit, hideDialogDuringSubmit }) {
this.id = uuidv4();
this.$dialog = null;
this.closeOnOutsideClick = closeOnOutsideClick || false;
this.onSubmit = onSubmit || (async () => {});
this.hideDialogDuringSubmit = hideDialogDuringSubmit || false;
}
get style() {
return `
<style>
.backdrop {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 10;
}
.dialog-container {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
min-height: 10px;
min-width: 10px;
width: auto;
height: auto;
background-color: var(--color-white);
border: 1px solid var(--color-border);
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.25);
z-index: 100;
}
.dialog-container header {
padding: var(--spacing-medium);
border-bottom: 1px solid var(--color-border);
}
</style>
`;
}
get templateDialog() {
return `
${this.style}
<div class='dialog-container'>
<form>
${this.templateHTML}
</form>
</div>
<div class='backdrop'/>
`;
}
show() {
document.body.appendChild(this.$dialog);
}
hide() {
document.body.removeChild(this.$dialog);
}
get templateHTML() {
return '';
}
handleCloseOnOutsideClick() {
if (this.closeOnOutsideClick) {
this.$dialog.querySelector('.backdrop').addEventListener('click', (e) => {
this.hide();
});
}
}
inject(callback) {
callback();
this.handleCloseOnOutsideClick();
}
async noneDisplayDialogWithSubmit(onSubmit, $dialog, props) {
$dialog.style.display = 'none';
const process = await onSubmit(props);
$dialog.style.display = 'block';
return process;
}
open() {
this.$dialog = document.createElement('div');
this.$dialog.setAttribute('id', this.id);
this.$dialog.innerHTML = this.templateDialog;
this.show();
this.inject();
return new Promise((resolve) => {
const $form = this.$dialog.querySelector('form');
$form.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const props = {};
for (const [key, value] of formData.entries()) {
props[key] = value;
}
const onSubmit = this.hideDialogDuringSubmit ? this.noneDisplayDialogWithSubmit.bind(null, this.onSubmit, this.$dialog) : this.onSubmit;
const process = await onSubmit(props);
if (process === false) {
return;
}
resolve(props);
this.hide();
});
});
}
}
export default Dialog;