-
Notifications
You must be signed in to change notification settings - Fork 152
/
toolbar.ts
2560 lines (2530 loc) · 110 KB
/
toolbar.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 @typescript-eslint/no-explicit-any */
import { Component, EventHandler, Property, Event, EmitType, BaseEventArgs } from '@syncfusion/ej2-base';
import { addClass, removeClass, isVisible, closest, attributes, detach, classList, KeyboardEvents } from '@syncfusion/ej2-base';
import { selectAll, setStyleAttribute as setStyle, KeyboardEventArgs, select } from '@syncfusion/ej2-base';
import { isNullOrUndefined as isNOU, getUniqueID, formatUnit, Collection, compile as templateCompiler } from '@syncfusion/ej2-base';
import { INotifyPropertyChanged, NotifyPropertyChanges, ChildProperty, Browser, SanitizeHtmlHelper } from '@syncfusion/ej2-base';
import { Popup } from '@syncfusion/ej2-popups';
import { calculatePosition } from '@syncfusion/ej2-popups';
import { Button, IconPosition } from '@syncfusion/ej2-buttons';
import { HScroll } from '../common/h-scroll';
import { VScroll } from '../common/v-scroll';
import { ToolbarModel, ItemModel } from './toolbar-model';
/**
* Specifies the options for supporting element types of Toolbar command.
* ```props
* Button :- Creates the Button control with its given properties like text, prefixIcon, etc.
* Separator :- Adds a horizontal line that separates the Toolbar commands.
* Input :- Creates an input element that is applicable to template rendering with Syncfusion controls like DropDownList, AutoComplete, etc.
* ```
*/
export type ItemType = 'Button' | 'Separator' | 'Input';
/**
* Specifies the options of where the text will be displayed in popup mode of the Toolbar.
* ```props
* Toolbar :- Text will be displayed on Toolbar only.
* Overflow :- Text will be displayed only when content overflows to popup.
* Both :- Text will be displayed on popup and Toolbar.
* ```
*/
export type DisplayMode = 'Both' | 'Overflow' | 'Toolbar';
/**
* Specifies the options of the Toolbar item display area when the Toolbar content overflows to available space.Applicable to `popup` mode.
* ```props
* Show :- Always shows the item as the primary priority on the *Toolbar*.
* Hide :- Always shows the item as the secondary priority on the *popup*.
* None :- No priority for display, and as per normal order moves to popup when content exceeds.
* ```
*/
export type OverflowOption = 'None' | 'Show' | 'Hide';
/**
* Specifies the options of Toolbar display mode. Display option is considered when Toolbar content exceeds the available space.
* ```props
* Scrollable :- All the elements are displayed in a single line with horizontal scrolling enabled.
* Popup :- Prioritized elements are displayed on the Toolbar and the rest of elements are moved to the popup.
* MultiRow :- Displays the overflow toolbar items as an in-line of a toolbar.
* Extended :- Hide the overflowing toolbar items in the next row. Show the overflowing toolbar items when you click the expand icons, if the popup content overflows the height of the page, the rest of the elements will be hidden.
* ```
*/
export type OverflowMode = 'Scrollable' |'Popup' | 'MultiRow' | 'Extended';
type HTEle = HTMLElement;
type Str = string;
type ItmAlign = 'lefts' | 'centers' | 'rights';
/**
* Specifies the options for aligning the Toolbar items.
* ```props
* Left :- To align commands to the left side of the Toolbar.
* Center :- To align commands at the center of the Toolbar.
* Right :- To align commands to the right side of the Toolbar.
* ```
*/
export type ItemAlign = 'Left' | 'Center' | 'Right';
const CLS_VERTICAL: Str = 'e-vertical';
const CLS_ITEMS: Str = 'e-toolbar-items';
const CLS_ITEM: Str = 'e-toolbar-item';
const CLS_RTL: Str = 'e-rtl';
const CLS_SEPARATOR: Str = 'e-separator';
const CLS_POPUPICON: Str = 'e-popup-up-icon';
const CLS_POPUPDOWN: Str = 'e-popup-down-icon';
const CLS_POPUPOPEN: Str = 'e-popup-open';
const CLS_TEMPLATE: Str = 'e-template';
const CLS_DISABLE: Str = 'e-overlay';
const CLS_POPUPTEXT: Str = 'e-toolbar-text';
const CLS_TBARTEXT: Str = 'e-popup-text';
const CLS_TBAROVERFLOW: Str = 'e-overflow-show';
const CLS_POPOVERFLOW: Str = 'e-overflow-hide';
const CLS_TBARBTN: Str = 'e-tbar-btn';
const CLS_TBARNAV: Str = 'e-hor-nav';
const CLS_TBARSCRLNAV: Str = 'e-scroll-nav';
const CLS_TBARRIGHT: Str = 'e-toolbar-right';
const CLS_TBARLEFT: Str = 'e-toolbar-left';
const CLS_TBARCENTER: Str = 'e-toolbar-center';
const CLS_TBARPOS: Str = 'e-tbar-pos';
const CLS_HSCROLLCNT: Str = 'e-hscroll-content';
const CLS_VSCROLLCNT: Str = 'e-vscroll-content';
const CLS_HSCROLLBAR: Str = 'e-hscroll-bar';
const CLS_POPUPNAV: Str = 'e-hor-nav';
const CLS_POPUPCLASS: Str = 'e-toolbar-pop';
const CLS_POPUP: Str = 'e-toolbar-popup';
const CLS_TBARBTNTEXT: Str = 'e-tbar-btn-text';
const CLS_TBARNAVACT: Str = 'e-nav-active';
const CLS_TBARIGNORE: Str = 'e-ignore';
const CLS_POPPRI: Str = 'e-popup-alone';
const CLS_HIDDEN: string = 'e-hidden';
const CLS_MULTIROW: string = 'e-toolbar-multirow';
const CLS_MULTIROWPOS: string = 'e-multirow-pos';
const CLS_MULTIROW_SEPARATOR: string = 'e-multirow-separator';
const CLS_EXTENDABLE_SEPARATOR: string = 'e-extended-separator';
const CLS_EXTEANDABLE_TOOLBAR: Str = 'e-extended-toolbar';
const CLS_EXTENDABLECLASS: Str = 'e-toolbar-extended';
const CLS_EXTENDPOPUP: Str = 'e-expended-nav';
const CLS_EXTENDEDPOPOPEN: Str = 'e-tbar-extended';
interface Template {
appendTo: (elemnt: HTMLElement) => void
}
interface ToolbarItemAlignIn {
lefts: HTMLElement[]
centers: HTMLElement[]
rights: HTMLElement[]
}
/** An interface that holds options to control the toolbar clicked action. */
export interface ClickEventArgs extends BaseEventArgs {
/** Defines the current Toolbar Item Object. */
item: ItemModel
/**
* Defines the current Event arguments.
*/
originalEvent: Event
/** Defines the prevent action. */
cancel?: boolean
}
/** An interface that holds options to control before the toolbar create. */
export interface BeforeCreateArgs extends BaseEventArgs {
/** Enable or disable the popup collision. */
enableCollision: boolean
/** Specifies the scrolling distance in scroller. */
scrollStep: number
}
/** @hidden */
interface EJ2Instance extends HTMLElement {
// eslint-disable-next-line camelcase
ej2_instances: Object[]
}
/**
* An item object that is used to configure Toolbar commands.
*/
export class Item extends ChildProperty<Item> {
/**
* Specifies the unique ID to be used with button or input element of Toolbar items.
*
* @default ""
*/
@Property('')
public id: string;
/**
* Specifies the text to be displayed on the Toolbar button.
*
* @default ""
*/
@Property('')
public text: string;
/**
* Specifies the width of the Toolbar button commands.
*
* @default 'auto'
*/
@Property('auto')
public width: number | string;
/**
* Defines single/multiple classes (separated by space) to be used for customization of commands.
*
* @default ""
*/
@Property('')
public cssClass: string;
/**
* Defines the priority of items to display it in popup always.
* It allows to maintain toolbar item on popup always but it does not work for toolbar priority items.
*
* @default false
*/
@Property(false)
public showAlwaysInPopup: boolean;
/**
* Specifies whether an item should be disabled or not.
*
* @default false
*/
@Property(false)
public disabled: boolean;
/**
* Defines single/multiple classes separated by space used to specify an icon for the button.
* The icon will be positioned before the text content if text is available, otherwise the icon alone will be rendered.
*
* @default ""
*/
@Property('')
public prefixIcon: string;
/**
* Defines single/multiple classes separated by space used to specify an icon for the button.
* The icon will be positioned after the text content if text is available.
*
* @default ""
*/
@Property('')
public suffixIcon: string;
/**
* Specifies whether an item should be hidden or not.
*
* @default true
*/
@Property(true)
public visible: boolean;
/**
* Specifies the Toolbar command display area when an element's content is too large to fit available space.
* This is applicable only to `popup` mode. The possible values for this property as follows
* * `Show`: Always shows the item as the primary priority on the *Toolbar*.
* * `Hide`: Always shows the item as the secondary priority on the *popup*.
* * `None`: No priority for display, and as per normal order moves to popup when content exceeds.
*
* @default 'None'
*/
@Property('None')
public overflow: OverflowOption;
/**
* Specifies the HTML element/element ID as a string that can be added as a Toolbar command.
* ```
* E.g - items: [{ template: '<input placeholder="Search"/>' },{ template: '#checkbox1' }]
* ```
*
* @default ""
* @angularType string | object
* @reactType string | function | JSX.Element
* @vueType string | function
* @aspType string
*/
@Property('')
public template: string | Object | Function;
/**
* Specifies the types of command to be rendered in the Toolbar.
* Supported types are:
* * `Button`: Creates the Button control with its given properties like text, prefixIcon, etc.
* * `Separator`: Adds a horizontal line that separates the Toolbar commands.
* * `Input`: Creates an input element that is applicable to template rendering with Syncfusion controls like DropDownList,
* AutoComplete, etc.
*
* @default 'Button'
*/
@Property('Button')
public type: ItemType;
/**
* Specifies where the button text will be displayed on *popup mode* of the Toolbar.
* The possible values for this property as follows
* * `Toolbar`: Text will be displayed on *Toolbar* only.
* * `Overflow`: Text will be displayed only when content overflows to *popup*.
* * `Both`: Text will be displayed on *popup* and *Toolbar*.
*
* @default 'Both'
*/
@Property('Both')
public showTextOn: DisplayMode;
/**
* Defines htmlAttributes used to add custom attributes to Toolbar command.
* Supports HTML attributes such as style, class, etc.
*
* @default null
*/
@Property(null)
public htmlAttributes: { [key: string]: string };
/**
* Specifies the text to be displayed on hovering the Toolbar button.
*
* @default ""
*/
@Property('')
public tooltipText: string;
/**
* Specifies the location for aligning Toolbar items on the Toolbar. Each command will be aligned according to the `align` property.
* The possible values for this property as follows
* * `Left`: To align commands to the left side of the Toolbar.
* * `Center`: To align commands at the center of the Toolbar.
* * `Right`: To align commands to the right side of the Toolbar.
* ```html
* <div id="element"> </div>
* ```
* ```typescript
* let toolbar: Toolbar = new Toolbar({
* items: [
* { text: "Home" },
* { text: "My Home Page" , align: 'Center' },
* { text: "Search", align: 'Right' }
* { text: "Settings", align: 'Right' }
* ]
* });
* toolbar.appendTo('#element');
* ```
*
* @default "Left"
* @aspPopulateDefaultValue
*/
@Property('Left')
public align: ItemAlign;
/**
* Event triggers when `click` the toolbar item.
*
* @event click
*/
@Event()
public click: EmitType<ClickEventArgs>;
/**
* Specifies the tab order of the Toolbar items. When positive values assigned, it allows to switch focus to the next/previous toolbar 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 tool bar items, then tab switches based on element order.
*
* @default -1
*/
@Property(-1)
public tabIndex: number;
}
/**
* The Toolbar control contains a group of commands that are aligned horizontally.
* ```html
* <div id="toolbar"/>
* <script>
* var toolbarObj = new Toolbar();
* toolbarObj.appendTo("#toolbar");
* </script>
* ```
*/
@NotifyPropertyChanges
export class Toolbar extends Component<HTMLElement> implements INotifyPropertyChanged {
private trgtEle: HTEle;
private ctrlTem: HTEle;
private popObj: Popup;
private tbarEle: HTMLElement[];
private tbarAlgEle: ToolbarItemAlignIn;
private tbarAlign: boolean;
private tbarEleMrgn: number;
private tbResize: boolean;
private offsetWid: number;
private keyModule: KeyboardEvents;
private scrollModule: HScroll | VScroll;
private activeEle: HTEle;
private popupPriCount: number;
private tbarItemsCol: ItemModel[];
private isVertical: boolean;
private tempId: string[];
private isExtendedOpen: boolean;
private clickEvent: EventListenerOrEventListenerObject;
private scrollEvent: EventListenerOrEventListenerObject;
private resizeContext: EventListenerObject = this.resize.bind(this);
private orientationChangeContext: EventListenerObject = this.orientationChange.bind(this);
/**
* Contains the keyboard configuration of the Toolbar.
*/
private keyConfigs: { [key: string]: Str } = {
moveLeft: 'leftarrow',
moveRight: 'rightarrow',
moveUp: 'uparrow',
moveDown: 'downarrow',
popupOpen: 'enter',
popupClose: 'escape',
tab: 'tab',
home: 'home',
end: 'end'
};
/**
* An array of items that is used to configure Toolbar commands.
*
* @default []
*/
@Collection<ItemModel>([], Item)
public items: ItemModel[];
/**
* Specifies the width of the Toolbar in pixels/numbers/percentage. Number value is considered as pixels.
*
* @default 'auto'
*/
@Property('auto')
public width: string | number;
/**
* Specifies the height of the Toolbar in pixels/number/percentage. Number value is considered as pixels.
*
* @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 Toolbar display mode when Toolbar content exceeds the viewing area.
* The possible values for this property as follows
* - Scrollable: All the elements are displayed in a single line with horizontal scrolling enabled.
* - Popup: Prioritized elements are displayed on the Toolbar and the rest of elements are moved to the *popup*.
* - MultiRow: Displays the overflow toolbar items as an in-line of a toolbar.
* - Extended: Hide the overflowing toolbar items in the next row. Show the overflowing toolbar items when you click the expand icons.
* If the popup content overflows the height of the page, the rest of the elements will be hidden.
*
* @default 'Scrollable'
*/
@Property('Scrollable')
public overflowMode: OverflowMode;
/**
* Specifies the scrolling distance in scroller.
* The possible values for this property as follows
* * Scrollable - All the elements are displayed in a single line with horizontal scrolling enabled.
* * Popup - Prioritized elements are displayed on the Toolbar and the rest of elements are moved to the *popup*.
* * MultiRow - Displays the overflow toolbar items as an in-line of a toolbar.
* * Extended - Hide the overflowing toolbar items in the next row. Show the overflowing toolbar items when you click the expand icons.
* * If the popup content overflows the height of the page, the rest of the elements will be hidden.
*
* {% codeBlock src='toolbar/scrollStep/index.md' %}{% endcodeBlock %}
*
* @default null
*/
@Property()
public scrollStep: number;
/**
* Enable or disable the popup collision.
*
* @default true
*/
@Property(true)
public enableCollision: boolean;
/**
* Defines whether to allow the cross-scripting site or not.
*
* @default true
*/
@Property(true)
public enableHtmlSanitizer: boolean;
/**
* When this property is set to true, it allows the keyboard interaction in toolbar.
*
* @default true
*/
@Property(true)
public allowKeyboard: boolean;
/**
* The event will be fired on clicking the Toolbar elements.
*
* @event clicked
*/
@Event()
public clicked: EmitType<ClickEventArgs>;
/**
* The event will be fired when the control is rendered.
*
* @event created
*/
@Event()
public created: EmitType<Event>;
/**
* The event will be fired when the control gets destroyed.
*
* @event destroyed
*/
@Event()
public destroyed: EmitType<Event>;
/**
* The event will be fired before the control is rendered on a page.
*
* @event beforeCreate
*/
@Event()
public beforeCreate: EmitType<BeforeCreateArgs>;
/**
* Removes the control from the DOM and also removes all its related events.
*
* @returns {void}.
*/
public destroy(): void {
if ((this as any).isReact || (this as any).isAngular) {
this.clearTemplate();
}
const btnItems: NodeList = this.element.querySelectorAll('.e-control.e-btn');
[].slice.call(btnItems).forEach((el: EJ2Instance) => {
if (!isNOU(el) && !isNOU(el.ej2_instances) && !isNOU(el.ej2_instances[0]) && !((el.ej2_instances[0] as Button).isDestroyed)) {
(el.ej2_instances[0] as Button).destroy();
}
});
this.unwireEvents();
this.tempId.forEach((ele: Str): void => {
if (!isNOU(this.element.querySelector(ele))) {
(<HTEle>document.body.appendChild(this.element.querySelector(ele))).style.display = 'none';
}
});
this.destroyItems();
while (this.element.lastElementChild) {
this.element.removeChild(this.element.lastElementChild);
}
if (this.trgtEle) {
this.element.appendChild(this.ctrlTem);
this.trgtEle = null;
this.ctrlTem = null;
}
if (this.popObj) {
this.popObj.destroy();
detach(this.popObj.element);
}
if (this.activeEle) {
this.activeEle = null;
}
this.popObj = null;
this.tbarAlign = null;
this.tbarItemsCol = [];
this.remove(this.element, 'e-toolpop');
if (this.cssClass) {
removeClass([this.element], this.cssClass.split(' '));
}
this.element.removeAttribute('style');
['aria-disabled', 'aria-orientation', 'role'].forEach((attrb: string): void =>
this.element.removeAttribute(attrb));
super.destroy();
}
/**
* Initialize the event handler
*
* @private
* @returns {void}
*/
protected preRender(): void {
const eventArgs: BeforeCreateArgs = { enableCollision: this.enableCollision, scrollStep: this.scrollStep };
this.trigger('beforeCreate', eventArgs);
this.enableCollision = eventArgs.enableCollision;
this.scrollStep = eventArgs.scrollStep;
this.scrollModule = null;
this.popObj = null;
this.tempId = [];
this.tbarItemsCol = this.items;
this.isVertical = this.element.classList.contains(CLS_VERTICAL) ? true : false;
this.isExtendedOpen = false;
this.popupPriCount = 0;
if (this.enableRtl) {
this.add(this.element, CLS_RTL);
}
}
/**
* Initializes a new instance of the Toolbar class.
*
* @param {ToolbarModel} options - Specifies Toolbar model properties as options.
* @param { string | HTMLElement} element - Specifies the element that is rendered as a Toolbar.
*/
public constructor(options?: ToolbarModel, element?: string | HTMLElement) {
super(options, <HTEle | Str>element);
}
private wireEvents(): void {
EventHandler.add(this.element, 'click', this.clickHandler, this);
window.addEventListener('resize', this.resizeContext);
window.addEventListener('orientationchange', this.orientationChangeContext);
if (this.allowKeyboard) {
this.wireKeyboardEvent();
}
}
private wireKeyboardEvent(): void {
this.keyModule = new KeyboardEvents(this.element, {
keyAction: this.keyActionHandler.bind(this),
keyConfigs: this.keyConfigs
});
EventHandler.add(this.element, 'keydown', this.docKeyDown, this);
this.updateTabIndex('0');
}
private updateTabIndex(tabIndex: string): void {
const ele: HTEle = <HTEle>this.element.querySelector('.' + CLS_ITEM + ':not(.' + CLS_DISABLE + ' ):not(.' + CLS_SEPARATOR + ' ):not(.' + CLS_HIDDEN + ' )');
if (!isNOU(ele) && !isNOU(ele.firstElementChild)) {
const dataTabIndex: string = ele.firstElementChild.getAttribute('data-tabindex');
if (dataTabIndex && dataTabIndex === '-1' && ele.firstElementChild.tagName !== 'INPUT') {
ele.firstElementChild.setAttribute('tabindex', tabIndex);
}
}
}
private unwireKeyboardEvent(): void {
if (this.keyModule) {
EventHandler.remove(this.element, 'keydown', this.docKeyDown);
this.keyModule.destroy();
this.keyModule = null;
}
}
private docKeyDown(e: KeyboardEvent): void {
if ((<HTEle>e.target).tagName === 'INPUT') {
return;
}
const popCheck: boolean = !isNOU(this.popObj) && isVisible(this.popObj.element) && this.overflowMode !== 'Extended';
if (e.keyCode === 9 && (<HTEle>e.target).classList.contains('e-hor-nav') === true && popCheck) {
this.popObj.hide({ name: 'FadeOut', duration: 100 });
}
const keyCheck: boolean = (e.keyCode === 40 || e.keyCode === 38 || e.keyCode === 35 || e.keyCode === 36);
if (keyCheck) {
e.preventDefault();
}
}
private unwireEvents(): void {
EventHandler.remove(this.element, 'click', this.clickHandler);
this.destroyScroll();
this.unwireKeyboardEvent();
window.removeEventListener('resize', this.resizeContext);
window.removeEventListener('orientationchange', this.orientationChangeContext);
document.removeEventListener('scroll', this.clickEvent);
document.removeEventListener('click', this.scrollEvent);
this.scrollEvent = null;
this.clickEvent = null;
}
private clearProperty(): void {
this.tbarEle = [];
this.tbarAlgEle = { lefts: [], centers: [], rights: [] };
}
private docEvent(e: Event): void {
const popEle: Element = closest(<Element>e.target, '.e-popup');
if (this.popObj && isVisible(this.popObj.element) && !popEle && this.overflowMode === 'Popup') {
this.popObj.hide({ name: 'FadeOut', duration: 100 });
}
}
private destroyScroll(): void {
if (this.scrollModule) {
if (this.tbarAlign) {
this.add(this.scrollModule.element, CLS_TBARPOS);
}
this.scrollModule.destroy(); this.scrollModule = null;
}
}
private destroyItems(): void {
if (this.element) {
[].slice.call(this.element.querySelectorAll('.' + CLS_ITEM)).forEach((el: HTEle) => { detach(el); });
}
if (this.tbarAlign) {
const tbarItems: HTEle = <HTEle>this.element.querySelector('.' + CLS_ITEMS);
[].slice.call(tbarItems.children).forEach((el: HTEle) => {
detach(el);
});
this.tbarAlign = false;
this.remove(tbarItems, CLS_TBARPOS);
}
this.clearProperty();
}
private destroyMode(): void {
if (this.scrollModule) {
this.remove(this.scrollModule.element, CLS_RTL);
this.destroyScroll();
}
this.remove(this.element, CLS_EXTENDEDPOPOPEN);
this.remove(this.element, CLS_EXTEANDABLE_TOOLBAR);
const tempEle: HTMLElement = this.element.querySelector('.e-toolbar-multirow');
if (tempEle) {
this.remove(tempEle, CLS_MULTIROW);
}
if (this.popObj) {
this.popupRefresh(this.popObj.element, true);
}
}
private add(ele: HTEle, val: Str): void {
ele.classList.add(val);
}
private remove(ele: HTEle, val: Str): void {
ele.classList.remove(val);
}
private elementFocus(ele: HTEle): void {
const fChild: HTEle = <HTEle>ele.firstElementChild;
if (fChild) {
fChild.focus();
this.activeEleSwitch(ele);
} else {
ele.focus();
}
}
private clstElement(tbrNavChk: boolean, trgt: HTEle): HTEle {
let clst: HTEle;
if (tbrNavChk && this.popObj && isVisible(this.popObj.element)) {
clst = <HTEle>this.popObj.element.querySelector('.' + CLS_ITEM);
} else if (this.element === trgt || tbrNavChk) {
clst = <HTEle>this.element.querySelector('.' + CLS_ITEM + ':not(.' + CLS_DISABLE + ' ):not(.' + CLS_SEPARATOR + ' ):not(.' + CLS_HIDDEN + ' )');
} else {
clst = <HTEle>closest(trgt, '.' + CLS_ITEM);
}
return clst;
}
private keyHandling(clst: HTEle, e: KeyboardEventArgs, trgt: HTEle, navChk: boolean, scrollChk: boolean): void {
const popObj: Popup = this.popObj;
const rootEle: HTEle = this.element;
const popAnimate: Object = { name: 'FadeOut', duration: 100 };
const value: Str = e.action === 'moveUp' ? 'previous' : 'next';
let ele: HTEle;
let nodes: NodeList;
switch (e.action) {
case 'moveRight':
if (this.isVertical) {
return;
}
if (rootEle === trgt) {
this.elementFocus(clst);
} else if (!navChk) {
this.eleFocus(clst, 'next');
}
break;
case 'moveLeft':
if (this.isVertical) {
return;
}
if (!navChk) {
this.eleFocus(clst, 'previous');
}
break;
case 'home':
case 'end':
if (clst) {
let popupCheck: HTEle = <HTEle>closest(clst, '.e-popup');
const extendedPopup: HTEle = this.element.querySelector('.' + CLS_EXTENDABLECLASS);
if (this.overflowMode === 'Extended' && extendedPopup && extendedPopup.classList.contains('e-popup-open')) {
popupCheck = e.action === 'end' ? extendedPopup : null;
}
if (popupCheck) {
if (isVisible(this.popObj.element)) {
nodes = [].slice.call(popupCheck.children);
if (e.action === 'home') {
ele = this.focusFirstVisibleEle(nodes);
} else {
ele = this.focusLastVisibleEle(nodes);
}
}
} else {
nodes = this.element.querySelectorAll('.' + CLS_ITEMS + ' .' + CLS_ITEM + ':not(.' + CLS_SEPARATOR + ')');
if (e.action === 'home') {
ele = this.focusFirstVisibleEle(nodes);
} else {
ele = this.focusLastVisibleEle(nodes);
}
}
if (ele) {
this.elementFocus(ele);
}
}
break;
case 'moveUp':
case 'moveDown':
if (!this.isVertical) {
if (popObj && closest(trgt, '.e-popup')) {
const popEle: HTEle = popObj.element;
const popFrstEle: HTEle = popEle.firstElementChild as HTEle;
if ((value === 'previous' && popFrstEle === clst)) {
(<HTEle>popEle.lastElementChild.firstChild).focus();
} else if (value === 'next' && popEle.lastElementChild === clst) {
(<HTEle>popFrstEle.firstChild).focus();
} else {
this.eleFocus(clst, value);
}
} else if (e.action === 'moveDown' && popObj && isVisible(popObj.element)) {
this.elementFocus(clst);
}
} else {
if (e.action === 'moveUp') {
this.eleFocus(clst, 'previous');
} else {
this.eleFocus(clst, 'next');
}
}
break;
case 'tab':
if (!scrollChk && !navChk) {
const ele: HTEle = (<HTEle>clst.firstElementChild);
if (rootEle === trgt) {
if (this.activeEle) {
this.activeEle.focus();
} else {
this.activeEleRemove(ele);
ele.focus();
}
}
}
break;
case 'popupClose':
if (popObj && this.overflowMode !== 'Extended') {
popObj.hide(popAnimate);
}
break;
case 'popupOpen':
if (!navChk) {
return;
}
if (popObj && !isVisible(popObj.element)) {
popObj.element.style.top = rootEle.offsetHeight + 'px';
popObj.show({ name: 'FadeIn', duration: 100 });
} else {
popObj.hide(popAnimate);
}
break;
}
}
private keyActionHandler(e: KeyboardEventArgs): void {
const trgt: HTEle = <HTEle>e.target;
if (trgt.tagName === 'INPUT' || trgt.tagName === 'TEXTAREA' || this.element.classList.contains(CLS_DISABLE)) {
return;
}
e.preventDefault();
const tbrNavChk: boolean = trgt.classList.contains(CLS_TBARNAV);
const tbarScrollChk: boolean = trgt.classList.contains(CLS_TBARSCRLNAV);
const clst: HTEle = this.clstElement(tbrNavChk, trgt);
if (clst || tbarScrollChk) {
this.keyHandling(clst, e, trgt, tbrNavChk, tbarScrollChk);
}
}
/**
* Specifies the value to disable/enable the Toolbar component.
* When set to `true`, the component will be disabled.
*
* @param {boolean} value - Based on this Boolean value, Toolbar will be enabled (false) or disabled (true).
* @returns {void}.
*/
public disable(value: boolean): void {
const rootEle: HTMLElement = this.element;
if (value) {
rootEle.classList.add(CLS_DISABLE);
} else {
rootEle.classList.remove(CLS_DISABLE);
}
if (this.activeEle) {
this.activeEle.setAttribute('tabindex', this.activeEle.getAttribute('data-tabindex'));
}
if (this.scrollModule) {
this.scrollModule.disable(value);
}
if (this.popObj) {
if (isVisible(this.popObj.element) && this.overflowMode !== 'Extended') {
this.popObj.hide();
}
rootEle.querySelector('#' + rootEle.id + '_nav').setAttribute('tabindex', !value ? '0' : '-1');
}
}
private eleContains(el: HTEle): string | boolean {
return el.classList.contains(CLS_SEPARATOR) || el.classList.contains(CLS_DISABLE) || el.getAttribute('disabled') || el.classList.contains(CLS_HIDDEN) || !isVisible(el) || !el.classList.contains(CLS_ITEM);
}
private focusFirstVisibleEle(nodes: NodeList): HTMLElement {
let element: HTMLElement;
let index: number = 0;
while (index < nodes.length) {
const ele: HTMLElement = nodes[parseInt(index.toString(), 10)] as HTMLElement;
if (!ele.classList.contains(CLS_HIDDEN) && !ele.classList.contains(CLS_DISABLE)) {
return ele;
}
index++;
}
return element;
}
private focusLastVisibleEle(nodes: NodeList): HTMLElement {
let element: HTMLElement;
let index: number = nodes.length - 1;
while (index >= 0) {
const ele: HTMLElement = nodes[parseInt(index.toString(), 10)] as HTMLElement;
if (!ele.classList.contains(CLS_HIDDEN) && !ele.classList.contains(CLS_DISABLE)) {
return ele;
}
index--;
}
return element;
}
private eleFocus(closest: HTEle, pos: Str): void {
const sib: HTEle = Object(closest)[pos + 'ElementSibling'];
if (sib) {
const skipEle: string | boolean = this.eleContains(sib);
if (skipEle) {
this.eleFocus(sib, pos); return;
}
this.elementFocus(sib);
} else if (this.tbarAlign) {
let elem: HTEle = Object(closest.parentElement)[pos + 'ElementSibling'] as HTEle;
if (!isNOU(elem) && elem.children.length === 0) {
elem = Object(elem)[pos + 'ElementSibling'] as HTEle;
}
if (!isNOU(elem) && elem.children.length > 0) {
if (pos === 'next') {
const el: HTEle = <HTEle>elem.querySelector('.' + CLS_ITEM);
if (this.eleContains(el)) {
this.eleFocus(el, pos);
} else {
(<HTEle>el.firstElementChild).focus();
this.activeEleSwitch(el);
}
} else {
const el: HTEle = <HTEle>elem.lastElementChild;
if (this.eleContains(el)) {
this.eleFocus(el, pos);
} else {
this.elementFocus(el);
}
}
}
} else if (!isNOU(closest)) {
const tbrItems: NodeList = this.element.querySelectorAll('.' + CLS_ITEMS + ' .' + CLS_ITEM + ':not(.' + CLS_SEPARATOR + ')' + ':not(.' + CLS_DISABLE + ')' + ':not(.' + CLS_HIDDEN + ')');
if (pos === 'next' && tbrItems) {
this.elementFocus(tbrItems[0] as HTMLElement);
} else if (pos === 'previous' && tbrItems) {
this.elementFocus(tbrItems[tbrItems.length - 1] as HTMLElement);
}
}
}
private clickHandler(e: Event): void {
const trgt: HTEle = <HTEle>e.target;
const ele: HTEle = this.element;
const isPopupElement: boolean = !isNOU(closest(trgt, '.' + CLS_POPUPCLASS));
let clsList: DOMTokenList = trgt.classList;
let popupNav: HTEle = <HTEle>closest(trgt, ('.' + CLS_TBARNAV));
if (!popupNav) {
popupNav = trgt;
}
if (!ele.children[0].classList.contains('e-hscroll') && !ele.children[0].classList.contains('e-vscroll')
&& (clsList.contains(CLS_TBARNAV))) {
clsList = trgt.querySelector('.e-icons').classList;
}
if (clsList.contains(CLS_POPUPICON) || clsList.contains(CLS_POPUPDOWN)) {
this.popupClickHandler(ele, popupNav, CLS_RTL);
}
let itemObj: ItemModel;
const clst: HTEle = <HTEle>closest(<Node>e.target, '.' + CLS_ITEM);
if ((isNOU(clst) || clst.classList.contains(CLS_DISABLE)) && !popupNav.classList.contains(CLS_TBARNAV)) {
return;
}
if (clst) {
const tempItem: ItemModel = this.items[this.tbarEle.indexOf(clst)];
itemObj = tempItem;
}
const eventArgs: ClickEventArgs = { originalEvent: e, item: itemObj };
const isClickBinded: boolean = itemObj && !isNOU(itemObj.click) && typeof itemObj.click == 'object' ?
!isNOU((itemObj as any).click.observers) && (itemObj as any).click.observers.length > 0 :
!isNOU(itemObj) && !isNOU(itemObj.click);
if (isClickBinded) {
this.trigger('items[' + this.tbarEle.indexOf(clst) + '].click', eventArgs);
}
if (!eventArgs.cancel) {
this.trigger('clicked', eventArgs, (clickedArgs: ClickEventArgs) => {
if (!isNOU(this.popObj) && isPopupElement && !clickedArgs.cancel && this.overflowMode === 'Popup' &&
clickedArgs.item && clickedArgs.item.type !== 'Input') {
this.popObj.hide({ name: 'FadeOut', duration: 100 });
}
});
}
}
private popupClickHandler(ele: HTMLElement, popupNav: HTMLElement, CLS_RTL: Str): void {
const popObj: Popup = this.popObj;
if (isVisible(popObj.element)) {
popupNav.classList.remove(CLS_TBARNAVACT);
popObj.hide({ name: 'FadeOut', duration: 100 });
} else {
if (ele.classList.contains(CLS_RTL)) {
popObj.enableRtl = true;
popObj.position = { X: 'left', Y: 'top' };
}
if (popObj.offsetX === 0 && !ele.classList.contains(CLS_RTL)) {
popObj.enableRtl = false;
popObj.position = { X: 'right', Y: 'top' };
}
if (this.overflowMode === 'Extended') {
popObj.element.style.minHeight = '0px';
popObj.width = this.getToolbarPopupWidth(this.element);
}
popObj.dataBind();
popObj.refreshPosition();
popObj.element.style.top = this.getElementOffsetY() + 'px';
popupNav.classList.add(CLS_TBARNAVACT);
popObj.show({ name: 'FadeIn', duration: 100 });
}
}
private getToolbarPopupWidth(ele: HTMLElement): number {
const eleStyles: CSSStyleDeclaration = window.getComputedStyle(ele);
return parseFloat(eleStyles.width) + ((parseFloat(eleStyles.borderRightWidth)) * 2);
}
/**
* To Initialize the control rendering
*
* @private
* @returns {void}
*/
protected render(): void {
this.initialize();
this.renderControl();
this.wireEvents();
this.clickEvent = this.docEvent.bind(this);
this.scrollEvent = this.docEvent.bind(this);
this.renderComplete();
if ((this as any).isReact && (this as Record<string, any>).portals && (this as Record<string, any>).portals.length > 0) {
this.renderReactTemplates(() => {
this.refreshOverflow();
});
}
}
private initialize(): void {
const width: Str = formatUnit(this.width);
const height: Str = formatUnit(this.height);
if (Browser.info.name !== 'msie' || this.height !== 'auto' || this.overflowMode === 'MultiRow') {
setStyle(this.element, { 'height': height });
}
setStyle(this.element, { 'width': width });
const ariaAttr: { [key: string]: Str } = {
'role': 'toolbar', 'aria-disabled': 'false',
'aria-orientation': !this.isVertical ? 'horizontal' : 'vertical'