-
Notifications
You must be signed in to change notification settings - Fork 12
/
modal.js
executable file
·135 lines (106 loc) · 2.79 KB
/
modal.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
import Component from '@ember/component';
import { camelize } from '@ember/string';
import { computed, action } from '@ember/object';
import onTransitionEnd from 'ember-transition-end/utils/on-transition-end';
import { hasTransitions } from 'ember-modal-service/utils/css-transitions';
import { inject as service } from '@ember/service';
import { next } from '@ember/runloop';
import { tracked } from '@glimmer/tracking';
import { buildWaiter } from '@ember/test-waiters';
const openWaiter = buildWaiter('ember-modal-service:open-waiter');
const closeWaiter = buildWaiter('ember-modal-service:close-waiter');
const isNotDestroyed = (target, key, descriptor) => {
const targetMethod = descriptor.value;
descriptor.value = function(...args) {
// istanbul ignore if: lifecycle check.
if (this.isDestroyed || this.isDestroying) {
return null;
}
return targetMethod.apply(this, args);
};
return descriptor;
};
export default class ModalComponent extends Component {
@service modal;
attributeBindings = ['data-modal-show', 'data-id'];
ariaRole = 'dialog';
@tracked visible = false;
@computed('model.fullname')
get 'data-id'() {
return camelize(this.model.fullname);
}
@computed('visible')
get 'data-modal-show'() {
return String(this.visible);
}
didInsertElement() {
super.didInsertElement(...arguments);
// [Service closes modal] Prevent creating an uncaught promise.
this.model.promise.catch(() => {
this._close();
});
next(this, '_open');
}
@isNotDestroyed
_safeDidOpen() {
this.didOpen && this.didOpen();
}
@isNotDestroyed
_open() {
const element = this.element;
this.visible = true;
if (hasTransitions(element)) {
const token = openWaiter.beginAsync();
const callback = () => {
openWaiter.endAsync(token);
this._safeDidOpen();
};
onTransitionEnd(this.element, callback, {
transitionProperty: 'all',
once: true,
onlyTarget: true,
});
} else {
this._safeDidOpen();
}
}
@isNotDestroyed
_close() {
const element = this.element;
// Close modal.
this.visible = false;
// Remove modal from array when transition ends.
if (hasTransitions(element)) {
const token = closeWaiter.beginAsync();
const callback = () => {
closeWaiter.endAsync(token);
this._remove();
};
onTransitionEnd(this.element, callback, {
transitionProperty: 'all',
once: true,
onlyTarget: true,
});
} else {
this._remove();
}
}
@isNotDestroyed
_remove() {
this.modal._closeByModel(this.model);
}
@action
resolve(data) {
this._fullfillmentFn = () => this.model.resolve(data);
this._close();
}
@action
reject(error) {
this._fullfillmentFn = () => this.model.reject(error);
this._close();
}
willDestroy() {
super.willDestroy(...arguments);
this._fullfillmentFn && this._fullfillmentFn();
}
}