-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathcombo-box.ts
1261 lines (1227 loc) · 55.9 KB
/
combo-box.ts
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
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path='../drop-down-list/drop-down-list-model.d.ts'/>
import { EventHandler, Property, Event, EmitType, addClass, Browser, KeyboardEventArgs, removeClass, detach } from '@syncfusion/ej2-base';
import { isNullOrUndefined, NotifyPropertyChanges, getValue, setValue } from '@syncfusion/ej2-base';
import { DropDownList, dropDownListClasses } from '../drop-down-list/drop-down-list';
import { FilteringEventArgs } from '../drop-down-base/drop-down-base';
import { FieldSettingsModel } from '../drop-down-base/drop-down-base-model';
import { ComboBoxModel } from '../combo-box/combo-box-model';
import { Search } from '../common/incremental-search';
import { createSpinner, showSpinner, hideSpinner } from '@syncfusion/ej2-popups';
import { Input, InputObject, FloatLabelType } from '@syncfusion/ej2-inputs';
import { DataManager, DataOptions, Predicate, Query } from '@syncfusion/ej2-data';
const SPINNER_CLASS: string = 'e-atc-spinner-icon';
dropDownListClasses.root = 'e-combobox';
const inputObject: InputObject = {
container: null,
buttons: []
};
/**
* The ComboBox component allows the user to type a value or choose an option from the list of predefined options.
* ```html
* <select id="list">
* <option value='1'>Badminton</option>
* <option value='2'>Basketball</option>
* <option value='3'>Cricket</option>
* <option value='4'>Football</option>
* <option value='5'>Tennis</option>
* </select>
* ```
* ```typescript
* let games:ComboBox = new ComboBox();
* games.appendTo("#list");
* ```
*/
@NotifyPropertyChanges
export class ComboBox extends DropDownList {
/**
* Specifies whether suggest a first matched item in input when searching. No action happens when no matches found.
*
* @default false
*/
@Property(false)
public autofill: boolean;
/**
* Specifies whether the component allows user defined value which does not exist in data source.
*
* @default true
*/
@Property(true)
public allowCustom: boolean;
/**
* Allows additional HTML attributes such as title, name, etc., and
* accepts n number of attributes in a key-value pair format.
*
* {% codeBlock src='combobox/htmlAttributes/index.md' %}{% endcodeBlock %}
*
* @default {}
* @deprecated
*/
@Property({})
public htmlAttributes: { [key: string]: string };
/**
* When allowFiltering is set to true, show the filter bar (search box) of the component.
* The filter action retrieves matched items through the `filtering` event based on
* the characters typed in the search TextBox.
* If no match is found, the value of the `noRecordsTemplate` property will be displayed.
*
* {% codeBlock src="combobox/allow-filtering-api/index.ts" %}{% endcodeBlock %}
*
* {% codeBlock src="combobox/allow-filtering-api/index.html" %}{% endcodeBlock %}
*
* @default false
* @deprecated
*/
@Property(false)
public allowFiltering: boolean;
/**
* Defines whether the popup opens in fullscreen mode on mobile devices when filtering is enabled. When set to false, the popup will display similarly on both mobile and desktop devices.
*
* @default true
*/
@Property(true)
public isDeviceFullScreen: boolean;
/**
* Accepts the external `Query`
* that execute along with [`data processing`](../../combo-box/data-binding).
*
* {% codeBlock src='combobox/query/index.md' %}{% endcodeBlock %}
*
* @default null
* @deprecated
*/
@Property(null)
public query: Query;
/**
* Gets or sets the index of the selected item in the component.
*
* {% codeBlock src="combobox/index-api/index.ts" %}{% endcodeBlock %}
*
* {% codeBlock src="combobox/index-api/index.html" %}{% endcodeBlock %}
*
* @default null
* @aspType double
* @deprecated
*/
@Property(null)
public index: number | null;
/**
* Specifies whether to show or hide the clear button.
* When the clear button is clicked, `value`, `text`, and `index` properties are reset to null.
*
* @default true
*/
@Property(true)
public showClearButton: boolean;
/**
* Enable or disable rendering component in right to left direction.
*
* @default false
* @deprecated
*/
@Property(false)
public enableRtl: boolean;
/**
* Triggers on set a
* [`custom value`](../../combo-box/getting-started#custom-values) to this component.
*
* @event customValueSpecifier
*/
@Event()
public customValueSpecifier: EmitType<CustomValueSpecifierEventArgs>;
/**
* Triggers on typing a character in the component.
* > For more details about the filtering refer to [`Filtering`](../../combo-box/filtering) documentation.
*
* @event filtering
*/
@Event()
public filtering: EmitType<FilteringEventArgs>;
/**
* Not applicable to this component.
*
* @default null
* @aspType string
* @private
*/
@Property(null)
public valueTemplate: string | Function;
/**
* Specifies whether to display the floating label above the input element.
* Possible values are:
* * Never: The label will never float in the input when the placeholder is available.
* * Always: The floating label will always float above the input.
* * Auto: The floating label will float above the input after focusing or entering a value in the input.
*
* {% codeBlock src="combobox/float-label-type-api/index.ts" %}{% endcodeBlock %}
*
* {% codeBlock src="combobox/float-label-type-api/index.html" %}{% endcodeBlock %}
*
* @default Syncfusion.EJ2.Inputs.FloatLabelType.Never
* @aspType Syncfusion.EJ2.Inputs.FloatLabelType
* @isEnumeration true
* @deprecated
*/
@Property('Never')
public floatLabelType: FloatLabelType;
/**
* Not applicable to this component.
*
* @default null
* @private
* @deprecated
*/
@Property(null)
public filterBarPlaceholder: string;
/**
* Sets CSS classes to the root element of the component that allows customization of appearance.
*
* @default null
* @deprecated
*/
@Property(null)
public cssClass: string;
/**
* Accepts the template design and assigns it to the header container of the popup list.
* > For more details about the available template options refer to [`Template`](../../drop-down-list/templates) documentation.
*
* @default null
* @aspType string
* @deprecated
*/
@Property(null)
public headerTemplate: string | Function;
/**
* Accepts the template design and assigns it to the footer container of the popup list.
* > For more details about the available template options refer to [`Template`](../../drop-down-list/templates) documentation.
*
* @default null
* @aspType string
* @deprecated
*/
@Property(null)
public footerTemplate: string | Function;
/**
* Specifies a short hint that describes the expected value of the DropDownList component.
*
* @default null
* @deprecated
*/
@Property(null)
public placeholder: string;
/**
* Specifies the width of the component. By default, the component width sets based on the width of
* its parent container. You can also set the width in pixel values.
*
* @default '100%'
* @aspType string
* @deprecated
*/
@Property('100%')
public width: string | number;
/**
* Specifies the height of the popup list.
* > For more details about the popup configuration refer to
* [`Popup Configuration`](../../drop-down-list/getting-started#configure-the-popup-list) documentation.
*
* @default '300px'
* @aspType string
* @deprecated
*/
@Property('300px')
public popupHeight: string | number;
/**
* Specifies the width of the popup list. By default, the popup width sets based on the width of
* the component.
* > For more details about the popup configuration refer to
* [`Popup Configuration`](../../drop-down-list/getting-started#configure-the-popup-list) documentation.
*
* @default '100%'
* @aspType string
* @deprecated
*/
@Property('100%')
public popupWidth: string | number;
/**
* When set to true, the user interactions on the component are disabled.
*
* @default false
* @deprecated
*/
@Property(false)
public readonly: boolean;
/**
* Gets or sets the display text of the selected item in the component.
*
* @default null
* @aspType string
* @deprecated
*/
@Property(null)
public text: string | null;
/**
* Gets or sets the value of the selected item in the component.
*
* @default null
* @isGenericType true
* @deprecated
*/
@Property(null)
public value: number | string | boolean | object | null;
/**
* Defines whether the object binding is allowed or not in the component.
*
* @default false
*/
@Property(false)
public allowObjectBinding: boolean;
/**
* *Constructor for creating the component
*
* @param {ComboBoxModel} options - Specifies the ComboBox model.
* @param {string | HTMLElement} element - Specifies the element to render as component.
* @private
*/
public constructor(options?: ComboBoxModel, element?: string | HTMLElement) {
super(options, element);
}
/**
* Initialize the event handler
*
* @private
* @returns {void}
*/
protected preRender(): void {
super.preRender();
}
protected getLocaleName(): string {
return 'combo-box';
}
protected wireEvent(): void {
if (this.getModuleName() === 'combobox') {
EventHandler.add(this.inputWrapper.buttons[0], 'mousedown', this.preventBlur, this);
EventHandler.add(this.inputWrapper.container, 'blur', this.onBlurHandler, this);
}
if (!isNullOrUndefined(this.inputWrapper.buttons[0])) {
EventHandler.add(this.inputWrapper.buttons[0], 'mousedown', this.dropDownClick, this);
}
EventHandler.add(this.inputElement, 'focus', this.targetFocus, this);
if (!this.readonly) {
EventHandler.add(this.inputElement, 'input', this.onInput, this);
EventHandler.add(this.inputElement, 'keyup', this.onFilterUp, this);
EventHandler.add(this.inputElement, 'keydown', this.onFilterDown, this);
EventHandler.add(this.inputElement, 'paste', this.pasteHandler, this);
EventHandler.add(<HTMLElement & Window><unknown>window, 'resize', this.windowResize, this);
}
this.bindCommonEvent();
}
private preventBlur(e: MouseEvent): void {
if ((!this.allowFiltering && document.activeElement !== this.inputElement &&
!document.activeElement.classList.contains(dropDownListClasses.input) && Browser.isDevice || !Browser.isDevice)) {
e.preventDefault();
}
}
protected onBlurHandler(e: MouseEvent): void {
const inputValue: string = this.inputElement && this.inputElement.value === '' ?
null : this.inputElement && this.inputElement.value;
if (!isNullOrUndefined(this.listData) && !isNullOrUndefined(inputValue) && inputValue !== this.text) {
this.customValue(e);
}
super.onBlurHandler(e);
}
protected targetElement(): HTMLElement | HTMLInputElement {
return this.inputElement;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected setOldText(text: string): void {
Input.setValue(this.text, this.inputElement, this.floatLabelType, this.showClearButton);
this.customValue();
this.removeSelection();
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected setOldValue(value: string | number | object): void {
if (this.allowCustom) {
this.selectedLI = this.getElementByValue(this.value) as HTMLElement;
this.valueMuteChange(this.value);
} else {
this.valueMuteChange(null);
}
this.removeSelection();
this.setHiddenValue();
}
private valueMuteChange(value: string | number | boolean | object): void {
value = this.allowObjectBinding && !isNullOrUndefined(value) ? getValue((this.fields.value) ? this.fields.value : '', value) : value;
const inputValue: string = isNullOrUndefined(value) ? null : value.toString();
Input.setValue(inputValue, this.inputElement, this.floatLabelType, this.showClearButton);
let changeData: { [key: string]: Object } = {};
if (this.allowObjectBinding) {
value = this.getDataByValue(value as string | number | boolean);
if (isNullOrUndefined(value)) {
const fields: FieldSettingsModel = this.fields;
let isvalidTextField: boolean = false;
let isValidValue: boolean = false;
if (this.allowObjectBinding) {
const keys: string[] = Object.keys(this.value);
keys.forEach((key: string) => {
if (key === fields.value) {
isValidValue = true;
return;
}
});
keys.forEach((key: string) => {
if (key === fields.text) {
isvalidTextField = true;
return;
}
});
}
changeData = {
text: isValidValue ? isvalidTextField ? getValue(fields.text, this.value) : getValue(fields.value, this.value) : null,
value: isValidValue ? this.value : null,
index: null
};
}
}
if (this.allowObjectBinding) {
this.setProperties(changeData, true);
}
else {
this.setProperties({ value: value, text: value ? value.toString() : value, index: null }, true);
}
this.activeIndex = this.index;
const fields: FieldSettingsModel = this.fields;
const dataItem: { [key: string]: string | Object } = {};
dataItem[fields.text] = isNullOrUndefined(value) ? null : value.toString();
dataItem[fields.value] = isNullOrUndefined(value) ? null : value.toString();
this.itemData = <{ [key: string]: Object }>dataItem;
this.item = null;
if ((!this.allowObjectBinding && (this.previousValue !== this.value)) ||
(this.allowObjectBinding && this.previousValue && this.value &&
!this.isObjectInArray(this.previousValue, [this.value]))) {
this.detachChangeEvent(null);
}
}
protected updateValues(): void {
if (this.fields.disabled) {
if (this.value != null) {
this.value = !this.isDisableItemValue(this.value) ? this.value : null;
}
if (this.text != null) {
this.text = !this.isDisabledItemByIndex(this.getIndexByValue(this.getValueByText(this.text))) ? this.text : null;
}
if (this.index != null) {
this.index = !this.isDisabledItemByIndex(this.index) ? this.index : null;
this.activeIndex = this.index;
}
}
if (!isNullOrUndefined(this.value)) {
const currentValue: string | number | boolean = this.allowObjectBinding && !isNullOrUndefined(this.value) ? getValue((this.fields.value) ? this.fields.value : '', this.value) : this.value;
const li: Element = this.getElementByValue(currentValue);
let doesItemExist: boolean = !isNullOrUndefined(li) ? true : false;
if (this.enableVirtualization && this.value) {
const fields: string = (this.fields.value) ? this.fields.value : '';
const currentValue: string | number | boolean = this.allowObjectBinding && !isNullOrUndefined(this.value) ? getValue((this.fields.value) ? this.fields.value : '', this.value) : this.value;
if (this.dataSource instanceof DataManager) {
const getItem: any = <{ [key: string]: Object }[] | string[] | number[] | boolean[]>new DataManager(
this.virtualGroupDataSource as DataOptions | JSON[]).executeLocal(new Query().where(new Predicate(fields, 'equal', currentValue)));
if (getItem && getItem.length > 0) {
this.itemData = getItem[0];
doesItemExist = true;
const dataItem: { [key: string]: string } = this.getItemData();
const value: string | number | boolean | Object = this.allowObjectBinding
? this.getDataByValue(dataItem.value)
: dataItem.value;
if ((this.value === dataItem.value && this.text !== dataItem.text)
|| (this.value !== dataItem.value && this.text === dataItem.text)) {
this.setProperties({ 'text': dataItem.text ? dataItem.text.toString() : dataItem.text, 'value': value });
}
}
}
else{
const getItem: any = <{ [key: string]: Object }[] | string[] | number[] | boolean[]>new DataManager(
this.dataSource as DataOptions | JSON[]).executeLocal(new Query().where(new Predicate(fields, 'equal', currentValue)));
if (getItem && getItem.length > 0) {
this.itemData = getItem[0];
doesItemExist = true;
const dataItem: { [key: string]: string } = this.getItemData();
const value: string | number | boolean | Object = this.allowObjectBinding
? this.getDataByValue(dataItem.value)
: dataItem.value;
if ((this.value === dataItem.value && this.text !== dataItem.text)
|| (this.value !== dataItem.value && this.text === dataItem.text)) {
this.setProperties({ 'text': dataItem.text ? dataItem.text.toString() : dataItem.text, 'value': value });
}
}
}
}
if (li) {
this.setSelection(li, null);
} else if (
(!this.enableVirtualization && this.allowCustom) ||
(this.allowCustom && this.enableVirtualization && !doesItemExist)
) {
this.valueMuteChange(this.value);
} else if (!this.enableVirtualization || (this.enableVirtualization && !doesItemExist)) {
this.valueMuteChange(null);
}
} else if (this.text && isNullOrUndefined(this.value)) {
const li: Element = this.getElementByText(this.text);
if (li) {
this.setSelection(li, null);
} else {
Input.setValue(this.text, this.inputElement, this.floatLabelType, this.showClearButton);
this.customValue();
}
} else {
this.setSelection(this.liCollections[this.activeIndex], null);
}
this.setHiddenValue();
Input.setValue(this.text, this.inputElement, this.floatLabelType, this.showClearButton);
}
protected updateIconState(): void {
if (this.showClearButton) {
if (this.inputElement && this.inputElement.value !== '' && !this.readonly) {
removeClass([this.inputWrapper.clearButton], dropDownListClasses.clearIconHide);
} else {
addClass([this.inputWrapper.clearButton], dropDownListClasses.clearIconHide);
}
}
}
protected getAriaAttributes(): { [key: string]: string } {
const ariaAttributes: { [key: string]: string } = {
'role': 'combobox',
'aria-autocomplete': 'both',
'aria-labelledby': this.hiddenElement.id,
'aria-expanded': 'false',
'aria-readonly': this.readonly ? this.readonly.toString() : 'false',
'autocomplete': 'off',
'autocapitalize': 'off',
'spellcheck': 'false'
};
return ariaAttributes;
}
protected searchLists(e: KeyboardEventArgs | MouseEvent): void {
this.isTyped = true;
if (this.isFiltering()) {
super.searchLists(e);
if (this.ulElement && this.filterInput.value.trim() === '') {
this.setHoverList(this.ulElement.querySelector('.' + dropDownListClasses.li));
}
} else {
if (this.ulElement && this.inputElement.value === '' && this.preventAutoFill) {
this.setHoverList(this.ulElement.querySelector('.' + dropDownListClasses.li));
}
this.incrementalSearch(e as KeyboardEventArgs);
}
}
protected getNgDirective(): string {
return 'EJS-COMBOBOX';
}
protected setSearchBox(): InputObject {
this.filterInput = this.inputElement;
const searchBoxContainer: InputObject = (this.isFiltering() || ((this as any).isReact && this.getModuleName() === 'combobox')) ? this.inputWrapper : inputObject;
return searchBoxContainer;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected onActionComplete(ulElement: HTMLElement, list: { [key: string]: Object }[], e?: Object, isUpdated?: boolean): void {
super.onActionComplete(ulElement, list, e);
if (this.isSelectCustom) {
this.removeSelection();
}
if (!this.preventAutoFill && this.getModuleName() === 'combobox' && this.isTyped && !this.enableVirtualization) {
setTimeout(() => {
this.inlineSearch();
});
}
}
protected getFocusElement(): Element {
const dataItem: { [key: string]: string } = this.isSelectCustom ? { text: '' } : this.getItemData();
const selected: HTMLElement = !isNullOrUndefined(this.list) ? <HTMLElement>this.list.querySelector('.' + dropDownListClasses.selected) : this.list;
const isSelected: boolean = dataItem.text && dataItem.text.toString() === this.inputElement.value && !isNullOrUndefined(selected);
if (isSelected) {
return selected;
}
if ((Browser.isDevice && !this.isDropDownClick || !Browser.isDevice) &&
!isNullOrUndefined(this.liCollections) && this.liCollections.length > 0) {
const inputValue: string = this.inputElement.value;
const dataSource: { [key: string]: Object }[] = this.sortedData as { [key: string]: Object }[];
const type: string = this.typeOfData(dataSource).typeof as string;
let activeItem: { [key: string]: Element | number } =
Search(inputValue, this.liCollections, this.filterType, true, dataSource, this.fields, type, this.ignoreAccent);
if (this.enableVirtualization && inputValue !== '' && this.getModuleName() !== 'autocomplete' && this.isTyped && !this.allowFiltering) {
let updatingincrementalindex: boolean = false;
const isEndIndexValid: boolean =
this.viewPortInfo.endIndex >= this.incrementalEndIndex &&
this.incrementalEndIndex <= this.totalItemCount;
const isIncrementalEndIndexZero: boolean = this.incrementalEndIndex === 0;
if (isEndIndexValid || isIncrementalEndIndexZero) {
updatingincrementalindex = true;
this.incrementalStartIndex = this.incrementalEndIndex;
if (isIncrementalEndIndexZero) {
this.incrementalEndIndex = Math.min(100, this.totalItemCount);
} else {
this.incrementalEndIndex = Math.min(this.incrementalEndIndex + 100, this.totalItemCount);
}
this.updateIncrementalInfo(this.incrementalStartIndex, this.incrementalEndIndex);
updatingincrementalindex = true;
}
if (this.viewPortInfo.startIndex !== 0 || updatingincrementalindex) {
this.updateIncrementalView(0, this.itemCount);
}
activeItem = Search(inputValue, this.incrementalLiCollections, this.filterType, true, dataSource, this.fields, type);
while (isNullOrUndefined(activeItem.item) && this.incrementalEndIndex < this.totalItemCount) {
this.incrementalStartIndex = this.incrementalEndIndex;
this.incrementalEndIndex = this.incrementalEndIndex + 100 > this.totalItemCount
? this.totalItemCount
: this.incrementalEndIndex + 100;
this.updateIncrementalInfo(this.incrementalStartIndex, this.incrementalEndIndex);
updatingincrementalindex = true;
if (this.viewPortInfo.startIndex !== 0 || updatingincrementalindex) {
this.updateIncrementalView(0, this.itemCount);
}
activeItem = Search(inputValue, this.incrementalLiCollections, this.filterType, true, dataSource, this.fields, type);
if (!isNullOrUndefined(activeItem)) {
activeItem.index = (activeItem.index as number) + this.incrementalStartIndex;
break;
}
if (isNullOrUndefined(activeItem) && this.incrementalEndIndex >= this.totalItemCount) {
this.incrementalStartIndex = 0;
this.incrementalEndIndex = 100 > this.totalItemCount ? this.totalItemCount : 100;
break;
}
}
const startIndex: number =
(activeItem.index as number) - ((this.itemCount / 2) - 2) > 0
? (activeItem.index as number) - ((this.itemCount / 2) - 2)
: 0;
const endIndex: number =
this.viewPortInfo.startIndex + this.itemCount > this.totalItemCount
? this.totalItemCount
: this.viewPortInfo.startIndex + this.itemCount;
if (startIndex !== this.viewPortInfo.startIndex) {
this.updateIncrementalView(startIndex, endIndex);
}
if (!isNullOrUndefined(activeItem.item)) {
const startIndex: number = this.viewPortInfo.startIndex + ((this.itemCount / 2) - 2) < this.totalItemCount
? this.viewPortInfo.startIndex + ((this.itemCount / 2) - 2)
: this.totalItemCount;
const endIndex: number = this.viewPortInfo.startIndex + this.itemCount > this.totalItemCount
? this.totalItemCount
: this.viewPortInfo.startIndex + this.itemCount;
this.updateIncrementalView(startIndex, endIndex);
activeItem.item = this.getElementByValue((activeItem.item as Element).getAttribute('data-value'));
} else {
this.updateIncrementalView(0, this.itemCount);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(this.list.getElementsByClassName('e-virtual-ddl-content')[0] as any).style = this.getTransformValues();
this.list.scrollTop = 0;
}
if (activeItem && activeItem.item){
activeItem.item = this.getElementByValue((activeItem.item as Element).getAttribute('data-value'));
}
}
const activeElement: Element = activeItem.item as Element;
if (!isNullOrUndefined(activeElement)) {
const count: number = this.getIndexByValue(activeElement.getAttribute('data-value')) - 1;
const height: number = parseInt(getComputedStyle(this.liCollections[0], null).getPropertyValue('height'), 10);
if (!isNaN(height) && this.getModuleName() !== 'autocomplete') {
this.removeFocus();
const fixedHead: number = this.fields.groupBy ? this.liCollections[0].offsetHeight : 0;
if (!this.enableVirtualization) {
this.list.scrollTop = count * height + fixedHead;
} else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const virtualContent: any = this.list.getElementsByClassName('e-virtual-ddl-content')[0];
virtualContent.style = this.getTransformValues();
if (this.enableVirtualization && !this.fields.groupBy) {
const selectedLiOffsetTop: number = (activeElement as HTMLElement).offsetTop;
const virtualListInfoStartIndex: number = this.virtualListInfo && this.virtualListInfo.startIndex
? this.virtualListInfo.startIndex
: 0;
const virtualListHeight: number = (activeElement as HTMLElement).offsetHeight;
const selectedLiOffsetTopWithStartIndex: number = selectedLiOffsetTop +
(virtualListInfoStartIndex * virtualListHeight);
const virtualListLength: number = this.list.querySelectorAll('.e-virtual-list').length;
const scrollTopOffset: number = virtualListLength * virtualListHeight;
this.list.scrollTop = selectedLiOffsetTopWithStartIndex - scrollTopOffset;
}
}
addClass([activeElement], dropDownListClasses.focus);
}
} else {
if (this.isSelectCustom && this.inputElement.value.trim() !== '') {
this.removeFocus();
if (!this.enableVirtualization) {
this.list.scrollTop = 0;
}
}
}
return activeElement;
} else {
return null;
}
}
protected setValue(e?: KeyboardEventArgs): boolean {
if ((e && e.type === 'keydown' && e.action === 'enter') || (e && e.type === 'click')) {
this.removeFillSelection();
}
if (this.autofill && this.getModuleName() === 'combobox' && e && e.type === 'keydown' && e.action !== 'enter') {
this.preventAutoFill = false;
this.inlineSearch(e);
return false;
} else {
return super.setValue(e);
}
}
protected checkCustomValue(): void {
const value: string | number | boolean = this.allowObjectBinding && !isNullOrUndefined(this.value) ? getValue((this.fields.value) ? this.fields.value : '', this.value) : this.value;
this.itemData = this.getDataByValue(value);
const dataItem: { [key: string]: string } = this.getItemData();
const setValue: string | number | boolean | Object = this.allowObjectBinding ? this.itemData : dataItem.value;
if (!(this.allowCustom && isNullOrUndefined(dataItem.value) && isNullOrUndefined(dataItem.text))) {
this.setProperties({ 'value': setValue }, !this.allowCustom);
}
}
/**
* Shows the spinner loader.
*
* @returns {void}
* @deprecated
*/
public showSpinner(): void {
if (isNullOrUndefined(this.spinnerElement)) {
this.spinnerElement = (this.getModuleName() === 'autocomplete') ? (this.inputWrapper.buttons[0] ||
this.inputWrapper.clearButton ||
Input.appendSpan('e-input-group-icon ' + SPINNER_CLASS, this.inputWrapper.container, this.createElement)) :
(this.inputWrapper.buttons[0] || this.inputWrapper.clearButton);
addClass([this.spinnerElement], dropDownListClasses.disableIcon);
createSpinner(
{
target: this.spinnerElement,
width: Browser.isDevice ? '16px' : '14px'
},
this.createElement);
showSpinner(this.spinnerElement);
}
}
/**
* Hides the spinner loader.
*
* @returns {void}
* @deprecated
*/
public hideSpinner(): void {
if (!isNullOrUndefined(this.spinnerElement)) {
hideSpinner(this.spinnerElement);
removeClass([this.spinnerElement], dropDownListClasses.disableIcon);
if (this.spinnerElement.classList.contains(SPINNER_CLASS)) {
detach(this.spinnerElement);
} else {
this.spinnerElement.innerHTML = '';
}
this.spinnerElement = null;
}
}
protected setAutoFill(activeElement: Element, isHover?: boolean): void {
if (!isHover) {
this.setHoverList(activeElement);
}
if (this.autofill && !this.preventAutoFill) {
const currentValue: string = <string>this.getTextByValue(activeElement.getAttribute('data-value')).toString();
const currentFillValue: string | number | boolean = this.getFormattedValue(activeElement.getAttribute('data-value'));
if (this.getModuleName() === 'combobox') {
if (!this.isSelected && ((!this.allowObjectBinding && this.previousValue !== currentFillValue)) ||
(this.allowObjectBinding && this.previousValue && currentFillValue &&
!this.isObjectInArray(this.previousValue, [this.getDataByValue(currentFillValue)]))) {
this.updateSelectedItem(activeElement, null);
this.isSelected = true;
this.previousValue = this.allowObjectBinding ?
this.getDataByValue(this.getFormattedValue(activeElement.getAttribute('data-value'))) :
this.getFormattedValue(activeElement.getAttribute('data-value'));
} else {
this.updateSelectedItem(activeElement, null, true);
}
}
if (!this.isAndroidAutoFill(currentValue)) {
this.setAutoFillSelection(currentValue, isHover);
}
}
}
private isAndroidAutoFill(value: string): boolean {
if (Browser.isAndroid) {
const currentPoints: { [key: string]: number } = this.getSelectionPoints();
const prevEnd: number = this.prevSelectPoints.end;
const curEnd: number = currentPoints.end;
const prevStart: number = this.prevSelectPoints.start;
const curStart: number = currentPoints.start;
if (prevEnd !== 0 && ((prevEnd === value.length && prevStart === value.length) ||
(prevStart > curStart && prevEnd > curEnd) || (prevEnd === curEnd && prevStart === curStart))) {
return true;
} else {
return false;
}
} else {
return false;
}
}
protected clearAll(e?: MouseEvent | KeyboardEventArgs, property?: ComboBoxModel): void {
if (isNullOrUndefined(property) || (!isNullOrUndefined(property) && isNullOrUndefined(property.dataSource))) {
super.clearAll(e);
}
if (this.isFiltering() && !isNullOrUndefined(e) && e.target === this.inputWrapper.clearButton) {
this.typedString = this.filterInput.value;
this.searchLists(e);
}
}
protected isSelectFocusItem(element: Element): boolean {
return !isNullOrUndefined(element);
}
private inlineSearch(e?: KeyboardEventArgs): void {
const isKeyNavigate: boolean = (e && (e.action === 'down' || e.action === 'up' ||
e.action === 'home' || e.action === 'end' || e.action === 'pageUp' || e.action === 'pageDown'));
const activeElement: Element = isKeyNavigate ? this.liCollections[this.activeIndex] : this.getFocusElement();
if (!isNullOrUndefined(activeElement)) {
if (!isKeyNavigate) {
const value: string | number | boolean = this.getFormattedValue(activeElement.getAttribute('data-value'));
this.activeIndex = this.getIndexByValue(value);
this.activeIndex = !isNullOrUndefined(this.activeIndex) ? this.activeIndex : null;
}
this.preventAutoFill = this.inputElement.value === '' ? false : this.preventAutoFill;
this.setAutoFill(activeElement, isKeyNavigate);
} else if (!isNullOrUndefined(this.inputElement) && this.inputElement.value === '') {
this.activeIndex = null;
if (!isNullOrUndefined(this.list)) {
if (!this.enableVirtualization) {
this.list.scrollTop = 0;
}
const focusItem: Element = this.list.querySelector('.' + dropDownListClasses.li);
this.setHoverList(focusItem);
}
} else {
this.activeIndex = null;
this.removeSelection();
if (this.liCollections && this.liCollections.length > 0 && !this.isCustomFilter) {
this.removeFocus();
}
}
}
protected incrementalSearch(e: KeyboardEventArgs): void {
this.showPopup(e);
if (!isNullOrUndefined(this.listData)) {
this.inlineSearch(e);
e.preventDefault();
}
}
private setAutoFillSelection(currentValue: string, isKeyNavigate: boolean = false): void {
const selection: { [key: string]: number } = this.getSelectionPoints();
const value: string = this.inputElement.value.substr(0, selection.start);
if (value && (value.toLowerCase() === currentValue.substr(0, selection.start).toLowerCase())) {
const inputValue: string = value + currentValue.substr(value.length, currentValue.length);
Input.setValue(inputValue, this.inputElement, this.floatLabelType, this.showClearButton);
this.inputElement.setSelectionRange(selection.start, this.inputElement.value.length);
} else if (isKeyNavigate) {
Input.setValue(currentValue, this.inputElement, this.floatLabelType, this.showClearButton);
this.inputElement.setSelectionRange(0, this.inputElement.value.length);
}
}
protected getValueByText(text: string): string | number | boolean {
return super.getValueByText(text, true, this.ignoreAccent);
}
protected unWireEvent(): void {
if (this.getModuleName() === 'combobox') {
EventHandler.remove(this.inputWrapper.buttons[0], 'mousedown', this.preventBlur);
EventHandler.remove(this.inputWrapper.container, 'blur', this.onBlurHandler);
}
if (!isNullOrUndefined(this.inputWrapper.buttons[0])) {
EventHandler.remove(this.inputWrapper.buttons[0], 'mousedown', this.dropDownClick);
}
if (this.inputElement) {
EventHandler.remove(this.inputElement, 'focus', this.targetFocus);
if (!this.readonly) {
EventHandler.remove(this.inputElement, 'input', this.onInput);
EventHandler.remove(this.inputElement, 'keyup', this.onFilterUp);
EventHandler.remove(this.inputElement, 'keydown', this.onFilterDown);
EventHandler.remove(this.inputElement, 'paste', this.pasteHandler);
EventHandler.remove(<HTMLElement & Window><unknown>window, 'resize', this.windowResize);
}
}
this.unBindCommonEvent();
}
protected setSelection(li: Element, e: MouseEvent | KeyboardEventArgs | TouchEvent): void {
super.setSelection(li, e);
if (!isNullOrUndefined(li) && !this.autofill && !this.isDropDownClick) {
this.removeFocus();
}
}
protected selectCurrentItem(e: KeyboardEventArgs): void {
let li: Element;
if (this.isPopupOpen) {
if (this.isSelected) {
li = this.list.querySelector('.' + dropDownListClasses.selected);
} else {
li = this.list.querySelector('.' + dropDownListClasses.focus);
}
if (this.isDisabledElement(li as HTMLElement)) {
return;
}
if (li) {
this.setSelection(li, e);
this.isTyped = false;
}
if (this.isSelected) {
this.isSelectCustom = false;
this.onChangeEvent(e);
}
}
if (e.action === 'enter' && this.inputElement.value === '') {
this.clearAll(e);
} else if (this.isTyped && !this.isSelected && isNullOrUndefined(li)) {
this.customValue(e);
}
this.hidePopup(e);
}
protected setHoverList(li: Element): void {
this.removeSelection();
if (this.isValidLI(li) && !li.classList.contains(dropDownListClasses.selected)) {
this.removeFocus();
li.classList.add(dropDownListClasses.focus);
}
}
private targetFocus(e: MouseEvent): void {
if (Browser.isDevice && !this.allowFiltering) {
this.preventFocus = false;
}
this.onFocus(e);
Input.calculateWidth(this.inputElement, this.inputWrapper.container);
}
protected dropDownClick(e: MouseEvent): void {
e.preventDefault();
if (Browser.isDevice && !this.isFiltering()) {
this.preventFocus = true;
}
super.dropDownClick(e);
}
private customValue(e?: MouseEvent | KeyboardEventArgs | TouchEvent): void {
let value: string | number | boolean | object = this.getValueByText(this.inputElement.value);
if (!this.allowCustom && this.inputElement.value !== '') {
const previousValue: string | number | boolean | object = this.previousValue;
const currentValue: string | number | boolean | object = this.value;
value = this.allowObjectBinding ? this.getDataByValue(value) : value;
this.setProperties({ value: value });
if (isNullOrUndefined(this.value)) {
Input.setValue('', this.inputElement, this.floatLabelType, this.showClearButton);
}
if (this.autofill && (
(!this.allowObjectBinding && previousValue === this.value) ||
(this.allowObjectBinding && previousValue && this.isObjectInArray(previousValue, [this.value]))
) && (
(!this.allowObjectBinding && currentValue !== this.value) ||
(this.allowObjectBinding && currentValue && !this.isObjectInArray(currentValue, [this.value]))
)) {
this.onChangeEvent(null);
}
} else if (this.inputElement.value !== '') {
const previousValue: string | number | boolean | object = this.value;
if (isNullOrUndefined(value)) {
const value: string | Object = this.inputElement.value === '' ? null : this.inputElement.value;
// eslint-disable-next-line max-len
const eventArgs: { [key: string]: Object | string | number } = <{ [key: string]: Object | string | number }>{ text: value, item: {} };
this.isObjectCustomValue = true;
if (!this.initial) {
this.trigger('customValueSpecifier', eventArgs, (eventArgs: { [key: string]: Object | string | number }) => {
this.updateCustomValueCallback(value, eventArgs, previousValue, e);
});
} else {
this.updateCustomValueCallback(value, eventArgs, previousValue);
}
} else {
this.isSelectCustom = false;
value = this.allowObjectBinding ? this.getDataByValue(value) : value;
this.setProperties({ value: value });
if ((!this.allowObjectBinding && previousValue !== this.value) ||
(this.allowObjectBinding && previousValue && this.value && !this.isObjectInArray(previousValue, [this.value]))
) {
this.onChangeEvent(e);
}
}
} else if (this.allowCustom && this.isFocused) {
this.isSelectCustom = true;
}
}
private updateCustomValueCallback(
value: string | Object,
eventArgs: { [key: string]: Object | string | number },
previousValue: string | number | boolean | object,
e?: MouseEvent | KeyboardEventArgs | TouchEvent): void {
const fields: FieldSettingsModel = this.fields;
const item: { [key: string]: string | Object } = <{ [key: string]: string | Object }>eventArgs.item;
let dataItem: { [key: string]: string | Object } = {};
if (item && getValue(fields.text, item) && getValue(fields.value, item)) {
dataItem = item;
} else {
setValue(fields.text, value, dataItem);
setValue(fields.value, value, dataItem);
}
this.itemData = <{ [key: string]: Object }>dataItem;
const emptyObject: { [key: string]: any } = {};