-
Notifications
You must be signed in to change notification settings - Fork 1
/
cosmoz-dialog-mixin.js
255 lines (213 loc) · 5.55 KB
/
cosmoz-dialog-mixin.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import '@polymer/paper-dialog';
import { microTask } from '@polymer/polymer/lib/utils/async';
import { templatize } from '@polymer/polymer/lib/utils/templatize';
import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer';
import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin';
/**
* @polymer
* @mixinFunction
*/
export const dialogOpener = dedupingMixin(base => // eslint-disable-line max-lines-per-function
class extends base {
static get properties() {
return {
/**
* Set to true to display a backdrop behind the overlay
*/
withBackdrop: {
type: Boolean,
value: true
},
/**
* Set to true to disable auto-focusing the overlay or child nodes with
* the `autofocus` attribute` when the overlay is opened.
*/
noAutoFocus: {
type: Boolean,
value: false
},
/**
* True if the dialog should be rendered before first call to `open`.
*/
prerender: {
type: Boolean,
value: false
},
/**
* Set to true to disable closing dialog with the ESC key
*/
noCancelOnEscKey: {
type: Boolean,
value: false
},
/**
* Set to true to diable closing dialog when clicking outside of dialog
*/
noCancelOnOutsideClick: {
type: Boolean,
value: true
}
};
}
static get hostAttributes() {
return {
role: 'dialog',
tabindex: '-1'
};
}
constructor() {
super();
// The generated paper-dialog element
this._paperDialog = null;
this._templateInstance = null;
this._dialogEventsHandler = this._handlePaperDialogEvents.bind(this);
this._dialogCloseEventHandler = this.close.bind(this);
this._pendingProps = {};
this._spawn = this._spawn.bind(this);
this._spawnSteps = [this._createDialog, this._createInstance];
}
connectedCallback() {
super.connectedCallback();
this._observer = new FlattenedNodesObserver(this, this._onNodesChange);
window.addEventListener('cosmoz-dialog-closeall', this._dialogCloseEventHandler);
}
disconnectedCallback() {
super.disconnectedCallback();
if (this._observer) {
this._observer.disconnect();
this._observer = null;
}
this._detachDialog();
window.removeEventListener('cosmoz-dialog-closeall', this._dialogCloseEventHandler);
this._templateInstance = null;
}
_spawn() {
const step = this._spawnSteps.shift();
if (!step) {
return;
}
step.call(this);
microTask.run(() => this._spawn());
}
/**
* Get the generated paper-dialog element
*/
get paperDialog() {
return this._paperDialog;
}
open() {
if (!this._userTemplate) {
return;
}
this._spawnSteps.splice(0).forEach(step => step.call(this));
this._attachDialog();
this._paperDialog.open();
}
_attachDialog() {
if (!this._paperDialog.parentNode) {
document.body.appendChild(this._paperDialog);
}
}
_detachDialog() {
const dialog = this._paperDialog;
if (dialog == null) {
return;
}
dialog.removeEventListener('iron-overlay-opened', this._dialogEventsHandler);
dialog.removeEventListener('iron-overlay-closed', this._dialogEventsHandler);
if (!dialog.parentNode) {
return;
}
dialog.parentNode.removeChild(dialog);
}
close() {
if (this._paperDialog != null) {
this._paperDialog.close();
}
}
fit() {
if (this._paperDialog != null && this._paperDialog.opened) {
this._paperDialog.fit();
}
}
refit() {
if (this._paperDialog != null && this._paperDialog.opened) {
this._paperDialog.refit();
}
}
center() {
if (this._paperDialog != null && this._paperDialog.opened) {
this._paperDialog.center();
}
}
_onNodesChange({ addedNodes }) {
if (this._userTemplate) {
return;
}
const template = Array.from(addedNodes)
.filter(n => n.nodeName === 'TEMPLATE')
.pop();
if (!template) {
// eslint-disable-next-line no-console
console.warn('cosmoz-dialog-behavior requires a template');
this._spawnSteps.splice(0);
}
this._userTemplate = template;
this._instanceCtor = templatize(template, this, {
instanceProps: {},
parentModel: true,
forwardHostProp: this._forwardHostProp
});
if (!this.prerender) {
return;
}
microTask.run(() => this._spawn());
}
_createDialog() {
if (!this._userTemplate) {
return;
}
const dialog = document.createElement('paper-dialog');
dialog.noAutoFocus = this.noAutoFocus;
dialog.noCancelOnEscKey = this.noCancelOnEscKey;
dialog.noCancelOnOutsideClick = this.noCancelOnOutsideClick;
dialog.withBackdrop = this.withBackdrop;
dialog.addEventListener('iron-overlay-opened', this._dialogEventsHandler);
dialog.addEventListener('iron-overlay-closed', this._dialogEventsHandler);
this._paperDialog = dialog;
return dialog;
}
_createInstance() {
if (!this._paperDialog) {
return;
}
const instance = new this._instanceCtor(this._pendingProps);
this._templateInstance = instance;
this._paperDialog.appendChild(instance.root);
if (instance._flushProperties) {
instance._flushProperties(true);
}
}
_handlePaperDialogEvents(event) {
this.dispatchEvent(new CustomEvent(event.type, {
bubbles: true,
composed: true,
detail: {
originalTarget: event.target
}
}));
}
_forwardHostProp(prop, value) {
const instance = this._templateInstance;
if (!instance) {
this._pendingProps[prop] = value;
return;
}
if (instance.forwardHostProp) {
instance.forwardHostProp(prop, value);
return;
}
instance[prop] = value;
}
}
);