-
Notifications
You must be signed in to change notification settings - Fork 198
/
jquery-accessibleMegaMenu.js
1048 lines (962 loc) · 45 KB
/
jquery-accessibleMegaMenu.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
/*
Copyright © 2013 Adobe Systems Incorporated.
Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* See <a href="http://jquery.com">http://jquery.com</a>.
* @name jquery
* @class
* See the jQuery Library (<a href="http://jquery.com">http://jquery.com</a>) for full details. This just
* documents the function and classes that are added to jQuery by this plug-in.
*/
/**
* See <a href="http://jquery.com">http://jquery.com</a>
* @name fn
* @class
* See the jQuery Library (<a href="http://jquery.com">http://jquery.com</a>) for full details. This just
* documents the function and classes that are added to jQuery by this plug-in.
* @memberOf jquery
*/
/**
* @fileOverview accessibleMegaMenu plugin
*
*<p>Licensed under the Apache License, Version 2.0 (the “License”)
*<br />Copyright © 2013 Adobe Systems Incorporated.
*<br />Project page <a href="https://github.com/adobe-accessibility/Accessible-Mega-Menu">https://github.com/adobe-accessibility/Accessible-Mega-Menu</a>
* @version 0.1
* @author Michael Jordan
* @requires jquery
*/
/*jslint browser: true, devel: true, plusplus: true, nomen: true */
/*global jQuery */
(function ($, window, document) {
"use strict";
var pluginName = "accessibleMegaMenu",
defaults = {
uuidPrefix: "accessible-megamenu", // unique ID's are required to indicate aria-owns, aria-controls and aria-labelledby
menuClass: "accessible-megamenu", // default css class used to define the megamenu styling
topNavItemClass: "accessible-megamenu-top-nav-item", // default css class for a top-level navigation item in the megamenu
panelClass: "accessible-megamenu-panel", // default css class for a megamenu panel
panelGroupClass: "accessible-megamenu-panel-group", // default css class for a group of items within a megamenu panel
hoverClass: "hover", // default css class for the hover state
focusClass: "focus", // default css class for the focus state
openClass: "open" // default css class for the open state
},
Keyboard = {
BACKSPACE: 8,
COMMA: 188,
DELETE: 46,
DOWN: 40,
END: 35,
ENTER: 13,
ESCAPE: 27,
HOME: 36,
LEFT: 37,
PAGE_DOWN: 34,
PAGE_UP: 33,
PERIOD: 190,
RIGHT: 39,
SPACE: 32,
TAB: 9,
UP: 38,
keyMap: {
48: "0",
49: "1",
50: "2",
51: "3",
52: "4",
53: "5",
54: "6",
55: "7",
56: "8",
57: "9",
59: ";",
65: "a",
66: "b",
67: "c",
68: "d",
69: "e",
70: "f",
71: "g",
72: "h",
73: "i",
74: "j",
75: "k",
76: "l",
77: "m",
78: "n",
79: "o",
80: "p",
81: "q",
82: "r",
83: "s",
84: "t",
85: "u",
86: "v",
87: "w",
88: "x",
89: "y",
90: "z",
96: "0",
97: "1",
98: "2",
99: "3",
100: "4",
101: "5",
102: "6",
103: "7",
104: "8",
105: "9",
190: "."
}
};
/**
* @desc Creates a new accessible mega menu instance.
* @param {jquery} element
* @param {object} [options] Mega Menu options
* @param {string} [options.uuidPrefix=accessible-megamenu] - Prefix for generated unique id attributes, which are required to indicate aria-owns, aria-controls and aria-labelledby
* @param {string} [options.menuClass=accessible-megamenu] - CSS class used to define the megamenu styling
* @param {string} [options.topNavItemClass=accessible-megamenu-top-nav-item] - CSS class for a top-level navigation item in the megamenu
* @param {string} [options.panelClass=accessible-megamenu-panel] - CSS class for a megamenu panel
* @param {string} [options.panelGroupClass=accessible-megamenu-panel-group] - CSS class for a group of items within a megamenu panel
* @param {string} [options.hoverClass=hover] - CSS class for the hover state
* @param {string} [options.focusClass=focus] - CSS class for the focus state
* @param {string} [options.openClass=open] - CSS class for the open state
* @constructor
*/
function AccessibleMegaMenu(element, options) {
this.element = element;
// merge optional settings and defaults into settings
this.settings = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.mouseTimeoutID = null;
this.focusTimeoutID = null;
this.mouseFocused = false;
this.justFocused = false;
this.init();
}
AccessibleMegaMenu.prototype = (function () {
/* private attributes and methods ------------------------ */
var uuid = 0,
keydownTimeoutDuration = 1000,
keydownSearchString = "",
isTouch = typeof window.hasOwnProperty === "function" && !!window.hasOwnProperty("ontouchstart"),
_getPlugin,
_addUniqueId,
_togglePanel,
_clickHandler,
_clickOutsideHandler,
_DOMAttrModifiedHandler,
_focusInHandler,
_focusOutHandler,
_keyDownHandler,
_mouseDownHandler,
_mouseOverHandler,
_mouseOutHandler,
_toggleExpandedEventHandlers;
/**
* @name jQuery.fn.accessibleMegaMenu~_getPlugin
* @desc Returns the parent accessibleMegaMenu instance for a given element
* @param {jQuery} element
* @memberof jQuery.fn.accessibleMegaMenu
* @inner
* @private
*/
_getPlugin = function (element) {
return $(element).closest(':data(plugin_' + pluginName + ')').data("plugin_" + pluginName);
};
/**
* @name jQuery.fn.accessibleMegaMenu~_addUniqueId
* @desc Adds a unique id and element.
* The id string starts with the
* string defined in settings.uuidPrefix.
* @param {jQuery} element
* @memberof jQuery.fn.accessibleMegaMenu
* @inner
* @private
*/
_addUniqueId = function (element) {
element = $(element);
var settings = this.settings;
if (!element.attr("id")) {
element.attr("id", settings.uuidPrefix + "-" + new Date().getTime() + "-" + (++uuid));
}
};
/**
* @name jQuery.fn.accessibleMegaMenu~_togglePanel
* @desc Toggle the display of mega menu panels in response to an event.
* The optional boolean value 'hide' forces all panels to hide.
* @param {event} event
* @param {Boolean} [hide] Hide all mega menu panels when true
* @memberof jQuery.fn.accessibleMegaMenu
* @inner
* @private
*/
_togglePanel = function (event, hide) {
var target = $(event.target),
that = this,
settings = this.settings,
menu = this.menu,
topli = target.closest('.' + settings.topNavItemClass),
panel = target.hasClass(settings.panelClass) ? target : target.closest('.' + settings.panelClass),
newfocus;
_toggleExpandedEventHandlers.call(this, hide);
$('html').off('mouseup.outside-accessible-megamenu, touchend.outside-accessible-megamenu, mspointerup.outside-accessible-megamenu, pointerup.outside-accessible-megamenu', _clickOutsideHandler);
menu.find('[aria-expanded].' + this.settings.panelClass).off('DOMAttrModified.accessible-megamenu');
if (hide) {
topli = menu.find('.' + settings.topNavItemClass + ' .' + settings.openClass + ':first').closest('.' + settings.topNavItemClass);
if (!(topli.is(event.relatedTarget) || topli.has(event.relatedTarget).length > 0)) {
if ((event.type === 'mouseout' || event.type === 'focusout') && topli.has(document.activeElement).length > 0) {
return;
}
topli.find('[aria-expanded]')
.attr('aria-expanded', 'false')
.removeClass(settings.openClass)
.filter('.' + settings.panelClass)
.attr('aria-hidden', 'true');
if ((event.type === 'keydown' && event.keyCode === Keyboard.ESCAPE) || event.type === 'DOMAttrModified') {
newfocus = topli.find(':tabbable:first');
setTimeout(function () {
menu.find('[aria-expanded].' + that.settings.panelClass).off('DOMAttrModified.accessible-megamenu');
newfocus.focus();
that.justFocused = false;
}, 99);
}
} else if (topli.length === 0) {
menu.find('[aria-expanded=true]')
.attr('aria-expanded', 'false')
.removeClass(settings.openClass)
.filter('.' + settings.panelClass)
.attr('aria-hidden', 'true');
}
} else {
clearTimeout(that.focusTimeoutID);
topli.siblings()
.find('[aria-expanded]')
.attr('aria-expanded', 'false')
.removeClass(settings.openClass)
.filter('.' + settings.panelClass)
.attr('aria-hidden', 'true');
topli.find('[aria-expanded]')
.attr('aria-expanded', 'true')
.addClass(settings.openClass)
.filter('.' + settings.panelClass)
.attr('aria-hidden', 'false');
if (event.type === 'mouseover' && target.is(':tabbable') && topli.length === 1 && panel.length === 0 && menu.has(document.activeElement).length > 0) {
target.focus();
that.justFocused = false;
}
_toggleExpandedEventHandlers.call(that);
}
};
/**
* @name jQuery.fn.accessibleMegaMenu~_clickHandler
* @desc Handle click event on mega menu item
* @param {event} Event object
* @memberof jQuery.fn.accessibleMegaMenu
* @inner
* @private
*/
_clickHandler = function (event) {
var target = $(event.target),
topli = target.closest('.' + this.settings.topNavItemClass),
panel = target.closest('.' + this.settings.panelClass);
if (topli.length === 1
&& panel.length === 0
&& topli.find('.' + this.settings.panelClass).length === 1) {
if (!target.hasClass(this.settings.openClass)) {
event.preventDefault();
event.stopPropagation();
_togglePanel.call(this, event);
} else {
if (this.justFocused) {
event.preventDefault();
event.stopPropagation();
this.justFocused = false;
} else if (isTouch) {
event.preventDefault();
event.stopPropagation();
_togglePanel.call(this, event, target.hasClass(this.settings.openClass));
}
}
}
};
/**
* @name jQuery.fn.accessibleMegaMenu~_clickOutsideHandler
* @desc Handle click event outside of a the megamenu
* @param {event} Event object
* @memberof jQuery.fn.accessibleMegaMenu
* @inner
* @private
*/
_clickOutsideHandler = function (event) {
if (this.menu.has($(event.target)).length === 0) {
event.preventDefault();
event.stopPropagation();
_togglePanel.call(this, event, true);
}
};
/**
* @name jQuery.fn.accessibleMegaMenu~_DOMAttrModifiedHandler
* @desc Handle DOMAttrModified event on panel to respond to Windows 8 Narrator ExpandCollapse pattern
* @param {event} Event object
* @memberof jQuery.fn.accessibleMegaMenu
* @inner
* @private
*/
_DOMAttrModifiedHandler = function (event) {
if (event.originalEvent.attrName === 'aria-expanded'
&& event.originalEvent.newValue === 'false'
&& $(event.target).hasClass(this.settings.openClass)) {
event.preventDefault();
event.stopPropagation();
_togglePanel.call(this, event, true);
}
};
/**
* @name jQuery.fn.accessibleMegaMenu~_focusInHandler
* @desc Handle focusin event on mega menu item.
* @param {event} Event object
* @memberof jQuery.fn.accessibleMegaMenu
* @inner
* @private
*/
_focusInHandler = function (event) {
clearTimeout(this.focusTimeoutID);
$(event.target)
.addClass(this.settings.focusClass)
.on('click.accessible-megamenu', $.proxy(_clickHandler, this));
this.justFocused = !this.mouseFocused;
this.mouseFocused = false;
if (this.panels.filter('.' + this.settings.openClass).length) {
_togglePanel.call(this, event);
}
};
/**
* @name jQuery.fn.accessibleMegaMenu~_focusOutHandler
* @desc Handle focusout event on mega menu item.
* @param {event} Event object
* @memberof jQuery.fn.accessibleMegaMenu
* @inner
* @private
*/
_focusOutHandler = function (event) {
this.justFocused = false;
var that = this,
target = $(event.target),
topli = target.closest('.' + this.settings.topNavItemClass),
keepOpen = false;
target
.removeClass(this.settings.focusClass)
.off('click.accessible-megamenu', $.proxy(_clickHandler, this));
if (window.cvox) {
// If ChromeVox is running...
that.focusTimeoutID = setTimeout(function () {
window.cvox.Api.getCurrentNode(function (node) {
if (topli.has(node).length) {
// and the current node being voiced is in
// the mega menu, clearTimeout,
// so the panel stays open.
clearTimeout(that.focusTimeoutID);
} else {
that.focusTimeoutID = setTimeout(function (scope, event, hide) {
_togglePanel.call(scope, event, hide);
}, 275, that, event, true);
}
});
}, 25);
} else {
that.focusTimeoutID = setTimeout(function () {
_togglePanel.call(that, event, true);
}, 300);
}
};
/**
* @name jQuery.fn.accessibleMegaMenu~_keyDownHandler
* @desc Handle keydown event on mega menu.
* @param {event} Event object
* @memberof jQuery.fn.accessibleMegaMenu
* @inner
* @private
*/
_keyDownHandler = function (event) {
var that = (this.constructor === AccessibleMegaMenu) ? this : _getPlugin(this), // determine the AccessibleMegaMenu plugin instance
settings = that.settings,
target = $($(this).is('.' + settings.hoverClass + ':tabbable') ? this : event.target), // if the element is hovered the target is this, otherwise, its the focused element
menu = that.menu,
topnavitems = that.topnavitems,
topli = target.closest('.' + settings.topNavItemClass),
tabbables = menu.find(':tabbable'),
panel = target.hasClass(settings.panelClass) ? target : target.closest('.' + settings.panelClass),
panelGroups = panel.find('.' + settings.panelGroupClass),
currentPanelGroup = target.closest('.' + settings.panelGroupClass),
next,
keycode = event.keyCode || event.which,
start,
i,
o,
label,
found = false,
newString = Keyboard.keyMap[event.keyCode] || '',
regex,
isTopNavItem = (topli.length === 1 && panel.length === 0);
if (target.is("input:focus, select:focus, textarea:focus, button:focus")) {
// if the event target is a form element we should handle keydown normally
return;
}
if (target.is('.' + settings.hoverClass + ':tabbable')) {
$('html').off('keydown.accessible-megamenu');
}
switch (keycode) {
case Keyboard.ESCAPE:
_togglePanel.call(that, event, true);
break;
case Keyboard.DOWN:
event.preventDefault();
if (isTopNavItem) {
_togglePanel.call(that, event);
found = (topli.find('.' + settings.panelClass + ' :tabbable:first').focus().length === 1);
} else {
found = (tabbables.filter(':gt(' + tabbables.index(target) + '):first').focus().length === 1);
}
if (!found && window.opera && opera.toString() === "[object Opera]" && (event.ctrlKey || event.metaKey)) {
tabbables = $(':tabbable');
i = tabbables.index(target);
found = ($(':tabbable:gt(' + $(':tabbable').index(target) + '):first').focus().length === 1);
}
break;
case Keyboard.UP:
event.preventDefault();
if (isTopNavItem && target.hasClass(settings.openClass)) {
_togglePanel.call(that, event, true);
next = topnavitems.filter(':lt(' + topnavitems.index(topli) + '):last');
if (next.children('.' + settings.panelClass).length) {
found = (next.children()
.attr('aria-expanded', 'true')
.addClass(settings.openClass)
.filter('.' + settings.panelClass)
.attr('aria-hidden', 'false')
.find(':tabbable:last')
.focus() === 1);
}
} else if (!isTopNavItem) {
found = (tabbables.filter(':lt(' + tabbables.index(target) + '):last').focus().length === 1);
}
if (!found && window.opera && opera.toString() === "[object Opera]" && (event.ctrlKey || event.metaKey)) {
tabbables = $(':tabbable');
i = tabbables.index(target);
found = ($(':tabbable:lt(' + $(':tabbable').index(target) + '):first').focus().length === 1);
}
break;
case Keyboard.RIGHT:
event.preventDefault();
if (isTopNavItem) {
found = (topnavitems.filter(':gt(' + topnavitems.index(topli) + '):first').find(':tabbable:first').focus().length === 1);
} else {
if (panelGroups.length && currentPanelGroup.length) {
// if the current panel contains panel groups, and we are able to focus the first tabbable element of the next panel group
found = (panelGroups.filter(':gt(' + panelGroups.index(currentPanelGroup) + '):first').find(':tabbable:first').focus().length === 1);
}
if (!found) {
found = (topli.find(':tabbable:first').focus().length === 1);
}
}
break;
case Keyboard.LEFT:
event.preventDefault();
if (isTopNavItem) {
found = (topnavitems.filter(':lt(' + topnavitems.index(topli) + '):last').find(':tabbable:first').focus().length === 1);
} else {
if (panelGroups.length && currentPanelGroup.length) {
// if the current panel contains panel groups, and we are able to focus the first tabbable element of the previous panel group
found = (panelGroups.filter(':lt(' + panelGroups.index(currentPanelGroup) + '):last').find(':tabbable:first').focus().length === 1);
}
if (!found) {
found = (topli.find(':tabbable:first').focus().length === 1);
}
}
break;
case Keyboard.TAB:
i = tabbables.index(target);
if (event.shiftKey && isTopNavItem && target.hasClass(settings.openClass)) {
_togglePanel.call(that, event, true);
next = topnavitems.filter(':lt(' + topnavitems.index(topli) + '):last');
if (next.children('.' + settings.panelClass).length) {
found = next.children()
.attr('aria-expanded', 'true')
.addClass(settings.openClass)
.filter('.' + settings.panelClass)
.attr('aria-hidden', 'false')
.find(':tabbable:last')
.focus();
}
} else if (event.shiftKey && i > 0) {
found = (tabbables.filter(':lt(' + i + '):last').focus().length === 1);
} else if (!event.shiftKey && i < tabbables.length - 1) {
found = (tabbables.filter(':gt(' + i + '):first').focus().length === 1);
} else if (window.opera && opera.toString() === "[object Opera]") {
tabbables = $(':tabbable');
i = tabbables.index(target);
if (event.shiftKey) {
found = ($(':tabbable:lt(' + $(':tabbable').index(target) + '):last').focus().length === 1);
} else {
found = ($(':tabbable:gt(' + $(':tabbable').index(target) + '):first').focus().length === 1);
}
}
if (found) {
event.preventDefault();
}
break;
case Keyboard.SPACE:
if (isTopNavItem) {
event.preventDefault();
_clickHandler.call(that, event);
} else {
return true;
}
break;
case Keyboard.ENTER:
return true;
break;
default:
// alphanumeric filter
clearTimeout(this.keydownTimeoutID);
keydownSearchString += newString !== keydownSearchString ? newString : '';
if (keydownSearchString.length === 0) {
return;
}
this.keydownTimeoutID = setTimeout(function () {
keydownSearchString = '';
}, keydownTimeoutDuration);
if (isTopNavItem && !target.hasClass(settings.openClass)) {
tabbables = tabbables.filter(':not(.' + settings.panelClass + ' :tabbable)');
} else {
tabbables = topli.find(':tabbable');
}
if (event.shiftKey) {
tabbables = $(tabbables.get()
.reverse());
}
for (i = 0; i < tabbables.length; i++) {
o = tabbables.eq(i);
if (o.is(target)) {
start = (keydownSearchString.length === 1) ? i + 1 : i;
break;
}
}
regex = new RegExp('^' + keydownSearchString.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&'), 'i');
for (i = start; i < tabbables.length; i++) {
o = tabbables.eq(i);
label = $.trim(o.text());
if (regex.test(label)) {
found = true;
o.focus();
break;
}
}
if (!found) {
for (i = 0; i < start; i++) {
o = tabbables.eq(i);
label = $.trim(o.text());
if (regex.test(label)) {
o.focus();
break;
}
}
}
break;
}
that.justFocused = false;
};
/**
* @name jQuery.fn.accessibleMegaMenu~_mouseDownHandler
* @desc Handle mousedown event on mega menu.
* @param {event} Event object
* @memberof accessibleMegaMenu
* @inner
* @private
*/
_mouseDownHandler = function (event) {
if ($(event.target).is(":tabbable, :focusable, ." + this.settings.panelClass)) {
this.mouseFocused = true;
}
this.mouseTimeoutID = setTimeout(function () {
clearTimeout(this.focusTimeoutID);
}, 1);
};
/**
* @name jQuery.fn.accessibleMegaMenu~_mouseOverHandler
* @desc Handle mouseover event on mega menu.
* @param {event} Event object
* @memberof jQuery.fn.accessibleMegaMenu
* @inner
* @private
*/
_mouseOverHandler = function (event) {
clearTimeout(this.mouseTimeoutID);
$(event.target)
.addClass(this.settings.hoverClass);
_togglePanel.call(this, event);
if ($(event.target).is(':tabbable')) {
$('html').on('keydown.accessible-megamenu', $.proxy(_keyDownHandler, event.target));
}
};
/**
* @name jQuery.fn.accessibleMegaMenu~_mouseOutHandler
* @desc Handle mouseout event on mega menu.
* @param {event} Event object
* @memberof jQuery.fn.accessibleMegaMenu
* @inner
* @private
*/
_mouseOutHandler = function (event) {
var that = this;
$(event.target)
.removeClass(that.settings.hoverClass);
that.mouseTimeoutID = setTimeout(function () {
_togglePanel.call(that, event, true);
}, 250);
if ($(event.target).is(':tabbable')) {
$('html').off('keydown.accessible-megamenu');
}
};
_toggleExpandedEventHandlers = function (hide) {
var menu = this.menu;
if (hide) {
$('html').off('mouseup.outside-accessible-megamenu, touchend.outside-accessible-megamenu, mspointerup.outside-accessible-megamenu, pointerup.outside-accessible-megamenu', _clickOutsideHandler);
menu.find('[aria-expanded].' + this.settings.panelClass).off('DOMAttrModified.accessible-megamenu', _DOMAttrModifiedHandler);
} else {
$('html').on('mouseup.outside-accessible-megamenu, touchend.outside-accessible-megamenu, mspointerup.outside-accessible-megamenu, pointerup.outside-accessible-megamenu', $.proxy(_clickOutsideHandler, this));
/* Narrator in Windows 8 automatically toggles the aria-expanded property on double tap or click.
To respond to the change to collapse the panel, we must add a listener for a DOMAttrModified event. */
menu.find('[aria-expanded=true].' + this.settings.panelClass).on('DOMAttrModified.accessible-megamenu', $.proxy(_DOMAttrModifiedHandler, this));
}
};
/* public attributes and methods ------------------------- */
return {
constructor: AccessibleMegaMenu,
/**
* @lends jQuery.fn.accessibleMegaMenu
* @desc Initializes an instance of the accessibleMegaMenu plugins
* @memberof jQuery.fn.accessibleMegaMenu
* @instance
*/
init: function () {
var that = this,
settings = this.settings,
nav = this.nav = $(this.element),
menu = this.menu = nav.children().first(),
topnavitems = this.topnavitems = menu.children();
nav.attr("role", "navigation");
menu.addClass(settings.menuClass);
topnavitems.each(function (i, topnavitem) {
var topnavitemlink, topnavitempanel;
topnavitem = $(topnavitem);
topnavitem.addClass(settings.topNavItemClass);
topnavitemlink = topnavitem.find(":tabbable:first");
topnavitempanel = topnavitem.children(":not(:tabbable):last");
_addUniqueId.call(that, topnavitemlink);
if (topnavitempanel.length) {
_addUniqueId.call(that, topnavitempanel);
topnavitemlink.attr({
"aria-haspopup": true,
"aria-owns": topnavitempanel.attr("id"),
"aria-controls": topnavitempanel.attr("id"),
"aria-expanded": false
});
topnavitempanel.attr({
"role": "group",
"aria-expanded": false,
"aria-hidden": true
})
.addClass(settings.panelClass)
.not("[aria-labelledby]")
.attr("aria-labelledby", topnavitemlink.attr("id"));
}
});
this.panels = menu.find("." + settings.panelClass);
menu.on("focusin.accessible-megamenu", ":tabbable, :focusable, ." + settings.panelClass, $.proxy(_focusInHandler, this))
.on("focusout.accessible-megamenu", ":tabbable, :focusable, ." + settings.panelClass, $.proxy(_focusOutHandler, this))
.on("keydown.accessible-megamenu", $.proxy(_keyDownHandler, this))
.on("mouseover.accessible-megamenu", $.proxy(_mouseOverHandler, this))
.on("mouseout.accessible-megamenu", $.proxy(_mouseOutHandler, this))
.on("mousedown.accessible-megamenu", $.proxy(_mouseDownHandler, this));
if (isTouch) {
menu.on("touchstart.accessible-megamenu", $.proxy(_clickHandler, this));
}
menu.find("hr").attr("role", "separator");
},
/**
* @desc Get default values
* @example $(selector).accessibleMegaMenu("getDefaults");
* @return {object}
* @memberof jQuery.fn.accessibleMegaMenu
* @instance
*/
getDefaults: function () {
return this._defaults;
},
/**
* @desc Get any option set to plugin using its name (as string)
* @example $(selector).accessibleMegaMenu("getOption", some_option);
* @param {string} opt
* @return {string}
* @memberof jQuery.fn.accessibleMegaMenu
* @instance
*/
getOption: function (opt) {
return this.settings[opt];
},
/**
* @desc Get all options
* @example $(selector).accessibleMegaMenu("getAllOptions");
* @return {object}
* @memberof jQuery.fn.accessibleMegaMenu
* @instance
*/
getAllOptions: function () {
return this.settings;
},
/**
* @desc Set option
* @example $(selector).accessibleMegaMenu("setOption", "option_name", "option_value", reinitialize);
* @param {string} opt - Option name
* @param {string} val - Option value
* @param {boolean} [reinitialize] - boolean to re-initialize the menu.
* @memberof jQuery.fn.accessibleMegaMenu
* @instance
*/
setOption: function (opt, value, reinitialize) {
this.settings[opt] = value;
if (reinitialize) {
this.init();
}
}
};
}());
/* lightweight plugin wrapper around the constructor,
to prevent against multiple instantiations */
/**
* @class accessibleMegaMenu
* @memberOf jQuery.fn
* @classdesc Implements an accessible mega menu as a jQuery plugin.
* <p>The mega-menu It is modeled after the mega menu on {@link http://adobe.com|adobe.com} but has been simplified for use by others. A brief description of the interaction design choices can be found in a blog post at {@link http://blogs.adobe.com/accessibility/2013/05/adobe-com.html|Mega menu accessibility on adobe.com}.</p>
* <h3>Keyboard Accessibility</h3>
* <p>The accessible mega menu supports keyboard interaction modeled after the behavior described in the {@link http://www.w3.org/TR/wai-aria-practices/#menu|WAI-ARIA Menu or Menu bar (widget) design pattern}, however we also try to respect users' general expectations for the behavior of links in a global navigation. To this end, the accessible mega menu implementation permits tab focus on each of the six top-level menu items. When one of the menu items has focus, pressing the Enter key, Spacebar or Down arrow will open the submenu panel, and pressing the Left or Right arrow key will shift focus to the adjacent menu item. Links within the submenu panels are included in the tab order when the panel is open. They can also be navigated with the arrow keys or by typing the first character in the link name, which speeds up keyboard navigation considerably. Pressing the Escape key closes the submenu and restores focus to the parent menu item.</p>
* <h3>Screen Reader Accessibility</h3>
* <p>The accessible mega menu models its use of WAI-ARIA Roles, States, and Properties after those described in the {@link http://www.w3.org/TR/wai-aria-practices/#menu|WAI-ARIA Menu or Menu bar (widget) design pattern} with some notable exceptions, so that it behaves better with screen reader user expectations for global navigation. We don't use <code class="prettyprint prettyprinted" style=""><span class="pln">role</span><span class="pun">=</span><span class="str">"menu"</span></code> for the menu container and <code class="prettyprint prettyprinted" style=""><span class="pln">role</span><span class="pun">=</span><span class="str">"menuitem"</span></code> for each of the links therein, because if we do, assistive technology will no longer interpret the links as links, but instead, as menu items, and the links in our global navigation will no longer show up when a screen reader user executes a shortcut command to bring up a list of links in the page.</p>
* @example <h4>HTML</h4><hr/>
<nav>
<ul class="nav-menu">
<li class="nav-item">
<a href="?movie">Movies</a>
<div class="sub-nav">
<ul class="sub-nav-group">
<li><a href="?movie&genre=0">Action &amp; Adventure</a></li>
<li><a href="?movie&genre=2">Children &amp; Family</a></li>
<li>&#8230;</li>
</ul>
<ul class="sub-nav-group">
<li><a href="?movie&genre=7">Dramas</a></li>
<li><a href="?movie&genre=9">Foreign</a></li>
<li>&#8230;</li>
</ul>
<ul class="sub-nav-group">
<li><a href="?movie&genre=14">Musicals</a></li>
<li><a href="?movie&genre=15">Romance</a></li>
<li>&#8230;</li>
</ul>
</div>
</li>
<li class="nav-item">
<a href="?tv">TV Shows</a>
<div class="sub-nav">
<ul class="sub-nav-group">
<li><a href="?tv&genre=20">Classic TV</a></li>
<li><a href="?tv&genre=21">Crime TV</a></li>
<li>&#8230;</li>
</ul>
<ul class="sub-nav-group">
<li><a href="?tv&genre=27">Reality TV</a></li>
<li><a href="?tv&genre=30">TV Action</a></li>
<li>&#8230;</li>
</ul>
<ul class="sub-nav-group">
<li><a href="?tv&genre=33">TV Dramas</a></li>
<li><a href="?tv&genre=34">TV Horror</a></li>
<li>&#8230;</li>
</ul>
</div>
</li>
</ul>
</nav>
* @example <h4>CSS</h4><hr/>
/* Rudimentary mega menu CSS for demonstration */
/* mega menu list */
.nav-menu {
display: block;
position: relative;
list-style: none;
margin: 0;
padding: 0;
z-index: 15;
}
/* a top level navigation item in the mega menu */
.nav-item {
list-style: none;
display: inline-block;
padding: 0;
margin: 0;
}
/* first descendant link within a top level navigation item */
.nav-item > a {
position: relative;
display: inline-block;
padding: 0.5em 1em;
margin: 0 0 -1px 0;
border: 1px solid transparent;
}
/* focus/open states of first descendant link within a top level
navigation item */
.nav-item > a:focus,
.nav-item > a.open {
border: 1px solid #dedede;
}
/* open state of first descendant link within a top level
navigation item */
.nav-item > a.open {
background-color: #fff;
border-bottom: none;
z-index: 1;
}
/* sub-navigation panel */
.sub-nav {
position: absolute;
display: none;
top: 2.2em;
margin-top: -1px;
padding: 0.5em 1em;
border: 1px solid #dedede;
background-color: #fff;
}
/* sub-navigation panel open state */
.sub-nav.open {
display: block;
}
/* list of items within sub-navigation panel */
.sub-nav ul {
display: inline-block;
vertical-align: top;
margin: 0 1em 0 0;
padding: 0;
}
/* list item within sub-navigation panel */
.sub-nav li {
display: block;
list-style-type: none;
margin: 0;
padding: 0;
}
* @example <h4>JavaScript</h4><hr/>
<!-- include jquery -->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<!-- include the jquery-accessibleMegaMenu plugin script -->
<script src="js/jquery-accessibleMegaMenu.js"></script>
<!-- initialize a selector as an accessibleMegaMenu -->
<script>
$("nav:first").accessibleMegaMenu({
/* prefix for generated unique id attributes, which are required to indicate aria-owns, aria-controls and aria-labelledby */
uuidPrefix: "accessible-megamenu",
/* css class used to define the megamenu styling */
menuClass: "nav-menu",
/* css class for a top-level navigation item in the megamenu */
topNavItemClass: "nav-item",
/* css class for a megamenu panel */
panelClass: "sub-nav",
/* css class for a group of items within a megamenu panel */
panelGroupClass: "sub-nav-group",
/* css class for the hover state */
hoverClass: "hover",
/* css class for the focus state */
focusClass: "focus",
/* css class for the open state */
openClass: "open"
});
</script>
* @param {object} [options] Mega Menu options
* @param {string} [options.uuidPrefix=accessible-megamenu] - Prefix for generated unique id attributes, which are required to indicate aria-owns, aria-controls and aria-labelledby
* @param {string} [options.menuClass=accessible-megamenu] - CSS class used to define the megamenu styling
* @param {string} [options.topNavItemClass=accessible-megamenu-top-nav-item] - CSS class for a top-level navigation item in the megamenu
* @param {string} [options.panelClass=accessible-megamenu-panel] - CSS class for a megamenu panel
* @param {string} [options.panelGroupClass=accessible-megamenu-panel-group] - CSS class for a group of items within a megamenu panel
* @param {string} [options.hoverClass=hover] - CSS class for the hover state
* @param {string} [options.focusClass=focus] - CSS class for the focus state
* @param {string} [options.openClass=open] - CSS class for the open state
*/
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new AccessibleMegaMenu(this, options));
}
});
};
/* :focusable and :tabbable selectors from
https://raw.github.com/jquery/jquery-ui/master/ui/jquery.ui.core.js */
/**
* @private
*/
function visible(element) {
return $.expr.filters.visible(element) && !$(element).parents().addBack().filter(function () {