-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskiptoMenuButton.js
1407 lines (1228 loc) · 47.3 KB
/
skiptoMenuButton.js
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
/* skiptoMenuButton.js */
/* Imports */
import DebugLogging from './debug.js';
import {
isNotEmptyString
} from './utils.js';
import SkipToContentInfoDialog from './skipToContentInfoDialog.js';
import ShortcutsMessage from './shortcutsMessage.js';
import {
getLandmarksAndHeadings,
skipToElement,
queryDOMForSkipToId
} from './landmarksHeadings.js';
import {
highlightElement,
removeHighlight
} from './highlightElement.js';
import {
getFocusElement,
navigateContent
} from './shortcuts.js';
import {
elementTakesText,
onlyShiftPressed,
noModifierPressed,
onlyAltPressed,
onlyOptionPressed
} from './keyboardHelpers.js';
/* Constants */
const debug = new DebugLogging('SkipToButton', false);
debug.flag = false;
/**
* @class SkiptoMenuButton
*
* @desc Constructor for creating a button to open a menu of headings and landmarks on
* a web page
*
* @param {Object} skipToContentElem - The skip-to-content objecy
*
* @returns {Object} DOM element node that is the container for the button and the menu
*/
export default class SkiptoMenuButton {
constructor (skipToContentElem) {
this.skipToContentElem = skipToContentElem;
this.config = skipToContentElem.config;
this.skiptoId = skipToContentElem.skipToId;
// check for 'nav' element, if not use 'div' element
const ce = this.config.containerElement.toLowerCase().trim() === 'nav' ? 'nav' : 'div';
this.containerNode = document.createElement(ce);
skipToContentElem.shadowRoot.appendChild(this.containerNode);
this.containerNode.id = this.skiptoId;
if (ce === 'nav') {
this.containerNode.setAttribute('aria-label', this.config.buttonLabel);
}
if (isNotEmptyString(this.config.customClass)) {
this.containerNode.classList.add(this.config.customClass);
}
this.setDisplayOption(this.config.displayOption);
// Create button
const [buttonVisibleLabel, buttonAriaLabel] = this.getBrowserSpecificShortcut(this.config);
this.buttonNode = document.createElement('button');
this.buttonNode.setAttribute('aria-haspopup', 'menu');
this.buttonNode.setAttribute('aria-expanded', 'false');
this.buttonNode.setAttribute('aria-label', buttonAriaLabel);
this.buttonNode.setAttribute('aria-controls', 'id-skip-to-menu');
this.buttonNode.addEventListener('keydown', this.handleButtonKeydown.bind(this));
this.buttonNode.addEventListener('click', this.handleButtonClick.bind(this));
this.containerNode.appendChild(this.buttonNode);
this.textButtonNode = document.createElement('span');
this.buttonNode.appendChild(this.textButtonNode);
this.textButtonNode.classList.add('skipto-text');
this.textButtonNode.textContent = buttonVisibleLabel;
this.smallButtonNode = document.createElement('span');
this.buttonNode.appendChild(this.smallButtonNode);
this.smallButtonNode.classList.add('skipto-small');
this.smallButtonNode.textContent = this.config.smallButtonLabel;
this.mediumButtonNode = document.createElement('span');
this.buttonNode.appendChild(this.mediumButtonNode);
this.mediumButtonNode.classList.add('skipto-medium');
this.mediumButtonNode.textContent = this.config.buttonLabel;
// Create menu container
this.menuitemNodes = [];
this.menuNode = document.createElement('div');
this.menuNode.setAttribute('id', 'id-skip-to-menu');
this.menuNode.setAttribute('role', 'menu');
this.menuNode.setAttribute('aria-label', this.config.menuLabel);
this.containerNode.appendChild(this.menuNode);
this.landmarkGroupLabelNode = document.createElement('div');
this.landmarkGroupLabelNode.setAttribute('id', 'id-skip-to-menu-landmark-group-label');
this.landmarkGroupLabelNode.setAttribute('role', 'separator');
this.landmarkGroupLabelNode.textContent = this.addNumberToGroupLabel(this.config.landmarkGroupLabel);
this.menuNode.appendChild(this.landmarkGroupLabelNode);
this.landmarkGroupNode = document.createElement('div');
this.landmarkGroupNode.setAttribute('id', 'id-skip-to-menu-landmark-group');
this.landmarkGroupNode.setAttribute('role', 'group');
this.landmarkGroupNode.className = 'overflow';
this.landmarkGroupNode.setAttribute('aria-labelledby', 'id-skip-to-menu-landmark-group-label');
this.menuNode.appendChild(this.landmarkGroupNode);
this.headingGroupLabelNode = document.createElement('div');
this.headingGroupLabelNode.setAttribute('id', 'id-skip-to-menu-heading-group-label');
this.headingGroupLabelNode.setAttribute('role', 'separator');
this.headingGroupLabelNode.textContent = this.addNumberToGroupLabel(this.config.headingGroupLabel);
this.menuNode.appendChild(this.headingGroupLabelNode);
this.headingGroupNode = document.createElement('div');
this.headingGroupNode.setAttribute('id', 'id-skip-to-menu-heading-group');
this.headingGroupNode.setAttribute('role', 'group');
this.headingGroupNode.className = 'overflow';
this.headingGroupNode.setAttribute('aria-labelledby', 'id-skip-to-menu-heading-group-label');
this.menuNode.appendChild(this.headingGroupNode);
this.shortcutsGroupLabelNode = document.createElement('div');
this.shortcutsGroupLabelNode.setAttribute('id', 'id-skip-to-menu-shortcuts-group-label');
this.shortcutsGroupLabelNode.setAttribute('role', 'separator');
if (this.config.shortcuts === 'enabled') {
this.shortcutsGroupLabelNode.textContent = this.config.shortcutsGroupEnabledLabel;
}
else {
this.shortcutsGroupLabelNode.textContent = this.config.shortcutsGroupDisabledLabel;
}
this.menuNode.appendChild(this.shortcutsGroupLabelNode);
this.shortcutsGroupNode = document.createElement('div');
this.shortcutsGroupNode.setAttribute('id', 'id-skip-to-menu-shortcuts-group');
this.shortcutsGroupNode.setAttribute('role', 'group');
this.shortcutsGroupNode.setAttribute('aria-labelledby', 'id-skip-to-menu-shortcutse-group-label');
this.menuNode.appendChild(this.shortcutsGroupNode);
if (this.config.aboutSupported === 'true') {
this.renderAboutToMenu(this.menuNode, this.config);
}
this.infoDialog = document.querySelector("skip-to-content-info-dialog");
if (!this.infoDialog) {
window.customElements.define("skip-to-content-info-dialog", SkipToContentInfoDialog);
this.infoDialog = document.createElement('skip-to-content-info-dialog');
this.infoDialog.configureStyle(this.config);
document.body.appendChild(this.infoDialog);
}
this.shortcutsMessage = document.querySelector("skip-to-shortcuts-message");
if (!this.shortcutsMessage) {
window.customElements.define("skip-to-shortcuts-message", ShortcutsMessage);
this.shortcutsMessage = document.createElement('skip-to-shortcuts-message');
this.shortcutsMessage.configureStyle(this.config);
document.body.appendChild(this.shortcutsMessage);
}
this.containerNode.addEventListener('focusin', this.handleFocusin.bind(this));
this.containerNode.addEventListener('focusout', this.handleFocusout.bind(this));
this.containerNode.addEventListener('pointerdown', this.handleContinerPointerdown.bind(this), true);
document.documentElement.addEventListener('pointerdown', this.handleBodyPointerdown.bind(this), true);
if (this.usesAltKey || this.usesOptionKey) {
document.addEventListener(
'keydown',
this.handleDocumentKeydown.bind(this)
);
}
skipToContentElem.shadowRoot.appendChild(this.containerNode);
this.focusMenuitem = null;
}
/*
* @get highlightTarget
*
* @desc Returns normalized value for the highlightTarget option
*/
get highlightTarget () {
let value = this.config.highlightTarget.trim().toLowerCase();
if ('enabled smooth'.includes(value)) {
return 'smooth';
}
if (value === 'instant') {
return 'instant';
}
return '';
}
/*
* @method focusButton
*
* @desc Sets keyboard focus on the menu button
*/
focusButton() {
this.buttonNode.focus();
this.skipToContentElem.setAttribute('focus', 'button');
}
/*
* @method addNumberToGroupLabel
*
* @desc Updates group label with the number of items in group,
* The '#' character in the string is replaced with the number
* if number is not provided, just remove number
*
* @param {String} label - Label to include number,
* @param {Number} num - Number to add to label
*
* @return {String} see @desc
*/
addNumberToGroupLabel(label, num=0) {
if (num > 0) {
return `${label} (${num})`;
}
return label;
}
/*
* @method updateLabels
*
* @desc Updates labels, important for configuration changes in browser
* add-ons and extensions
*/
updateLabels(config) {
if (this.containerNode.hasAttribute('aria-label')) {
this.containerNode.setAttribute('aria-label', config.buttonLabel);
}
const [buttonVisibleLabel, buttonAriaLabel] = this.getBrowserSpecificShortcut(config);
this.buttonNode.setAttribute('aria-label', buttonAriaLabel);
this.textButtonNode.textContent = buttonVisibleLabel;
this.smallButtonNode.textContent = config.smallButtonLabel;
this.mediumButtonNode.textContent = config.buttonLabel;
this.menuNode.setAttribute('aria-label', config.menuLabel);
this.landmarkGroupLabelNode.textContent = this.addNumberToGroupLabel(config.landmarkGroupLabel);
this.headingGroupLabelNode.textContent = this.addNumberToGroupLabel(config.headingGroupLabel);
}
/*
* @method getBrowserSpecificShortcut
*
* @desc Identifies the operating system and updates labels for
* shortcut key to use either the "alt" or the "option"
* label
*
* @param {Object} - SkipTp configure object
*
* @return {Array} - An array of two strings used for the button label
*/
getBrowserSpecificShortcut (config) {
const platform = navigator.platform.toLowerCase();
const userAgent = navigator.userAgent.toLowerCase();
const hasWin = platform.indexOf('win') >= 0;
const hasMac = platform.indexOf('mac') >= 0;
const hasLinux = platform.indexOf('linux') >= 0 || platform.indexOf('bsd') >= 0;
const hasAndroid = userAgent.indexOf('android') >= 0;
this.usesAltKey = hasWin || (hasLinux && !hasAndroid);
this.usesOptionKey = hasMac;
let label = config.buttonLabel;
let ariaLabel = config.buttonLabel;
let buttonShortcut;
// Check to make sure a shortcut key is defined
if (config.altShortcut && config.optionShortcut) {
if (this.usesAltKey || this.usesOptionKey) {
buttonShortcut = config.buttonShortcut.replace(
'$key',
config.altShortcut
);
}
if (this.usesAltKey) {
buttonShortcut = buttonShortcut.replace(
'$modifier',
config.altLabel
);
label = label + buttonShortcut;
ariaLabel = config.buttonAriaLabel.replace('$key', config.altShortcut);
ariaLabel = ariaLabel.replace('$buttonLabel', config.buttonLabel);
ariaLabel = ariaLabel.replace('$modifierLabel', config.altLabel);
ariaLabel = ariaLabel.replace('$shortcutLabel', config.shortcutLabel);
}
if (this.usesOptionKey) {
buttonShortcut = buttonShortcut.replace(
'$modifier',
config.optionLabel
);
label = label + buttonShortcut;
ariaLabel = config.buttonAriaLabel.replace('$key', config.altShortcut);
ariaLabel = ariaLabel.replace('$buttonLabel', config.buttonLabel);
ariaLabel = ariaLabel.replace('$modifierLabel', config.optionLabel);
ariaLabel = ariaLabel.replace('$shortcutLabel', config.shortcutLabel);
}
}
return [label, ariaLabel];
}
/*
* @method getFirstChar
*
* @desc Gets the first character in a menuitem to use as a shortcut key
*
* @param {Object} menuitem - DOM element node
*
* @returns {String} see @desc
*/
getFirstChar(menuitem) {
const label = menuitem.querySelector('.label');
if (label && isNotEmptyString(label.textContent)) {
return label.textContent.trim()[0].toLowerCase();
}
return '';
}
/*
* @method getHeadingLevelFromAttribute
*
* @desc Returns the the heading level of the menu item
*
* @param {Object} menuitem - DOM element node
*
* @returns {String} see @desc
*/
getHeadingLevelFromAttribute(menuitem) {
if (menuitem.hasAttribute('data-level')) {
return menuitem.getAttribute('data-level');
}
return '';
}
/*
* @method updateKeyboardShortCuts
*
* @desc Updates the keyboard short cuts for the curent menu items
*/
updateKeyboardShortCuts () {
let mi;
this.firstChars = [];
this.headingLevels = [];
for(let i = 0; i < this.menuitemNodes.length; i += 1) {
mi = this.menuitemNodes[i];
this.firstChars.push(this.getFirstChar(mi));
this.headingLevels.push(this.getHeadingLevelFromAttribute(mi));
}
}
/*
* @method updateMenuitems
*
* @desc Updates the menu information with the current menu items
* used for menu navigation commands and adds event handlers
*/
updateMenuitems () {
let menuitemNodes = this.menuNode.querySelectorAll('[role=menuitem');
this.menuitemNodes = [];
for(let i = 0; i < menuitemNodes.length; i += 1) {
const menuitemNode = menuitemNodes[i];
menuitemNode.addEventListener('keydown', this.handleMenuitemKeydown.bind(this));
menuitemNode.addEventListener('click', this.handleMenuitemClick.bind(this));
menuitemNode.addEventListener('pointerenter', this.handleMenuitemPointerenter.bind(this));
menuitemNode.addEventListener('pointerleave', this.handleMenuitemPointerleave.bind(this));
menuitemNode.addEventListener('pointerover', this.handleMenuitemPointerover.bind(this));
this.menuitemNodes.push(menuitemNode);
}
this.firstMenuitem = this.menuitemNodes[0];
this.lastMenuitem = this.menuitemNodes[this.menuitemNodes.length-1];
this.lastMenuitem.classList.add('last');
this.updateKeyboardShortCuts();
}
/*
* @method renderAboutToMenu
*
* @desc Render the about menuitem
*
* @param {Object} menuNode - DOM element node for the menu
*/
renderAboutToMenu (menuNode, config) {
const separatorNode = document.createElement('div');
separatorNode.setAttribute('role', 'separator');
const menuitemNode = document.createElement('div');
menuitemNode.setAttribute('role', 'menuitem');
menuitemNode.setAttribute('data-about-info', '');
menuitemNode.className = 'skip-to-nav skip-to-nesting-level-0';
menuitemNode.tabIndex = -1;
const labelNode = document.createElement('span');
labelNode.classList.add('label');
labelNode.textContent = config.aboutInfoLabel;
menuitemNode.appendChild(labelNode);
menuNode.appendChild(separatorNode);
menuNode.appendChild(menuitemNode);
}
/*
* @method renderMenuitemToGroup
*
* @desc Renders a menuitem using an information object about the menuitem
*
* @param {Object} groupNode - DOM element node for the menu group
* @param {Object} mi - object with menuitem information
*/
renderMenuitemToGroup (groupNode, mi) {
let tagNode, tagNodeChild, labelNode, nestingNode;
let menuitemNode = document.createElement('div');
menuitemNode.setAttribute('role', 'menuitem');
menuitemNode.classList.add(mi.class);
if (isNotEmptyString(mi.tagName)) {
menuitemNode.classList.add('skip-to-' + mi.tagName.toLowerCase());
}
menuitemNode.setAttribute('data-id', mi.dataId);
menuitemNode.tabIndex = -1;
if (isNotEmptyString(mi.ariaLabel)) {
menuitemNode.setAttribute('aria-label', mi.ariaLabel);
}
// add to group
groupNode.appendChild(menuitemNode);
// add heading level and label
if (mi.class.includes('heading')) {
if (this.config.enableHeadingLevelShortcuts) {
tagNode = document.createElement('span');
tagNodeChild = document.createElement('span');
tagNodeChild.appendChild(document.createTextNode(mi.level));
tagNode.append(tagNodeChild);
tagNode.appendChild(document.createTextNode(')'));
tagNode.classList.add('level');
menuitemNode.append(tagNode);
} else {
menuitemNode.classList.add('no-level');
}
menuitemNode.setAttribute('data-level', mi.level);
if (isNotEmptyString(mi.tagName)) {
menuitemNode.classList.add('skip-to-' + mi.tagName);
}
}
// add nesting level for landmarks
if (mi.class.includes('landmark')) {
menuitemNode.setAttribute('data-nesting', mi.nestingLevel);
menuitemNode.classList.add('skip-to-nesting-level-' + mi.nestingLevel);
if (mi.nestingLevel > 0 && (mi.nestingLevel > this.lastNestingLevel)) {
nestingNode = document.createElement('span');
nestingNode.classList.add('nesting');
menuitemNode.append(nestingNode);
}
this.lastNestingLevel = mi.nestingLevel;
}
labelNode = document.createElement('span');
labelNode.appendChild(document.createTextNode(mi.name));
labelNode.classList.add('label');
menuitemNode.append(labelNode);
return menuitemNode;
}
/*
* @method renderMenuitemsToGroup
*
* @desc Renders either the landmark region or headings menu group
*
* @param {Object} groupNode - DOM element node for the menu group
* @param {Array} menuitems - Array of objects with menu item information
* @param {String} msgNoItesmFound - Message to render if there are no menu items
*/
renderMenuitemsToGroup(groupNode, menuitems, msgNoItemsFound) {
// remove all child nodes
while (groupNode.firstChild) {
groupNode.removeChild(groupNode.firstChild);
}
this.lastNestingLevel = 0;
if (menuitems.length === 0) {
const item = {};
item.name = msgNoItemsFound;
item.tagName = '';
item.class = 'no-items';
item.dataId = '';
this.renderMenuitemToGroup(groupNode, item);
}
else {
for (let i = 0; i < menuitems.length; i += 1) {
this.renderMenuitemToGroup(groupNode, menuitems[i]);
}
}
}
/*
* @method renderMenu
*
* @desc
*/
renderMenu(config, skipToId) {
// remove landmark menu items
while (this.landmarkGroupNode.lastElementChild) {
this.landmarkGroupNode.removeChild(this.landmarkGroupNode.lastElementChild);
}
// remove heading menu items
while (this.headingGroupNode.lastElementChild) {
this.headingGroupNode.removeChild(this.headingGroupNode.lastElementChild);
}
// Create landmarks group
const [landmarkElements, headingElements] = getLandmarksAndHeadings(config, skipToId);
this.renderMenuitemsToGroup(this.landmarkGroupNode, landmarkElements, config.msgNoLandmarksFound);
this.renderMenuitemsToGroup(this.headingGroupNode, headingElements, config.msgNoHeadingsFound);
debug.flag && debug.log(`[shortcutsSupported]: ${config.shortcutsSupported}`);
this.renderMenuitemsToShortcutsGroup(this.shortcutsGroupLabelNode, this.shortcutsGroupNode);
// Update list of menuitems
this.updateMenuitems();
// Are all headings in the main region
const allInMain = headingElements.length > 0 ?
headingElements.reduce( (flag, item) => {
return flag && item.inMain;
}, true) :
false;
this.landmarkGroupLabelNode.textContent = this.addNumberToGroupLabel(config.landmarkGroupLabel, landmarkElements.length);
if (config.headings.includes('main') && allInMain) {
this.headingGroupLabelNode.textContent = this.addNumberToGroupLabel(config.headingMainGroupLabel, headingElements.length);
}
else {
this.headingGroupLabelNode.textContent = this.addNumberToGroupLabel(config.headingGroupLabel, headingElements.length);
}
}
/*
* @method renderMenuitemsToShortcutsGroup
*
* @desc Updates separator and menuitems related to page navigation
*
* @param {Object} groupLabelNode - DOM element node for the label for page navigation group
* @param {Object} groupLabelNode - DOM element node for the page navigation group
*/
renderMenuitemsToShortcutsGroup (groupLabelNode, groupNode) {
// remove page navigation menu items
while (groupNode.lastElementChild) {
groupNode.removeChild(groupNode.lastElementChild);
}
if (this.config.shortcutsSupported === 'true') {
groupNode.classList.remove('shortcuts-disabled');
groupLabelNode.classList.remove('shortcuts-disabled');
const shortcutsToggleNode = document.createElement('div');
shortcutsToggleNode.setAttribute('role', 'menuitem');
shortcutsToggleNode.className = 'shortcuts skip-to-nav skip-to-nesting-level-0';
shortcutsToggleNode.setAttribute('tabindex', '-1');
groupNode.appendChild(shortcutsToggleNode);
const shortcutsToggleLabelNode = document.createElement('span');
shortcutsToggleLabelNode.className = 'label';
shortcutsToggleNode.appendChild(shortcutsToggleLabelNode);
if (this.config.shortcuts === 'enabled') {
groupLabelNode.textContent = this.config.shortcutsGroupEnabledLabel;
shortcutsToggleNode.setAttribute('data-shortcuts-toggle', 'disable');
shortcutsToggleLabelNode.textContent = this.config.shortcutsToggleDisableLabel;
}
else {
groupLabelNode.textContent = this.config.shortcutsGroupDisabledLabel;
shortcutsToggleNode.setAttribute('data-shortcuts-toggle', 'enable');
shortcutsToggleLabelNode.textContent = this.config.shortcutsToggleEnableLabel;
}
groupNode.appendChild(shortcutsToggleNode);
const shortcutsInfoNode = document.createElement('div');
shortcutsInfoNode.setAttribute('role', 'menuitem');
shortcutsInfoNode.className = 'shortcuts skip-to-nav skip-to-nesting-level-0';
shortcutsInfoNode.setAttribute('tabindex', '-1');
shortcutsInfoNode.setAttribute('data-shortcuts-info', '');
groupNode.appendChild(shortcutsInfoNode);
const shortcutsInfoLabelNode = document.createElement('span');
shortcutsInfoLabelNode.className = 'label';
shortcutsInfoLabelNode.textContent = this.config.shortcutsInfoLabel;
shortcutsInfoNode.appendChild(shortcutsInfoLabelNode);
}
else {
groupNode.classList.add('shortcuts-disabled');
groupLabelNode.classList.add('shortcuts-disabled');
}
}
//
// Menu scripting helper functions and event handlers
//
/*
* @method setFocusToMenuitem
*
* @desc Moves focus to menu item
*
* @param {Object} menuItem - DOM element node used as a menu item
*/
setFocusToMenuitem(menuitem) {
if (menuitem) {
this.removeHoverClass(menuitem);
menuitem.classList.add('hover');
menuitem.focus();
this.skipToContentElem.setAttribute('focus', 'menu');
this.focusMenuitem = menuitem;
if (menuitem.hasAttribute('data-id')) {
const elem = queryDOMForSkipToId(menuitem.getAttribute('data-id'));
highlightElement(elem, this.highlightTarget);
}
else {
removeHighlight();
}
}
}
/*
* @method setFocusToFirstMenuitem
*
* @desc Moves focus to first menu item
*/
setFocusToFirstMenuitem() {
this.setFocusToMenuitem(this.firstMenuitem);
}
/*
* @method setFocusToLastMenuitem
*
* @desc Moves focus to last menu item
*/
setFocusToLastMenuitem() {
this.setFocusToMenuitem(this.lastMenuitem);
}
/*
* @method setFocusToPreviousMenuitem
*
* @desc Moves focus to previous menu item
*
* @param {Object} menuItem - DOM element node
*/
setFocusToPreviousMenuitem(menuitem) {
let newMenuitem, index;
if (menuitem === this.firstMenuitem) {
newMenuitem = this.lastMenuitem;
} else {
index = this.menuitemNodes.indexOf(menuitem);
newMenuitem = this.menuitemNodes[index - 1];
}
this.setFocusToMenuitem(newMenuitem);
return newMenuitem;
}
/*
* @method setFocusToNextMenuitem
*
* @desc Moves focus to next menu item
*
* @param {Object} menuItem - DOM element node
*/
setFocusToNextMenuitem(menuitem) {
let newMenuitem, index;
if (menuitem === this.lastMenuitem) {
newMenuitem = this.firstMenuitem;
} else {
index = this.menuitemNodes.indexOf(menuitem);
newMenuitem = this.menuitemNodes[index + 1];
}
this.setFocusToMenuitem(newMenuitem);
return newMenuitem;
}
/*
* @method setFocusByFirstCharacter
*
* @desc Moves focus to next menu item based on shortcut key
*
* @param {Object} menuItem - Starting DOM element node
* @param {String} char - Shortcut key to identify the
* next menu item
*/
setFocusByFirstCharacter(menuitem, char) {
let start, index;
if (char.length > 1) {
return;
}
char = char.toLowerCase();
// Get start index for search based on position of currentItem
start = this.menuitemNodes.indexOf(menuitem) + 1;
if (start >= this.menuitemNodes.length) {
start = 0;
}
// Check remaining items in the menu
index = this.firstChars.indexOf(char, start);
// If not found in remaining items, check headings
if (index === -1) {
index = this.headingLevels.indexOf(char, start);
}
// If not found in remaining items, check from beginning
if (index === -1) {
index = this.firstChars.indexOf(char, 0);
}
// If not found in remaining items, check headings from beginning
if (index === -1) {
index = this.headingLevels.indexOf(char, 0);
}
// If match was found...
if (index > -1) {
this.setFocusToMenuitem(this.menuitemNodes[index]);
}
}
/*
* @method getIndexFirstChars
*
* @desc
*
* @returns {Number}
*/
getIndexFirstChars(startIndex, char) {
for (let i = startIndex; i < this.firstChars.length; i += 1) {
if (char === this.firstChars[i]) {
return i;
}
}
return -1;
}
/*
* @method openPopup
*
* @desc Opens the menu of landmark regions and headings
*/
openPopup() {
debug.flag && debug.log(`[openPopup]`);
this.menuNode.setAttribute('aria-busy', 'true');
// Compute height of menu to not exceed about 80% of screen height
const h = (30 * window.innerHeight) / 100;
this.landmarkGroupNode.style.maxHeight = h + 'px';
this.headingGroupNode.style.maxHeight = h + 'px';
this.renderMenu(this.config, this.skipToId);
this.menuNode.style.display = 'block';
// make sure menu is on screen and not clipped in the right edge of the window
const buttonRect = this.buttonNode.getBoundingClientRect();
const menuRect = this.menuNode.getBoundingClientRect();
const diff = window.innerWidth - buttonRect.left - menuRect.width - 8;
if (diff < 0) {
if (buttonRect.left + diff < 0) {
this.menuNode.style.left = (8 - buttonRect.left) + 'px';
} else {
this.menuNode.style.left = diff + 'px';
}
}
this.menuNode.removeAttribute('aria-busy');
this.buttonNode.setAttribute('aria-expanded', 'true');
// use custom element attribute to set focus to the menu
this.skipToContentElem.setAttribute('focus', 'menu');
}
/*
* @method closePopup
*
* @desc Closes the memu of landmark regions and headings
*/
closePopup() {
debug.flag && debug.log(`[closePopup]`);
if (this.isOpen()) {
this.buttonNode.setAttribute('aria-expanded', 'false');
this.menuNode.style.display = 'none';
removeHighlight();
}
}
/*
* @method isOpen
*
* @desc Returns true if menu is open, otherwise false
*
* @returns {Boolean} see @desc
*/
isOpen() {
return this.buttonNode.getAttribute('aria-expanded') === 'true';
}
/*
* @method removeHoverClass
*
* @desc Removes hover class for menuitems
*/
removeHoverClass(target=null) {
this.menuitemNodes.forEach( node => {
if (node !== target) {
node.classList.remove('hover');
}
});
}
/*
* @method getMenuitem
*
* @desc Returns menuitem dom node if pointer is over it
*
* @param {Number} x: client x coordinator of pointer
* @param {Number} y: client y coordinator of pointer
*
* @return {object} see @desc
*/
getMenuitem(x, y) {
for (let i = 0; i < this.menuitemNodes.length; i += 1) {
const node = this.menuitemNodes[i];
const rect = node.getBoundingClientRect();
if ((rect.left <= x) &&
(rect.right >= x) &&
(rect.top <= y) &&
(rect.bottom >= y)) {
return node;
}
}
return false;
}
/*
* @method isOverButton
*
* @desc Returns true if pointer over button
*
* @param {Number} x: client x coordinator of pointer
* @param {Number} y: client y coordinator of pointer
*
* @return {object} see @desc
*/
isOverButton(x, y) {
const node = this.buttonNode;
const rect = node.getBoundingClientRect();
return (rect.left <= x) &&
(rect.right >= x) &&
(rect.top <= y) &&
(rect.bottom >= y);
}
/*
* @method isOverMenu
*
* @desc Returns true if pointer over the menu
*
* @param {Number} x: client x coordinator of pointer
* @param {Number} y: client y coordinator of pointer
*
* @return {object} see @desc
*/
isOverMenu(x, y) {
const node = this.menuNode;
const rect = node.getBoundingClientRect();
return (rect.left <= x) &&
(rect.right >= x) &&
(rect.top <= y) &&
(rect.bottom >= y);
}
/*
* @method setDisplayOption
*
* @desc Set display option for button visibility wehn it does not
* have focus
*
* @param {String} value - String with configuration information
*/
setDisplayOption(value) {
if (typeof value === 'string') {
value = value.trim().toLowerCase();
if (value.length && this.containerNode) {
this.containerNode.classList.remove('static');
this.containerNode.classList.remove('popup');
this.containerNode.classList.remove('show-border');
switch (value) {
case 'static':
this.containerNode.classList.add('static');
break;
case 'onfocus': // Legacy option
case 'popup':
this.containerNode.classList.add('popup');
break;
case 'popup-border':
this.containerNode.classList.add('popup');
this.containerNode.classList.add('show-border');
break;
default:
break;
}
}
}
}
// Menu event handlers
handleFocusin() {
this.containerNode.classList.add('focus');
this.skipToContentElem.setAttribute('focus', 'button');
}
handleFocusout() {
this.containerNode.classList.remove('focus');
this.skipToContentElem.setAttribute('focus', 'none');
}
handleButtonKeydown(event) {
let key = event.key,
flag = false;
switch (key) {
case ' ':
case 'Enter':
case 'ArrowDown':
case 'Down':
this.openPopup();
this.setFocusToFirstMenuitem();
flag = true;
break;
case 'Esc':
case 'Escape':
this.closePopup();
this.buttonNode.focus();
this.skipToContentElem.setAttribute('focus', 'button');
flag = true;
break;
case 'Up':
case 'ArrowUp':
this.openPopup();
this.setFocusToLastMenuitem();
flag = true;
break;
default:
break;
}
if (flag) {
event.stopPropagation();
event.preventDefault();
}
}
handleButtonClick(event) {
debug.flag && debug.log(`[handleButtonClick]`);