-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.gjs
686 lines (556 loc) · 14.5 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/* eslint-disable ember/no-runloop */
import { action } from '@ember/object';
import { cached } from '@glimmer/tracking';
import { filter } from '@zestia/ember-select-box/utils';
import { hash } from '@ember/helper';
import { localCopy } from 'tracked-toolbox';
import { makeArray } from '@ember/array';
import { on } from '@ember/modifier';
import { scheduleOnce } from '@ember/runloop';
import {
startsWithString,
pressingModifier
} from '@zestia/ember-select-box/-private/utils';
import { task } from 'ember-concurrency';
import { tracked } from 'tracked-built-ins';
import Component from '@glimmer/component';
import lifecycle from '@zestia/ember-select-box/modifiers/lifecycle';
import Dropdown from '@zestia/ember-select-box/components/dropdown/index';
import SelectBoxGroup from '@zestia/ember-select-box/components/select-box/group';
import SelectBoxInput from '@zestia/ember-select-box/components/select-box/input';
import SelectBoxOption from '@zestia/ember-select-box/components/select-box/option';
import SelectBoxOptions from '@zestia/ember-select-box/components/select-box/options';
const { assign } = Object;
export default class SelectBox extends Component {
@tracked _activeOption;
@tracked _options = tracked([]);
@tracked dropdown;
@tracked element;
@tracked inputElement;
@tracked optionsElement;
@tracked query = null;
@tracked triggerElement;
@localCopy('args.value') _value;
@localCopy('args.options') results;
chars = '';
charTimer;
Dropdown;
Group;
Input;
Option;
Options;
Trigger;
registerComponents = (components) => {
assign(this, components);
};
get value() {
return this.isMultiple ? makeArray(this._value) : this._value;
}
get isDisabled() {
return this.args.disabled;
}
get isMultiple() {
return this.args.multiple;
}
get isSingle() {
return !this.isMultiple;
}
get isComboBox() {
return this.hasOptions && (this.hasInput || this.hasTrigger);
}
get isListBox() {
return this.hasOptions && !(this.hasInput || this.hasTrigger);
}
get canAutoOpen() {
return this.hasTrigger && !this.dropdown.isOpen;
}
get canAutoClose() {
return this.isSingle && this.hasTrigger && this.dropdown.isOpen;
}
get canAutoSelect() {
return this.isSingle && this.hasTrigger && !this.dropdown.isOpen;
}
get isBusy() {
return this.hasSearch ? this.searchTask.isRunning : null;
}
get isTyping() {
return this.chars.trim() !== '';
}
get hasOptions() {
return !!this.optionsElement;
}
get hasTrigger() {
return !!this.triggerElement;
}
get hasInput() {
return !!this.inputElement;
}
get hasSearch() {
return typeof this.args.onSearch === 'function';
}
get optionsTabIndex() {
return this.isListBox ? '0' : null;
}
get dropdownTabIndex() {
return this.dropdown ? '-1' : null;
}
get triggerTabIndex() {
return this.isDisabled || this.hasInput ? '-1' : '0';
}
get triggerRole() {
return this.hasInput ? 'button' : 'combobox';
}
get triggerActiveDescendant() {
return this.hasInput ? null : this.activeOption?.element?.id;
}
@cached
get activeOption() {
return this.specificActiveOption || this.optionForValue;
}
get specificActiveOption() {
return this.options.includes(this._activeOption)
? this._activeOption
: null;
}
get optionForValue() {
return this.options.find((option) => option.args.value === this.value);
}
get activeOptionIndex() {
return this.activeOption ? this.activeOption.index : -1;
}
get previousOption() {
return this.options[this.activeOptionIndex - 1];
}
get nextOption() {
return this.options[this.activeOptionIndex + 1];
}
get optionElements() {
return [...this.element.querySelectorAll('.select-box__option')];
}
get interactiveElement() {
if (this.hasInput) {
return this.inputElement;
}
if (this.hasTrigger) {
return this.triggerElement;
}
return this.optionsElement;
}
get hasFocus() {
return this.interactiveElement === document.activeElement;
}
@cached
get options() {
if (!this.element) {
return [];
}
const els = this.optionElements;
return this._options
.filter((option) => !option.isDisabled)
.sort((a, b) => els.indexOf(a.element) - els.indexOf(b.element));
}
@action
handleInsertElement(element) {
this.element = element;
this.args.onReady?.(this.api);
}
@action
handleDestroyElement() {
this.element = null;
}
@action
handleInsertOption(option) {
this._options.push(option);
}
@action
handleDestroyOption(option) {
// https://github.com/tracked-tools/tracked-built-ins/issues/405
// https://stackoverflow.com/questions/30304719/javascript-fastest-way-to-remove-object-from-array
// this._options.splice(this._options.indexOf(option), 1);
const index = this._options.indexOf(option);
this._options[index] = this._options[this._options.length - 1];
this._options.pop();
}
@action
handleInsertOptions(element) {
this.optionsElement = element;
}
@action
handleDestroyOptions() {
this.optionsElement = null;
}
@action
handleInsertTrigger(element) {
this.triggerElement = element;
}
@action
handleDestroyTrigger() {
this.triggerElement = null;
}
@action
handleInsertInput(element) {
this.inputElement = element;
}
@action
handleDestroyInput() {
this.inputElement = null;
}
@action
registerDropdown(dropdown) {
this.dropdown = dropdown;
}
@action
handleDestroyDropdown() {
this.dropdown = null;
}
@action
handleInput() {
this._search(this.inputElement.value);
}
@action
handleMouseLeave() {
if (this.hasFocus) {
return;
}
this._forgetActiveOption();
}
@action
handleMouseDown(event) {
event.preventDefault();
this._ensureFocus();
}
@action
handleFocusOut() {
if (this.dropdown) {
return;
}
this._forgetActiveOption();
}
@action
handleKeyDownTrigger(event) {
this._handleKeyDown(event);
this._handleInputChar(event);
}
@action
handleKeyDownInput(event) {
this._handleKeyDown(event);
}
@action
handleKeyDownOptions(event) {
if (this.isComboBox) {
return;
}
this._handleKeyDown(event);
this._handleInputChar(event);
}
@action
handleMouseEnterOption(option) {
this._activateOption(option);
}
@action
handleMouseUpOption(option, event) {
if (event.button !== 0) {
return;
}
this._activateOption(option);
this._selectOption(option);
}
@action
handleFocusInOption(option) {
this._activateOption(option);
}
@action
handleKeyDownOption(event) {
this._handleKeyDown(event);
}
@action
handleOpenDropdown() {
this.activeOption?.scrollIntoView();
this._ensureFocus();
}
@action
handleCloseDropdown(reason) {
this._forgetActiveOption();
if (reason?.description !== 'FOCUS_LEAVE') {
this._ensureFocus();
}
}
@action
search(query) {
return this._search(query);
}
@action
update(value) {
this._setValue(value);
}
@action
select(value) {
this._selectValue(value);
}
_handleKeyDown(event) {
switch (event.key) {
case 'ArrowUp':
this._handleArrowUp(event);
break;
case 'ArrowDown':
this._handleArrowDown(event);
break;
case 'Enter':
this._handleEnter(event);
break;
case ' ':
this._handleSpace(event);
break;
}
}
_handleArrowUp(event) {
event.preventDefault();
if (this.canAutoOpen) {
this.dropdown.open();
return;
}
this._activateOption(this.previousOption, true);
}
_handleArrowDown(event) {
event.preventDefault();
if (this.canAutoOpen) {
this.dropdown.open();
return;
}
this._activateOption(this.nextOption, true);
}
_handleEnter(event) {
if (this.isComboBox) {
event.preventDefault();
}
this._handleEnterOrSpace();
}
_handleSpace(event) {
if (event.target === this.inputElement) {
return;
}
event.preventDefault();
if (this.isTyping) {
return;
}
this._handleEnterOrSpace();
}
_handleEnterOrSpace(event) {
if (this.canAutoOpen) {
this.dropdown.open();
return;
}
this._selectActiveOption();
}
_handleSelected() {
const close = this.args.onSelect?.(this.api) ?? this.canAutoClose;
if (close) {
this.dropdown.close();
}
}
_handleSearched() {
this.activeOption?.scrollIntoView();
}
_handleInputChar(event) {
const { key: char } = event;
if (char.length > 1 || pressingModifier(event)) {
return;
}
clearTimeout(this.charTimer);
this.chars = this.chars.concat(char);
this.charTimer = setTimeout(() => (this.chars = ''), 1000);
let option = this.activeOption;
const repeating = this.chars.split('').every((c) => c === char);
const string = repeating ? char : this.chars;
const offset = repeating ? 1 : 0;
const index = (option ? option.index : -1) + offset;
const before = this.options.slice(0, index);
const after = this.options.slice(index);
const options = after.concat(before);
[option] = this._getOptionsByTextContent(options, string);
this._activateOption(option, true);
if (this.canAutoSelect) {
this._selectActiveOption();
}
}
_forgetActiveOption() {
this._activeOption = null;
}
_activateOption(option, scrollIntoView = false) {
if (!option || option.isDisabled || option.isActive) {
return;
}
this._activeOption = option;
if (scrollIntoView) {
this._activeOption.scrollIntoView();
}
this.args.onActivate?.(option.args.value, this.api);
}
_selectActiveOption() {
this._selectOption(this.activeOption);
}
_selectOption(option) {
if (!option || option.isDisabled) {
return;
}
const value = this._buildSelection(option.args.value);
this._selectValue(value);
}
_setValue(value) {
this._value = value;
}
_valueChanged(value) {
return this.value !== value;
}
_selectValue(value) {
if (this._valueChanged(value)) {
this._setValue(value);
this.args.onChange?.(this.value, this.api);
}
this._handleSelected();
}
_ensureFocus() {
this.interactiveElement?.focus({ focusVisible: false });
}
_getOptionsByTextContent(options, query) {
return filter(options)
.by((option) => option.element.textContent)
.query(query)
.using(startsWithString)
.run();
}
_buildSelection(value) {
const build =
this.args.onBuildSelection ?? this._defaultBuildSelection.bind(this);
return build(value, this.value);
}
_defaultBuildSelection(newValue, oldValue) {
let value = newValue;
if (this.isMultiple) {
const temp = [...oldValue];
if (temp.includes(newValue)) {
temp.splice(temp.indexOf(newValue), 1);
} else {
temp.push(newValue);
}
value = temp;
}
return value;
}
_search(query) {
this.searchTask?.cancelAll();
return this.searchTask.perform(query);
}
searchTask = task(async (query) => {
const string = query ?? '';
const search = this.args.onSearch ?? this._defaultSearch.bind(this);
this.results = await search(string, this.api);
this.query = string;
scheduleOnce('afterRender', this, '_handleSearched');
});
_defaultSearch(query) {
return filter(this.args.options).query(query).run();
}
get _api() {
return {
// Components
Dropdown: this.Dropdown,
Group: this.Group,
Input: this.Input,
Option: this.Option,
Options: this.Options,
Trigger: this.Trigger,
Content: this.Content,
// Properties
element: this.element,
isBusy: this.isBusy,
options: this.results,
query: this.query,
value: this.value,
dropdown: this.dropdown,
// Actions
search: this.search,
select: this.select,
update: this.update
};
}
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
Input=(component
SelectBoxInput
disabled=this.isDisabled
aria-busy=this.isBusy
aria-activedescendant=this.activeOption.element.id
aria-expanded=this.dropdown.isOpen
aria-controls=this.optionsElement.id
onInsert=this.handleInsertInput
onDestroy=this.handleDestroyInput
onInput=this.handleInput
onKeyDown=this.handleKeyDownInput
)
Group=SelectBoxGroup
Options=(component
SelectBoxOptions
aria-multiselectable=this.isMultiple
tabindex=this.optionsTabIndex
onInsert=this.handleInsertOptions
onDestroy=this.handleDestroyOptions
onKeyDown=this.handleKeyDownOptions
)
Option=(component
SelectBoxOption
selectBox=this
onInsert=this.handleInsertOption
onDestroy=this.handleDestroyOption
onMouseEnter=this.handleMouseEnterOption
onMouseUp=this.handleMouseUpOption
onKeyDown=this.handleKeyDownOption
onFocusIn=this.handleFocusInOption
)
Dropdown=(component
Dropdown
onReady=this.registerDropdown
onOpenClosure=this.handleOpenDropdown
onCloseClosure=this.handleCloseDropdown
onDestroy=this.handleDestroyDropdown
)
Trigger=(component
this.dropdown.Trigger
role=this.triggerRole
aria-busy=this.isBusy
aria-disabled=this.isDisabled
aria-activedescendant=this.triggerActiveDescendant
aria-controls=this.optionsElement.id
tabindex=this.triggerTabIndex
onInsertClosure=this.handleInsertTrigger
onDestroy=this.handleDestroyTrigger
onKeyDown=this.handleKeyDownTrigger
)
Content=(component this.dropdown.Content tabindex="-1")
)
~}}
<div
class="select-box"
data-busy="{{this.isBusy}}"
data-disabled="{{this.isDisabled}}"
{{on "mouseleave" this.handleMouseLeave}}
{{on "mousedown" this.handleMouseDown}}
{{on "focusout" this.handleFocusOut}}
{{lifecycle
onInsert=this.handleInsertElement
onDestroy=this.handleDestroyElement
}}
...attributes
>
{{~yield this.api~}}
</div>
</template>
}