1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Aria menubar functionality. Enhances a simple nested list structure into a full aria widget.
|
|
|
18 |
* Based on the open ajax example: http://oaa-accessibility.org/example/26/
|
|
|
19 |
*
|
|
|
20 |
* @module tool_lp/menubar
|
|
|
21 |
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
define(['jquery'], function($) {
|
|
|
25 |
|
|
|
26 |
/** @property {boolean} Flag to indicate if we have already registered a click event handler for the document. */
|
|
|
27 |
var documentClickHandlerRegistered = false;
|
|
|
28 |
|
|
|
29 |
/** @property {boolean} Flag to indicate whether there's an active, open menu. */
|
|
|
30 |
var menuActive = false;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Close all open submenus anywhere in the page (there should only ever be one open at a time).
|
|
|
34 |
*
|
|
|
35 |
* @method closeAllSubMenus
|
|
|
36 |
*/
|
|
|
37 |
var closeAllSubMenus = function() {
|
|
|
38 |
$('.tool-lp-menu .tool-lp-sub-menu').attr('aria-hidden', 'true');
|
|
|
39 |
// Every menu's closed at this point, so set the menu active flag to false.
|
|
|
40 |
menuActive = false;
|
|
|
41 |
};
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Constructor
|
|
|
45 |
*
|
|
|
46 |
* @param {jQuery} menuRoot Jquery collection matching the root of the menu.
|
|
|
47 |
* @param {Function[]} handlers called when a menu item is chosen.
|
|
|
48 |
*/
|
|
|
49 |
var Menubar = function(menuRoot, handlers) {
|
|
|
50 |
// Setup private class variables.
|
|
|
51 |
this.menuRoot = menuRoot;
|
|
|
52 |
this.handlers = handlers;
|
|
|
53 |
this.rootMenus = this.menuRoot.children('li');
|
|
|
54 |
this.subMenus = this.rootMenus.children('ul');
|
|
|
55 |
this.subMenuItems = this.subMenus.children('li');
|
|
|
56 |
this.allItems = this.rootMenus.add(this.subMenuItems);
|
|
|
57 |
this.activeItem = null;
|
|
|
58 |
this.isChildOpen = false;
|
|
|
59 |
|
|
|
60 |
this.keys = {
|
|
|
61 |
tab: 9,
|
|
|
62 |
enter: 13,
|
|
|
63 |
esc: 27,
|
|
|
64 |
space: 32,
|
|
|
65 |
left: 37,
|
|
|
66 |
up: 38,
|
|
|
67 |
right: 39,
|
|
|
68 |
down: 40
|
|
|
69 |
};
|
|
|
70 |
|
|
|
71 |
this.addAriaAttributes();
|
|
|
72 |
// Add the event listeners.
|
|
|
73 |
this.addEventListeners();
|
|
|
74 |
};
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Open a submenu, first it closes all other sub-menus and sets the open direction.
|
|
|
78 |
* @method openSubMenu
|
|
|
79 |
* @param {Node} menu
|
|
|
80 |
*/
|
|
|
81 |
Menubar.prototype.openSubMenu = function(menu) {
|
|
|
82 |
this.setOpenDirection();
|
|
|
83 |
closeAllSubMenus();
|
|
|
84 |
menu.attr('aria-hidden', 'false');
|
|
|
85 |
// Set menu active flag to true when a menu is opened.
|
|
|
86 |
menuActive = true;
|
|
|
87 |
};
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Bind the event listeners to the DOM
|
|
|
92 |
* @method addEventListeners
|
|
|
93 |
*/
|
|
|
94 |
Menubar.prototype.addEventListeners = function() {
|
|
|
95 |
var currentThis = this;
|
|
|
96 |
|
|
|
97 |
// When clicking outside the menubar.
|
|
|
98 |
if (documentClickHandlerRegistered === false) {
|
|
|
99 |
$(document).click(function() {
|
|
|
100 |
// Check if a menu is opened.
|
|
|
101 |
if (menuActive) {
|
|
|
102 |
// Close menu.
|
|
|
103 |
closeAllSubMenus();
|
|
|
104 |
}
|
|
|
105 |
});
|
|
|
106 |
// Set this flag to true so that we won't need to add a document click handler for the other Menubar instances.
|
|
|
107 |
documentClickHandlerRegistered = true;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
// Hovers.
|
|
|
111 |
this.subMenuItems.mouseenter(function() {
|
|
|
112 |
$(this).addClass('menu-hover');
|
|
|
113 |
return true;
|
|
|
114 |
});
|
|
|
115 |
|
|
|
116 |
this.subMenuItems.mouseout(function() {
|
|
|
117 |
$(this).removeClass('menu-hover');
|
|
|
118 |
return true;
|
|
|
119 |
});
|
|
|
120 |
|
|
|
121 |
// Mouse listeners.
|
|
|
122 |
this.allItems.click(function(e) {
|
|
|
123 |
return currentThis.handleClick($(this), e);
|
|
|
124 |
});
|
|
|
125 |
|
|
|
126 |
// Key listeners.
|
|
|
127 |
this.allItems.keydown(function(e) {
|
|
|
128 |
return currentThis.handleKeyDown($(this), e);
|
|
|
129 |
});
|
|
|
130 |
|
|
|
131 |
this.allItems.focus(function() {
|
|
|
132 |
return currentThis.handleFocus($(this));
|
|
|
133 |
});
|
|
|
134 |
|
|
|
135 |
this.allItems.blur(function() {
|
|
|
136 |
return currentThis.handleBlur($(this));
|
|
|
137 |
});
|
|
|
138 |
};
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Process click events for the top menus.
|
|
|
142 |
*
|
|
|
143 |
* @method handleClick
|
|
|
144 |
* @param {Object} item is the jquery object of the item firing the event
|
|
|
145 |
* @param {Event} e is the associated event object
|
|
|
146 |
* @return {boolean} Returns false
|
|
|
147 |
*/
|
|
|
148 |
Menubar.prototype.handleClick = function(item, e) {
|
|
|
149 |
e.stopPropagation();
|
|
|
150 |
|
|
|
151 |
var parentUL = item.parent();
|
|
|
152 |
|
|
|
153 |
if (parentUL.is('.tool-lp-menu')) {
|
|
|
154 |
// Toggle the child menu open/closed.
|
|
|
155 |
if (item.children('ul').first().attr('aria-hidden') == 'true') {
|
|
|
156 |
this.openSubMenu(item.children('ul').first());
|
|
|
157 |
} else {
|
|
|
158 |
item.children('ul').first().attr('aria-hidden', 'true');
|
|
|
159 |
}
|
|
|
160 |
} else {
|
|
|
161 |
// Remove hover and focus styling.
|
|
|
162 |
this.allItems.removeClass('menu-hover menu-focus');
|
|
|
163 |
|
|
|
164 |
// Clear the active item.
|
|
|
165 |
this.activeItem = null;
|
|
|
166 |
|
|
|
167 |
// Close the menu.
|
|
|
168 |
this.menuRoot.find('ul').not('.root-level').attr('aria-hidden', 'true');
|
|
|
169 |
// Follow any link, or call the click handlers.
|
|
|
170 |
var anchor = item.find('a').first();
|
|
|
171 |
var clickEvent = new $.Event('click');
|
|
|
172 |
clickEvent.target = anchor;
|
|
|
173 |
var eventHandled = false;
|
|
|
174 |
if (this.handlers) {
|
|
|
175 |
$.each(this.handlers, function(selector, handler) {
|
|
|
176 |
if (eventHandled) {
|
|
|
177 |
return;
|
|
|
178 |
}
|
|
|
179 |
if (item.find(selector).length > 0) {
|
|
|
180 |
var callable = $.proxy(handler, anchor);
|
|
|
181 |
// False means stop propogatting events.
|
|
|
182 |
eventHandled = (callable(clickEvent) === false) || clickEvent.isDefaultPrevented();
|
|
|
183 |
}
|
|
|
184 |
});
|
|
|
185 |
}
|
|
|
186 |
// If we didn't find a handler, and the HREF is # that probably means that
|
|
|
187 |
// we are handling it from somewhere else. Let's just do nothing in that case.
|
|
|
188 |
if (!eventHandled && anchor.attr('href') !== '#') {
|
|
|
189 |
window.location.href = anchor.attr('href');
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
return false;
|
|
|
193 |
};
|
|
|
194 |
|
|
|
195 |
/*
|
|
|
196 |
* Process focus events for the menu.
|
|
|
197 |
*
|
|
|
198 |
* @method handleFocus
|
|
|
199 |
* @param {Object} item is the jquery object of the item firing the event
|
|
|
200 |
* @return boolean Returns false
|
|
|
201 |
*/
|
|
|
202 |
Menubar.prototype.handleFocus = function(item) {
|
|
|
203 |
|
|
|
204 |
// If activeItem is null, we are getting focus from outside the menu. Store
|
|
|
205 |
// the item that triggered the event.
|
|
|
206 |
if (this.activeItem === null) {
|
|
|
207 |
this.activeItem = item;
|
|
|
208 |
} else if (item[0] != this.activeItem[0]) {
|
|
|
209 |
return true;
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
// Get the set of jquery objects for all the parent items of the active item.
|
|
|
213 |
var parentItems = this.activeItem.parentsUntil('ul.tool-lp-menu').filter('li');
|
|
|
214 |
|
|
|
215 |
// Remove focus styling from all other menu items.
|
|
|
216 |
this.allItems.removeClass('menu-focus');
|
|
|
217 |
|
|
|
218 |
// Add focus styling to the active item.
|
|
|
219 |
this.activeItem.addClass('menu-focus');
|
|
|
220 |
|
|
|
221 |
// Add focus styling to all parent items.
|
|
|
222 |
parentItems.addClass('menu-focus');
|
|
|
223 |
|
|
|
224 |
// If the bChildOpen flag has been set, open the active item's child menu (if applicable).
|
|
|
225 |
if (this.isChildOpen === true) {
|
|
|
226 |
|
|
|
227 |
var itemUL = item.parent();
|
|
|
228 |
|
|
|
229 |
// If the itemUL is a root-level menu and item is a parent item,
|
|
|
230 |
// show the child menu.
|
|
|
231 |
if (itemUL.is('.tool-lp-menu') && (item.attr('aria-haspopup') == 'true')) {
|
|
|
232 |
this.openSubMenu(item.children('ul').first());
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
return true;
|
|
|
237 |
};
|
|
|
238 |
|
|
|
239 |
/*
|
|
|
240 |
* Process blur events for the menu.
|
|
|
241 |
*
|
|
|
242 |
* @method handleBlur
|
|
|
243 |
* @param {Object} item is the jquery object of the item firing the event
|
|
|
244 |
* @return boolean Returns false
|
|
|
245 |
*/
|
|
|
246 |
Menubar.prototype.handleBlur = function(item) {
|
|
|
247 |
item.removeClass('menu-focus');
|
|
|
248 |
|
|
|
249 |
return true;
|
|
|
250 |
};
|
|
|
251 |
|
|
|
252 |
/*
|
|
|
253 |
* Determine if the menu should open to the left, or the right,
|
|
|
254 |
* based on the screen size and menu position.
|
|
|
255 |
* @method setOpenDirection
|
|
|
256 |
*/
|
|
|
257 |
Menubar.prototype.setOpenDirection = function() {
|
|
|
258 |
var pos = this.menuRoot.offset();
|
|
|
259 |
var isRTL = $(document.body).hasClass('dir-rtl');
|
|
|
260 |
var openLeft = true;
|
|
|
261 |
var heightmenuRoot = this.rootMenus.outerHeight();
|
|
|
262 |
var widthmenuRoot = this.rootMenus.outerWidth();
|
|
|
263 |
// Sometimes the menuMinWidth is not enough to figure out if menu exceeds the window width.
|
|
|
264 |
// So we have to calculate the real menu width.
|
|
|
265 |
var subMenuContainer = this.rootMenus.find('ul.tool-lp-sub-menu');
|
|
|
266 |
|
|
|
267 |
// Reset margins.
|
|
|
268 |
subMenuContainer.css('margin-right', '');
|
|
|
269 |
subMenuContainer.css('margin-left', '');
|
|
|
270 |
subMenuContainer.css('margin-top', '');
|
|
|
271 |
|
|
|
272 |
subMenuContainer.attr('aria-hidden', false);
|
|
|
273 |
var menuRealWidth = subMenuContainer.outerWidth(),
|
|
|
274 |
menuRealHeight = subMenuContainer.outerHeight();
|
|
|
275 |
|
|
|
276 |
var margintop = null,
|
|
|
277 |
marginright = null,
|
|
|
278 |
marginleft = null;
|
|
|
279 |
var top = pos.top - $(window).scrollTop();
|
|
|
280 |
// Top is the same for RTL and LTR.
|
|
|
281 |
if (top + menuRealHeight > $(window).height()) {
|
|
|
282 |
margintop = menuRealHeight + heightmenuRoot;
|
|
|
283 |
subMenuContainer.css('margin-top', '-' + margintop + 'px');
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
if (isRTL) {
|
|
|
287 |
if (pos.left - menuRealWidth < 0) {
|
|
|
288 |
marginright = menuRealWidth - widthmenuRoot;
|
|
|
289 |
subMenuContainer.css('margin-right', '-' + marginright + 'px');
|
|
|
290 |
}
|
|
|
291 |
} else {
|
|
|
292 |
if (pos.left + menuRealWidth > $(window).width()) {
|
|
|
293 |
marginleft = menuRealWidth - widthmenuRoot;
|
|
|
294 |
subMenuContainer.css('margin-left', '-' + marginleft + 'px');
|
|
|
295 |
}
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
if (openLeft) {
|
|
|
299 |
this.menuRoot.addClass('tool-lp-menu-open-left');
|
|
|
300 |
} else {
|
|
|
301 |
this.menuRoot.removeClass('tool-lp-menu-open-left');
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
};
|
|
|
305 |
|
|
|
306 |
/*
|
|
|
307 |
* Process keyDown events for the menu.
|
|
|
308 |
*
|
|
|
309 |
* @method handleKeyDown
|
|
|
310 |
* @param {Object} item is the jquery object of the item firing the event
|
|
|
311 |
* @param {Event} e is the associated event object
|
|
|
312 |
* @return boolean Returns false if consuming the event
|
|
|
313 |
*/
|
|
|
314 |
Menubar.prototype.handleKeyDown = function(item, e) {
|
|
|
315 |
|
|
|
316 |
if (e.altKey || e.ctrlKey) {
|
|
|
317 |
// Modifier key pressed: Do not process.
|
|
|
318 |
return true;
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
switch (e.keyCode) {
|
|
|
322 |
case this.keys.tab: {
|
|
|
323 |
|
|
|
324 |
// Hide all menu items and update their aria attributes.
|
|
|
325 |
this.menuRoot.find('ul').attr('aria-hidden', 'true');
|
|
|
326 |
|
|
|
327 |
// Remove focus styling from all menu items.
|
|
|
328 |
this.allItems.removeClass('menu-focus');
|
|
|
329 |
|
|
|
330 |
this.activeItem = null;
|
|
|
331 |
|
|
|
332 |
this.isChildOpen = false;
|
|
|
333 |
|
|
|
334 |
break;
|
|
|
335 |
}
|
|
|
336 |
case this.keys.esc: {
|
|
|
337 |
var itemUL = item.parent();
|
|
|
338 |
|
|
|
339 |
if (itemUL.is('.tool-lp-menu')) {
|
|
|
340 |
// Hide the child menu and update the aria attributes.
|
|
|
341 |
item.children('ul').first().attr('aria-hidden', 'true');
|
|
|
342 |
} else {
|
|
|
343 |
|
|
|
344 |
// Move up one level.
|
|
|
345 |
this.activeItem = itemUL.parent();
|
|
|
346 |
|
|
|
347 |
// Reset the isChildOpen flag.
|
|
|
348 |
this.isChildOpen = false;
|
|
|
349 |
|
|
|
350 |
// Set focus on the new item.
|
|
|
351 |
this.activeItem.focus();
|
|
|
352 |
|
|
|
353 |
// Hide the active menu and update the aria attributes.
|
|
|
354 |
itemUL.attr('aria-hidden', 'true');
|
|
|
355 |
}
|
|
|
356 |
|
|
|
357 |
e.stopPropagation();
|
|
|
358 |
return false;
|
|
|
359 |
}
|
|
|
360 |
case this.keys.enter:
|
|
|
361 |
case this.keys.space: {
|
|
|
362 |
// Trigger click handler.
|
|
|
363 |
return this.handleClick(item, e);
|
|
|
364 |
}
|
|
|
365 |
|
|
|
366 |
case this.keys.left: {
|
|
|
367 |
|
|
|
368 |
this.activeItem = this.moveToPrevious(item);
|
|
|
369 |
|
|
|
370 |
this.activeItem.focus();
|
|
|
371 |
|
|
|
372 |
e.stopPropagation();
|
|
|
373 |
return false;
|
|
|
374 |
}
|
|
|
375 |
case this.keys.right: {
|
|
|
376 |
|
|
|
377 |
this.activeItem = this.moveToNext(item);
|
|
|
378 |
|
|
|
379 |
this.activeItem.focus();
|
|
|
380 |
|
|
|
381 |
e.stopPropagation();
|
|
|
382 |
return false;
|
|
|
383 |
}
|
|
|
384 |
case this.keys.up: {
|
|
|
385 |
|
|
|
386 |
this.activeItem = this.moveUp(item);
|
|
|
387 |
|
|
|
388 |
this.activeItem.focus();
|
|
|
389 |
|
|
|
390 |
e.stopPropagation();
|
|
|
391 |
return false;
|
|
|
392 |
}
|
|
|
393 |
case this.keys.down: {
|
|
|
394 |
|
|
|
395 |
this.activeItem = this.moveDown(item);
|
|
|
396 |
|
|
|
397 |
this.activeItem.focus();
|
|
|
398 |
|
|
|
399 |
e.stopPropagation();
|
|
|
400 |
return false;
|
|
|
401 |
}
|
|
|
402 |
}
|
|
|
403 |
|
|
|
404 |
return true;
|
|
|
405 |
|
|
|
406 |
};
|
|
|
407 |
|
|
|
408 |
|
|
|
409 |
/**
|
|
|
410 |
* Move to the next menu level.
|
|
|
411 |
* This will be either the next root-level menu or the child of a menu parent. If
|
|
|
412 |
* at the root level and the active item is the last in the menu, this function will loop
|
|
|
413 |
* to the first menu item.
|
|
|
414 |
*
|
|
|
415 |
* If the menu is a horizontal menu, the first child element of the newly selected menu will
|
|
|
416 |
* be selected
|
|
|
417 |
*
|
|
|
418 |
* @method moveToNext
|
|
|
419 |
* @param {Object} item is the active menu item
|
|
|
420 |
* @return {Object} Returns the item to move to. Returns item is no move is possible
|
|
|
421 |
*/
|
|
|
422 |
Menubar.prototype.moveToNext = function(item) {
|
|
|
423 |
// Item's containing menu.
|
|
|
424 |
var itemUL = item.parent();
|
|
|
425 |
|
|
|
426 |
// The items in the currently active menu.
|
|
|
427 |
var menuItems = itemUL.children('li');
|
|
|
428 |
|
|
|
429 |
// The number of items in the active menu.
|
|
|
430 |
var menuNum = menuItems.length;
|
|
|
431 |
// The items index in its menu.
|
|
|
432 |
var menuIndex = menuItems.index(item);
|
|
|
433 |
var newItem = null;
|
|
|
434 |
var childMenu = null;
|
|
|
435 |
|
|
|
436 |
if (itemUL.is('.tool-lp-menu')) {
|
|
|
437 |
// This is the root level move to next sibling. This will require closing
|
|
|
438 |
// the current child menu and opening the new one.
|
|
|
439 |
|
|
|
440 |
if (menuIndex < menuNum - 1) {
|
|
|
441 |
// Not the last root menu.
|
|
|
442 |
newItem = item.next();
|
|
|
443 |
} else { // Wrap to first item.
|
|
|
444 |
newItem = menuItems.first();
|
|
|
445 |
}
|
|
|
446 |
|
|
|
447 |
// Close the current child menu (if applicable).
|
|
|
448 |
if (item.attr('aria-haspopup') == 'true') {
|
|
|
449 |
|
|
|
450 |
childMenu = item.children('ul').first();
|
|
|
451 |
|
|
|
452 |
if (childMenu.attr('aria-hidden') == 'false') {
|
|
|
453 |
// Update the child menu's aria-hidden attribute.
|
|
|
454 |
childMenu.attr('aria-hidden', 'true');
|
|
|
455 |
this.isChildOpen = true;
|
|
|
456 |
}
|
|
|
457 |
}
|
|
|
458 |
|
|
|
459 |
// Remove the focus styling from the current menu.
|
|
|
460 |
item.removeClass('menu-focus');
|
|
|
461 |
|
|
|
462 |
// Open the new child menu (if applicable).
|
|
|
463 |
if ((newItem.attr('aria-haspopup') === 'true') && (this.isChildOpen === true)) {
|
|
|
464 |
|
|
|
465 |
childMenu = newItem.children('ul').first();
|
|
|
466 |
|
|
|
467 |
// Update the child's aria-hidden attribute.
|
|
|
468 |
this.openSubMenu(childMenu);
|
|
|
469 |
}
|
|
|
470 |
} else {
|
|
|
471 |
// This is not the root level. If there is a child menu to be moved into, do that;
|
|
|
472 |
// otherwise, move to the next root-level menu if there is one.
|
|
|
473 |
if (item.attr('aria-haspopup') == 'true') {
|
|
|
474 |
|
|
|
475 |
childMenu = item.children('ul').first();
|
|
|
476 |
|
|
|
477 |
newItem = childMenu.children('li').first();
|
|
|
478 |
|
|
|
479 |
// Show the child menu and update its aria attributes.
|
|
|
480 |
this.openSubMenu(childMenu);
|
|
|
481 |
} else {
|
|
|
482 |
// At deepest level, move to the next root-level menu.
|
|
|
483 |
|
|
|
484 |
var parentMenus = null;
|
|
|
485 |
var rootItem = null;
|
|
|
486 |
|
|
|
487 |
// Get list of all parent menus for item, up to the root level.
|
|
|
488 |
parentMenus = item.parentsUntil('ul.tool-lp-menu').filter('ul').not('.tool-lp-menu');
|
|
|
489 |
|
|
|
490 |
// Hide the current menu and update its aria attributes accordingly.
|
|
|
491 |
parentMenus.attr('aria-hidden', 'true');
|
|
|
492 |
|
|
|
493 |
// Remove the focus styling from the active menu.
|
|
|
494 |
parentMenus.find('li').removeClass('menu-focus');
|
|
|
495 |
parentMenus.last().parent().removeClass('menu-focus');
|
|
|
496 |
|
|
|
497 |
// The containing root for the menu.
|
|
|
498 |
rootItem = parentMenus.last().parent();
|
|
|
499 |
|
|
|
500 |
menuIndex = this.rootMenus.index(rootItem);
|
|
|
501 |
|
|
|
502 |
// If this is not the last root menu item, move to the next one.
|
|
|
503 |
if (menuIndex < this.rootMenus.length - 1) {
|
|
|
504 |
newItem = rootItem.next();
|
|
|
505 |
} else {
|
|
|
506 |
// Loop.
|
|
|
507 |
newItem = this.rootMenus.first();
|
|
|
508 |
}
|
|
|
509 |
|
|
|
510 |
// Add the focus styling to the new menu.
|
|
|
511 |
newItem.addClass('menu-focus');
|
|
|
512 |
|
|
|
513 |
if (newItem.attr('aria-haspopup') == 'true') {
|
|
|
514 |
childMenu = newItem.children('ul').first();
|
|
|
515 |
|
|
|
516 |
newItem = childMenu.children('li').first();
|
|
|
517 |
|
|
|
518 |
// Show the child menu and update it's aria attributes.
|
|
|
519 |
this.openSubMenu(childMenu);
|
|
|
520 |
this.isChildOpen = true;
|
|
|
521 |
}
|
|
|
522 |
}
|
|
|
523 |
}
|
|
|
524 |
|
|
|
525 |
return newItem;
|
|
|
526 |
};
|
|
|
527 |
|
|
|
528 |
/**
|
|
|
529 |
* Member function to move to the previous menu level.
|
|
|
530 |
* This will be either the previous root-level menu or the child of a menu parent. If
|
|
|
531 |
* at the root level and the active item is the first in the menu, this function will loop
|
|
|
532 |
* to the last menu item.
|
|
|
533 |
*
|
|
|
534 |
* If the menu is a horizontal menu, the first child element of the newly selected menu will
|
|
|
535 |
* be selected
|
|
|
536 |
*
|
|
|
537 |
* @method moveToPrevious
|
|
|
538 |
* @param {Object} item is the active menu item
|
|
|
539 |
* @return {Object} Returns the item to move to. Returns item is no move is possible
|
|
|
540 |
*/
|
|
|
541 |
Menubar.prototype.moveToPrevious = function(item) {
|
|
|
542 |
// Item's containing menu.
|
|
|
543 |
var itemUL = item.parent();
|
|
|
544 |
// The items in the currently active menu.
|
|
|
545 |
var menuItems = itemUL.children('li');
|
|
|
546 |
// The items index in its menu.
|
|
|
547 |
var menuIndex = menuItems.index(item);
|
|
|
548 |
var newItem = null;
|
|
|
549 |
var childMenu = null;
|
|
|
550 |
|
|
|
551 |
if (itemUL.is('.tool-lp-menu')) {
|
|
|
552 |
// This is the root level move to previous sibling. This will require closing
|
|
|
553 |
// the current child menu and opening the new one.
|
|
|
554 |
|
|
|
555 |
if (menuIndex > 0) {
|
|
|
556 |
// Not the first root menu.
|
|
|
557 |
newItem = item.prev();
|
|
|
558 |
} else {
|
|
|
559 |
// Wrap to last item.
|
|
|
560 |
newItem = menuItems.last();
|
|
|
561 |
}
|
|
|
562 |
|
|
|
563 |
// Close the current child menu (if applicable).
|
|
|
564 |
if (item.attr('aria-haspopup') == 'true') {
|
|
|
565 |
childMenu = item.children('ul').first();
|
|
|
566 |
|
|
|
567 |
if (childMenu.attr('aria-hidden') == 'false') {
|
|
|
568 |
// Update the child menu's aria-hidden attribute.
|
|
|
569 |
childMenu.attr('aria-hidden', 'true');
|
|
|
570 |
this.isChildOpen = true;
|
|
|
571 |
}
|
|
|
572 |
}
|
|
|
573 |
|
|
|
574 |
// Remove the focus styling from the current menu.
|
|
|
575 |
item.removeClass('menu-focus');
|
|
|
576 |
|
|
|
577 |
// Open the new child menu (if applicable).
|
|
|
578 |
if ((newItem.attr('aria-haspopup') === 'true') && (this.isChildOpen === true)) {
|
|
|
579 |
|
|
|
580 |
childMenu = newItem.children('ul').first();
|
|
|
581 |
|
|
|
582 |
// Update the child's aria-hidden attribute.
|
|
|
583 |
this.openSubMenu(childMenu);
|
|
|
584 |
|
|
|
585 |
}
|
|
|
586 |
} else {
|
|
|
587 |
// This is not the root level. If there is a parent menu that is not the
|
|
|
588 |
// root menu, move up one level; otherwise, move to first item of the previous
|
|
|
589 |
// root menu.
|
|
|
590 |
|
|
|
591 |
var parentLI = itemUL.parent();
|
|
|
592 |
var parentUL = parentLI.parent();
|
|
|
593 |
|
|
|
594 |
// If this is a vertical menu or is not the first child menu
|
|
|
595 |
// of the root-level menu, move up one level.
|
|
|
596 |
if (!parentUL.is('.tool-lp-menu')) {
|
|
|
597 |
|
|
|
598 |
newItem = itemUL.parent();
|
|
|
599 |
|
|
|
600 |
// Hide the active menu and update aria-hidden.
|
|
|
601 |
itemUL.attr('aria-hidden', 'true');
|
|
|
602 |
|
|
|
603 |
// Remove the focus highlight from the item.
|
|
|
604 |
item.removeClass('menu-focus');
|
|
|
605 |
|
|
|
606 |
} else {
|
|
|
607 |
// Move to previous root-level menu.
|
|
|
608 |
|
|
|
609 |
// Hide the current menu and update the aria attributes accordingly.
|
|
|
610 |
itemUL.attr('aria-hidden', 'true');
|
|
|
611 |
|
|
|
612 |
// Remove the focus styling from the active menu.
|
|
|
613 |
item.removeClass('menu-focus');
|
|
|
614 |
parentLI.removeClass('menu-focus');
|
|
|
615 |
|
|
|
616 |
menuIndex = this.rootMenus.index(parentLI);
|
|
|
617 |
|
|
|
618 |
if (menuIndex > 0) {
|
|
|
619 |
// Move to the previous root-level menu.
|
|
|
620 |
newItem = parentLI.prev();
|
|
|
621 |
} else {
|
|
|
622 |
// Loop to last root-level menu.
|
|
|
623 |
newItem = this.rootMenus.last();
|
|
|
624 |
}
|
|
|
625 |
|
|
|
626 |
// Add the focus styling to the new menu.
|
|
|
627 |
newItem.addClass('menu-focus');
|
|
|
628 |
|
|
|
629 |
if (newItem.attr('aria-haspopup') == 'true') {
|
|
|
630 |
childMenu = newItem.children('ul').first();
|
|
|
631 |
|
|
|
632 |
// Show the child menu and update it's aria attributes.
|
|
|
633 |
this.openSubMenu(childMenu);
|
|
|
634 |
this.isChildOpen = true;
|
|
|
635 |
|
|
|
636 |
newItem = childMenu.children('li').first();
|
|
|
637 |
}
|
|
|
638 |
}
|
|
|
639 |
}
|
|
|
640 |
|
|
|
641 |
return newItem;
|
|
|
642 |
};
|
|
|
643 |
|
|
|
644 |
/**
|
|
|
645 |
* Member function to select the next item in a menu.
|
|
|
646 |
* If the active item is the last in the menu, this function will loop to the
|
|
|
647 |
* first menu item.
|
|
|
648 |
*
|
|
|
649 |
* @method moveDown
|
|
|
650 |
* @param {Object} item is the active menu item
|
|
|
651 |
* @param {String} startChr is the character to attempt to match against the beginning of the
|
|
|
652 |
* menu item titles. If found, focus moves to the next menu item beginning with that character.
|
|
|
653 |
* @return {Object} Returns the item to move to. Returns item is no move is possible
|
|
|
654 |
*/
|
|
|
655 |
Menubar.prototype.moveDown = function(item, startChr) {
|
|
|
656 |
// Item's containing menu.
|
|
|
657 |
var itemUL = item.parent();
|
|
|
658 |
// The items in the currently active menu.
|
|
|
659 |
var menuItems = itemUL.children('li').not('.separator');
|
|
|
660 |
// The number of items in the active menu.
|
|
|
661 |
var menuNum = menuItems.length;
|
|
|
662 |
// The items index in its menu.
|
|
|
663 |
var menuIndex = menuItems.index(item);
|
|
|
664 |
var newItem = null;
|
|
|
665 |
var newItemUL = null;
|
|
|
666 |
|
|
|
667 |
if (itemUL.is('.tool-lp-menu')) {
|
|
|
668 |
// This is the root level menu.
|
|
|
669 |
|
|
|
670 |
if (item.attr('aria-haspopup') != 'true') {
|
|
|
671 |
// No child menu to move to.
|
|
|
672 |
return item;
|
|
|
673 |
}
|
|
|
674 |
|
|
|
675 |
// Move to the first item in the child menu.
|
|
|
676 |
newItemUL = item.children('ul').first();
|
|
|
677 |
newItem = newItemUL.children('li').first();
|
|
|
678 |
|
|
|
679 |
// Make sure the child menu is visible.
|
|
|
680 |
this.openSubMenu(newItemUL);
|
|
|
681 |
|
|
|
682 |
return newItem;
|
|
|
683 |
}
|
|
|
684 |
|
|
|
685 |
// If $item is not the last item in its menu, move to the next item. If startChr is specified, move
|
|
|
686 |
// to the next item with a title that begins with that character.
|
|
|
687 |
if (startChr) {
|
|
|
688 |
var match = false;
|
|
|
689 |
var curNdx = menuIndex + 1;
|
|
|
690 |
|
|
|
691 |
// Check if the active item was the last one on the list.
|
|
|
692 |
if (curNdx == menuNum) {
|
|
|
693 |
curNdx = 0;
|
|
|
694 |
}
|
|
|
695 |
|
|
|
696 |
// Iterate through the menu items (starting from the current item and wrapping) until a match is found
|
|
|
697 |
// or the loop returns to the current menu item.
|
|
|
698 |
while (curNdx != menuIndex) {
|
|
|
699 |
|
|
|
700 |
var titleChr = menuItems.eq(curNdx).html().charAt(0);
|
|
|
701 |
|
|
|
702 |
if (titleChr.toLowerCase() == startChr) {
|
|
|
703 |
match = true;
|
|
|
704 |
break;
|
|
|
705 |
}
|
|
|
706 |
|
|
|
707 |
curNdx = curNdx + 1;
|
|
|
708 |
|
|
|
709 |
if (curNdx == menuNum) {
|
|
|
710 |
// Reached the end of the list, start again at the beginning.
|
|
|
711 |
curNdx = 0;
|
|
|
712 |
}
|
|
|
713 |
}
|
|
|
714 |
|
|
|
715 |
if (match === true) {
|
|
|
716 |
newItem = menuItems.eq(curNdx);
|
|
|
717 |
|
|
|
718 |
// Remove the focus styling from the current item.
|
|
|
719 |
item.removeClass('menu-focus');
|
|
|
720 |
|
|
|
721 |
return newItem;
|
|
|
722 |
} else {
|
|
|
723 |
return item;
|
|
|
724 |
}
|
|
|
725 |
} else {
|
|
|
726 |
if (menuIndex < menuNum - 1) {
|
|
|
727 |
newItem = menuItems.eq(menuIndex + 1);
|
|
|
728 |
} else {
|
|
|
729 |
newItem = menuItems.first();
|
|
|
730 |
}
|
|
|
731 |
}
|
|
|
732 |
|
|
|
733 |
// Remove the focus styling from the current item.
|
|
|
734 |
item.removeClass('menu-focus');
|
|
|
735 |
|
|
|
736 |
return newItem;
|
|
|
737 |
};
|
|
|
738 |
|
|
|
739 |
/**
|
|
|
740 |
* Function moveUp() is a member function to select the previous item in a menu.
|
|
|
741 |
* If the active item is the first in the menu, this function will loop to the
|
|
|
742 |
* last menu item.
|
|
|
743 |
*
|
|
|
744 |
* @method moveUp
|
|
|
745 |
* @param {Object} item is the active menu item
|
|
|
746 |
* @return {Object} Returns the item to move to. Returns item is no move is possible
|
|
|
747 |
*/
|
|
|
748 |
Menubar.prototype.moveUp = function(item) {
|
|
|
749 |
// Item's containing menu.
|
|
|
750 |
var itemUL = item.parent();
|
|
|
751 |
// The items in the currently active menu.
|
|
|
752 |
var menuItems = itemUL.children('li').not('.separator');
|
|
|
753 |
// The items index in its menu.
|
|
|
754 |
var menuIndex = menuItems.index(item);
|
|
|
755 |
var newItem = null;
|
|
|
756 |
|
|
|
757 |
if (itemUL.is('.tool-lp-menu')) {
|
|
|
758 |
// This is the root level menu.
|
|
|
759 |
// Nothing to do.
|
|
|
760 |
return item;
|
|
|
761 |
}
|
|
|
762 |
|
|
|
763 |
// If item is not the first item in its menu, move to the previous item.
|
|
|
764 |
if (menuIndex > 0) {
|
|
|
765 |
newItem = menuItems.eq(menuIndex - 1);
|
|
|
766 |
} else {
|
|
|
767 |
// Loop to top of menu.
|
|
|
768 |
newItem = menuItems.last();
|
|
|
769 |
}
|
|
|
770 |
|
|
|
771 |
// Remove the focus styling from the current item.
|
|
|
772 |
item.removeClass('menu-focus');
|
|
|
773 |
|
|
|
774 |
return newItem;
|
|
|
775 |
};
|
|
|
776 |
|
|
|
777 |
/**
|
|
|
778 |
* Enhance the dom with aria attributes.
|
|
|
779 |
* @method addAriaAttributes
|
|
|
780 |
*/
|
|
|
781 |
Menubar.prototype.addAriaAttributes = function() {
|
|
|
782 |
this.menuRoot.attr('role', 'menubar');
|
|
|
783 |
this.rootMenus.attr('role', 'menuitem');
|
|
|
784 |
this.rootMenus.attr('tabindex', '0');
|
|
|
785 |
this.rootMenus.attr('aria-haspopup', 'true');
|
|
|
786 |
this.subMenus.attr('role', 'menu');
|
|
|
787 |
this.subMenus.attr('aria-hidden', 'true');
|
|
|
788 |
this.subMenuItems.attr('role', 'menuitem');
|
|
|
789 |
this.subMenuItems.attr('tabindex', '-1');
|
|
|
790 |
|
|
|
791 |
// For CSS styling and effects.
|
|
|
792 |
this.menuRoot.addClass('tool-lp-menu');
|
|
|
793 |
this.allItems.addClass('tool-lp-menu-item');
|
|
|
794 |
this.rootMenus.addClass('tool-lp-root-menu');
|
|
|
795 |
this.subMenus.addClass('tool-lp-sub-menu');
|
|
|
796 |
this.subMenuItems.addClass('dropdown-item');
|
|
|
797 |
};
|
|
|
798 |
|
|
|
799 |
return /** @alias module:tool_lp/menubar */ {
|
|
|
800 |
/**
|
|
|
801 |
* Create a menu bar object for every node matching the selector.
|
|
|
802 |
*
|
|
|
803 |
* The expected DOM structure is shown below.
|
|
|
804 |
* <ul> <- This is the target of the selector parameter.
|
|
|
805 |
* <li> <- This is repeated for each top level menu.
|
|
|
806 |
* Text <- This is the text for the top level menu.
|
|
|
807 |
* <ul> <- This is a list of the entries in this top level menu.
|
|
|
808 |
* <li> <- This is repeated for each menu entry.
|
|
|
809 |
* <a href="someurl">Choice 1</a> <- The anchor for the menu.
|
|
|
810 |
* </li>
|
|
|
811 |
* </ul>
|
|
|
812 |
* </li>
|
|
|
813 |
* </ul>
|
|
|
814 |
*
|
|
|
815 |
* @method enhance
|
|
|
816 |
* @param {String} selector - The selector for the outer most menu node.
|
|
|
817 |
* @param {Function} handler - Javascript handler for when a menu item was chosen. If the
|
|
|
818 |
* handler returns true (or does not exist), the
|
|
|
819 |
* menu will look for an anchor with a link to follow.
|
|
|
820 |
* For example, if the menu entry has a "data-action" attribute
|
|
|
821 |
* and we want to call a javascript function when that entry is chosen,
|
|
|
822 |
* we could pass a list of handlers like this:
|
|
|
823 |
* { "[data-action='add']" : callAddFunction }
|
|
|
824 |
*/
|
|
|
825 |
enhance: function(selector, handler) {
|
|
|
826 |
$(selector).each(function(index, element) {
|
|
|
827 |
var menuRoot = $(element);
|
|
|
828 |
// Don't enhance the same menu twice.
|
|
|
829 |
if (menuRoot.data("menubarEnhanced") !== true) {
|
|
|
830 |
(new Menubar(menuRoot, handler));
|
|
|
831 |
menuRoot.data("menubarEnhanced", true);
|
|
|
832 |
}
|
|
|
833 |
});
|
|
|
834 |
},
|
|
|
835 |
|
|
|
836 |
/**
|
|
|
837 |
* Handy function to close all open menus anywhere on the page.
|
|
|
838 |
* @method closeAll
|
|
|
839 |
*/
|
|
|
840 |
closeAll: closeAllSubMenus
|
|
|
841 |
};
|
|
|
842 |
});
|