forked from syncfusion/ej2-javascript-ui-controls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtab.ts
2812 lines (2777 loc) · 123 KB
/
tab.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 { Component, Property, Event, EmitType, closest, Collection, Complex, attributes, detach, Instance, isNullOrUndefined, animationMode } from '@syncfusion/ej2-base';
import { INotifyPropertyChanged, NotifyPropertyChanges, ChildProperty, select, isVisible } from '@syncfusion/ej2-base';
import { KeyboardEvents, KeyboardEventArgs, MouseEventArgs, Effect, Browser, formatUnit, DomElements, L10n } from '@syncfusion/ej2-base';
import { setStyleAttribute as setStyle, isNullOrUndefined as isNOU, selectAll, addClass, removeClass, remove } from '@syncfusion/ej2-base';
import { EventHandler, rippleEffect, Touch, SwipeEventArgs, compile, Animation, AnimationModel, BaseEventArgs } from '@syncfusion/ej2-base';
import { getRandomId, SanitizeHtmlHelper, Draggable, DragEventArgs as DragArgs, DropEventArgs } from '@syncfusion/ej2-base';
import { Base } from '@syncfusion/ej2-base';
import { Popup, PopupModel } from '@syncfusion/ej2-popups';
import { Toolbar, OverflowMode, ClickEventArgs } from '../toolbar/toolbar';
import { TabModel, TabItemModel, HeaderModel, TabActionSettingsModel, TabAnimationSettingsModel } from './tab-model';
type HTEle = HTMLElement;
type Str = string;
/**
* Specifies the orientation of the Tab header.
* ```props
* Top :- Places the Tab header on the top.
* Bottom :- Places the Tab header on the bottom.
* Left :- Places the Tab header on the left.
* Right :- Places the Tab header on the right.
* ```
*/
export type HeaderPosition = 'Top' | 'Bottom' | 'Left' | 'Right';
/**
* Options to set the content element height adjust modes.
* ```props
* None :- Based on the given height property, the content panel height is set.
* Auto :- Tallest panel height of a given Tab content is set to all the other panels.
* Content :- Based on the corresponding content height, the content panel height is set.
* Fill :- Content element take height based on the parent height.
* ```
*/
export type HeightStyles = 'None' | 'Auto' | 'Content' | 'Fill';
/**
* Enables or disables the tab swiping action through Touch and Mouse.
* - `Both`: Enables swiping for both touch and mouse input.
* - `Touch`: Enables swiping only for touch input.
* - `Mouse`: Enables swiping only for mouse input.
* - `None`: Disables swiping for both touch and mouse input.
*/
export type TabSwipeMode = 'Both' | 'Touch' | 'Mouse' | 'None'
/**
* Specifies the options of Tab content display mode.
* ```props
* Demand :- The content of the selected tab alone is loaded initially. The content of the tabs which were loaded once will be maintained in the DOM.
* Dynamic :- The content of all the tabs are rendered on the initial load and maintained in the DOM.
* Init :- The content of all the tabs are rendered on the initial load and maintained in the DOM.
* ```
*/
export type ContentLoad = 'Dynamic' | 'Init' | 'Demand';
const CLS_TAB: string = 'e-tab';
const CLS_HEADER: string = 'e-tab-header';
const CLS_BLA_TEM: string = 'blazor-template';
const CLS_CONTENT: string = 'e-content';
const CLS_NEST: string = 'e-nested';
const CLS_ITEMS: string = 'e-items';
const CLS_ITEM: string = 'e-item';
const CLS_TEMPLATE: string = 'e-template';
const CLS_RTL: string = 'e-rtl';
const CLS_ACTIVE: string = 'e-active';
const CLS_DISABLE: string = 'e-disable';
const CLS_HIDDEN: string = 'e-hidden';
const CLS_FOCUS: string = 'e-focused';
const CLS_ICONS: string = 'e-icons';
const CLS_ICON: string = 'e-icon';
const CLS_ICON_TAB: string = 'e-icon-tab';
const CLS_ICON_CLOSE: string = 'e-close-icon';
const CLS_CLOSE_SHOW: string = 'e-close-show';
const CLS_TEXT: string = 'e-tab-text';
const CLS_INDICATOR: string = 'e-indicator';
const CLS_WRAP: string = 'e-tab-wrap';
const CLS_TEXT_WRAP: string = 'e-text-wrap';
const CLS_TAB_ICON: string = 'e-tab-icon';
const CLS_TB_ITEMS: string = 'e-toolbar-items';
const CLS_TB_ITEM: string = 'e-toolbar-item';
const CLS_TB_POP: string = 'e-toolbar-pop';
const CLS_TB_POPUP: string = 'e-toolbar-popup';
const CLS_HOR_NAV: string = 'e-hor-nav';
const CLS_POPUP_OPEN: string = 'e-popup-open';
const CLS_POPUP_CLOSE: string = 'e-popup-close';
const CLS_PROGRESS: string = 'e-progress';
const CLS_IGNORE: string = 'e-ignore';
const CLS_OVERLAY: string = 'e-overlay';
const CLS_HSCRCNT: string = 'e-hscroll-content';
const CLS_VSCRCNT: string = 'e-vscroll-content';
const CLS_VTAB: string = 'e-vertical-tab';
const CLS_VERTICAL: string = 'e-vertical';
const CLS_VLEFT: string = 'e-vertical-left';
const CLS_VRIGHT: string = 'e-vertical-right';
const CLS_HBOTTOM: string = 'e-horizontal-bottom';
const CLS_FILL: string = 'e-fill-mode';
const TABITEMPREFIX: string = 'tabitem_';
const CLS_REORDER_ACTIVE_ITEM: string = 'e-reorder-active-item';
/** An interface that holds options to control the selected item action. */
export interface SelectEventArgs extends BaseEventArgs {
/** Defines the previous Tab item element. */
previousItem: HTMLElement
/** Defines the previous Tab item index. */
previousIndex: number
/** Defines the selected Tab item element. */
selectedItem: HTMLElement
/** Defines the selected Tab item index. */
selectedIndex: number
/** Defines the content selection done through swiping. */
isSwiped: boolean
/** Defines the prevent action. */
cancel?: boolean
/** Defines the selected content. */
selectedContent: HTMLElement
/** Determines whether the event is triggered via user interaction or programmatic way. True, if the event is triggered by user interaction. */
isInteracted?: boolean
/** Determines whether the Tab item needs to focus or not after it is selected */
preventFocus?: boolean
}
/** An interface that holds options to control the selecting item action. */
export interface SelectingEventArgs extends SelectEventArgs {
/** Defines the selecting Tab item element. */
selectingItem: HTMLElement
/** Defines the selecting Tab item index. */
selectingIndex: number
/** Defines the selecting Tab item content. */
selectingContent: HTMLElement
/** Defines the type of the event. */
event?: Event
}
/** An interface that holds options to control the removing and removed item action. */
export interface RemoveEventArgs extends BaseEventArgs {
/** Defines the removed Tab item element. */
removedItem: HTMLElement
/** Defines the removed Tab item index. */
removedIndex: number
/** Defines the prevent action. */
cancel?: boolean
}
/** An interface that holds options to control the adding and added item action. */
export interface AddEventArgs extends BaseEventArgs {
/** Defines the added Tab item element */
addedItems: TabItemModel[]
/** Defines the prevent action. */
cancel?: boolean
}
/** An interface that holds option to control the dragging and dragged item action. */
export interface DragEventArgs extends BaseEventArgs {
/** Defines the current dragged Tab item. */
draggedItem: HTMLElement
/** Defines the dropped Tab item. */
droppedItem: HTMLElement
/** defines the Dragged Tab item index. */
index: number
/** Return the actual event. */
event: MouseEvent
/** Return the target element */
target: HTMLElement
/** Return the clone element */
clonedElement: HTMLElement
/** Defines the prevent action. */
cancel?: boolean
}
/**
* Objects used for configuring the Tab selecting item action properties.
*/
export class TabActionSettings extends ChildProperty<TabActionSettings> {
/**
* Specifies the animation effect for displaying Tab content.
*
* @default 'SlideLeftIn'
* @aspType string
*/
@Property('SlideLeftIn')
public effect: 'None' | Effect;
/**
* Specifies the time duration to transform content.
*
* @default 600
*/
@Property(600)
public duration: number;
/**
* Specifies easing effect applied while transforming content.
*
* @default 'ease'
*/
@Property('ease')
public easing: string;
}
/**
* Objects used for configuring the Tab animation properties.
*/
export class TabAnimationSettings extends ChildProperty<TabAnimationSettings> {
/**
* Specifies the animation to appear while moving to previous Tab content.
*
* @default { effect: 'SlideLeftIn', duration: 600, easing: 'ease' }
*/
@Complex<TabActionSettingsModel>({ effect: 'SlideLeftIn', duration: 600, easing: 'ease' }, TabActionSettings)
public previous: TabActionSettingsModel;
/**
* Specifies the animation to appear while moving to next Tab content.
*
* @default { effect: 'SlideRightIn', duration: 600, easing: 'ease' }
*/
@Complex<TabActionSettingsModel>({ effect: 'SlideRightIn', duration: 600, easing: 'ease' }, TabActionSettings)
public next: TabActionSettingsModel;
}
/**
* Objects used for configuring the Tab item header properties.
*/
export class Header extends ChildProperty<Header> {
/**
* Specifies the display text of the Tab item header.
*
* @default ''
*/
@Property('')
public text: string | HTMLElement;
/**
* Specifies the icon class that is used to render an icon in the Tab header.
*
* @default ''
*/
@Property('')
public iconCss: string;
/**
* Options for positioning the icon in the Tab item header. This property depends on `iconCss` property.
* The possible values for this property as follows
* * `Left`: Places the icon to the left of the item.
* * `Top`: Places the icon on the top of the item.
* * `Right`: Places the icon to the right end of the item.
* * `Bottom`: Places the icon at the bottom of the item.
*
* @default 'left'
*/
@Property('left')
public iconPosition: string;
}
/**
* An array of object that is used to configure the Tab.
*/
export class TabItem extends ChildProperty<TabItem> {
/**
* The object used for configuring the Tab item header properties.
*
* @default {}
*/
@Complex<HeaderModel>({}, Header)
public header: HeaderModel;
/**
* Specifies the header text of Tab item.
*
* @default null
* @angularType string | object
* @reactType string | function | JSX.Element
* @vueType string | function
* @aspType string
*/
@Property(null)
public headerTemplate: string | Function;
/**
* Specifies the content of Tab item, that is displayed when concern item header is selected.
*
* @default ''
* @angularType string | object
* @reactType string | function | JSX.Element
* @vueType string | function
* @aspType string
*/
@Property('')
public content: string | HTMLElement | Function;
/**
* Sets the CSS classes to the Tab item to customize its styles.
*
* @default ''
*/
@Property('')
public cssClass: string;
/**
* Sets true to disable user interactions of the Tab item.
*
* @default false
*/
@Property(false)
public disabled: boolean;
/**
* Sets false to hide the Tab item.
*
* @default true
*/
@Property(true)
public visible: boolean;
/**
* Sets unique ID to Tab item.
*
* @default null
*/
@Property()
public id: string;
/**
* Specifies the tab order of the Tabs items. When positive values assigned, it allows to switch focus to the next/previous tabs items with Tab/ShiftTab keys.
* By default, user can able to switch between items only via arrow keys.
* If the value is set to 0 for all tabs items, then tab switches based on element order.
*
* @default -1
*/
@Property(-1)
public tabIndex: number;
}
/** @hidden */
interface EJ2Instance extends HTMLElement {
/* eslint-disable */
ej2_instances: Object[]
}
/**
* Tab is a content panel to show multiple contents in a single space, one at a time.
* Each Tab item has an associated content, that will be displayed based on the active Tab header item.
* ```html
* <div id="tab"></div>
* <script>
* var tabObj = new Tab();
* tab.appendTo("#tab");
* </script>
* ```
*/
@NotifyPropertyChanges
export class Tab extends Component<HTMLElement> implements INotifyPropertyChanged {
private hdrEle: HTEle;
private cntEle: HTEle;
private tbObj: Toolbar;
public tabId: string;
private tbItems: HTEle;
private tbItem: HTEle[];
private tbPop: HTEle;
private isTemplate: boolean;
private isPopup: boolean;
private isReplace: boolean;
private prevIndex: number;
private prevItem: HTEle;
private popEle: DomElements;
private actEleId: string;
private bdrLine: HTEle;
private popObj: Popup;
private btnCls: HTEle;
private cnt: string;
private show: object = {};
private hide: object = {};
private enableAnimation: boolean;
private keyModule: KeyboardEvents;
private tabKeyModule: KeyboardEvents;
private touchModule: Touch;
private maxHeight: number = 0;
private title: Str = 'Close';
private initRender: boolean;
private isInteracted: boolean = false;
private prevActiveEle: string;
private lastIndex: number = 0;
private isSwiped: boolean;
private isNested: boolean;
private itemIndexArray: string[];
private templateEle: string[];
private scrCntClass: string;
private isAdd: boolean = false;
private content: HTEle;
private selectedID: string;
private selectingID: string;
private isIconAlone: boolean = false;
private dragItem: HTMLElement;
private cloneElement: HTMLElement;
private droppedIndex: number;
private draggingItems: TabItemModel[];
private draggableItems: Draggable[] = [];
private tbId: string;
private resizeContext: EventListenerObject = this.refreshActiveTabBorder.bind(this);
/**
* Contains the keyboard configuration of the Tab.
*/
private keyConfigs: { [key: string]: Str } = {
tab: 'tab',
home: 'home',
end: 'end',
enter: 'enter',
space: 'space',
delete: 'delete',
moveLeft: 'leftarrow',
moveRight: 'rightarrow',
moveUp: 'uparrow',
moveDown: 'downarrow'
};
/**
* An array of object that is used to configure the Tab component.
*
* {% codeBlock src='tab/items/index.md' %}{% endcodeBlock %}
*
* @default []
*/
@Collection<TabItemModel>([], TabItem)
public items: TabItemModel[];
/**
* Specifies the width of the Tab component. Default, Tab width sets based on the width of its parent.
*
* @default '100%'
*/
@Property('100%')
public width: string | number;
/**
* Defines whether the tab transition should occur or not when performing Touch/Mouse swipe action.
*
* @remarks
* - `Both`: Enables swiping for both touch and mouse input.
* - `Touch`: Enables swiping only for touch input.
* - `Mouse`: Enables swiping only for mouse input.
* - `None`: Disables swiping for both touch and mouse input.
*
* @default "Both"
*/
@Property('Both')
public swipeMode: TabSwipeMode;
/**
* Specifies the height of the Tab component. By default, Tab height is set based on the height of its parent.
* To use height property, heightAdjustMode must be set to 'None'.
*
* @default 'auto'
*/
@Property('auto')
public height: string | number;
/**
* Sets the CSS classes to root element of the Tab that helps to customize component styles.
*
* @default ''
*/
@Property('')
public cssClass: string;
/**
* Specifies the index for activating the current Tab item.
*
* {% codeBlock src='tab/selectedItem/index.md' %}{% endcodeBlock %}
*
* @default 0
*/
@Property(0)
public selectedItem: number;
/**
* Specifies the orientation of Tab header.
* The possible values for this property as follows
* * `Top`: Places the Tab header on the top.
* * `Bottom`: Places the Tab header at the bottom.
* * `Left`: Places the Tab header on the left.
* * `Right`: Places the Tab header at the right.
*
* @default 'Top'
*/
@Property('Top')
public headerPlacement: HeaderPosition;
/**
* Specifies the height style for Tab content.
* The possible values for this property as follows
* * `None`: Based on the given height property, the content panel height is set.
* * `Auto`: Tallest panel height of a given Tab content is set to all the other panels.
* * `Content`: Based on the corresponding content height, the content panel height is set.
* * `Fill`: Based on the parent height, the content panel height is set.
*
* @default 'Content'
*/
@Property('Content')
public heightAdjustMode: HeightStyles;
/**
* Specifies the Tab display mode when Tab content exceeds the viewing area.
* The possible modes are:
* * `Scrollable`: All the elements are displayed in a single line with horizontal scrolling enabled.
* * `Popup`: Tab container holds the items that can be placed within the available space and rest of the items are moved to the popup.
* If the popup content overflows the height of the page, the rest of the elements can be viewed by scrolling the popup.
*
* @default 'Scrollable'
*/
@Property('Scrollable')
public overflowMode: OverflowMode;
/**
* Specifies the modes for Tab content.
* The possible modes are:
* * `Demand` - The content of the selected tab alone is loaded initially. The content of the tabs which were loaded once will be maintained in the DOM.
* * `Dynamic` - The content of all the tabs are rendered on the initial load and maintained in the DOM.
* * `Init` - The content of all the tabs are rendered on the initial load and maintained in the DOM.
*
* @default 'Demand'
*/
@Property('Demand')
public loadOn: ContentLoad;
/**
* Enable or disable persisting component's state between page reloads.
* If enabled, following list of states will be persisted.
* 1. selectedItem
*
* @default false
*/
@Property(false)
public enablePersistence: boolean;
/**
* Specifies whether to enable the rendering of untrusted HTML values in the Tab component.
* When this property is enabled, the component will sanitize any suspected untrusted strings and scripts before rendering them.
*
* @default true
*/
@Property(true)
public enableHtmlSanitizer: boolean;
/**
* Specifies whether to show the close button for header items to remove the item from the Tab.
*
* @default false
*/
@Property(false)
public showCloseButton: boolean;
/**
* Determines whether to re-order tab items to show active tab item in the header area or popup when OverflowMode is Popup.
* True, if active tab item should be visible in header area instead of pop-up. The default value is true.
*
* @default true
*/
@Property(true)
public reorderActiveTab: boolean;
/**
* Specifies the scrolling distance in scroller.
*
* @default null
*/
@Property()
public scrollStep: number;
/**
* Defines the area in which the draggable element movement will be occurring. Outside that area will be restricted
* for the draggable element movement. By default, the draggable element movement occurs in the toolbar.
*
* @default null
*/
@Property()
public dragArea: string;
/**
* Sets true to allow drag and drop the Tab items
*
* @default false
*/
@Property(false)
public allowDragAndDrop: boolean;
/**
* Specifies whether the templates need to be cleared or not while changing the Tab items dynamically.
* @default true
*/
@Property(true)
public clearTemplates: boolean;
/**
* Specifies the animation configuration settings while showing the content of the Tab.
*
* @default
* { previous: { effect: 'SlideLeftIn', duration: 600, easing: 'ease' },
* next: { effect: 'SlideRightIn', duration: 600, easing: 'ease' } }
*/
@Complex<TabAnimationSettingsModel>({}, TabAnimationSettings)
public animation: TabAnimationSettingsModel;
/**
* The event will be fired once the component rendering is completed.
*
* @event
*/
@Event()
public created: EmitType<Event>;
/**
* The event will be fired before adding the item to the Tab.
*
* @event
*/
@Event()
public adding: EmitType<AddEventArgs>;
/**
* The event will be fired after adding the item to the Tab.
*
* @event
*/
@Event()
public added: EmitType<AddEventArgs>;
/**
* The event will be fired before the item gets selected.
*
* @event
*/
@Event()
public selecting: EmitType<SelectingEventArgs>;
/**
* The event will be fired after the item gets selected.
*
* @event
*/
@Event()
public selected: EmitType<SelectEventArgs>;
/**
* The event will be fired before removing the item from the Tab.
*
* @event
*/
@Event()
public removing: EmitType<RemoveEventArgs>;
/**
* The event will be fired after removing the item from the Tab.
*
* @event
*/
@Event()
public removed: EmitType<RemoveEventArgs>;
/**
* The event will be fired before dragging the item from Tab
* @event
*/
@Event()
public onDragStart: EmitType<DragEventArgs>;
/**
* The event will be fired while dragging the Tab item
* @event
*/
@Event()
public dragging: EmitType<DragEventArgs>;
/**
* The event will be fired after dropping the Tab item
* @event
*/
@Event()
public dragged: EmitType<DragEventArgs>;
/**
* The event will be fired when the component gets destroyed.
*
* @event
*/
@Event()
public destroyed: EmitType<Event>;
/**
* Removes the component from the DOM and detaches all its related event handlers, attributes and classes.
*
* @returns {void}
*/
public destroy(): void {
if (this.isReact || this.isAngular) {
this.clearTemplate();
}
if (!isNOU(this.tbObj)) {
this.tbObj.destroy();
this.tbObj = null;
}
this.unWireEvents();
this.element.removeAttribute('aria-disabled');
this.expTemplateContent();
if (!this.isTemplate) {
while (this.element.firstElementChild) {
remove(this.element.firstElementChild);
}
} else {
const cntEle: Element = select('.' + CLS_TAB + ' > .' + CLS_CONTENT, this.element);
this.element.classList.remove(CLS_TEMPLATE);
if (!isNOU(cntEle)) {
cntEle.innerHTML = this.cnt;
}
}
if (this.btnCls) {
this.btnCls = null;
}
this.hdrEle = null;
this.cntEle = null;
this.tbItems = null;
this.tbItem = null;
this.tbPop = null;
this.prevItem = null;
this.popEle = null;
this.bdrLine = null;
this.content = null;
this.dragItem = null;
this.cloneElement = null;
this.draggingItems = [];
if (this.draggableItems && this.draggableItems.length > 0) {
for (let i: number = 0; i < this.draggableItems.length; i++) {
this.draggableItems[i].destroy();
this.draggableItems[i] = null;
}
this.draggableItems = [];
}
super.destroy();
this.trigger('destroyed');
}
/**
* Refresh the tab component
*
* @returns {void}
*/
public refresh(): void {
if (this.isReact) {
this.clearTemplate();
}
super.refresh();
if (this.isReact) {
this.renderReactTemplates();
}
}
/**
* Reorganizes and adjusts the Tab headers to fit the available width without re-rendering the entire Tab component.
*
* This method is useful for optimizing the layout when:
* - A hidden tab item becomes visible.
* - The number of tab items changes dynamically.
*
* @returns {void} This method does not return a value.
*/
public refreshOverflow(): void {
if (!isNOU(this.tbObj)) {
this.tbObj.refreshOverflow();
}
}
/**
* Initialize component
*
* @private
* @returns {void}
*/
protected preRender(): void {
const nested: Element = closest(this.element, '.' + CLS_CONTENT);
this.prevIndex = 0;
this.isNested = false;
this.isPopup = false;
this.initRender = true;
this.isSwiped = false;
this.itemIndexArray = [];
this.templateEle = [];
if (this.allowDragAndDrop) {
this.dragArea = !isNOU(this.dragArea) ? this.dragArea : '#' + this.element.id + ' ' + ('.' + CLS_HEADER);
}
if (!isNOU(nested)) {
nested.parentElement.classList.add(CLS_NEST);
this.isNested = true;
}
const name: Str = Browser.info.name;
const css: Str = (name === 'msie') ? 'e-ie' : (name === 'edge') ? 'e-edge' : (name === 'safari') ? 'e-safari' : '';
setStyle(this.element, { 'width': formatUnit(this.width), 'height': formatUnit(this.height) });
this.setCssClass(this.element, this.cssClass, true);
attributes(this.element, { 'aria-disabled': 'false' });
this.setCssClass(this.element, css, true);
this.updatePopAnimationConfig();
}
/**
* Initializes a new instance of the Tab class.
*
* @param {TabModel} options - Specifies Tab model properties as options.
* @param {string | HTMLElement} element - Specifies the element that is rendered as a Tab.
*/
public constructor(options?: TabModel, element?: string | HTMLElement) {
super(options, <HTEle | Str>element);
}
/**
* Initialize the component rendering
*
* @private
* @returns {void}
*/
protected render(): void {
this.btnCls = this.createElement('span', { className: CLS_ICONS + ' ' + CLS_ICON_CLOSE, attrs: { title: this.title } });
this.tabId = this.element.id.length > 0 ? ('-' + this.element.id) : getRandomId();
this.renderContainer();
this.wireEvents();
this.initRender = false;
if (this.isReact && (this as Record<string, any>).portals && (this as Record<string, any>).portals.length > 0) {
this.renderReactTemplates(() => {
this.refreshOverflow();
this.refreshActiveBorder();
});
}
}
private renderContainer(): void {
const ele: HTEle = this.element;
this.items.forEach((item: TabItemModel, index: number) => {
if (isNOU(item.id) && !isNOU((item as Base<HTMLElement>).setProperties)) {
(item as Base<HTMLElement>).setProperties({ id: TABITEMPREFIX + index.toString() }, true);
}
});
if (this.items.length > 0 && ele.children.length === 0) {
ele.appendChild(this.createElement('div', { className: CLS_CONTENT }));
this.setOrientation(this.headerPlacement, this.createElement('div', { className: CLS_HEADER }));
this.isTemplate = false;
} else if (this.element.children.length > 0) {
this.isTemplate = true;
ele.classList.add(CLS_TEMPLATE);
const header: HTEle = <HTEle>ele.querySelector('.' + CLS_HEADER);
if (header && this.headerPlacement === 'Bottom') {
this.setOrientation(this.headerPlacement, header);
}
}
if (!isNOU(select('.' + CLS_HEADER, this.element)) && !isNOU(select('.' + CLS_CONTENT, this.element))) {
this.renderHeader();
this.tbItems = <HTEle>select('.' + CLS_HEADER + ' .' + CLS_TB_ITEMS, this.element);
if (!isNOU(this.tbItems)) {
rippleEffect(this.tbItems, { selector: '.e-tab-wrap' });
}
this.renderContent();
if (selectAll('.' + CLS_TB_ITEM, this.element).length > 0) {
this.tbItems = <HTEle>select('.' + CLS_HEADER + ' .' + CLS_TB_ITEMS, this.element);
this.bdrLine = this.createElement('div', { className: CLS_INDICATOR + ' ' + CLS_HIDDEN + ' ' + CLS_IGNORE });
const scrCnt: HTEle = <HTEle>select('.' + this.scrCntClass, this.tbItems);
if (!isNOU(scrCnt)) {
scrCnt.insertBefore(this.bdrLine, scrCnt.firstChild);
} else {
this.tbItems.insertBefore(this.bdrLine, this.tbItems.firstChild);
}
this.setContentHeight(true);
this.select(this.selectedItem);
}
this.setRTL(this.enableRtl);
}
}
private renderHeader(): void {
const hdrPlace: HeaderPosition = this.headerPlacement;
let tabItems: Object[] = [];
this.hdrEle = this.getTabHeader();
this.addVerticalClass();
if (!this.isTemplate) {
tabItems = this.parseObject(this.items, 0);
} else {
if (this.element.children.length > 1 && this.element.children[1].classList.contains(CLS_HEADER)) {
this.setProperties({ headerPlacement: 'Bottom' }, true);
}
const count: number = this.hdrEle.children.length;
const hdrItems: Element[] = [];
for (let i: number = 0; i < count; i++) {
hdrItems.push(this.hdrEle.children.item(i));
}
if (count > 0) {
const tabItems: HTMLElement = this.createElement('div', { className: CLS_ITEMS });
this.hdrEle.appendChild(tabItems);
hdrItems.forEach((item: Element, index: number) => {
this.lastIndex = index;
const attr: object = {
className: CLS_ITEM, id: CLS_ITEM + this.tabId + '_' + index
};
const txt: Str = this.createElement('span', {
className: CLS_TEXT, attrs: { 'role': 'presentation' }
}).outerHTML;
const cont: Str = this.createElement('div', {
className: CLS_TEXT_WRAP, innerHTML: txt + this.btnCls.outerHTML
}).outerHTML;
const wrap: HTEle = this.createElement('div', {
className: CLS_WRAP, innerHTML: cont,
attrs: { role: 'tab', tabIndex: '-1', 'aria-selected': 'false', 'aria-controls': CLS_CONTENT + this.tabId + '_' + index, 'aria-disabled': 'false' }
});
wrap.querySelector('.' + CLS_TEXT).appendChild(item);
tabItems.appendChild(this.createElement('div', attr));
selectAll('.' + CLS_ITEM, tabItems)[index].appendChild(wrap);
});
}
}
this.tbObj = new Toolbar({
width: (hdrPlace === 'Left' || hdrPlace === 'Right') ? 'auto' : '100%',
height: (hdrPlace === 'Left' || hdrPlace === 'Right') ? '100%' : 'auto',
overflowMode: this.overflowMode,
items: (tabItems.length !== 0) ? tabItems : [],
clicked: this.clickHandler.bind(this),
scrollStep: this.scrollStep,
enableHtmlSanitizer: this.enableHtmlSanitizer,
cssClass: this.cssClass
});
this.tbObj.isStringTemplate = true;
this.tbObj.createElement = this.createElement;
this.tbObj.appendTo(<HTEle>this.hdrEle);
attributes(this.hdrEle, { role: 'tablist' });
if (!isNOU(this.element.getAttribute('aria-label'))) {
this.hdrEle.setAttribute('aria-label', this.element.getAttribute('aria-label'));
this.element.removeAttribute('aria-label');
} else if (!isNOU(this.element.getAttribute('aria-labelledby'))) {
this.hdrEle.setAttribute('aria-labelledby', this.element.getAttribute('aria-labelledby'));
this.element.removeAttribute('aria-labelledby');
}
this.setCloseButton(this.showCloseButton);
const toolbarHeader: HTEle = this.tbObj.element.querySelector('.' + CLS_TB_ITEMS);
if (!isNOU(toolbarHeader)) {
if (isNOU(toolbarHeader.id) || toolbarHeader.id === '') {
toolbarHeader.id = this.element.id + '_' + 'tab_header_items';
}
}
}
private createContentElement(index: number): Node {
const contentElement = this.createElement('div', {
id: CLS_CONTENT + this.tabId + '_' + index, className: CLS_ITEM,
attrs: { 'role': 'tabpanel', 'aria-labelledby': CLS_ITEM + this.tabId + '_' + index }
});
if (['Dynamic', 'Demand'].indexOf(this.loadOn) !== -1 ||
(this.loadOn === 'Init' && index === Number(this.extIndex(this.itemIndexArray[0])))) {
addClass([contentElement], CLS_ACTIVE);
}
return contentElement;
}
private renderContent(): void {
this.cntEle = <HTEle>select('.' + CLS_CONTENT, this.element);
const hdrItem: HTEle[] = selectAll('.' + CLS_TB_ITEM, this.element);
if (this.isTemplate) {
this.cnt = (this.cntEle.children.length > 0) ? this.cntEle.innerHTML : '';
const contents: HTMLCollection = this.cntEle.children;
for (let i: number = 0; i < hdrItem.length; i++) {
if (contents.length - 1 >= i) {
addClass([contents.item(i)], CLS_ITEM);
attributes(contents.item(i), { 'role': 'tabpanel', 'aria-labelledby': CLS_ITEM + this.tabId + '_' + i });
contents.item(i).id = CLS_CONTENT + this.tabId + '_' + i;
}
}
} else {
if (selectAll('.' + CLS_TB_ITEM, this.element).length > 0) {
if (this.loadOn === 'Init') {
for (let i = 0; i < this.itemIndexArray.length; i++) {
if (this.itemIndexArray[i]) {
this.cntEle.appendChild(this.createContentElement(Number(this.extIndex(this.itemIndexArray[i]))));
}
}
} else if (this.loadOn === 'Dynamic') {
this.cntEle.appendChild(this.createContentElement(this.selectedItem > 0 ?
this.selectedItem : Number(this.extIndex(this.itemIndexArray[0]))));
}
}
}
}
private reRenderItems(): void {
this.renderContainer();
if (!isNOU(this.cntEle)) {
this.bindSwipeEvents();
}
}
private parseObject(items: TabItemModel[], index: number): object[] {
const tbItems: HTMLElement[] = Array.prototype.slice.call(selectAll('.e-tab-header .' + CLS_TB_ITEM, this.element));
let maxId: number = this.lastIndex;
if (!this.isReplace && tbItems.length > 0) {
maxId = this.getMaxIndicesFromItems(tbItems);
}
const tItems: Object[] = [];
let txtWrapEle: HTEle;
const spliceArray: number[] = [];
const i: number = 0;
items.forEach((item: TabItemModel, i: number) => {
const pos: Str = (isNOU(item.header) || isNOU(item.header.iconPosition)) ? '' : item.header.iconPosition;
const css: Str = (isNOU(item.header) || isNOU(item.header.iconCss)) ? '' : item.header.iconCss;
if ((isNOU(item.headerTemplate)) && (isNOU(item.header) || isNOU(item.header.text) ||
(((<string>item.header.text).length === 0)) && (css === ''))) {
spliceArray.push(i);
return;
}
let txt: Str | HTEle | Function = item.headerTemplate || item.header.text;
if (typeof txt === 'string' && this.enableHtmlSanitizer) {
txt = SanitizeHtmlHelper.sanitize(<Str>txt);
}
let itemIndex: number;
if (this.isReplace && !isNOU(this.tbId) && this.tbId !== '') {
itemIndex = parseInt(this.tbId.substring(this.tbId.lastIndexOf('_') + 1), 10);
this.tbId = '';
} else {
itemIndex = index + i;
}
this.lastIndex = ((tbItems.length === 0) ? i : ((this.isReplace) ? (itemIndex) : (maxId + 1 + i)));
const disabled: Str = (item.disabled) ? ' ' + CLS_DISABLE + ' ' + CLS_OVERLAY : '';
const hidden: Str = (item.visible === false) ? ' ' + CLS_HIDDEN : '';
txtWrapEle = this.createElement('div', { className: CLS_TEXT, attrs: { 'role': 'presentation' } });
const tHtml: Str = ((txt instanceof Object) ? (<HTEle>txt).outerHTML : txt);
const txtEmpty: boolean = (!isNOU(tHtml) && tHtml !== '');
if (!isNOU((<HTEle>txt).tagName)) {
txtWrapEle.appendChild(txt as HTEle);
} else {
this.headerTextCompile(txtWrapEle, txt as string, i);
}
let tEle: HTEle;
const icon: HTEle = this.createElement('span', {
className: CLS_ICONS + ' ' + CLS_TAB_ICON + ' ' + CLS_ICON + '-' + pos + ' ' + css
});
const tCont: HTEle = this.createElement('div', { className: CLS_TEXT_WRAP });
tCont.appendChild(txtWrapEle);
if ((txt !== '' && txt !== undefined) && css !== '') {
if ((pos === 'left' || pos === 'top')) {
tCont.insertBefore(icon, tCont.firstElementChild);
} else {
tCont.appendChild(icon);
}
tEle = txtWrapEle;
this.isIconAlone = false;
} else {
tEle = ((css === '') ? txtWrapEle : icon);
if (tEle === icon) {
detach(txtWrapEle);
tCont.appendChild(icon);
this.isIconAlone = true;
}
}
const tabIndex : string = isNOU(item.tabIndex) ? '-1' : item.tabIndex.toString();
const wrapAttrs: { [key: string]: string } = (item.disabled) ? { role: 'tab', 'aria-disabled': 'true'} : { tabIndex: tabIndex, 'data-tabindex': tabIndex , role: 'tab', 'aria-selected': 'false', 'aria-disabled': 'false' };