This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
interaction.js
171 lines (144 loc) · 5.03 KB
/
interaction.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
/**
* @ngdoc module
* @name material.core.interaction
* @description
* User interaction detection to provide proper accessibility.
*/
angular
.module('material.core.interaction', [])
.service('$mdInteraction', MdInteractionService);
/**
* @ngdoc service
* @name $mdInteraction
* @module material.core.interaction
*
* @description
*
* Service which keeps track of the last interaction type and validates them for several browsers.
* The service hooks into the document's body and listens for touch, mouse and keyboard events.
*
* The most recent interaction type can be retrieved by calling the `getLastInteractionType` method.
*
* Here is an example markup for using the interaction service.
*
* <hljs lang="js">
* var lastType = $mdInteraction.getLastInteractionType();
*
* if (lastType === 'keyboard') {
* // We only restore the focus for keyboard users.
* restoreFocus();
* }
* </hljs>
*
*/
function MdInteractionService($timeout, $mdUtil, $rootScope) {
this.$timeout = $timeout;
this.$mdUtil = $mdUtil;
this.$rootScope = $rootScope;
// IE browsers can also trigger pointer events, which also leads to an interaction.
this.pointerEvent = 'MSPointerEvent' in window ? 'MSPointerDown' : 'PointerEvent' in window ? 'pointerdown' : null;
this.bodyElement = angular.element(document.body);
this.isBuffering = false;
this.bufferTimeout = null;
this.lastInteractionType = null;
this.lastInteractionTime = null;
this.inputHandler = this.onInputEvent.bind(this);
this.bufferedInputHandler = this.onBufferInputEvent.bind(this);
// Type Mappings for the different events
// There will be three three interaction types
// `keyboard`, `mouse` and `touch`
// type `pointer` will be evaluated in `pointerMap` for IE Browser events
this.inputEventMap = {
'keydown': 'keyboard',
'mousedown': 'mouse',
'mouseenter': 'mouse',
'touchstart': 'touch',
'pointerdown': 'pointer',
'MSPointerDown': 'pointer'
};
// IE PointerDown events will be validated in `touch` or `mouse`
// Index numbers referenced here: https://msdn.microsoft.com/library/windows/apps/hh466130.aspx
this.iePointerMap = {
2: 'touch',
3: 'touch',
4: 'mouse'
};
this.initializeEvents();
this.$rootScope.$on('$destroy', this.deregister.bind(this));
}
/**
* Removes all event listeners created by $mdInteration on the
* body element.
*/
MdInteractionService.prototype.deregister = function() {
this.bodyElement.off('keydown mousedown', this.inputHandler);
if ('ontouchstart' in document.documentElement) {
this.bodyElement.off('touchstart', this.bufferedInputHandler);
}
if (this.pointerEvent) {
this.bodyElement.off(this.pointerEvent, this.inputHandler);
}
};
/**
* Initializes the interaction service, by registering all interaction events to the
* body element.
*/
MdInteractionService.prototype.initializeEvents = function() {
this.bodyElement.on('keydown mousedown', this.inputHandler);
if ('ontouchstart' in document.documentElement) {
this.bodyElement.on('touchstart', this.bufferedInputHandler);
}
if (this.pointerEvent) {
this.bodyElement.on(this.pointerEvent, this.inputHandler);
}
};
/**
* Event listener for normal interaction events, which should be tracked.
* @param event {MouseEvent|KeyboardEvent|PointerEvent|TouchEvent}
*/
MdInteractionService.prototype.onInputEvent = function(event) {
if (this.isBuffering) {
return;
}
var type = this.inputEventMap[event.type];
if (type === 'pointer') {
type = this.iePointerMap[event.pointerType] || event.pointerType;
}
this.lastInteractionType = type;
this.lastInteractionTime = this.$mdUtil.now();
};
/**
* Event listener for interaction events which should be buffered (touch events).
* @param event {TouchEvent}
*/
MdInteractionService.prototype.onBufferInputEvent = function(event) {
this.$timeout.cancel(this.bufferTimeout);
this.onInputEvent(event);
this.isBuffering = true;
// The timeout of 650ms is needed to delay the touchstart, because otherwise the touch will call
// the `onInput` function multiple times.
this.bufferTimeout = this.$timeout(function() {
this.isBuffering = false;
}.bind(this), 650, false);
};
/**
* @ngdoc method
* @name $mdInteraction#getLastInteractionType
* @description Retrieves the last interaction type triggered in body.
* @returns {string|null} Last interaction type.
*/
MdInteractionService.prototype.getLastInteractionType = function() {
return this.lastInteractionType;
};
/**
* @ngdoc method
* @name $mdInteraction#isUserInvoked
* @description Method to detect whether any interaction happened recently or not.
* @param {number=} checkDelay Time to check for any interaction to have been triggered.
* @returns {boolean} Whether there was any interaction or not.
*/
MdInteractionService.prototype.isUserInvoked = function(checkDelay) {
var delay = angular.isNumber(checkDelay) ? checkDelay : 15;
// Check for any interaction to be within the specified check time.
return this.lastInteractionTime >= this.$mdUtil.now() - delay;
};