/
uk-modal.js
144 lines (118 loc) · 2.88 KB
/
uk-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
136
137
138
139
140
141
142
143
144
import Component from "@ember/component";
import { computed } from "@ember/object";
import layout from "../templates/components/uk-modal";
import UIkit from "uikit";
import { scheduleOnce } from "@ember/runloop";
import { getOwner } from "@ember/application";
const noop = () => {};
export default Component.extend({
layout,
modalClass: "",
dialogClass: "",
btnClose: true,
escClose: true,
bgClose: true,
stack: false,
container: true,
clsPage: "uk-modal-page",
clsPanel: "uk-modal-dialog",
selClose: [
".uk-modal-close",
".uk-modal-close-default",
".uk-modal-close-outside",
".uk-modal-close-full"
].join(", "),
isAnimating: false,
modalId: computed("elementId", function() {
return `modal-${this.elementId}`;
}),
modalSelector: computed("modalId", function() {
return `#${this.modalId}`;
}),
containerElement: computed("container", function() {
return getOwner(this)
.lookup("service:-document")
.querySelector(this.container);
}),
init() {
this._super(...arguments);
const config = getOwner(this).resolveRegistration("config:environment");
this.set("container", config.APP.rootElement || "body");
this.set("eventHandlers", {
hidden: async () => {
if (this.visible) {
await this.getWithDefault("on-hide", noop)();
}
this.set("isAnimating", false);
},
show: async () => {
if (!this.visible) {
await this.getWithDefault("on-show", noop)();
}
},
shown: () => {
this.set("isAnimating", false);
},
beforehide: () => {
this.set("isAnimating", true);
},
beforeshow: () => {
this.set("isAnimating", true);
}
});
},
didInsertElement() {
this.set(
"modal",
UIkit.modal(
`#${this.modalId}`,
this.getProperties(
"escClose",
"bgClose",
"stack",
"container",
"clsPage",
"clsPanel",
"selClose"
)
)
);
scheduleOnce("afterRender", this, "_setupEvents");
},
didReceiveAttrs() {
scheduleOnce("afterRender", this, "toggleModal");
},
willDestroyElement() {
if (this.modal) {
this._teardownEvents();
this.modal.$destroy(true);
this.set("modal", null);
}
},
_setupEvents() {
Object.keys(this.eventHandlers).forEach(event => {
UIkit.util.on(
this.modalSelector,
event,
this.get(`eventHandlers.${event}`)
);
});
},
_teardownEvents() {
Object.keys(this.eventHandlers).forEach(event => {
UIkit.util.off(
this.modalSelector,
event,
this.get(`eventHandlers.${event}`)
);
});
},
async toggleModal() {
if (!this.modal) return;
if (this.visible) {
await this.modal.show();
} else {
await this.modal.hide();
}
}
});