forked from syncfusion/ej2-javascript-ui-controls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-box.ts
2959 lines (2829 loc) · 129 KB
/
list-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-base/drop-down-base-model.d.ts'/>
import { Input, InputObject } from '@syncfusion/ej2-inputs';
import { DropDownBase, dropDownBaseClasses, FilteringEventArgs, SelectEventArgs } from '../drop-down-base/drop-down-base';
import { FieldSettingsModel } from '../drop-down-base/drop-down-base-model';
import { EventHandler, closest, removeClass, addClass, Complex, Property, ChildProperty, BaseEventArgs, L10n, setValue } from '@syncfusion/ej2-base';
import { ModuleDeclaration, NotifyPropertyChanges, getComponent, EmitType, Event, extend, detach, attributes } from '@syncfusion/ej2-base';
import { getUniqueID, Browser, formatUnit, isNullOrUndefined, getValue } from '@syncfusion/ej2-base';
import { prepend, append } from '@syncfusion/ej2-base';
import { cssClass, Sortable, moveTo, SortOrder } from '@syncfusion/ej2-lists';
import { SelectionSettingsModel, ListBoxModel, ToolbarSettingsModel } from './list-box-model';
import { Button } from '@syncfusion/ej2-buttons';
import { createSpinner, showSpinner, hideSpinner, getZindexPartial } from '@syncfusion/ej2-popups';
import { DataManager, Query } from '@syncfusion/ej2-data';
/**
* Defines the selection mode in ListBox component.
* ```props
* Multiple :- Specifies that the ListBox should allow multiple item selection.
* Single :- Specifies that the ListBox should allow single item selection.
* ```
*/
export type SelectionMode = 'Multiple' | 'Single';
/**
* Defines the position of the toolbar in ListBox component.
* ```props
* Left :- Specifies that the toolbar should be positioned to the left of the ListBox.
* Right :- Specifies that the toolbar should be positioned to the right of the ListBox.
* ```
*/
export type ToolBarPosition = 'Left' | 'Right';
/**
* Defines the position of the checkbox in ListBox component.
* ```props
* Left :- Specifies that the checkbox should be positioned to the left of the ListBox.
* Right :- Specifies that the checkbox should be positioned to the right of the ListBox.
* ```
*/
export type CheckBoxPosition = 'Left' | 'Right';
type dataType = { [key: string]: object } | string | boolean | number;
type obj = { [key: string]: object };
/**
* Defines the Selection settings of List Box.
*/
export class SelectionSettings extends ChildProperty<SelectionSettings> {
/**
* Specifies the selection modes. The possible values are
* * `Single`: Allows you to select a single item in the ListBox.
* * `Multiple`: Allows you to select more than one item in the ListBox.
*
* @default 'Multiple'
*/
@Property('Multiple')
public mode: SelectionMode;
/**
* If 'showCheckbox' is set to true, then 'checkbox' will be visualized in the list item.
*
* @default false
*/
@Property(false)
public showCheckbox: boolean;
/**
* Allows you to either show or hide the selectAll option on the component.
*
* @default false
*/
@Property(false)
public showSelectAll: boolean;
/**
* Set the position of the checkbox.
*
* @default 'Left'
*/
@Property('Left')
public checkboxPosition: CheckBoxPosition;
}
/**
* Defines the toolbar settings of List Box.
*/
export class ToolbarSettings extends ChildProperty<ToolbarSettings> {
/**
* Specifies the list of tools for dual ListBox.
* The predefined tools are 'moveUp', 'moveDown', 'moveTo', 'moveFrom', 'moveAllTo', and 'moveAllFrom'.
*
* @default []
*/
@Property([])
public items: string[];
/**
* Positions the toolbar before/after the ListBox.
* The possible values are:
* * Left: The toolbar will be positioned to the left of the ListBox.
* * Right: The toolbar will be positioned to the right of the ListBox.
*
* @default 'Right'
*/
@Property('Right')
public position: ToolBarPosition;
}
/**
* The ListBox is a graphical user interface component used to display a list of items.
* Users can select one or more items in the list using a checkbox or by keyboard selection.
* It supports sorting, grouping, reordering and drag and drop of items.
* ```html
* <select id="listbox">
* <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
* <script>
* var listObj = new ListBox();
* listObj.appendTo("#listbox");
* </script>
* ```
*/
@NotifyPropertyChanges
export class ListBox extends DropDownBase {
private prevSelIdx: number;
private listCurrentOptions: FieldSettingsModel;
private allowDragAll: boolean;
private checkBoxSelectionModule: { onDocumentClick: Function, checkAllParent: HTMLElement, clearIconElement: HTMLElement };
private tBListBox: ListBox;
private initLoad: boolean;
private spinner: HTMLElement;
private initialSelectedOptions: string[] | number[] | boolean[];
private showSelectAll: boolean;
private selectAllText: string;
private unSelectAllText: string;
private popupWrapper: Element;
private targetInputElement: HTMLInputElement | string;
private isValidKey: boolean = false;
private isFiltered: boolean;
private clearFilterIconElem: Element;
private remoteFilterAction: boolean;
private mainList: HTMLElement;
private remoteCustomValue: boolean;
private filterParent: HTMLElement;
protected inputString: string;
protected filterInput: HTMLInputElement;
protected isCustomFiltering: boolean;
private jsonData: { [key: string]: Object }[] | string[] | boolean[] | number[];
private toolbarAction: string;
private isDataSourceUpdate: boolean = false;
private dragValue: string;
private customDraggedItem: Object[];
private timer: number;
private inputFormName: string;
/**
* Sets the CSS classes to root element of this component, which helps to customize the
* complete styles.
*
* @default ''
*/
@Property('')
public cssClass: string;
/**
* Sets the specified item to the selected state or gets the selected item in the ListBox.
*
* @default []
* @aspType object
* @isGenericType true
*/
@Property([])
public value: string[] | number[] | boolean[];
/**
* Sets the height of the ListBox component.
*
* @default ''
*/
@Property('')
public height: number | string;
/**
* Specifies a value that indicates whether the component is enabled or not.
*
* @default true
* @deprecated
*/
@Property(true)
public enabled: boolean;
/**
* Enable or disable persisting component's state between page reloads.
* If enabled, following list of states will be persisted.
* 1. value
*
* @default false
* @deprecated
*/
@Property(false)
public enablePersistence: boolean;
/**
* If 'allowDragAndDrop' is set to true, then you can perform drag and drop of the list item.
* ListBox contains same 'scope' property enables drag and drop between multiple ListBox.
*
* @default false
*/
@Property(false)
public allowDragAndDrop: boolean;
/**
* Sets limitation to the value selection.
* based on the limitation, list selection will be prevented.
*
* @default 1000
*/
@Property(1000)
public maximumSelectionLength: number;
/**
* To enable the filtering option in this component.
* Filter action performs when type in search box and collect the matched item through `filtering` event.
* If searching character does not match, `noRecordsTemplate` property value will be shown.
*
* @default false
*/
@Property(false)
public allowFiltering: boolean;
/**
* Defines the scope value to group sets of draggable and droppable ListBox.
* A draggable with the same scope value will be accepted by the droppable.
*
* @default ''
*/
@Property('')
public scope: string;
/**
* When set to ‘false’, consider the `case-sensitive` on performing the search to find suggestions.
* By default consider the casing.
*
* @default true
* @private
*/
@Property(true)
public ignoreCase: boolean;
/**
* Accepts the value to be displayed as a watermark text on the filter bar.
*
* @default null
*/
@Property(null)
public filterBarPlaceholder: string;
/**
* Specifies the `sortOrder` to sort the data source. The available type of sort orders are
* * `None` - The data source is not sorting.
* * `Ascending` - The data source is sorting with ascending order.
* * `Descending` - The data source is sorting with descending order.
*
* @default null
* @asptype object
* @aspjsonconverterignore
*/
@Property<SortOrder>('None')
public sortOrder: SortOrder;
/**
* Triggers while rendering each list item.
*
* @event beforeItemRender
* @blazorProperty 'OnItemRender'
*/
@Event()
public beforeItemRender: EmitType<BeforeItemRenderEventArgs>;
/**
* Triggers on typing a character in the component.
*
* @event filtering
* @blazorProperty 'ItemSelected'
*/
@Event()
public filtering: EmitType<FilteringEventArgs>;
/**
* Triggers when an item in the popup is selected by the user either with mouse/tap or with keyboard navigation.
*
* @event select
* @private
*/
@Event()
public select: EmitType<SelectEventArgs>;
/**
* Adds a new item to the popup list. By default, new item appends to the list as the last item,
* but you can insert based on the index parameter.
*
* @param { Object[] } items - Specifies an array of JSON data or a JSON data.
* @param { number } itemIndex - Specifies the index to place the newly added item in the popup list.
* @returns {void}.
* @private
*/
public addItem(
items: { [key: string]: Object }[] | { [key: string]: Object } | string | boolean | number | string[] | boolean[] | number[],
itemIndex?: number): void {
super.addItem(items, itemIndex);
if (this.allowFiltering && this.filterInput.value !== '') {
this.filteringAction(this.jsonData, new Query(), this.fields);
}
}
/**
* Triggers while select / unselect the list item.
*
* @event change
* @blazorProperty 'ValueChange'
*/
@Event()
public change: EmitType<ListBoxChangeEventArgs>;
/**
* Triggers before dropping the list item on another list item.
*
* @event beforeDrop
* @blazorProperty 'OnDrop'
*/
@Event()
public beforeDrop: EmitType<DropEventArgs>;
/**
* Triggers after dragging the list item.
*
* @event dragStart
* @blazorProperty 'DragStart'
*/
@Event()
public dragStart: EmitType<DragEventArgs>;
/**
* Triggers while dragging the list item.
*
* @event drag
* @blazorProperty 'Dragging'
*/
@Event()
public drag: EmitType<DragEventArgs>;
/**
* Triggers before dropping the list item on another list item.
*
* @event drop
* @blazorProperty 'Dropped'
*/
@Event()
public drop: EmitType<DragEventArgs>;
/**
* Triggers when data source is populated in the list.
*
* @event dataBound
*/
@Event()
public dataBound: EmitType<Object>;
/**
* Accepts the template design and assigns it to the group headers present in the list.
*
* @default null
* @private
*/
@Property(null)
public groupTemplate: string;
/**
* Accepts the template and assigns it to the list content of the ListBox component
* when the data fetch request from the remote server fails.
*
* @default 'Request Failed'
* @private
*/
@Property('Request failed')
public actionFailureTemplate: string;
/**
* specifies the z-index value of the component popup element.
*
* @default 1000
* @private
*/
@Property(1000)
public zIndex: number;
/**
* ignoreAccent set to true, then ignores the diacritic characters or accents when filtering.
*
* @private
*/
@Property(false)
public ignoreAccent: boolean;
/**
* Specifies the toolbar items and its position.
*
* @default { items: [], position: 'Right' }
*/
@Complex<ToolbarSettingsModel>({}, ToolbarSettings)
public toolbarSettings: ToolbarSettingsModel;
/**
* Specifies the selection mode and its type.
*
* @default { mode: 'Multiple', type: 'Default' }
*/
@Complex<SelectionSettingsModel>({}, SelectionSettings)
public selectionSettings: SelectionSettingsModel;
/**
* Constructor for creating the ListBox component.
*
* @param {ListBoxModel} options - Specifies ListBox model
* @param {string | HTMLElement} element - Specifies the element.
*/
constructor(options?: ListBoxModel, element?: string | HTMLElement) {
super(options, element);
}
/**
* Build and render the component.
*
* @private
* @returns {void}
*/
public render(): void {
if (this.isAngular && this.allowFiltering) {
const originalElement: HTMLElement = this.element;
const clonedElement: HTMLElement = originalElement.cloneNode(true) as HTMLElement;
originalElement.parentNode.replaceChild(clonedElement, originalElement);
this.element = clonedElement;
setValue('ej2_instances', [this], this.element);
}
this.inputString = '';
this.initLoad = true;
this.isCustomFiltering = false;
this.initialSelectedOptions = this.value;
this.inputFormName = this.element.getAttribute('name');
super.render();
this.setEnabled();
this.renderComplete();
}
private initWrapper(): void {
const hiddenSelect: HTMLElement = this.createElement('select', { className: 'e-hidden-select', attrs: { 'multiple': '' } });
hiddenSelect.style.visibility = 'hidden';
this.list.classList.add('e-listbox-wrapper');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(this.list.querySelector('.e-list-parent') as any).setAttribute('role', 'presentation');
const groupHdrs: HTMLElement[] = <HTMLElement[] & NodeListOf<Element>>this.list.querySelectorAll('.e-list-group-item');
for (let i: number = 0; i < groupHdrs.length; i++) {
groupHdrs[i as number].removeAttribute('tabindex');
groupHdrs[i as number].setAttribute('role', 'option');
}
if (this.itemTemplate) {
this.list.classList.add('e-list-template');
}
this.list.classList.add('e-wrapper');
this.list.classList.add('e-lib');
if (this.element.tagName === 'EJS-LISTBOX') {
this.element.setAttribute('tabindex', '0');
if (this.initLoad) {
this.element.appendChild(this.list);
}
} else {
if (this.initLoad && this.element.parentElement) {
this.element.parentElement.insertBefore(this.list, this.element);
}
this.list.insertBefore(this.element, this.list.firstChild);
this.element.style.display = 'none';
}
this.list.insertBefore(hiddenSelect, this.list.firstChild);
if (this.list.getElementsByClassName('e-list-item')[0]) {
this.list.getElementsByClassName('e-list-item')[0].classList.remove(dropDownBaseClasses.focus);
}
if (this.itemTemplate) { this.renderReactTemplates(); }
removeClass([this.list], [dropDownBaseClasses.content, dropDownBaseClasses.root]);
this.validationAttribute(this.element as HTMLInputElement, hiddenSelect as HTMLSelectElement);
this.list.setAttribute('role', 'listbox');
attributes(this.list, { 'role': 'listbox', 'aria-label': 'listbox', 'aria-multiselectable': this.selectionSettings.mode === 'Multiple' ? 'true' : 'false' });
this.updateSelectionSettings();
}
private updateSelectionSettings(): void {
if (this.selectionSettings.showCheckbox && this.selectionSettings.showSelectAll && this.liCollections.length) {
const l10nSelect: L10n = new L10n(
this.getModuleName(), { selectAllText: 'Select All', unSelectAllText: 'Unselect All' }, this.locale);
this.showSelectAll = true;
this.selectAllText = l10nSelect.getConstant('selectAllText');
this.unSelectAllText = l10nSelect.getConstant('unSelectAllText');
this.popupWrapper = this.list;
this.checkBoxSelectionModule.checkAllParent = null;
this.notify('selectAll', {});
}
}
private initDraggable(): void {
if (this.ulElement) {
this.ulElement.id = this.element.id + '_parent';
}
if (this.allowDragAndDrop) {
new Sortable(this.ulElement, {
scope: this.scope,
itemClass: 'e-list-item',
beforeDragStart: this.triggerDragStart.bind(this),
drag: this.triggerDrag.bind(this),
beforeDrop: this.beforeDragEnd.bind(this),
drop: this.dragEnd.bind(this),
placeHolder: () => { return this.createElement('span', { className: 'e-placeholder' }); },
helper: (e: { sender: Element }) => {
const wrapper: HTMLElement = this.list.cloneNode() as HTMLElement;
const ele: HTMLElement = e.sender.cloneNode(true) as HTMLElement;
wrapper.appendChild(ele);
const refEle: HTMLElement = this.getItems()[0] as HTMLElement;
wrapper.style.width = refEle.offsetWidth + 'px';
wrapper.style.height = refEle.offsetHeight + 'px';
if ((this.value && this.value.length) > 1 && this.isSelected(ele)) {
ele.appendChild(this.createElement('span', {
className: 'e-list-badge', innerHTML: this.value.length + ''
}));
}
wrapper.style.zIndex = getZindexPartial(this.element) + '';
return wrapper;
}
}
);
}
}
protected updateActionCompleteData(li: HTMLElement, item: { [key: string]: Object }, index: number): void {
(this.jsonData as { [key: string]: Object }[]).splice(index === null ? this.jsonData.length : index, 0, item);
}
private initToolbar(): void {
const pos: string = this.toolbarSettings.position;
const prevScope: string = this.element.getAttribute('data-value');
this.toolbarSettings.items = isNullOrUndefined(this.toolbarSettings.items) ? [] : this.toolbarSettings.items;
if (this.toolbarSettings.items.length) {
const toolElem: Element = this.createElement('div', { className: 'e-listbox-tool', attrs: { 'role': 'toolbar' } });
const wrapper: Element = this.createElement('div', {
className: 'e-listboxtool-wrapper e-lib e-' + pos.toLowerCase()
});
this.list.parentElement.insertBefore(wrapper, this.list);
wrapper.appendChild(pos === 'Right' ? this.list : toolElem);
wrapper.appendChild(pos === 'Right' ? toolElem : this.list);
this.createButtons(toolElem);
if (!this.element.id) {
this.element.id = getUniqueID('e-' + this.getModuleName());
}
if (this.scope) {
document.querySelector(this.scope).setAttribute('data-value', this.element.id);
} else {
this.updateToolBarState();
}
}
const scope: string = this.element.getAttribute('data-value');
if (prevScope && scope && (prevScope !== scope)) {
this.tBListBox = getComponent(document.getElementById(prevScope), this.getModuleName());
this.tBListBox.updateToolBarState();
} else if (scope) {
this.tBListBox = getComponent(document.getElementById(scope), this.getModuleName());
this.tBListBox.updateToolBarState();
}
}
private createButtons(toolElem: Element): void {
let btn: Button;
let ele: HTMLButtonElement;
let title: string;
const l10n: L10n = new L10n(
this.getModuleName(),
{
moveUp: 'Move Up', moveDown: 'Move Down', moveTo: 'Move To',
moveFrom: 'Move From', moveAllTo: 'Move All To', moveAllFrom: 'Move All From'
},
this.locale
);
this.toolbarSettings.items.forEach((value: string) => {
title = l10n.getConstant(value);
ele = this.createElement('button', {
attrs: {
'type': 'button',
'data-value': value,
'title': title,
'aria-label': title
}
}) as HTMLButtonElement;
toolElem.appendChild(ele);
btn = new Button({ iconCss: 'e-icons e-' + value.toLowerCase() }, ele);
btn.createElement = this.createElement;
});
}
protected validationAttribute(input: HTMLInputElement, hiddenSelect: HTMLSelectElement): void {
if (this.inputFormName) { input.setAttribute('name', this.inputFormName); }
super.validationAttribute(input, hiddenSelect);
hiddenSelect.required = input.required;
input.required = false;
}
private setHeight(): void {
const ele: HTMLElement = this.toolbarSettings.items.length ? this.list.parentElement : this.list;
ele.style.height = formatUnit(this.height);
if (this.allowFiltering && this.height.toString().indexOf('%') < 0) {
addClass([this.list], 'e-filter-list');
} else {
removeClass([this.list], 'e-filter-list');
}
}
private setCssClass(): void {
const wrap: Element = this.toolbarSettings.items.length ? this.list.parentElement : this.list;
if (this.cssClass) {
addClass([wrap], this.cssClass.replace(/\s+/g, ' ').trim().split(' '));
}
if (this.enableRtl) {
addClass([this.list], 'e-rtl');
}
}
private setEnable(): void {
const ele: Element = this.toolbarSettings.items.length ? this.list.parentElement : this.list;
if (this.enabled) {
removeClass([ele], cssClass.disabled);
} else {
addClass([ele], cssClass.disabled);
}
}
public showSpinner(): void {
if (!this.spinner) {
this.spinner = this.createElement('div', { className: 'e-listbox-wrapper' });
}
this.spinner.style.height = formatUnit(this.height);
if (this.element.parentElement) {
this.element.parentElement.insertBefore(this.spinner, this.element.nextSibling);
}
createSpinner({ target: this.spinner }, this.createElement);
showSpinner(this.spinner);
}
public hideSpinner(): void {
if (this.spinner.querySelector('.e-spinner-pane')) {
hideSpinner(this.spinner);
}
if (this.spinner.parentElement) {
detach(this.spinner);
}
}
private onInput(): void {
this.isDataSourceUpdate = false;
if (this.keyDownStatus) {
this.isValidKey = true;
} else {
this.isValidKey = false;
}
this.keyDownStatus = false;
this.refreshClearIcon();
}
private clearText(): void {
this.filterInput.value = '';
this.refreshClearIcon();
const event: KeyboardEvent = document.createEvent('KeyboardEvent');
this.isValidKey = true;
this.KeyUp(event);
}
private refreshClearIcon(): void {
if (this.filterInput.parentElement.querySelector('.' + listBoxClasses.clearIcon)) {
const clearElement: HTMLElement = <HTMLElement>this.filterInput.parentElement.querySelector('.' + listBoxClasses.clearIcon);
clearElement.style.visibility = this.filterInput.value === '' ? 'hidden' : 'visible';
}
}
protected onActionComplete(
ulElement: HTMLElement,
list: obj[] | boolean[] | string[] | number[],
e?: Object): void {
let searchEle: Element; let filterElem: HTMLInputElement; let txtLength: number;
if (this.allowFiltering && this.list.getElementsByClassName('e-filter-parent')[0]) {
searchEle = this.list.getElementsByClassName('e-filter-parent')[0].cloneNode(true) as Element;
}
if (list.length === 0) {
const noRecElem: Element = ulElement.childNodes[0] as Element;
if (noRecElem) {
ulElement.removeChild(noRecElem);
}
}
if (this.allowFiltering) {
filterElem = (this.list.getElementsByClassName('e-input-filter')[0] as HTMLInputElement);
if (filterElem) {
txtLength = filterElem.selectionStart;
}
}
super.onActionComplete(ulElement, list, e);
if (this.allowFiltering && !isNullOrUndefined(searchEle)) {
this.list.insertBefore(searchEle, this.list.firstElementChild);
this.filterParent = this.list.getElementsByClassName('e-filter-parent')[0] as HTMLElement;
this.filterWireEvents(searchEle);
const inputSearch: HTMLElement = searchEle.querySelector('.e-input-filter');
if (inputSearch) {
inputSearch.addEventListener('focus', function(): void {
if (!(searchEle.childNodes[0] as HTMLElement).classList.contains('e-input-focus')) {
(searchEle.childNodes[0] as HTMLElement).classList.add('e-input-focus');
}
});
inputSearch.addEventListener('blur', function(): void {
if ((searchEle.childNodes[0] as HTMLElement).classList.contains('e-input-focus')) {
(searchEle.childNodes[0] as HTMLElement).classList.remove('e-input-focus');
}
});
}
}
this.initWrapper();
this.setSelection(this.value, true, false, !this.isRendered);
this.initDraggable();
this.mainList = this.ulElement;
if (this.initLoad) {
this.jsonData = []; extend(this.jsonData, list, []);
this.initToolbarAndStyles();
this.wireEvents();
if (this.showCheckbox) {
this.setCheckboxPosition();
}
if (this.allowFiltering) {
this.setFiltering();
}
} else {
if (this.isDataSourceUpdate) {
this.jsonData = []; extend(this.jsonData, list, []);
this.isDataSourceUpdate = false;
}
if (this.allowFiltering) {
filterElem = (this.list.getElementsByClassName('e-input-filter')[0] as HTMLInputElement);
if (isNullOrUndefined(filterElem)) { return; }
filterElem.selectionStart = txtLength;
filterElem.selectionEnd = txtLength;
if (filterElem.value !== '') {
filterElem.focus();
}
}
}
if (this.toolbarSettings.items.length && this.scope && this.scope.indexOf('#') > -1 && !isNullOrUndefined(e)) {
const scope: string = this.scope.replace('#', '');
const scopedLB: ListBox = getComponent(document.getElementById(scope), this.getModuleName());
scopedLB.initToolbar();
}
this.initLoad = false;
}
private initToolbarAndStyles(): void {
this.initToolbar();
this.setCssClass();
this.setEnable();
this.setHeight();
}
private triggerDragStart(args: DragEventArgs ): void {
let badge: Element;
const extendedArgs: any = extend(this.getDragArgs(args), { dragSelected: true }, {cancel: false}) as DragEventArgs;
if (Browser.isIos) {
this.list.style.overflow = 'hidden';
}
this.trigger('dragStart', extendedArgs, (dragEventArgs: DragEventArgs) => {
this.allowDragAll = dragEventArgs.dragSelected;
if (!this.allowDragAll) {
badge = this.ulElement.getElementsByClassName('e-list-badge')[0];
if (badge) { detach(badge); }
}
if (dragEventArgs.cancel) {
args.cancel = true;
}
});
}
private triggerDrag(args: DragEventArgs): void {
let scrollParent: HTMLElement; let boundRect: DOMRect; const scrollMoved: number = 36;
let scrollHeight: number = 10;
if (this.itemTemplate && args.target) {
if (args.target && args.target.closest('.e-list-item')) {
scrollHeight = args.target.closest('.e-list-item').scrollHeight;
} else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const listItem: HTMLElement = (args as any).element.querySelector('.e-list-item');
if (listItem) {
scrollHeight = listItem.scrollHeight;
}
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const event: any = (args as any).event; let wrapper: HTMLElement; this.stopTimer();
if (args.target && (args.target.classList.contains('e-listbox-wrapper') || args.target.classList.contains('e-list-item')
|| args.target.classList.contains('e-filter-parent') || args.target.classList.contains('e-input-group')
|| args.target.closest('.e-list-item'))) {
if (args.target.classList.contains('e-list-item') || args.target.classList.contains('e-filter-parent')
|| args.target.classList.contains('e-input-group')
|| args.target.closest('.e-list-item')) {
wrapper = args.target.closest('.e-listbox-wrapper') as HTMLElement;
} else {
wrapper = args.target as HTMLElement;
}
if (this.allowFiltering) {
scrollParent = wrapper.querySelector('.e-list-parent');
} else {
scrollParent = wrapper;
}
if (scrollParent) {
boundRect = scrollParent.getBoundingClientRect() as DOMRect;
if ((boundRect.y + scrollParent.offsetHeight) - (event.clientY + scrollMoved) < 1) {
this.timer = window.setInterval(() => { this.setScrollDown(scrollParent, scrollHeight, true); }, 70);
}
else if ((event.clientY - scrollMoved) - boundRect.y < 1) {
this.timer = window.setInterval(() => { this.setScrollDown(scrollParent, scrollHeight, false); }, 70);
}
}
}
if (args.target === null) {
return;
}
this.trigger('drag', this.getDragArgs(args as DragEventArgs));
}
private setScrollDown(scrollElem: Element, scrollPixel: number, isScrollDown: boolean): void {
if (isScrollDown) {
scrollElem.scrollTop = scrollElem.scrollTop + scrollPixel;
} else {
scrollElem.scrollTop = scrollElem.scrollTop - scrollPixel;
}
}
private stopTimer(): void {
window.clearInterval(this.timer);
}
private beforeDragEnd(args: DropEventArgs): void {
this.stopTimer();
const items: object[] = [];
this.dragValue = this.getFormattedValue(args.droppedElement.getAttribute('data-value')) as string;
if ((this.value as string[]).indexOf(this.dragValue) > -1) {
args.items = this.getDataByValues(this.value);
} else {
args.items = this.getDataByValues([this.dragValue]);
}
extend(items, args.items);
this.trigger('beforeDrop', args);
if (JSON.stringify(args.items) !== JSON.stringify(items)) {
this.customDraggedItem = args.items;
}
}
private dragEnd(args: DropEventArgs): void {
let listData: dataType[]; let liColl: HTMLElement[]; let jsonData: dataType[]; let droppedData: dataType;
let selectedOptions: (string | boolean | number)[]; let sortedData: dataType[];
const dropValue: string | number | boolean = this.getFormattedValue(args.droppedElement.getAttribute('data-value'));
const listObj: ListBox = this.getComponent(args.droppedElement);
const getArgs: Object = this.getDragArgs({ target: args.droppedElement } as DragEventArgs , true);
const sourceArgs: Object = { previousData: this.dataSource }; const destArgs: Object = { previousData: listObj.dataSource };
let dragArgs: Object = extend({}, getArgs, { target: args.target, source: { previousData: this.dataSource },
previousIndex: args.previousIndex, currentIndex: args.currentIndex });
if (listObj !== this) {
const sourceArgs1: Object = extend( sourceArgs, {currentData: this.listData});
dragArgs = extend(dragArgs, { source: sourceArgs1, destination: destArgs} );
}
if (Browser.isIos) {
this.list.style.overflow = '';
}
const targetListObj: ListBox = this.getComponent(args.target);
if (targetListObj && targetListObj.listData.length === 0) {
const noRecElem: Element = targetListObj.ulElement.childNodes[0] as Element;
if (noRecElem) {
targetListObj.ulElement.removeChild(noRecElem);
}
}
if (listObj === this) {
const ul: Element = this.ulElement;
listData = [].slice.call(this.listData); liColl = [].slice.call(this.liCollections);
jsonData = [].slice.call(this.jsonData); sortedData = [].slice.call(this.sortedData);
const toSortIdx: number = args.currentIndex;
let toIdx: number = args.currentIndex = this.getCurIdx(this, args.currentIndex);
const rIdx: number = listData.indexOf(this.getDataByValue(dropValue));
const jsonIdx: number = jsonData.indexOf(this.getDataByValue(dropValue));
const sIdx: number = sortedData.indexOf(this.getDataByValue(dropValue));
listData.splice(toIdx, 0, listData.splice(rIdx, 1)[0] as obj);
sortedData.splice(toSortIdx, 0, sortedData.splice(sIdx, 1)[0] as obj);
jsonData.splice(toIdx, 0, jsonData.splice(jsonIdx, 1)[0] as obj);
liColl.splice(toIdx, 0, liColl.splice(rIdx, 1)[0] as HTMLElement);
if (this.allowDragAll) {
selectedOptions = this.value && Array.prototype.indexOf.call(this.value, dropValue) > -1 ? this.value : [dropValue];
if (!isNullOrUndefined(this.customDraggedItem)) {
selectedOptions = [];
this.customDraggedItem.forEach((item: object) => {
selectedOptions.push(getValue(this.fields.value, item));
});
}
selectedOptions.forEach((value: string) => {
if (value !== dropValue) {
const idx: number = listData.indexOf(this.getDataByValue(value));
const jsonIdx: number = jsonData.indexOf(this.getDataByValue(value));
const sIdx: number = sortedData.indexOf(this.getDataByValue(value));
if (idx > toIdx) {
toIdx++;
}
jsonData.splice(toIdx, 0, jsonData.splice(jsonIdx, 1)[0] as obj);
listData.splice(toIdx, 0, listData.splice(idx, 1)[0] as obj);
sortedData.splice(toSortIdx, 0, sortedData.splice(sIdx, 1)[0] as obj);
liColl.splice(toIdx, 0, liColl.splice(idx, 1)[0] as HTMLElement);
ul.insertBefore(this.getItems()[this.getIndexByValue(value)], ul.getElementsByClassName('e-placeholder')[0]);
}
});
}
(this.listData as dataType[]) = listData; (this.jsonData as dataType[]) = jsonData;
(this.sortedData as dataType[]) = sortedData; this.liCollections = liColl;
} else {
let li: Element; const fLiColl: HTMLElement[] = [].slice.call(this.liCollections);
let currIdx: number = args.currentIndex = this.getCurIdx(listObj, args.currentIndex); const ul: Element = listObj.ulElement;
listData = [].slice.call(listObj.listData); liColl = [].slice.call(listObj.liCollections);
jsonData = [].slice.call(listObj.jsonData); sortedData = [].slice.call(listObj.sortedData);
selectedOptions = (this.value && Array.prototype.indexOf.call(this.value, dropValue) > -1 && this.allowDragAll)
? this.value : [dropValue];
if (!isNullOrUndefined(this.customDraggedItem)) {
selectedOptions = [];
this.customDraggedItem.forEach((item: object) => {
selectedOptions.push(getValue(this.fields.value, item));
});
}
const fListData: dataType[] = [].slice.call(this.listData); const fSortData: dataType[] = [].slice.call(this.sortedData);
selectedOptions.forEach((value: string) => {
droppedData = this.getDataByValue(value);
const srcIdx: number = (this.listData as dataType[]).indexOf(droppedData);
const jsonSrcIdx: number = (this.jsonData as dataType[]).indexOf(droppedData);
const sortIdx: number = (this.sortedData as dataType[]).indexOf(droppedData);
fListData.splice(srcIdx, 1); this.jsonData.splice(jsonSrcIdx, 1);
fSortData.splice(sortIdx, 1); (this.listData as dataType[]) = fListData; (this.sortedData as dataType[]) = fSortData;
const destIdx: number = value === dropValue ? args.currentIndex : currIdx;
listData.splice(destIdx, 0, droppedData); jsonData.splice(destIdx, 0, droppedData);
sortedData.splice(destIdx, 0, droppedData);
liColl.splice(destIdx, 0, fLiColl.splice(srcIdx, 1)[0]);
if (!value) {
const liCollElem: Element[] = this.getItems();
for (let i: number = 0; i < liCollElem.length; i++ ) {
if (liCollElem[i as number].getAttribute('data-value') === null && liCollElem[i as number].classList.contains('e-list-item')) {
li = liCollElem[i as number];
break;
}
}
} else {
li = this.getItems()[this.getIndexByValue(value)];
}
if (!li) { li = args.helper; }
this.removeSelected(this, value === dropValue ? [args.droppedElement] : [li]);
ul.insertBefore(li, ul.getElementsByClassName('e-placeholder')[0]);
currIdx++;
});
if (this.fields.groupBy) {
const sourceElem: HTMLElement = this.renderItems(this.listData as obj[], this.fields);
this.updateListItems(sourceElem, this.ulElement); this.setSelection();
}
if (listObj.sortOrder !== 'None' || this.selectionSettings.showCheckbox
!== listObj.selectionSettings.showCheckbox || listObj.fields.groupBy || listObj.itemTemplate || this.itemTemplate) {
const sortable: { placeHolderElement: Element } = getComponent(ul as HTMLElement, 'sortable');
const sourceElem: HTMLElement = listObj.renderItems(listData as obj[], listObj.fields);
listObj.updateListItems(sourceElem, ul as HTMLElement); this.setSelection();
if (sortable.placeHolderElement) {
ul.appendChild(sortable.placeHolderElement);
}
ul.appendChild(args.helper); listObj.setSelection();
}
this.liCollections = fLiColl; listObj.liCollections = liColl;
(listObj.jsonData as dataType[]) = extend([], [], jsonData, false) as dataType[];
(listObj.listData as dataType[]) = extend([], [], listData, false) as dataType[];
if (listObj.sortOrder === 'None') {
(listObj.sortedData as dataType[]) = extend([], [], sortedData, false) as dataType[];
}
if (this.listData.length === 0) {
this.l10nUpdate();
}
}
if (this === listObj) {
const sourceArgs1: Object = extend( sourceArgs, {currentData: listData});
dragArgs = extend(dragArgs, {source: sourceArgs1});
} else {
const dragArgs1: Object = extend(destArgs, {currentData: listData});
dragArgs = extend(dragArgs, { destination: dragArgs1 });
}
if (!isNullOrUndefined(this.customDraggedItem)) {
(dragArgs as DragEventArgs).items = this.customDraggedItem;
}