Skip to content

Commit

Permalink
MDL-51983 actionmenu: stop event propagation
Browse files Browse the repository at this point in the history
Stop event propagation on keyboard events that have been
successfully handled by the action menu code.
  • Loading branch information
ryanwyllie committed Nov 4, 2015
1 parent 7cb98a9 commit 992c414
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
Expand Up @@ -318,44 +318,49 @@ ACTIONMENU.prototype = {
*/
handleKeyboardEvent: function(e) {
var next;
var markEventHandled = function(e) {
e.preventDefault();
e.stopPropagation();
};

// Handle when the menu is still selected.
if (e.currentTarget.ancestor(SELECTOR.TOGGLE, true)) {
if ((e.keyCode === 40 || (e.keyCode === 9 && !e.shiftKey)) && this.firstMenuChild) {
this.firstMenuChild.focus();
e.preventDefault();
markEventHandled(e);
} else if (e.keyCode === 38 && this.lastMenuChild) {
this.lastMenuChild.focus();
e.preventDefault();
markEventHandled(e);
} else if (e.keyCode === 9 && e.shiftKey) {
this.hideMenu(e);
e.preventDefault();
markEventHandled(e);
}
return this;
}

if (e.keyCode === 27) {
// The escape key was pressed so close the menu.
this.hideMenu(e);
e.preventDefault();
markEventHandled(e);

} else if (e.keyCode === 32) {
// The space bar was pressed. Trigger a click.
e.preventDefault();
markEventHandled(e);
e.currentTarget.simulate('click');
} else if (e.keyCode === 9) {
// The tab key was pressed. Tab moves forwards, Shift + Tab moves backwards through the menu options.
// We only override the Shift + Tab on the first option, and Tab on the last option to change where the
// focus is moved to.
if (e.target === this.firstMenuChild && e.shiftKey) {
this.hideMenu(e);
e.preventDefault();
markEventHandled(e);
} else if (e.target === this.lastMenuChild && !e.shiftKey) {
if (this.hideMenu(e)) {
// Determine the next selector and focus on it.
next = this.menulink.next(SELECTOR.CAN_RECEIVE_FOCUS_SELECTOR);
if (next) {
next.focus();
markEventHandled(e);
}
}
}
Expand Down Expand Up @@ -404,7 +409,7 @@ ACTIONMENU.prototype = {

if (next) {
next.focus();
e.preventDefault();
markEventHandled(e);
}
}
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions lib/yui/build/moodle-core-actionmenu/moodle-core-actionmenu.js
Expand Up @@ -316,44 +316,49 @@ ACTIONMENU.prototype = {
*/
handleKeyboardEvent: function(e) {
var next;
var markEventHandled = function(e) {
e.preventDefault();
e.stopPropagation();
};

// Handle when the menu is still selected.
if (e.currentTarget.ancestor(SELECTOR.TOGGLE, true)) {
if ((e.keyCode === 40 || (e.keyCode === 9 && !e.shiftKey)) && this.firstMenuChild) {
this.firstMenuChild.focus();
e.preventDefault();
markEventHandled(e);
} else if (e.keyCode === 38 && this.lastMenuChild) {
this.lastMenuChild.focus();
e.preventDefault();
markEventHandled(e);
} else if (e.keyCode === 9 && e.shiftKey) {
this.hideMenu(e);
e.preventDefault();
markEventHandled(e);
}
return this;
}

if (e.keyCode === 27) {
// The escape key was pressed so close the menu.
this.hideMenu(e);
e.preventDefault();
markEventHandled(e);

} else if (e.keyCode === 32) {
// The space bar was pressed. Trigger a click.
e.preventDefault();
markEventHandled(e);
e.currentTarget.simulate('click');
} else if (e.keyCode === 9) {
// The tab key was pressed. Tab moves forwards, Shift + Tab moves backwards through the menu options.
// We only override the Shift + Tab on the first option, and Tab on the last option to change where the
// focus is moved to.
if (e.target === this.firstMenuChild && e.shiftKey) {
this.hideMenu(e);
e.preventDefault();
markEventHandled(e);
} else if (e.target === this.lastMenuChild && !e.shiftKey) {
if (this.hideMenu(e)) {
// Determine the next selector and focus on it.
next = this.menulink.next(SELECTOR.CAN_RECEIVE_FOCUS_SELECTOR);
if (next) {
next.focus();
markEventHandled(e);
}
}
}
Expand Down Expand Up @@ -401,7 +406,7 @@ ACTIONMENU.prototype = {

if (next) {
next.focus();
e.preventDefault();
markEventHandled(e);
}
}
},
Expand Down
19 changes: 12 additions & 7 deletions lib/yui/src/actionmenu/js/actionmenu.js
Expand Up @@ -316,44 +316,49 @@ ACTIONMENU.prototype = {
*/
handleKeyboardEvent: function(e) {
var next;
var markEventHandled = function(e) {
e.preventDefault();
e.stopPropagation();
};

// Handle when the menu is still selected.
if (e.currentTarget.ancestor(SELECTOR.TOGGLE, true)) {
if ((e.keyCode === 40 || (e.keyCode === 9 && !e.shiftKey)) && this.firstMenuChild) {
this.firstMenuChild.focus();
e.preventDefault();
markEventHandled(e);
} else if (e.keyCode === 38 && this.lastMenuChild) {
this.lastMenuChild.focus();
e.preventDefault();
markEventHandled(e);
} else if (e.keyCode === 9 && e.shiftKey) {
this.hideMenu(e);
e.preventDefault();
markEventHandled(e);
}
return this;
}

if (e.keyCode === 27) {
// The escape key was pressed so close the menu.
this.hideMenu(e);
e.preventDefault();
markEventHandled(e);

} else if (e.keyCode === 32) {
// The space bar was pressed. Trigger a click.
e.preventDefault();
markEventHandled(e);
e.currentTarget.simulate('click');
} else if (e.keyCode === 9) {
// The tab key was pressed. Tab moves forwards, Shift + Tab moves backwards through the menu options.
// We only override the Shift + Tab on the first option, and Tab on the last option to change where the
// focus is moved to.
if (e.target === this.firstMenuChild && e.shiftKey) {
this.hideMenu(e);
e.preventDefault();
markEventHandled(e);
} else if (e.target === this.lastMenuChild && !e.shiftKey) {
if (this.hideMenu(e)) {
// Determine the next selector and focus on it.
next = this.menulink.next(SELECTOR.CAN_RECEIVE_FOCUS_SELECTOR);
if (next) {
next.focus();
markEventHandled(e);
}
}
}
Expand Down Expand Up @@ -402,7 +407,7 @@ ACTIONMENU.prototype = {

if (next) {
next.focus();
e.preventDefault();
markEventHandled(e);
}
}
},
Expand Down

0 comments on commit 992c414

Please sign in to comment.