forked from angular/material
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(interaction): added service to detect last interaction
Sidenav want's to restore focus after the pane gets closed, this should only happen for keyboard interactions. The service got created to use that feature for other components too Fixes angular#5563 Fixes angular#5434 Closes angular#5583 Closes angular#5589 feat(interaction): add test for interaction which fakes a keyboard input and validates it test(interaction): add interaction spec for mousedown event + formats 4 spaces to 2 style(interaction): fix styling of the previous commits test(sidenav): add sidenav focus restore test triggered by keyboard test(sidenav): add focus restore test for mouse and patch other one fix(sidenav): test fix(sidenav): replace deprecated methods, and remove aria label test(sidenav): revert deprecated methods.
- Loading branch information
1 parent
5ae3d4c
commit 0cf58e4
Showing
5 changed files
with
169 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
angular | ||
.module('material.core.interaction', []) | ||
.service('$mdInteraction', MdInteractionService); | ||
|
||
function MdInteractionService($timeout) { | ||
var body = angular.element(document.body); | ||
var _mouseEvent = window.MSPointerEvent ? 'MSPointerDown' : window.PointerEvent ? 'pointerdown' : 'mousedown'; | ||
var buffer = false; | ||
var timer; | ||
var lastInteractionType; | ||
var inputMap = { | ||
'keydown': 'keyboard', | ||
'mousedown': 'mouse', | ||
'mouseenter': 'mouse', | ||
'touchstart': 'touch', | ||
'pointerdown': 'pointer', | ||
'MSPointerDown': 'pointer' | ||
}; | ||
var pointerMap = { | ||
2: 'touch', | ||
3: 'touch', | ||
4: 'mouse' | ||
}; | ||
|
||
function onInput(event) { | ||
if (buffer) return; | ||
var type = inputMap[event.type]; | ||
if (type === 'pointer') { | ||
type = (typeof event.pointerType === 'number') ? pointerMap[event.pointerType] : event.pointerType; | ||
} | ||
lastInteractionType = type; | ||
} | ||
|
||
function onBufferInput(event) { | ||
$timeout.cancel(timer); | ||
|
||
onInput(event); | ||
buffer = true; | ||
|
||
timer = $timeout(function() { | ||
buffer = false; | ||
}, 1000); | ||
} | ||
|
||
body.on('keydown', onInput); | ||
body.on(_mouseEvent, onInput); | ||
body.on('mouseenter', onInput); | ||
if ('ontouchstart' in document.documentElement) body.on('touchstart', onBufferInput); | ||
|
||
/** | ||
* Gets the last interaction type triggered by body. | ||
* Possible values for return are `mouse`, `keyboard` and `touch` | ||
* @returns {string} | ||
*/ | ||
this.getLastInteractionType = function() { | ||
return lastInteractionType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
describe("$mdInteraction", function() { | ||
beforeEach(module('material.core')); | ||
|
||
describe("last interaction type", function() { | ||
|
||
it("imitates a basic keyboard interaction and checks it", inject(function($mdInteraction) { | ||
|
||
var event = document.createEvent('Event'); | ||
event.keyCode = 37; | ||
event.initEvent('keydown', false, true); | ||
document.body.dispatchEvent(event); | ||
|
||
expect($mdInteraction.getLastInteractionType()).toBe('keyboard'); | ||
})); | ||
|
||
it("dispatches a mousedown event on the document body and checks it", inject(function($mdInteraction) { | ||
|
||
var event = document.createEvent("MouseEvent"); | ||
event.initMouseEvent("mousedown", true, true, window, null, 0, 0, 0, 0, false, false, false, false, 0, null); | ||
document.body.dispatchEvent(event); | ||
|
||
expect($mdInteraction.getLastInteractionType()).toBe("mouse"); | ||
})); | ||
|
||
}); | ||
}); |