-
Notifications
You must be signed in to change notification settings - Fork 105
/
Popup.js
213 lines (189 loc) · 5.86 KB
/
Popup.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { CLASS_HIDDEN } from './constants';
import { ICON_CLOSE } from './icons/icons';
import { decodeKeydown } from './util';
class Popup {
/**
* [constructor]
*
* @param {HTMLElement} containerEl - Container element
* @return {Popup} Popup
*/
constructor(containerEl) {
this.containerEl = containerEl;
const uniqueLabel = `popup_${new Date().getTime()}_label`;
this.popupEl = document.createElement('div');
this.popupEl.className = 'bp-modal-dialog bp-is-hidden';
// ARIA for accessibility
this.popupEl.setAttribute('role', 'alert');
this.popupEl.setAttribute('aria-labeledby', uniqueLabel);
this.popupEl.innerHTML = `
<div class='bp-modal-header'>
<button class='bp-modal-close-button'>
${ICON_CLOSE}
</button>
</div>
<div class='bp-modal-content'>
<p class='bp-modal-message' id=${uniqueLabel}></p>
<div class='bp-modal-actions'>
<button class='bp-btn bp-btn-primary bp-popup-btn'> </button>
</div>
</div>
<div class='bp-modal-backdrop'> </div>
`.trim();
// Save references to message, buttons, content, and backdrop
this.messageEl = this.popupEl.querySelector(`#${uniqueLabel}`);
this.buttonEl = this.popupEl.querySelector('button.bp-popup-btn');
this.closeButtonEl = this.popupEl.querySelector('button.bp-modal-close-button');
this.contentEl = this.popupEl.querySelector('div.bp-modal-content');
this.backdropEl = this.popupEl.querySelector('div.bp-modal-backdrop');
this.buttonDisabled = false;
this.popupEl.addEventListener('click', this.popupClickHandler);
document.addEventListener('keydown', this.keydownHandler);
// Append and position popup
const popupWrapperEl = document.createElement('div');
popupWrapperEl.className = 'bp-popup-modal';
popupWrapperEl.appendChild(this.popupEl);
containerEl.appendChild(popupWrapperEl);
}
/**
* [destructor]
*
* @return {void}
*/
destroy() {
if (!this.popupEl) {
return;
}
this.popupEl.removeEventListener('click', this.popupClickHandler);
document.removeEventListener('keydown', this.keydownHandler);
const popupWrapperEl = this.popupEl.parentNode;
popupWrapperEl.parentNode.removeChild(popupWrapperEl);
this.popupEl = null;
}
/**
* Shows a popup with a message.
*
* @public
* @param {string} message - Popup message
* @param {string} [buttonText] - Optional text to show in button
* @param {Function} [buttonHandler] - Optional onclick function for the button
* @return {void}
*/
show(message, buttonText, buttonHandler) {
this.messageEl.textContent = message;
if (buttonText) {
this.buttonEl.textContent = buttonText;
} else {
this.buttonEl.textContent = __('notification_button_default_text');
}
if (buttonHandler && typeof buttonHandler === 'function') {
this.buttonEl.handler = buttonHandler;
}
this.popupEl.classList.remove(CLASS_HIDDEN);
this.popupEl.focus();
}
/**
* Hides the popup message.
*
* @public
* @return {void}
*/
hide() {
this.popupEl.classList.add(CLASS_HIDDEN);
}
/**
* Adds custom content to the modal.
*
* @public
* @param {string} element - DOM element
* @param {boolean} prepend - prepends or appends the content to the content element
*
* @return {void}
*/
addContent(element, prepend) {
const contentEl = this.contentEl;
if (prepend) {
contentEl.insertBefore(element, contentEl.firstChild);
} else {
contentEl.appendChild(element);
}
}
/**
* Returns whether or not the popup is visible.
*
* @public
* @return {boolean} Whether or not popup is visible.
*/
isVisible() {
return !this.popupEl.classList.contains(CLASS_HIDDEN);
}
/**
* Gets whether or not the button is disabled.
*
* @public
* @return {boolean} Whether or not button is diabled
*/
isButtonDisabled() {
return this.buttonDisabled;
}
/**
* Disbles the button element.
*
* @public
* @return {void}
*/
disableButton() {
this.buttonDisabled = true;
this.buttonEl.classList.add('is-disabled');
}
/**
* Enables the button element.
*
* @public
* @return {void}
*/
enableButton() {
this.buttonDisabled = false;
this.buttonEl.classList.remove('is-disabled');
}
/**
* Click handler for popup.
*
* @private
* @param {Event} event - DOM event
* @return {void}
*/
popupClickHandler = (event) => {
event.preventDefault();
event.stopPropagation();
if (event.target === this.closeButtonEl || event.target === this.backdropEl) {
this.hide();
} else if (event.target === this.buttonEl && !this.isButtonDisabled()) {
if (typeof this.buttonEl.handler === 'function') {
this.buttonEl.handler();
} else {
this.hide();
}
}
};
/**
* Keydown handler for popup
*
* @private
* @param {Event} event - Keydown event
* @return {boolean} Consumed or not
*/
keydownHandler = (event) => {
const key = decodeKeydown(event);
switch (key) {
case 'Esc':
case 'Escape':
this.hide();
break;
default:
return false;
}
return true;
};
}
export default Popup;