-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomDialog.js
126 lines (110 loc) · 3.4 KB
/
CustomDialog.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
120
121
122
123
124
125
126
class CustomDialog {
constructor() {
this.dialog = document.createElement('dialog');
this.dialog.innerHTML = `
<article>
<h2></h2>
<p></p>
<ul class="details"></ul>
<footer>
<button class="secondary" data-action="cancel">Cancel</button>
<button data-action="confirm">Confirm</button>
</footer>
</article>
`;
this.dialog.classList.add('custom-dialog', 'modal-lg');
document.body.appendChild(this.dialog);
this.result = null;
this.inputField = null;
// Bind event listeners
this.dialog.addEventListener('click', (event) => {
if (event.target.dataset.action === 'cancel') {
this.close(null);
} else if (event.target.dataset.action === 'confirm') {
this.close(this.inputField && this.inputField.style.display !== 'none' ? this.inputField.value : true);
}
});
}
open({ type = 'alert', title = '', message = '', details = [] } = {}) {
return new Promise((resolve) => {
this.result = null;
this.resolve = resolve;
// Set content
this.dialog.querySelector('h2').textContent = title || this.capitalizeFirstLetter(type);
this.dialog.querySelector('p').textContent = message;
const detailsList = this.dialog.querySelector('.details');
detailsList.innerHTML = '';
if (details.length > 0) {
detailsList.style.display = 'block';
details.forEach((item) => {
const li = document.createElement('li');
li.textContent = item;
detailsList.appendChild(li);
});
} else {
detailsList.style.display = 'none';
}
// Add input field for prompt
if (type === 'prompt') {
if (!this.inputField) {
this.inputField = document.createElement('input');
this.inputField.type = 'text';
this.inputField.classList.add('prompt-input');
this.dialog.querySelector('article').insertBefore(this.inputField, this.dialog.querySelector('footer'));
}
this.inputField.style.display = 'block';
this.inputField.value = '';
this.inputField.focus();
} else if (this.inputField) {
this.inputField.style.display = 'none';
}
// Show/hide buttons based on type
this.dialog.querySelector('[data-action="cancel"]').style.display = type === 'alert' ? 'none' : '';
this.dialog.querySelector('[data-action="confirm"]').textContent = type === 'prompt' ? 'OK' : 'Confirm';
this.dialog.showModal();
});
}
close(value) {
this.dialog.close();
this.resolve(value);
}
capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
alert({ title, message, details }) {
return this.open({ type: 'alert', title, message, details });
}
confirm({ title, message, details }) {
return this.open({ type: 'confirm', title, message, details });
}
prompt({ title, message, details }) {
return this.open({ type: 'prompt', title, message, details });
}
}
// Usage example:
const dialog = new CustomDialog();
// Alert dialog
async function showAlert(message, title = 'Alert') {
await dialog.alert({
title: title,
message: message,
});
return true; //console.log('Alert closed');
}
// Confirm dialog
async function showConfirm(message, title = 'Confirm', details = []) {
const confirmed = await dialog.confirm({
title: title,
message: message,
details: details,
});
return confirmed;
}
// Prompt dialog
async function showPrompt(message, title = 'Input') {
const result = await dialog.prompt({
title: title,
message: message,
});
return result !== null ? result : false;
}