-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.gjs
271 lines (222 loc) · 5.43 KB
/
index.gjs
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* eslint-disable ember/no-runloop, ember/no-tracked-properties-from-args */
import { action } from '@ember/object';
import { cached, tracked } from '@glimmer/tracking';
import { concat, hash } from '@ember/helper';
import { on } from '@ember/modifier';
import Component from '@glimmer/component';
import lifecycle from '@zestia/ember-select-box/modifiers/lifecycle';
import DropdownTrigger from '@zestia/ember-select-box/components/dropdown/trigger';
import DropdownContent from '@zestia/ember-select-box/components/dropdown/content';
import { scheduleOnce } from '@ember/runloop';
import { modifier } from 'ember-modifier';
const { assign } = Object;
const FOCUS_LEAVE = Symbol('FOCUS_LEAVE');
const CLICK_OUTSIDE = Symbol('CLICK_OUTSIDE');
const ESCAPE = Symbol('ESCAPE');
export default class Dropdown extends Component {
@tracked triggerElement;
@tracked contentElement;
@tracked element;
@tracked _isOpen = this.args.open;
lastMouseDownElement;
Trigger;
Content;
registerComponents = (components) => {
assign(this, components);
};
get isOpen() {
return !!this._isOpen;
}
get isClosed() {
return !this.isOpen;
}
get canOpen() {
return this.isClosed;
}
get canClose() {
return this.isOpen;
}
documentListeners = modifier(() => {
document.addEventListener('mousedown', this.handleMouseDown);
document.addEventListener('mouseup', this.handleMouseUp);
return () => {
document.removeEventListener('mousedown', this.handleMouseDown);
document.removeEventListener('mouseup', this.handleMouseUp);
};
});
@action
handleInsertElement(element) {
this.element = element;
this.args.onReady?.(this.api);
}
@action
handleDestroyElement() {
this.element = null;
}
@action
handleInsertTrigger(element) {
this.triggerElement = element;
}
@action
handleDestroyTrigger() {
this.triggerElement = null;
}
@action
handleInsertContent(element) {
this.contentElement = element;
}
@action
handleDestroyContent() {
this.contentElement = null;
}
@action
handleMouseDownTrigger(event) {
if (event.button !== 0) {
return;
}
this.toggle();
}
@action
handleMouseDown(event) {
this.lastMouseDownElement = event.target;
}
@action
handleMouseUp(event) {
this.lastMouseDownElement = null;
if (this._isInside(event.target)) {
return;
}
this._handleClickOutside();
}
@action
handleFocusOut(event) {
const element = event.relatedTarget || this.lastMouseDownElement;
if (this._isInside(element)) {
return;
}
this._handleFocusLeave();
}
@action
handleKeyDownTrigger(event) {
if (event.key === 'Enter' || event.key === ' ') {
this._handleEnterOrSpace(event);
}
}
@action
handleKeyDown(event) {
if (event.key === 'Escape') {
this._handleEscape(event);
}
}
@action
open() {
if (!this.canOpen) {
return;
}
this._isOpen = true;
scheduleOnce('afterRender', this, '_handleOpened');
}
@action
close(reason) {
if (!this.canClose) {
return;
}
this._isOpen = false;
scheduleOnce('afterRender', this, '_handleClosed', reason);
}
@action
toggle() {
if (this.isOpen) {
this.close();
} else {
this.open();
}
}
_handleFocusLeave() {
this.close(FOCUS_LEAVE);
}
_handleClickOutside() {
this.close(CLICK_OUTSIDE);
}
_handleEscape(event) {
if (!this.canClose) {
return;
}
event.stopPropagation();
this.close(ESCAPE);
}
_handleEnterOrSpace(event) {
this.toggle();
}
_isInside(element) {
return (
element !== this.element &&
(this.element.contains(element) || this.contentElement?.contains(element))
);
}
_isInsideContent(element) {
return this.contentElement?.contains(element);
}
_handleOpened() {
this.args.onOpenClosure?.();
this.args.onOpen?.();
}
_handleClosed(reason) {
this.args.onCloseClosure?.(reason);
this.args.onClose?.(reason);
}
@cached
get _api() {
return {
Trigger: this.Trigger,
Content: this.Content,
element: this.element,
isOpen: this.isOpen,
open: this.open,
close: this.close,
toggle: this.toggle
};
}
api = new Proxy(this, {
get(target, key) {
return target._api[key];
},
set() {}
});
<template>
{{! template-lint-disable no-invalid-interactive no-pointer-down-event-binding }}
{{~this.registerComponents
(hash
Trigger=(component
DropdownTrigger
aria-expanded=this.isOpen
onMouseDown=this.handleMouseDownTrigger
onKeyDown=this.handleKeyDownTrigger
onInsert=this.handleInsertTrigger
onDestroy=this.handleDestroyTrigger
)
Content=(component
DropdownContent
onFocusOut=this.handleFocusOutContent
onMouseDown=this.handleMouseDownContent
onInsert=this.handleInsertContent
onDestroy=this.handleDestroyContent
)
)
~}}
<div
class={{concat "dropdown" (if @class (concat " " @class))}}
data-open="{{this.isOpen}}"
{{on "keydown" this.handleKeyDown}}
{{on "focusout" this.handleFocusOut}}
{{this.documentListeners}}
{{lifecycle
onInsert=this.handleInsertElement
onDestroy=this.handleDestroyElement
}}
...attributes
>
{{~yield this.api~}}
</div>
</template>
}