-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic-dialog.js
84 lines (71 loc) · 2.43 KB
/
dynamic-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
/* eslint-disable linebreak-style */
/* Create etools-dialog programmatically and add them directly to the body.
Now paper-dialog has an issues and the backdrop that covers all the content(if dialog is not a child of body),
everything becomes unselectable.
This way of creating the dialog will fix the issue. */
import './etools-dialog.js';
import {logError} from '@unicef-polymer/etools-behaviors/etools-logging';
export function createDynamicDialog(config) {
if (!_validateParams(config)) {
return null;
}
const dialog = document.createElement('etools-dialog');
_applyDefaultDialogConfig(dialog);
for (const propertyName in config) {
if (!Object.prototype.hasOwnProperty.call(config, propertyName) || propertyName === 'closeCallback') {
continue;
}
if (propertyName === 'noPadding' && typeof config[propertyName] === 'boolean') {
dialog.noPadding = config.noPadding;
continue;
}
if (typeof config[propertyName] === 'string' && config[propertyName] !== '') {
dialog[propertyName] = config[propertyName];
}
}
// set close callback
if (config.closeCallback) {
dialog.addEventListener('close', function(event) {
config.closeCallback(event);
});
}
document.querySelector('body').appendChild(dialog);
setTimeout(() => {
const msgPlaceholder = dialog.shadowRoot.querySelector('#dynamicContent');
if (msgPlaceholder) {
msgPlaceholder.appendChild(config.content);
}
}, 400);
return dialog;
}
export function createDialog(title, size, okBtnText, cancelBtnText, closeCallback, content, removePadding, theme) {
const config = {
title: title,
size: size,
okBtnText: okBtnText,
cancelBtnText: cancelBtnText,
closeCallback: closeCallback,
content: content,
noPadding: removePadding,
theme: theme
};
const dialog = createDynamicDialog(config);
document.querySelector('body').appendChild(dialog);
return dialog;
}
export function removeDialog(dialogElement) {
document.querySelector('body').removeChild(dialogElement);
}
function _validateParams(config) {
if (typeof config.content === 'undefined' || config.content === null) {
// eslint-disable-next-line no-console
logError('[DynamicDialogBehavior] You must provide a valid dialog content');
return false;
}
return true;
}
function _applyDefaultDialogConfig(dialog) {
dialog.theme = 'confirmation';
dialog.okBtnText = 'Yes';
dialog.cancelBtnText = 'No';
}