-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathCombobox.js
1061 lines (938 loc) · 33.2 KB
/
Combobox.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
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @module deliteful/Combobox */
import { html } from "lit-html";
import { ifDefined } from "lit-html/directives/if-defined";
import dcl from "dcl/dcl";
import has from "ibm-decor/sniff";
import register from "delite/register";
import HasDropDown from "delite/HasDropDown";
import StoreMap from "delite/StoreMap";
import messages from "requirejs-dplugins/i18n!./Combobox/nls/Combobox";
import "./Combobox/Combobox.css";
import string from "dojo/string";
import Filter from "dojo-dstore/Filter";
import LitFormValueWidget from "delite/LitFormValueWidget";
import CssState from "delite/CssState";
// Used in template.
import "./list/List";
const mobile = has("ios") || has("android");
// Counter used to generate unique ids for the dropdown items, so that aria-activedescendant is set to
// a reasonable value.
var idCounter = 1;
function getvalue (map, item, key, store) {
if (map[key + "Func"]) {
return map[key + "Func"](item, store);
} else if (map[key + "Attr"]) {
return item[map[key + "Attr"]];
} else {
return item[key];
}
}
/**
* A form-aware and store-aware multichannel widget leveraging the `deliteful/list/List`
* widget for rendering the options.
*
* The corresponding custom tag is `<d-combobox>`.
*
* The property `selectionMode` allows to choose between single and multiple
* choice modes.
*
* If the property `autoFilter` is set to `true` (default is `false`)
* the widget allows to type one or more characters which
* are used for filtering the shown list items. By default, the filtering is
* case-insensitive, and an item is shown if its label contains the entered
* string. The default filtering policy can be customized thanks to the
* `filterMode` and `ignoreCase` properties.
*
* The `value` property of the widget contains:
*
* - Single selection mode: the value of the selected list items. Defaults to `""`.
* - Multiple selection mode: an array containing the values of the selected items.
* Defaults to `[]`.
*
* If the widget is used in an HTML form, the submitted value contains:
*
* - Single selection mode: the same as widget's `value` property.
* - Multiple selection mode: a string containing a comma-separated list of the values
* of the selected items. Defaults to `""`.
*
* By default, the `label` field of the list render item is used as item value.
* A different field can be specified by using attribute mapping for `value`.
*
* Remark: the option items must be added, removed or updated exclusively using
* List's store API. Direct operations using the DOM API are not supported.
*
* @example <caption>Markup</caption>
* JS:
* require(["deliteful/Combobox", "requirejs-domready/domReady!"],
* function () {
* });
* HTML:
* <d-combobox id="combobox1">
* { "label": "France", "sales": 500, "profit": 50, "region": "EU" },
* { "label": "Germany", "sales": 450, "profit": 48, "region": "EU" },
* { "label": "UK", "sales": 700, "profit": 60, "region": "EU" },
* { "label": "USA", "sales": 2000, "profit": 250, "region": "America" },
* { "label": "Canada", "sales": 600, "profit": 30, "region": "America" },
* { "label": "Brazil", "sales": 450, "profit": 30, "region": "America" },
* { "label": "China", "sales": 500, "profit": 40, "region": "Asia" },
* { "label": "Japan", "sales": 900, "profit": 100, "region": "Asia" }
* </d-combobox>
*
* @example <caption>Programmatic</caption>
* JS:
* require(["deliteful/Combobox", ..., "requirejs-domready/domReady!"],
* function (Combobox, ...) {
* var dataStore = ...; // Create data store
* var combobox = new Combobox({source: dataStore, selectionMode: "multiple"}).
* placeAt(...);
* });
*
* @class module:deliteful/Combobox
*/
const supers = [ HTMLElement, LitFormValueWidget, CssState, HasDropDown ];
export default register("d-combobox", supers, /** @lends module:deliteful/Combobox# */ {
declaredClass: "deliteful/Combobox",
baseClass: "d-combobox",
/**
* Corresponds to the native HTML `<input>` element's attribute.
* @member {string}
*/
alt: "",
/**
* If `true`, the list of options can be filtered thanks to an editable
* input element.
* @member {boolean} module:deliteful/Combobox#autoFilter
* @default false
*/
autoFilter: false,
/**
* Default query to apply to the source. It can be a `function` or a `Object`.
* If it is a function, then it's invoked and the list's query will get the return value.
* If it is an Object, it's assigned to the list's query directly.
* It can be overridden depending of store used and the strategy to apply.
*/
defaultQuery: {},
/**
* Text displayed by the Combobox (as opposed to the internal value).
* @type {string}
*/
displayedValue: "",
/**
* Defines the milliseconds the widget has to wait until a new filter operation starts.
* @type {Number}
* @default 0
*/
filterDelay: 0,
/**
* The chosen filter mode. Only used if `autoFilter` is `true`.
*
* Valid values are:
*
* 1. "startsWith": the item matches if its label starts with the filter text.
* 2. "contains": the item matches if its label contains the filter text.
* 3. "is": the item matches if its label is the filter text.
*
* @member {string}
* @default "startsWith"
*/
filterMode: "startsWith",
/**
* Displays or not the down arrow button.
* @type {boolean}
* @default true
*/
hasDownArrow: true,
/**
* If `true`, the filtering of list items ignores case when matching possible items.
* Only used if `autoFilter` is `true`.
* @member {boolean}
* @default true
*/
ignoreCase: true,
/**
* Minimum number of characters before the dropdown automatically opens.
* However, aside the above, depending of its value, the widget behavior changes slightly.
* In fact:
* - if minFilterChars = 0
* -- show the dropdown on pointer down.
* -- show the dropdown even if the user clears the input field.
* - if minFilterChars >= 1
* -- do not show the dropdown on pointer down.
* -- clearing the input field will close the dropdown.
* @type {number}
* @default 1
*/
minFilterChars: 1,
/**
* For non-filterable Combobox with selectionMode=multiple,
* the text displayed in the input element when no option is selected.
* The default value is provided by the "multiple-choice-no-selection" key of
* the message bundle. This message can contains placeholders for the
* Combobox attributes to be replaced by their runtime value. For example, the
* message can include the number of selected items by using the
* placeholder `${items}`.
* @member {string}
* @default localized version of "{items} selected"
*/
multipleChoiceMsg: messages["multiple-choice"],
/**
* For non-filterable Combobox with selectionMode=multiple,
* the text displayed in the input element when no option is selected.
* The default value is provided by the "multiple-choice-no-selection" key of
* the message bundle.
* @member {string}
* @default "Select option(s)"
*/
multipleChoiceNoSelectionMsg: messages["multiple-choice-no-selection"],
/**
* The value of the placeholder attribute of the input element used
* for filtering the list of options. The default value is provided by the
* "search-placeholder" key of the message bundle.
* @member {string}
* @default "Search"
*/
placeholder: messages["search-placeholder"],
/**
* The chosen selection mode.
*
* Valid values are:
*
* 1. "single": Only one option can be selected at a time.
* 2. "multiple": Several options can be selected.
*
* The value of this property determines the value of the `selectionMode`
* property of the List instance used by this widget for displaying the options:
* * The value "single" is mapped to "radio".
* * The value "multiple" is mapped to "multiple".
*
* Note that, regardless of the selection mode, it is always possible to set
* several selected items using the `selectedItem` or `selectedItems` properties
* of the List instance.
* The mode will be enforced only when using `setSelected()` and/or
* `selectFromEvent()` APIs of the List.
*
* @member {string}
* @default "single"
*/
selectionMode: "single",
/**
* Source for the inner list.
* @type {dstore/Store|Array} Source set.
*/
source: null,
/**
* The order in which fields are traversed when user presses the tab key.
* @member {number}
* @default 0
*/
tabIndex: 0,
/**
* Mapping between the attribute of the item retrieved from the store
* and the label attribute expected by the default renderer
* @member {string}
* @default "label"
*/
labelAttr: "label",
/**
* Mapping between the attribute of the item retrieved from the store
* and the icon attribute expected by the default renderer
* @member {string}
* @default "iconclass"
*/
iconclassAttr: "iconclass",
/**
* Mapping between the attribute of the item retrieved from the store
* and the righttext attribute expected by the default renderer
* @member {string}
* @default "righttext"
*/
righttextAttr: "righttext",
/**
* Mapping between the attribute of the item retrieved from the store
* and the righticon attribute expected by the default renderer
* @member {string}
* @default "righticonclass"
*/
righticonclassAttr: "righticonclass",
/**
* Name of the list item attribute that define the category of a list item.
* If falsy and categoryFunc is also falsy, the list is not categorized.
* @member {string}
* @default ""
*/
categoryAttr: "",
/**
* A function (or the name of a function) that extracts the category of a list item
* from the following input parameters:
* - `item`: the list item from the store
* - `store`: the store
* If falsy and `categoryAttr` is also falsy, the list is not categorized.
* see {@link module:delite/StoreMap delite/StoreMap}
* @member
* @default null
*/
categoryFunc: null,
/**
* True iff the `<input>`'s value was set by user typing.
* We only filter the dropdown list when the value was set by the user typing into the `<input>`,
* and specifically avoid filtering the list to a single item when the user selects an item from
* list and then reopens the dropdown.
*/
_valueSetByUserInput: false,
/**
* Corresponds to the native HTML `<input>` element's attribute.
* @member {string}
*/
value: dcl.prop({
set: function (val) {
if (val !== this.value) {
this._set("value", val);
this._valueSetByUserInput = false;
}
},
get: function () {
return this._get("value") || "";
},
enumerable: true,
configurable: true
}),
// Node holding user input or currently selected value.
focusNode: dcl.prop({
get: function () {
return this.querySelector("input");
}
}),
// Hidden node used for form submission.
valueNode: dcl.prop({
get: function () {
return this.querySelector("input[hidden]");
}
}),
// Node to set aria-controls etc. attributes on.
popupStateNode: dcl.prop({
get: function () {
return this.querySelector("[role=combobox]");
}
}),
// Tell HasDropDown which button opens the dropdown.
buttonNode: dcl.prop({
get: function () {
return this.querySelector(".d-combobox-arrow");
}
}),
// Used to set list's source and query. Eventually this should be done from the template.
list: dcl.prop({
get: function () {
return this.querySelector("d-list");
}
}),
/**
* Render the Combobox, including the dropdown list.
* @returns {TemplateResult}
*/
render: function () {
return html`
${this.renderAnchor()}
${this.renderList()}
`;
},
/**
* Render the the <input> with the button to open the dropdown.
* @returns {TemplateResult}
*/
renderAnchor: function () {
const listbox = this.list && this.list.querySelector("[role=listbox]");
// The arrow is normally invisible to screen readers, but for mobile the user needs to be able to navigate
// to it, as they have no other way to open the dropdown without typing in a search string.
const arrow = this.hasDownArrow ?
(mobile ? html`<span class="d-combobox-arrow" role="button" aria-label="${messages.toggle}"></span>` :
html`<span class="d-combobox-arrow" aria-hidden="true"></span>`) : null;
return html`
<div class="d-combobox-anchor">
<input
id="${this.id || this.widgetId}-input"
class="d-combobox-input"
autocomplete="off"
autocorrect="off"
autocapitalize="none"
aria-autocomplete="list"
aria-controls="${ifDefined(listbox && listbox.id)}"
aria-expanded="${this.opened}"
disabled="${ifDefined(this.disabled ? "true" : undefined)}"
placeholder="${ifDefined(this.placeholder || undefined)}"
readonly="${ifDefined(this._inputReadOnly ? "true" : undefined)}"
role="combobox"
tabindex="${this.tabIndex}"
type="text"
.value="${this.displayedValue}"
@keydown="${this.inputKeydownHandler.bind(this)}"
@input="${this.inputInputHandler.bind(this)}"
@change="${this.inputChangeHandler.bind(this)}"
@click="${this.inputClickHandler.bind(this)}">
<input hidden name="${this.name}" value="${this.value}"
disabled="${ifDefined(this.disabled ? "true" : undefined)}">
${arrow}
</div>
`;
},
/**
* Render an empty list as the dropdown. Other code will set the list's source and query.
*/
renderList: function () {
return html`
<d-list
.ariaLabel="${this.getLabel()}"
.categoryAttr="${this.categoryAttr}"
.categoryFunc="${this.categoryFunc}"
class="d-popup d-combobox-list"
id="${this.id || this.widgetId}-list"
.focusDescendants="${false}"
.itemToRenderItem="${this.itemToRenderItem.bind(this)}"
.selectionMode="${this.selectionMode === "single" ? "radio" : "multiple"}"
.showNoItems="${true}"
style="display: none"
type="listbox"
@keynav-child-navigated="${this.listKeynavChildNavigatedHandler.bind(this)}"
@click="${this.listClickHandler.bind(this)}"
@execute="${this.listExecuteHandler.bind(this)}"
@selection-change="${this.listSelectionChangeHandler.bind(this)}"
@query-success="${this.listQuerySuccessHandler.bind(this)}"
></d-list>
`;
},
afterFormResetCallback: function () {
if (this.value !== this.valueNode.value) {
if (this.selectionMode === "single") {
this.value = this.valueNode.value || "";
} else if (this.selectionMode === "multiple") {
this.value = this.valueNode.value ? this.valueNode.value.split(",") : [];
}
}
},
beforeInitializeRendering: function () {
// If the control seems to contain JSON, then parse it as our data source.
if (!this.firstElementChild && this.textContent.trim()) {
var data = JSON.parse("[" + this.textContent + "]");
if (data.length) {
this.source = data;
for (var j = 0; j < data.length; j++) {
if (!data[j].id) {
data[j].id = Math.random();
}
}
}
this.textContent = "";
}
},
parseAttribute: dcl.superCall(function (sup) {
return function (name, value) {
var capitalize = /f(?=unc$)|a(?=ttr$)/;
if (/Attr$|Func$/i.test(name)) {
name = name.toLowerCase(); // needed only on IE9
name = this._propCaseMap[name] ||
name.replace(capitalize, capitalize.exec(name)[0].toUpperCase());
return {
prop: name,
value: /Attr$/.test(name) ? value :
this.parseFunctionAttribute(value, [ "item", "store", "value" ])
};
} else {
return sup.apply(this, arguments);
}
};
}),
connectedCallback: function () {
// Preparation for itemToRenderItem() to work.
StoreMap.prototype.introspectMappedProperties.call(this);
},
// Flag used for binding the readonly attribute of the input element in the template
_inputReadOnly: true,
// Set aria-hasdropdown=listbox rather than aria-hasdropdown=menu.
dropDownType: "listbox",
// Leave focus in the original <input>.
focusOnPointerOpen: false,
focusOnKeyboardOpen: false,
/**
* Handle clicks on the `<input>`.
* Note that HasDropDown handles clicks on the arrow icon.
*/
inputClickHandler: function (event) {
event.stopPropagation();
event.preventDefault();
if (this.disabled) {
return;
}
if (!this.minFilterChars || this._inputReadOnly) {
this.toggleDropDown();
}
},
computeProperties: function (oldValues) {
this._inputReadOnly = this.readOnly || !this.autoFilter;
// If value was specified as a string (like during creation from markup),
// but selectionMode === multiple, need to convert it to an array.
if (this.selectionMode === "multiple" && typeof this.value === "string") {
this.value = this.value ? this.value.split(",") : [];
// So computeProperties doesn't get called again and oldValues contains "value"
// but not "displayedValue", which would trigger code below to run.
this.discardComputing();
}
// Set this.displayedValue based on this.value.
if ("value" in oldValues && this.selectionMode === "multiple" && !this.autoFilter) {
if (this.value.length === 0) {
this.displayedValue = this.multipleChoiceNoSelectionMsg;
} else if (this.value.length > 1) {
this.displayedValue = string.substitute(this.multipleChoiceMsg, { items: this.value.length });
}
}
},
refreshRendering: function (oldValues) {
if ("value" in oldValues) {
// Update which list item(s) are selected.
this._setSelectedItems();
}
},
dropDownPosition: function () {
return [ "below", "above" ];
},
_dropDownClickHandler: dcl.superCall(function (sup) {
return function () {
sup.apply(this, arguments);
// Put focus on the <input>.
// Except, don't focus the <input> on mobile because we don't want the keyboard to pop up
// when the user just wants to select a value from the dropdown.
if (!mobile) {
this.defer(this.focus.bind(this));
}
};
}),
// HasDropDown#_dropDownKeyUpHandler() override.
// Do not call openDropDown if widget does not have a down arrow shown (auto-complete mode).
// In this mode the popup will open when the user typed something and text.length > this.minFilterChars.
_dropDownKeyUpHandler: dcl.superCall(function (sup) {
return function () {
if (this.hasDownArrow) {
sup.apply(this, arguments);
}
};
}),
/**
* Opens or closes the dropdown as appropriate.
*/
_showOrHideList: function (suppressChangeEvent) {
// Compute whether or not to show the list.
var inputElement = this.focusNode;
var showList = inputElement.value.length >= this.minFilterChars;
if (showList) {
this.openDropDown();
} else {
this.closeDropDown(true /*refocus*/, suppressChangeEvent);
}
},
openDropDown: dcl.superCall(function (sup) {
return function () {
// Adjust the dropdown contents to be filtered by the current value of the <input>.
this.filter(this.focusNode.value);
var promise = sup.apply(this, arguments);
return promise.then(function () {
// Remove aria-owns set by HasDropDown. We set aria-controls in template instead.
this.focusNode.removeAttribute("aria-owns");
this._updateScroll(undefined, true); // sets this.list.navigatedDescendant
this._setActiveDescendant(this.list.navigatedDescendant);
}.bind(this));
};
}),
closeDropDown: dcl.superCall(function (sup) {
return function (focus, suppressChangeEvent) {
this.focusNode.removeAttribute("aria-activedescendant");
// Closing the dropdown represents a commit interaction, unless the dropdown closes
// automatically because the user backspaced, in which case suppressChangeEvent is true.
if (!suppressChangeEvent) {
this.handleOnChange(this.value); // emit "change" event
}
sup.apply(this, arguments);
// For multi-select with auto-filter, the <input> never shows what the selected value is.
// To avoid confusion, clear the filter string the user typed.
if (this.selectionMode === "multiple" && this.autoFilter) {
this.displayedValue = "";
}
};
}),
/**
* Search for label of Combobox, so it can be applied to dropdown too.
* @returns {any}
*/
getLabel: function () {
var inputId = (this.id || this.widgetId) + "-input";
var labelNode = (inputId &&
this.ownerDocument.querySelector("label[for=" + inputId + "]")) ||
(this.hasAttribute("aria-labelledby") &&
this.ownerDocument.getElementById(this.getAttribute("aria-labelledby")));
return labelNode ? labelNode.textContent.trim() : (this.getAttribute("aria-label") || "");
},
loadDropDown: function () {
var dropDown = this.list;
this.dropDown = dropDown; // delite/HasDropDown's property
return dropDown;
},
// Override KeyNav#focusDescendants to not give list items focus when navigating,
// since that would blur the input field used for entering the filtering criteria.
focusDescendants: false,
focus: function () {
this.focusNode.focus();
// Set flag used in "input' handler.
this._justFocused = true;
this.defer(function () {
this._justFocused = false;
});
},
listKeynavChildNavigatedHandler: function (evt) {
var navigatedChild = evt.newValue; // never null
var rend = this.list.getEnclosingRenderer(navigatedChild);
var item = rend.item;
if (this.selectionMode === "single" && item && !this.list.isSelected(item)) {
this.list.selectFromEvent(evt, item, rend, true);
} // else do not change the selection state of an item already selected
if (evt.triggerEvent && // only for keyboard navigation
(evt.triggerEvent.type === "keydown" || evt.triggerEvent.type === "keypress")) {
this._updateScroll(item, true);
}
this._setActiveDescendant(navigatedChild);
},
listClickHandler: function (evt) {
if (this.selectionMode === "single") {
var rend = this.list.getEnclosingRenderer(evt.target);
if (rend && this.isItemRenderer(rend)) {
evt.preventDefault();
evt.stopPropagation();
this.defer(function () {
// deferred such that the user can see the selection feedback
// before the dropdown closes.
this.handleOnChange(this.value);
this.list.emit("execute");
}.bind(this), 100); // worth exposing a property for the delay?
}
}
},
listExecuteHandler: function (evt) {
// Don't let event bubble to a TooltipDialog containing the Combobox.
evt.stopPropagation();
},
listSelectionChangeHandler: function (evt) {
this._validateInput(evt);
this.handleOnInput(this.value); // emit "input" event
},
listQuerySuccessHandler: function () {
this.list.deliver();
this._setSelectedItems();
},
/**
* Override hook for subclasses that have drill down items, etc.
* Return true for "leaf node" renderers (i.e. renderers where
* clicking them should close the dropdown).
* @param renderer
*/
isItemRenderer: function (renderer) {
return renderer.getAttribute("role") === "option";
},
/**
* Returns the label of a List item renderer.
* @private
*/
_getItemRendererLabel: function (itemRenderer) {
return this._getItemLabel(itemRenderer.item);
},
/**
* Returns the value of a List item renderer. Defaults to its label
* if the underlying data item has no value.
* @private
*/
_getItemRendererValue: function (itemRenderer) {
return this._getItemValue(itemRenderer.item);
},
/**
* Returns the label of a List render item.
* @private
*/
_getItemLabel: function (item) {
return item.label;
},
/**
* Returns the value of a List render item. Defaults to its label
* if the underlying data item has no value.
* @private
*/
_getItemValue: function (item) {
return "value" in item ? item.value : item.label;
},
_setActiveDescendant: function (nd) {
if (nd) {
if (!nd.id) {
nd.id = "d-combobox-item-" + idCounter++;
}
this.focusNode.setAttribute("aria-activedescendant", nd.id);
}
},
/**
* Called when there's a "keydown" event on the <input>.
* @param evt
*/
inputKeydownHandler: function (evt) {
// deliteful issue #382: prevent the browser from navigating to
// the previous page when typing backspace in a readonly input
if (this.focusNode.readOnly && evt.key === "Backspace") {
evt.stopPropagation();
evt.preventDefault();
} else if (evt.key === "Enter" || (evt.key === "Spacebar" && !this.autoFilter)) {
evt.stopPropagation();
evt.preventDefault();
// Delegate handling to the list. This allows subclasses to implement hierarchical menus etc.
var activeDescendant = this.opened && this.list.querySelector(".d-active-descendant");
if (activeDescendant) {
activeDescendant.click();
}
} else if (evt.key === "ArrowDown" || evt.key === "ArrowUp" ||
evt.key === "PageDown" || evt.key === "PageUp" ||
evt.key === "Home" || evt.key === "End") {
evt.stopPropagation();
evt.preventDefault();
}
},
/**
* Called when there's an "input" event on the <input>.
* @param evt
*/
inputInputHandler: function (evt) {
// Stop the spurious "input" events emitted while the user types
// such that only the "input" events emitted via LitFormValueWidget.handleOnInput()
// bubble to widget's root node.
evt.stopPropagation();
evt.preventDefault();
if (this._justFocused) {
// Ignore spurious "input" event on IE when focusing an <input> with a placeholder.
return;
}
// save what user typed at each keystroke.
this.displayedValue = this.focusNode.value;
// TODO
// Would be nice to also have an "incrementalFilter" boolean property.
// On desktop, this would allow to redo the filtering only for "change"
// events, triggered when pressing ENTER. This would also fit for Chrome/Android,
// where pressing the search key of the virtual keyboard also triggers a
// change event. But there's no equivalent on Safari / iOS...
if (this.selectionMode === "single") {
// Clear value. No value until user selects something from dropdown again.
this.value = "";
if (this.list.selectedItems.length > 0) {
this.list.selectedItems = [];
}
this.handleOnInput(this.value); // if we just cleared this.value then emit "input" event
}
// Must set this for dropdown list to be filtered by value in <input>.
// Furthermore, it must be done after the code above that clears this.vale.
this._valueSetByUserInput = true;
if (this._timeoutHandle !== undefined) {
this._timeoutHandle.remove();
delete this._timeoutHandle;
}
this._timeoutHandle = this.defer(function () {
// Note: set suppressChangeEvent=true because we shouldn't get a change event because
// the dropdown closed just because the user backspaced while typing in the <input>.
this._showOrHideList(true);
}.bind(this), this.filterDelay);
},
/**
* Called when there's a "change" event on the <input>.
* @param evt
*/
inputChangeHandler: function (evt) {
// Stop the spurious "change" events emitted while the user types
// such that only the "change" events emitted via FormValueWidget.handleOnChange()
// bubble to widget's root node.
evt.stopPropagation();
evt.preventDefault();
},
_validateInput: function (evt) {
if (this.selectionMode === "single") {
this._validateSingle();
} else {
this._validateMultiple(evt);
}
},
/**
* Called for single-select when the user has selected an option in the dropdown.
* @private
*/
_validateSingle: function () {
var selectedItem = this.list.selectedItem;
// selectedItem non-null because List in radio selection mode, but
// the List can be empty, so:
this.displayedValue = selectedItem ? this._getItemLabel(selectedItem) : "";
this.value = selectedItem ? this._getItemValue(selectedItem) : "";
},
/**
* Called for multi-select when the user has toggled an option in the dropdown.
* @private
*/
_validateMultiple: function (evt) {
// If the list is filtered, this.list.selectedItems is a subset of all the selected items.
// Therefore, need to operate based on which option was toggled.
// Update this.value, intentionally creating a new array rather than modifying the old one.
const toggledValue = this._getItemValue(evt.renderer.item);
if (this.value.includes(toggledValue)) {
this.value = this.value.filter(value => value !== toggledValue);
} else {
this.value = [...this.value, toggledValue];
}
// Set the displayed value to represent the selection, unless filtering is enabled,
// in which case we shouldn't overwrite the user's filter, so the user can do more filtering.
if (!this.autoFilter) {
const selectedItems = this.list.selectedItems;
const n = selectedItems.length;
this.displayedValue = n === 0 ? this.multipleChoiceNoSelectionMsg :
n === 1 ? this._getItemLabel(selectedItems[0]) :
string.substitute(this.multipleChoiceMsg, { items: n });
}
// Make sure valueNode.value is set when LitFormValueWidget.handleOnInput() runs.
if (this.valueNode) {
this.valueNode.value = this.value.toString();
}
this.handleOnInput(this.value); // emit "input" event
},
/**
* Filters the embedded List to only show the items matching `filterTxt`.
* If `autoFilter` is `true` and `selectionMode` is `"single"`, the method
* is called automatically while the user types into the editable input
* element, with `filterTxt` being the currently entered text.
* @protected
*/
filter: function (inputText) {
if (!this.autoFilter || inputText.length === 0 || !this._valueSetByUserInput) {
// Display the full list.
this.list.query = this._getDefaultQuery();
} else {
// Display the list filtered by what user typed into <input>.
// Escape special chars in search string, see
// http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex.
var filterTxt = inputText.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
if (this.filterMode === "startsWith") {
filterTxt = "^" + filterTxt;
} else if (this.filterMode === "is") {
filterTxt = "^" + filterTxt + "$";
} // nothing to add for "contains"
var rexExp = new RegExp(filterTxt, this.ignoreCase ? "i" : "");
this.list.query = this.getQuery({
rexExp: rexExp,
inputText: inputText
});
}
if (this.source) {
this.list.source = this.source;
}
},
/**
* Gets the list's query.
* This method can be overridden when using other store types.
* The default implementation uses `dstore/Filter.match()`.
* The matching is performed against the `list.labelAttr` or `list.labelFunc` attributes of
* the data store items.
* The method can be overridden for implementing other filtering strategies.
* @protected
* @param {Object.<string, Object>} args Dictionary containing by default
* the regular expression and the original input text.
* @returns {Object} New query to set to the list.
*/
getQuery: function (args) {
return (new Filter()).match(this.labelAttr || this.labelFunc, args.rexExp);
},
_getDefaultQuery: function () {
return (typeof this.defaultQuery === "function") ?
this.defaultQuery() : this.defaultQuery;
},
/**
* Mark which item(s) in the dropdown list are selected.
* @private
*/
_setSelectedItems: function () {
if (this.list && this.list.source && this.list.renderItems) {
var selectedItems = [],
presetItems = Array.isArray(this.value) && this.value.length >= 1 ? this.value : [ this.value ];
selectedItems = this.list.renderItems.filter(function (renderItem) {
return presetItems.indexOf(this._getItemValue(renderItem)) >= 0;
}.bind(this));
this.list.selectedItems = selectedItems;
}
},
/**
* Scrolls the list to either:
*
* 1) the specified item
* 2) the first selected item
* 3) the first item
*
* If `navigate` is true, navigates to the item that it scrolls to.
*
* Note: Since List is in focus-less mode, it does not give focus to
* navigated items, thus the browser does not autoscroll.
* TODO: see deliteful #498
* @private
*/
_updateScroll: function (item, navigate) {
if (!item) {
const selectedItems = this.list.selectedItems;
item = selectedItems && selectedItems[0];
}
if (this.selectionMode === "single") {
if (item) {
const renderer = this.getRendererByItemId(this.list.getIdentity(item));