This repository was 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
/
Copy pathfabController.js
274 lines (218 loc) · 7.67 KB
/
fabController.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
(function() {
'use strict';
angular.module('material.components.fabShared', ['material.core'])
.controller('FabController', FabController);
function FabController($scope, $element, $animate, $mdUtil, $mdConstant, $timeout) {
var vm = this;
// NOTE: We use async evals below to avoid conflicts with any existing digest loops
vm.open = function() {
$scope.$evalAsync("vm.isOpen = true");
};
vm.close = function() {
// Async eval to avoid conflicts with existing digest loops
$scope.$evalAsync("vm.isOpen = false");
// Focus the trigger when the element closes so users can still tab to the next item
$element.find('md-fab-trigger')[0].focus();
};
// Toggle the open/close state when the trigger is clicked
vm.toggle = function() {
$scope.$evalAsync("vm.isOpen = !vm.isOpen");
};
setupDefaults();
setupListeners();
setupWatchers();
fireInitialAnimations();
function setupDefaults() {
// Set the default direction to 'down' if none is specified
vm.direction = vm.direction || 'down';
// Set the default to be closed
vm.isOpen = vm.isOpen || false;
// Start the keyboard interaction at the first action
resetActionIndex();
}
function setupListeners() {
var eventTypes = [
'$md.pressdown',
'click', // Fired via keyboard ENTER
'focusin', 'focusout'
];
// Add our listeners
angular.forEach(eventTypes, function(eventType) {
$element.on(eventType, parseEvents);
});
// Remove our listeners when destroyed
$scope.$on('$destroy', function() {
angular.forEach(eventTypes, function(eventType) {
$element.off(eventType, parseEvents);
});
// remove any attached keyboard handlers in case element is removed while
// speed dial is open
disableKeyboard();
});
}
var recentEvent;
function parseEvents(event) {
// If we've had a recent press/click event, or material is sending us an additional event,
// ignore it
if (recentEvent && (isClick(recentEvent) || recentEvent.$material)) {
return;
}
// Otherwise, handle our events
if (isClick(event)) {
handleItemClick(event);
// Store our recent click event
recentEvent = event;
} else if (event.type == 'focusin') {
vm.open();
} else if (event.type == 'focusout') {
vm.close();
}
// Clear the recent event after all others have fired so we stop ignoring
$timeout(function() {
recentEvent = null;
}, 100, false);
}
function isClick(event) {
return event.type == '$md.pressdown' || event.type == 'click';
}
function resetActionIndex() {
vm.currentActionIndex = -1;
}
function setupWatchers() {
// Watch for changes to the direction and update classes/attributes
$scope.$watch('vm.direction', function(newDir, oldDir) {
// Add the appropriate classes so we can target the direction in the CSS
$animate.removeClass($element, 'md-' + oldDir);
$animate.addClass($element, 'md-' + newDir);
// Reset the action index since it may have changed
resetActionIndex();
});
var trigger, actions;
// Watch for changes to md-open
$scope.$watch('vm.isOpen', function(isOpen) {
// Reset the action index since it may have changed
resetActionIndex();
// We can't get the trigger/actions outside of the watch because the component hasn't been
// linked yet, so we wait until the first watch fires to cache them.
if (!trigger || !actions) {
trigger = getTriggerElement();
actions = getActionsElement();
}
if (isOpen) {
enableKeyboard();
} else {
disableKeyboard();
}
var toAdd = isOpen ? 'md-is-open' : '';
var toRemove = isOpen ? '' : 'md-is-open';
// Set the proper ARIA attributes
trigger.attr('aria-haspopup', true);
trigger.attr('aria-expanded', isOpen);
actions.attr('aria-hidden', !isOpen);
// Animate the CSS classes
$animate.setClass($element, toAdd, toRemove);
});
}
// Fire the animations once in a separate digest loop to initialize them
function fireInitialAnimations() {
$mdUtil.nextTick(function() {
$animate.addClass($element, 'md-noop');
});
}
function enableKeyboard() {
angular.element(document).on('keydown', keyPressed);
// TODO: On desktop, we should be able to reset the indexes so you cannot tab through
//resetActionTabIndexes();
}
function disableKeyboard() {
angular.element(document).off('keydown', keyPressed);
}
function keyPressed(event) {
switch (event.which) {
case $mdConstant.KEY_CODE.SPACE: event.preventDefault(); return false;
case $mdConstant.KEY_CODE.ESCAPE: vm.close(); event.preventDefault(); return false;
case $mdConstant.KEY_CODE.LEFT_ARROW: doKeyLeft(event); return false;
case $mdConstant.KEY_CODE.UP_ARROW: doKeyUp(event); return false;
case $mdConstant.KEY_CODE.RIGHT_ARROW: doKeyRight(event); return false;
case $mdConstant.KEY_CODE.DOWN_ARROW: doKeyDown(event); return false;
}
}
function doActionPrev(event) {
focusAction(event, -1);
}
function doActionNext(event) {
focusAction(event, 1);
}
function focusAction(event, direction) {
var actions = resetActionTabIndexes();
// Increment/decrement the counter with restrictions
vm.currentActionIndex = vm.currentActionIndex + direction;
vm.currentActionIndex = Math.min(actions.length - 1, vm.currentActionIndex);
vm.currentActionIndex = Math.max(0, vm.currentActionIndex);
// Focus the element
var focusElement = angular.element(actions[vm.currentActionIndex]).children()[0];
angular.element(focusElement).attr('tabindex', 0);
focusElement.focus();
// Make sure the event doesn't bubble and cause something else
event.preventDefault();
event.stopImmediatePropagation();
}
function resetActionTabIndexes() {
// Grab all of the actions
var actions = getActionsElement()[0].querySelectorAll('.md-fab-action-item');
// Disable all other actions for tabbing
angular.forEach(actions, function(action) {
angular.element(angular.element(action).children()[0]).attr('tabindex', -1);
});
return actions;
}
function doKeyLeft(event) {
if (vm.direction === 'left') {
doActionNext(event);
} else {
doActionPrev(event);
}
}
function doKeyUp(event) {
if (vm.direction === 'down') {
doActionPrev(event);
} else {
doActionNext(event);
}
}
function doKeyRight(event) {
if (vm.direction === 'left') {
doActionPrev(event);
} else {
doActionNext(event);
}
}
function doKeyDown(event) {
if (vm.direction === 'up') {
doActionPrev(event);
} else {
doActionNext(event);
}
}
function isTrigger(element) {
return $mdUtil.getClosest(element, 'md-fab-trigger');
}
function isAction(element) {
return $mdUtil.getClosest(element, 'md-fab-actions');
}
function handleItemClick(event) {
if (isTrigger(event.target)) {
vm.toggle();
}
if (isAction(event.target)) {
vm.close();
}
}
function getTriggerElement() {
return $element.find('md-fab-trigger');
}
function getActionsElement() {
return $element.find('md-fab-actions');
}
}
})();