-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathconfirmation.js
71 lines (57 loc) · 2.24 KB
/
confirmation.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
const Confirm = {
open (options) {
options = Object.assign({}, {
title: '',
message: '',
okText: 'OK',
cancelText: 'Cancel',
onok: function () {},
oncancel: function () {}
}, options);
const html = `
<div class="confirm">
<div class="confirm__window">
<div class="confirm__titlebar">
<span class="confirm__title">${options.title}</span>
<button class="confirm__close">×</button>
</div>
<div class="confirm__content">${options.message}</div>
<div class="confirm__buttons">
<button class="confirm__button confirm__button--ok confirm__button--fill">${options.okText}</button>
<button class="confirm__button confirm__button--cancel">${options.cancelText}</button>
</div>
</div>
</div>
`;
const template = document.createElement('template');
template.innerHTML = html;
// Elements
const confirmEl = template.content.querySelector('.confirm');
const btnClose = template.content.querySelector('.confirm__close');
const btnOk = template.content.querySelector('.confirm__button--ok');
const btnCancel = template.content.querySelector('.confirm__button--cancel');
confirmEl.addEventListener('click', e => {
if (e.target === confirmEl) {
options.oncancel();
this._close(confirmEl);
}
});
btnOk.addEventListener('click', () => {
options.onok();
this._close(confirmEl);
});
[btnCancel, btnClose].forEach(el => {
el.addEventListener('click', () => {
options.oncancel();
this._close(confirmEl);
});
});
document.body.appendChild(template.content);
},
_close (confirmEl) {
confirmEl.classList.add('confirm--close');
confirmEl.addEventListener('animationend', () => {
document.body.removeChild(confirmEl);
});
}
};