forked from syncfusion/ej2-javascript-ui-controls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-view.ts
2706 lines (2516 loc) · 108 KB
/
list-view.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
import { Virtualization } from './virtualization';
import { merge, formatUnit, isNullOrUndefined, append, detach, ModuleDeclaration, extend } from '@syncfusion/ej2-base';
import { attributes, addClass, removeClass, prepend, closest, remove } from '@syncfusion/ej2-base';
import { Component, EventHandler, BaseEventArgs, Property, Complex, Event } from '@syncfusion/ej2-base';
import { NotifyPropertyChanges, INotifyPropertyChanged, ChildProperty } from '@syncfusion/ej2-base';
import { KeyboardEventArgs, EmitType, compile, SanitizeHtmlHelper } from '@syncfusion/ej2-base';
import { Animation, AnimationOptions, Effect, rippleEffect, Touch, SwipeEventArgs, animationMode } from '@syncfusion/ej2-base';
import { DataManager, Query } from '@syncfusion/ej2-data';
import { createCheckBox } from '@syncfusion/ej2-buttons';
import { ListBase, ListBaseOptions, SortOrder, getFieldValues, FieldsMapping } from '../common/list-base';
import { ListViewModel, FieldSettingsModel } from './list-view-model';
// Effect Configuration Effect[] = [fromViewBackward,fromViewForward,toViewBackward,toviewForward];
const effectsConfig: { [key: string]: Effect[] } = {
'None': [],
'SlideLeft': ['SlideRightOut', 'SlideLeftOut', 'SlideLeftIn', 'SlideRightIn'],
'SlideDown': ['SlideTopOut', 'SlideBottomOut', 'SlideBottomIn', 'SlideTopIn'],
'Zoom': ['FadeOut', 'FadeZoomOut', 'FadeZoomIn', 'FadeIn'],
'Fade': ['FadeOut', 'FadeOut', 'FadeIn', 'FadeIn']
};
const effectsRTLConfig: { [key: string]: Effect[] } = {
'None': [],
'SlideLeft': ['SlideLeftOut', 'SlideRightOut', 'SlideRightIn', 'SlideLeftIn'],
'SlideDown': ['SlideBottomOut', 'SlideTopOut', 'SlideTopIn', 'SlideBottomIn'],
'Zoom': ['FadeZoomOut', 'FadeOut', 'FadeIn', 'FadeZoomIn'],
'Fade': ['FadeOut', 'FadeOut', 'FadeIn', 'FadeIn']
};
// don't use space in classnames.
export const classNames: ClassNames = {
root: 'e-listview',
hover: 'e-hover',
selected: 'e-active',
focused: 'e-focused',
parentItem: 'e-list-parent',
listItem: 'e-list-item',
listIcon: 'e-list-icon',
textContent: 'e-text-content',
listItemText: 'e-list-text',
groupListItem: 'e-list-group-item',
hasChild: 'e-has-child',
view: 'e-view',
header: 'e-list-header',
headerText: 'e-headertext',
headerTemplateText: 'e-headertemplate-text',
text: 'e-text',
disable: 'e-disabled',
container: 'e-list-container',
icon: 'e-icons',
backIcon: 'e-icon-back',
backButton: 'e-back-button',
checkboxWrapper: 'e-checkbox-wrapper',
checkbox: 'e-checkbox',
checked: 'e-check',
checklist: 'e-checklist',
checkboxIcon: 'e-frame',
checkboxRight: 'e-checkbox-right',
checkboxLeft: 'e-checkbox-left',
listviewCheckbox: 'e-listview-checkbox',
itemCheckList: 'e-checklist',
virtualElementContainer: 'e-list-virtualcontainer'
};
const LISTVIEW_TEMPLATE_PROPERTY: string = 'Template';
const LISTVIEW_GROUPTEMPLATE_PROPERTY: string = 'GroupTemplate';
const LISTVIEW_HEADERTEMPLATE_PROPERTY: string = 'HeaderTemplate';
const swipeVelocity: number = 0.5;
/**
* An interface that holds options of fields.
*/
export interface Fields {
/**
* Specifies the id field mapped in dataSource.
*/
id?: string | number;
/**
* The `text` property is used to map the text value from the data source for each list item.
*/
text?: string | number;
/**
* It is used to map the custom field values of list item from the dataSource.
*/
[key: string]: Object | string | number | undefined;
}
/**
* Represents the field settings of the ListView.
*/
export class FieldSettings extends ChildProperty<FieldSettings> {
/**
* Specifies the id field mapped in dataSource.
*/
@Property('id')
public id: string;
/**
* The `text` property is used to map the text value from the data source for each list item.
*/
@Property('text')
public text: string;
/**
* The `isChecked` property is used to check whether the list items are in checked state or not.
*/
@Property('isChecked')
public isChecked: string;
/**
* The `isVisible` property is used to check whether the list items are in visible state or not.
*/
@Property('isVisible')
public isVisible: string;
/**
* Specifies the enabled state of the ListView component.
* And, we can disable the component using this property by setting its value as false.
*/
@Property('enabled')
public enabled: string;
/**
* The `iconCss` is used to customize the icon to the list items dynamically.
* We can add a specific image to the icons using `iconCss` property.
*/
@Property('iconCss')
public iconCss: string;
/**
* The `child` property is used for nested navigation of listed items.
*/
@Property('child')
public child: string;
/**
* The `tooltip` is used to display the information about the target element while hovering on list items.
*/
@Property('tooltip')
public tooltip: string;
/**
* The `groupBy` property is used to wraps the ListView elements into a group.
*/
@Property('groupBy')
public groupBy: string;
/**
* The `sortBy` property used to enable the sorting of list items to be ascending or descending order.
*/
@Property('text')
public sortBy: string;
/**
* The `htmlAttributes` allows additional attributes such as id, class, etc., and
* accepts n number of attributes in a key-value pair format.
*/
@Property('htmlAttributes')
public htmlAttributes: string;
/**
* Specifies the `tableName` used to fetch data from a specific table in the server.
*/
@Property('tableName')
public tableName: string;
}
/**
* An interface that holds animation settings.
*/
export interface AnimationSettings {
/**
* It is used to specify the effect which is shown in sub list transform.
*/
effect?: ListViewEffect;
/**
* It is used to specify the time duration of transform object.
*/
duration?: number;
/**
* It is used to specify the easing effect applied while transform
*/
easing?: string;
}
/**
* An enum type that denotes the effects of the ListView. Available options are as follows None, SlideLeft, SlideDown, Zoom, Fade;
* ```props
* None :- No animation is applied when items are added or removed from the ListView.
* SlideLeft :- Items slide in from the left when added and slide out to the left when removed.
* SlideDown :- Items slide in from the top when added and slide out to the top when removed.
* Zoom :- Items zoom in or out when added or removed.
* Fade :- Items fade in or out when added or removed.
* ```
*/
export type ListViewEffect = 'None' | 'SlideLeft' | 'SlideDown' | 'Zoom' | 'Fade';
/**
* An enum type that denotes the position of checkbox of the ListView. Available options are as follows Left and Right;
* ```props
* Left :- The checkbox is positioned on the left side of the ListView item.
* Right :- The checkbox is positioned on the right side of the ListView item.
* ```
*/
export type checkBoxPosition = 'Left' | 'Right';
/**
* Represents the EJ2 ListView control.
* ```html
* <div id="listview">
* <ul>
* <li>Favorite</li>
* <li>Documents</li>
* <li>Downloads</li>
* </ul>
* </div>
* ```
* ```typescript
* var listviewObject = new ListView({});
* listviewObject.appendTo("#listview");
* ```
*/
@NotifyPropertyChanges
export class ListView extends Component<HTMLElement> implements INotifyPropertyChanged {
private ulElement: HTMLElement;
private selectedLI: Element;
private onUIScrolled: Function;
private curUL: HTMLElement;
private curDSLevel: string[];
private curViewDS: DataSource[] | string[] | number[];
private curDSJSON: DataSource;
public localData: DataSource[];
private liCollection: HTMLElement[];
private headerEle: HTMLElement;
private contentContainer: HTMLElement;
private touchModule: Touch;
private listBaseOption: ListBaseOptions;
public virtualizationModule: Virtualization;
private animateOptions: AnimationOptions;
private rippleFn: Function;
private isNestedList: boolean;
private currentLiElements: HTMLElement[];
private selectedData: string[] | string;
private selectedId: string[];
private isWindow: boolean;
private selectedItems: SelectedItem;
private aniObj: Animation;
private LISTVIEW_TEMPLATE_ID: string;
private LISTVIEW_GROUPTEMPLATE_ID: string;
private LISTVIEW_HEADERTEMPLATE_ID: string;
private liElement: Element;
private virtualCheckBox: Element | string;
private liElementHeight: number;
private previousSelectedItems: string[] = [];
private hiddenItems: string[] = [];
private enabledItems: string[] = [];
private disabledItems: string[] = [];
private isOffline: boolean;
private previousScrollTop: number;
/**
* The `cssClass` property is used to add a user-preferred class name in the root element of the ListView,
* using which we can customize the component (both CSS and functionality customization)
*
* {% codeBlock src='listview/cssClass/index.md' %}{% endcodeBlock %}
*
* @default ''
*/
@Property('')
public cssClass: string;
/**
* If `enableVirtualization` set to true, which will increase the ListView performance, while loading a large amount of data.
*
* {% codeBlock src='listview/enableVirtualization/index.md' %}{% endcodeBlock %}
*
* @default false
*/
@Property(false)
public enableVirtualization: boolean;
/**
* The `htmlAttributes` allows additional attributes such as id, class, etc., and
* accepts n number of attributes in a key-value pair format.
*
* {% codeBlock src='listview/htmlAttributes/index.md' %}{% endcodeBlock %}
*
* @default {}
*/
@Property({})
public htmlAttributes: { [key: string]: string; };
/**
* If `enable` set to true, the list items are enabled.
* And, we can disable the component using this property by setting its value as false.
*
* {% codeBlock src='listview/enable/index.md' %}{% endcodeBlock %}
*
* @default true
*/
@Property(true)
public enable: boolean;
/* es-lint disable */
/**
* The `dataSource` provides the data to render the ListView component which is mapped with the fields of ListView.
*
* @isGenericType true
*
* {% codeBlock src='listview/dataSource/index.md' %}{% endcodeBlock %}
*
* @default []
*/
@Property([])
public dataSource: { [key: string]: Object }[] | string[] | number[] | DataManager;
/* es-lint enable */
/**
* The `query` is used to fetch the specific data from dataSource by using where and select keywords.
*
* {% codeBlock src='listview/query/index.md' %}{% endcodeBlock %}
*
* @default null
*/
@Property()
public query: Query;
/**
* The `fields` is used to map keys from the dataSource which extracts the appropriate data from the dataSource
* with specified mapped with the column fields to render the ListView.
*
* {% codeBlock src='listview/fields/index.md' %}{% endcodeBlock %}
*
* @default defaultMappedFields
*/
@Complex<FieldSettingsModel>(ListBase.defaultMappedFields, FieldSettings)
public fields: FieldSettingsModel;
/**
* The `animation` property provides an option to apply the different
* animations on the ListView component.
*
* {% codeBlock src='listview/animation/index.md' %}{% endcodeBlock %}
*
*
* @default { effect: 'SlideLeft', duration: 400, easing: 'ease' }
*/
@Property<AnimationSettings>({ effect: 'SlideLeft', duration: 400, easing: 'ease' })
public animation: AnimationSettings;
/**
* The `sortOrder` is used 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.
*
* {% codeBlock src='listview/sortOrder/index.md' %}{% endcodeBlock %}
*
* @default 'None'
*/
@Property<SortOrder>('None')
public sortOrder: SortOrder;
/**
* If `showIcon` set to true, which will show or hide the icon of the list item.
*
* {% codeBlock src='listview/showIcon/index.md' %}{% endcodeBlock %}
*
* @default false
*/
@Property<boolean>(false)
public showIcon: boolean;
/**
* If `showCheckBox` set to true, which will show or hide the checkbox.
*
* {% codeBlock src='listview/showCheckBox/index.md' %}{% endcodeBlock %}
*
*
* @default false
*/
@Property<boolean>(false)
public showCheckBox: boolean;
/**
* The `checkBoxPosition` is used to set the position of check box in a list item.
* By default, the `checkBoxPosition` is Left, which will appear before the text content in a list item.
*
* {% codeBlock src='listview/checkBoxPosition/index.md' %}{% endcodeBlock %}
*
* @default 'Left'
*/
@Property<string>('Left')
public checkBoxPosition: checkBoxPosition;
/**
* The `headerTitle` is used to set the title of the ListView component.
*
* {% codeBlock src='listview/headerTitle/index.md' %}{% endcodeBlock %}
*
*
* @default ""
*/
@Property<string>('')
public headerTitle: string;
/**
* If `showHeader` set to true, which will show or hide the header of the ListView component.
*
* {% codeBlock src='listview/showHeader/index.md' %}{% endcodeBlock %}
*
* @default false
*/
@Property<boolean>(false)
public showHeader: boolean;
/**
* Specifies whether HTML content should be sanitized or escaped.
* When set to `true`, any HTML content will be sanitized to remove potentially harmful elements.
*
* {% codeBlock src='listview/enableHtmlSanitizer/index.md' %}{% endcodeBlock %}
*
* @default false
*/
@Property(false)
public enableHtmlSanitizer: boolean;
/**
* Defines the height of the ListView component which accepts both string and number values.
*
* {% codeBlock src='listview/height/index.md' %}{% endcodeBlock %}
*
* @default ''
*/
@Property('')
public height: number | string;
/**
* Defines the width of the ListView component which accepts both string and number values.
*
* {% codeBlock src='listview/width/index.md' %}{% endcodeBlock %}
*
* @default ''
*/
@Property('')
public width: number | string;
/**
* The ListView component supports to customize the content of each list items with the help of `template` property.
*
* {% codeBlock src='listview/template/index.md' %}{% endcodeBlock %}
*
* @default null
* @angularType string | object
* @reactType string | function | JSX.Element
* @vueType string | function
* @aspType string
*/
@Property(null)
public template: string | Function;
/**
* The ListView has an option to custom design the ListView header title with the help of `headerTemplate` property.
*
* {% codeBlock src="listview/headerTemplate/index.md" %}{% endcodeBlock %}
*
* @default null
* @angularType string | object
* @reactType string | function | JSX.Element
* @vueType string | function
* @aspType string
*/
@Property(null)
public headerTemplate: string | Function;
/**
* The ListView has an option to custom design the group header title with the help of `groupTemplate` property.
*
* {% codeBlock src="listview/groupTemplate/index.md" %}{% endcodeBlock %}
*
* @default null
* @angularType string | object
* @reactType string | function | JSX.Element
* @vueType string | function
* @aspType string
*/
@Property(null)
public groupTemplate: string | Function;
/**
* Triggers when we select the list item in the component.
*
* @event 'object'
*/
@Event()
public select: EmitType<SelectEventArgs>;
/**
* Triggers when every ListView action starts.
*
* @event 'object'
*/
@Event()
public actionBegin: EmitType<object>;
/**
* Triggers when every ListView actions completed.
*
* @event 'object'
*/
/* es-lint disable */
@Event()
public actionComplete: EmitType<MouseEvent>;
/* es-lint enable */
/**
* Triggers, when the data fetch request from the remote server, fails.
*
* @event 'object'
*
*/
@Event()
public actionFailure: EmitType<MouseEvent>;
/**
* Triggers when scrollbar of the ListView component reaches to the top or bottom.
*
* @event 'object'
*/
@Event()
public scroll: EmitType<ScrolledEventArgs>;
/**
* Constructor for creating the widget
*
* @param options
*
* @param element
*/
constructor(options?: ListViewModel, element?: string | HTMLElement) {
super(options, <HTMLElement | string>element);
}
/**
* @param newProp
*
* @param oldProp
*
* @private
*/
public onPropertyChanged(newProp: ListViewModel, oldProp: ListViewModel): void {
for (const prop of Object.keys(newProp)) {
switch (prop) {
case 'htmlAttributes':
this.setHTMLAttribute();
break;
case 'cssClass':
this.setCSSClass(oldProp.cssClass);
break;
case 'enable':
this.setEnable();
break;
case 'width':
case 'height':
this.setSize();
break;
case 'enableRtl':
this.setEnableRTL();
break;
case 'fields':
// eslint-disable-next-line
this.listBaseOption.fields = (this.fields as any & { properties: object }).properties;
if (this.enableVirtualization) {
this.virtualizationModule.reRenderUiVirtualization();
} else {
this.reRender();
}
break;
case 'headerTitle':
if (!this.curDSLevel.length) {
this.header(this.headerTitle, false, 'header');
}
break;
case 'query':
if (this.enableVirtualization) {
this.virtualizationModule.reRenderUiVirtualization();
} else {
this.reRender();
}
break;
case 'showHeader':
this.header(this.headerTitle, false, 'header');
break;
case 'enableVirtualization':
if (!isNullOrUndefined(this.contentContainer)) {
detach(this.contentContainer);
}
this.refresh();
break;
case 'showCheckBox':
case 'checkBoxPosition':
if (this.enableVirtualization) {
this.virtualizationModule.reRenderUiVirtualization();
} else {
this.setCheckbox();
}
break;
case 'dataSource':
if (this.enableVirtualization) {
this.virtualizationModule.reRenderUiVirtualization();
} else {
this.reRender();
}
break;
case 'sortOrder':
case 'template':
if (!this.enableVirtualization) {
this.refresh();
}
break;
case 'showIcon':
if (this.enableVirtualization) {
this.virtualizationModule.reRenderUiVirtualization();
} else {
this.listBaseOption.showIcon = this.showIcon;
this.curViewDS = this.getSubDS();
this.resetCurrentList();
}
break;
default:
break;
}
}
}
// Model Changes
private setHTMLAttribute(): void {
if (Object.keys(this.htmlAttributes).length) {
attributes(this.element, this.htmlAttributes);
}
}
private setCSSClass(oldCSSClass?: string): void {
if (this.cssClass) {
addClass([this.element], this.cssClass.split(' ').filter((css: string) => css));
}
if (oldCSSClass) {
removeClass([this.element], oldCSSClass.split(' ').filter((css: string) => css));
}
}
private setSize(): void {
this.element.style.height = formatUnit(this.height);
this.element.style.width = formatUnit(this.width);
this.isWindow = this.element.clientHeight ? false : true;
}
private setEnable(): void {
this.enableElement(this.element, this.enable);
}
private setEnableRTL(): void {
if (this.enableRtl) {
this.element.classList.add('e-rtl');
} else {
this.element.classList.remove('e-rtl');
}
}
private enableElement(element: HTMLElement, isEnabled?: boolean): void {
if (isEnabled) {
element.classList.remove(classNames.disable);
} else {
element.classList.add(classNames.disable);
}
}
// Support Component Functions
private header(text?: string, showBack?: boolean, prop?: string): void {
if (this.headerEle === undefined && this.showHeader) {
this.headerEle = this.createElement('div', { className: classNames.header });
const innerHeaderEle: HTMLElement = this.createElement('span', { className: classNames.headerText });
if (this.enableHtmlSanitizer) {
this.setProperties({ headerTitle: SanitizeHtmlHelper.sanitize(this.headerTitle) }, true);
innerHeaderEle.innerText = this.headerTitle;
} else {
innerHeaderEle.innerHTML = this.headerTitle;
}
const textEle: HTMLElement = this.createElement('div', { className: classNames.text, innerHTML: innerHeaderEle.outerHTML });
const hedBackButton: HTMLElement = this.createElement('div', {
className: classNames.icon + ' ' + classNames.backIcon + ' ' + classNames.backButton,
attrs: { style: 'display:none;' }
});
this.headerEle.appendChild(hedBackButton);
this.headerEle.appendChild(textEle);
if (this.headerTemplate) {
const compiledString: Function = compile(this.headerTemplate);
const headerTemplateEle: HTMLElement = this.createElement('div', { className: classNames.headerTemplateText });
// eslint-disable-next-line
const compiledElement: any = compiledString({}, this, prop, this.LISTVIEW_HEADERTEMPLATE_ID, null, null, this.headerEle);
if (compiledElement) {
append(compiledElement, headerTemplateEle);
}
append([headerTemplateEle], this.headerEle);
// eslint-disable-next-line
if ((this as any).isReact) {
this.renderReactTemplates();
}
}
if (this.headerTemplate && this.headerTitle) {
textEle.classList.add('header');
}
this.element.classList.add('e-has-header');
prepend([this.headerEle], this.element);
} else if (this.headerEle) {
if (this.showHeader) {
this.headerEle.style.display = '';
const textEle: Element = this.headerEle.querySelector('.' + classNames.headerText);
const hedBackButton: Element = this.headerEle.querySelector('.' + classNames.backIcon);
if (this.enableHtmlSanitizer) {
text = SanitizeHtmlHelper.sanitize(text);
}
textEle.innerHTML = text;
if (this.headerTemplate && showBack) {
textEle.parentElement.classList.remove('header');
this.headerEle.querySelector('.' + classNames.headerTemplateText).classList.add('nested-header');
}
if (this.headerTemplate && !showBack) {
textEle.parentElement.classList.add('header');
this.headerEle.querySelector('.' + classNames.headerTemplateText).classList.remove('nested-header');
this.headerEle.querySelector('.' + classNames.headerTemplateText).classList.add('header');
}
if (showBack === true) {
(hedBackButton as HTMLElement).style.display = '';
} else {
(hedBackButton as HTMLElement).style.display = 'none';
}
} else {
this.headerEle.style.display = 'none';
}
}
}
// Animation Related Functions
private switchView(fromView: HTMLElement, toView: HTMLElement, reverse?: boolean): void {
if (fromView && toView) {
const fPos: string = fromView.style.position;
const overflow: string = (this.element.style.overflow !== 'hidden') ? this.element.style.overflow : '';
fromView.style.position = 'absolute';
fromView.classList.add('e-view');
let anim: Effect[];
let duration: number = this.animation.duration;
if (this.animation.effect) {
anim = (this.enableRtl ? effectsRTLConfig[this.animation.effect] : effectsConfig[this.animation.effect]);
} else {
const slideLeft: string = 'SlideLeft';
anim = effectsConfig[`${slideLeft}`];
reverse = this.enableRtl;
duration = 0;
}
this.element.style.overflow = 'hidden';
this.aniObj.animate(fromView, {
name: (reverse === true ? anim[0] : anim[1]),
duration: (duration === 0 && animationMode === 'Enable') ? 400 : duration,
timingFunction: this.animation.easing,
// eslint-disable-next-line
end: (model: AnimationOptions): void => {
fromView.style.display = 'none';
this.element.style.overflow = overflow;
fromView.style.position = fPos;
fromView.classList.remove('e-view');
}
});
toView.style.display = '';
this.aniObj.animate(toView, {
name: (reverse === true ? anim[2] : anim[3]),
duration: (duration === 0 && animationMode === 'Enable') ? 400 : duration,
timingFunction: this.animation.easing,
end: (): void => {
this.trigger('actionComplete');
}
});
this.curUL = toView;
}
}
protected preRender(): void {
if (this.template) {
try {
if (typeof this.template !== 'function' && document.querySelectorAll(this.template).length) {
this.setProperties({ template: document.querySelector(this.template).innerHTML.trim() }, true);
}
} catch (e) {
compile(this.template);
}
}
this.listBaseOption = {
template: this.template,
headerTemplate: this.headerTemplate,
groupTemplate: this.groupTemplate, expandCollapse: true, listClass: '',
ariaAttributes: {
itemRole: 'listitem', listRole: 'list', itemText: '',
groupItemRole: 'presentation', wrapperRole: 'presentation'
},
// eslint-disable-next-line
fields: ((this.fields as any & { properties: Object }).properties) as FieldsMapping,
sortOrder: this.sortOrder,
showIcon: this.showIcon,
itemCreated: this.renderCheckbox.bind(this),
templateID: `${this.element.id}${LISTVIEW_TEMPLATE_PROPERTY}`,
groupTemplateID: `${this.element.id}${LISTVIEW_GROUPTEMPLATE_PROPERTY}`,
enableHtmlSanitizer: this.enableHtmlSanitizer
};
this.initialization();
}
private initialization(): void {
this.curDSLevel = [];
this.animateOptions = {};
this.curViewDS = [];
this.currentLiElements = [];
this.isNestedList = false;
this.selectedData = [];
this.selectedId = this.enablePersistence ? this.selectedId : [];
this.LISTVIEW_TEMPLATE_ID = `${this.element.id}${LISTVIEW_TEMPLATE_PROPERTY}`;
this.LISTVIEW_GROUPTEMPLATE_ID = `${this.element.id}${LISTVIEW_GROUPTEMPLATE_PROPERTY}`;
this.LISTVIEW_HEADERTEMPLATE_ID = `${this.element.id}${LISTVIEW_HEADERTEMPLATE_PROPERTY}`;
this.aniObj = new Animation(this.animateOptions);
this.removeElement(this.curUL);
this.removeElement(this.ulElement);
this.removeElement(this.headerEle);
this.removeElement(this.contentContainer);
this.curUL = this.ulElement = this.liCollection = this.headerEle = this.contentContainer = undefined;
}
private renderCheckbox(args: ItemCreatedArgs): void {
if (args.item.classList.contains(classNames.hasChild)) {
this.isNestedList = true;
}
if (this.showCheckBox && args.item.classList.contains(classNames.listItem)) {
let checkboxElement: Element;
let fieldData: DataSource;
// eslint-disable-next-line prefer-const
checkboxElement = createCheckBox(this.createElement, false, {
checked: false, enableRtl: this.enableRtl,
cssClass: classNames.listviewCheckbox
});
checkboxElement.setAttribute('role', 'checkbox');
const frameElement: Element = checkboxElement.querySelector('.' + classNames.checkboxIcon);
args.item.classList.add(classNames.itemCheckList);
args.item.firstElementChild.classList.add(classNames.checkbox);
if (typeof (this.dataSource as string[])[0] !== 'string' && typeof (this.dataSource as number[])[0] !== 'number') {
fieldData = <DataSource>getFieldValues(args.curData, this.listBaseOption.fields);
if (this.enablePersistence && !isNullOrUndefined(this.selectedId)) {
const index: number = this.selectedId.findIndex(e => e === fieldData[this.listBaseOption.fields.id].toString());
if (index !== -1) {
this.checkInternally(args, checkboxElement);
}
}
else if (<object>fieldData[this.listBaseOption.fields.isChecked]) {
this.checkInternally(args, checkboxElement);
}
} else if (((typeof (this.dataSource as string[])[0] === 'string' ||
typeof (this.dataSource as number[])[0] === 'number') && this.selectedData.indexOf(args.text) !== -1)) {
this.checkInternally(args, checkboxElement);
}
checkboxElement.setAttribute('aria-checked', frameElement.classList.contains(classNames.checked) ? 'true' : 'false');
checkboxElement.setAttribute('aria-label', args.text);
if (this.checkBoxPosition === 'Left') {
checkboxElement.classList.add(classNames.checkboxLeft);
args.item.firstElementChild.classList.add(classNames.checkboxLeft);
args.item.firstElementChild.insertBefore(checkboxElement, args.item.firstElementChild.childNodes[0]);
} else {
checkboxElement.classList.add(classNames.checkboxRight);
args.item.firstElementChild.classList.add(classNames.checkboxRight);
args.item.firstElementChild.appendChild(checkboxElement);
}
this.currentLiElements.push(args.item);
if (this.checkBoxPosition === 'Left') {
this.virtualCheckBox = args.item.firstElementChild.children[0];
}
else {
this.virtualCheckBox = args.item.firstElementChild.lastElementChild;
}
}
}
private checkInternally(args: ItemCreatedArgs, checkboxElement: Element): void {
args.item.classList.add(classNames.selected);
checkboxElement.querySelector('.' + classNames.checkboxIcon).classList.add(classNames.checked);
checkboxElement.setAttribute('aria-checked', 'true');
}
/**
* Checks the specific list item by passing the unchecked fields as an argument to this method.
*
* @param {Fields | HTMLElement | Element} item - It accepts Fields or HTML list element as an argument.
*/
public checkItem(item: Fields | HTMLElement | Element): void {
this.toggleCheckBase(item, true);
}
private toggleCheckBase(item: Fields | Element | HTMLElement, checked: boolean): void {
if (this.showCheckBox) {
let liElement: Element = item as Element;
if (item instanceof Object && (item as Object).constructor !== HTMLLIElement) {
liElement = this.getLiFromObjOrElement(item);
}
if (!isNullOrUndefined(liElement)) {
const checkboxIcon: Element = liElement.querySelector('.' + classNames.checkboxIcon);
if (checked === true) {
liElement.classList.add(classNames.selected);
}
else {
liElement.classList.remove(classNames.selected);
}
if (checked === true) {
checkboxIcon.classList.add(classNames.checked);
}
else {
checkboxIcon.classList.remove(classNames.checked);
}
checkboxIcon.parentElement.setAttribute('aria-checked', checked ? 'true' : 'false');
}
this.setSelectedItemData(liElement);
this.updateSelectedId();
}
}
/**
* Uncheck the specific list item by passing the checked fields as an argument to this method.
*
* @param {Fields | HTMLElement | Element} item - It accepts Fields or HTML list element as an argument.
*/
public uncheckItem(item: Fields | HTMLElement | Element): void {
this.toggleCheckBase(item, false);
}
/**
* Checks all the unchecked items in the ListView.
*/
public checkAllItems(): void {
this.toggleAllCheckBase(true);
}
/**
* Uncheck all the checked items in ListView.
*/
public uncheckAllItems(): void {
this.toggleAllCheckBase(false);
}
private toggleAllCheckBase(checked: boolean): void {
if (this.showCheckBox) {
for (let i: number = 0; i < this.liCollection.length; i++) {
const checkIcon: Element = this.liCollection[i as number].querySelector('.' + classNames.checkboxIcon);
if (checkIcon) {
if (checked) {
if (!checkIcon.classList.contains(classNames.checked)) {
this.checkItem(this.liCollection[i as number]);
}
} else {
if (checkIcon.classList.contains(classNames.checked)) {
this.uncheckItem(this.liCollection[i as number]);
}
}
}
}
if (this.enableVirtualization) {
this.virtualizationModule.checkedItem(checked);
}
this.updateSelectedId();
}
}
private setCheckbox(): void {
if (this.showCheckBox) {
const liCollection: HTMLElement[] = Array.prototype.slice.call(this.element.querySelectorAll('.' + classNames.listItem));
const args: ItemCreatedArgs = {
item: undefined, curData: undefined, dataSource: undefined, fields: undefined,
options: undefined, text: ''
};
for (let i: number = 0; i < liCollection.length; i++) {
const element: HTMLElement = liCollection[i as number];
args.item = element;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args.curData = this.getItemData(element) as { [key: string]: Object } as any;
if (element.querySelector('.' + classNames.checkboxWrapper)) {
this.removeElement(element.querySelector('.' + classNames.checkboxWrapper));
}
this.renderCheckbox(args);
if (args.item.classList.contains(classNames.selected)) {
this.checkInternally(args, args.item.querySelector('.' + classNames.checkboxWrapper));
}
}
} else {
const liCollection: HTMLElement[] = Array.prototype.slice.call(this.element.querySelectorAll('.' + classNames.itemCheckList));
for (let i: number = 0; i < liCollection.length; i++) {
const element: HTMLElement = liCollection[i as number];