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
/
menuBarController.js
257 lines (218 loc) · 7.29 KB
/
menuBarController.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
angular
.module('material.components.menuBar')
.controller('MenuBarController', MenuBarController);
var BOUND_MENU_METHODS = ['handleKeyDown', 'handleMenuHover', 'scheduleOpenHoveredMenu', 'cancelScheduledOpen'];
/**
* @ngInject
*/
function MenuBarController($scope, $rootScope, $element, $attrs, $mdConstant, $document, $mdUtil, $timeout) {
this.$element = $element;
this.$attrs = $attrs;
this.$mdConstant = $mdConstant;
this.$mdUtil = $mdUtil;
this.$document = $document;
this.$scope = $scope;
this.$rootScope = $rootScope;
this.$timeout = $timeout;
var self = this;
angular.forEach(BOUND_MENU_METHODS, function(methodName) {
self[methodName] = angular.bind(self, self[methodName]);
});
}
MenuBarController.prototype.init = function() {
var $element = this.$element;
var $mdUtil = this.$mdUtil;
var $scope = this.$scope;
var self = this;
var deregisterFns = [];
$element.on('keydown', this.handleKeyDown);
this.parentToolbar = $mdUtil.getClosest($element, 'MD-TOOLBAR');
deregisterFns.push(this.$rootScope.$on('$mdMenuOpen', function(event, el) {
if (self.getMenus().indexOf(el[0]) != -1) {
$element[0].classList.add('md-open');
el[0].classList.add('md-open');
self.currentlyOpenMenu = el.controller('mdMenu');
self.currentlyOpenMenu.registerContainerProxy(self.handleKeyDown);
self.enableOpenOnHover();
}
}));
deregisterFns.push(this.$rootScope.$on('$mdMenuClose', function(event, el, opts) {
var rootMenus = self.getMenus();
if (rootMenus.indexOf(el[0]) != -1) {
$element[0].classList.remove('md-open');
el[0].classList.remove('md-open');
}
var ctrl = angular.element(el[0]).controller('mdMenu');
if (ctrl.isInMenuBar && ctrl.mdMenuBarCtrl === self) {
var parentMenu = el[0];
while (parentMenu && rootMenus.indexOf(parentMenu) == -1) {
parentMenu = $mdUtil.getClosest(parentMenu, 'MD-MENU', true);
}
if (parentMenu) {
if (!opts.skipFocus) parentMenu.querySelector('button:not([disabled])').focus();
self.currentlyOpenMenu = undefined;
}
self.disableOpenOnHover();
self.setKeyboardMode(true);
}
}));
$scope.$on('$destroy', function() {
self.disableOpenOnHover();
while (deregisterFns.length) {
deregisterFns.shift()();
}
});
this.setKeyboardMode(true);
};
MenuBarController.prototype.setKeyboardMode = function(enabled) {
if (enabled) this.$element[0].classList.add('md-keyboard-mode');
else this.$element[0].classList.remove('md-keyboard-mode');
};
MenuBarController.prototype.enableOpenOnHover = function() {
if (this.openOnHoverEnabled) return;
var self = this;
self.openOnHoverEnabled = true;
if (self.parentToolbar) {
self.parentToolbar.classList.add('md-has-open-menu');
// Needs to be on the next tick so it doesn't close immediately.
self.$mdUtil.nextTick(function() {
angular.element(self.parentToolbar).on('click', self.handleParentClick);
}, false);
}
angular
.element(self.getMenus())
.on('mouseenter', self.handleMenuHover);
};
MenuBarController.prototype.handleMenuHover = function(e) {
this.setKeyboardMode(false);
if (this.openOnHoverEnabled) {
this.scheduleOpenHoveredMenu(e);
}
};
MenuBarController.prototype.disableOpenOnHover = function() {
if (!this.openOnHoverEnabled) return;
this.openOnHoverEnabled = false;
if (this.parentToolbar) {
this.parentToolbar.classList.remove('md-has-open-menu');
angular.element(this.parentToolbar).off('click', this.handleParentClick);
}
angular
.element(this.getMenus())
.off('mouseenter', this.handleMenuHover);
};
MenuBarController.prototype.scheduleOpenHoveredMenu = function(e) {
var menuEl = angular.element(e.currentTarget);
var menuCtrl = menuEl.controller('mdMenu');
this.setKeyboardMode(false);
this.scheduleOpenMenu(menuCtrl);
};
MenuBarController.prototype.scheduleOpenMenu = function(menuCtrl) {
var self = this;
var $timeout = this.$timeout;
if (menuCtrl != self.currentlyOpenMenu) {
$timeout.cancel(self.pendingMenuOpen);
self.pendingMenuOpen = $timeout(function() {
self.pendingMenuOpen = undefined;
if (self.currentlyOpenMenu) {
self.currentlyOpenMenu.close(true, { closeAll: true });
}
menuCtrl.open();
}, 200, false);
}
};
MenuBarController.prototype.handleKeyDown = function(e) {
var keyCodes = this.$mdConstant.KEY_CODE;
var currentMenu = this.currentlyOpenMenu;
var wasOpen = currentMenu && currentMenu.isOpen;
this.setKeyboardMode(true);
var handled, newMenu, newMenuCtrl;
switch (e.keyCode) {
case keyCodes.DOWN_ARROW:
if (currentMenu) {
currentMenu.focusMenuContainer();
} else {
this.openFocusedMenu();
}
handled = true;
break;
case keyCodes.UP_ARROW:
currentMenu && currentMenu.close();
handled = true;
break;
case keyCodes.LEFT_ARROW:
newMenu = this.focusMenu(-1);
if (wasOpen) {
newMenuCtrl = angular.element(newMenu).controller('mdMenu');
this.scheduleOpenMenu(newMenuCtrl);
}
handled = true;
break;
case keyCodes.RIGHT_ARROW:
newMenu = this.focusMenu(+1);
if (wasOpen) {
newMenuCtrl = angular.element(newMenu).controller('mdMenu');
this.scheduleOpenMenu(newMenuCtrl);
}
handled = true;
break;
}
if (handled) {
e && e.preventDefault && e.preventDefault();
e && e.stopImmediatePropagation && e.stopImmediatePropagation();
}
};
MenuBarController.prototype.focusMenu = function(direction) {
var menus = this.getMenus();
var focusedIndex = this.getFocusedMenuIndex();
if (focusedIndex == -1) { focusedIndex = this.getOpenMenuIndex(); }
var changed = false;
if (focusedIndex == -1) { focusedIndex = 0; changed = true; }
else if (
direction < 0 && focusedIndex > 0 ||
direction > 0 && focusedIndex < menus.length - direction
) {
focusedIndex += direction;
changed = true;
}
if (changed) {
menus[focusedIndex].querySelector('button').focus();
return menus[focusedIndex];
}
};
MenuBarController.prototype.openFocusedMenu = function() {
var menu = this.getFocusedMenu();
menu && angular.element(menu).controller('mdMenu').open();
};
MenuBarController.prototype.getMenus = function() {
var $element = this.$element;
return this.$mdUtil.nodesToArray($element[0].children)
.filter(function(el) { return el.nodeName == 'MD-MENU'; });
};
MenuBarController.prototype.getFocusedMenu = function() {
return this.getMenus()[this.getFocusedMenuIndex()];
};
MenuBarController.prototype.getFocusedMenuIndex = function() {
var $mdUtil = this.$mdUtil;
var focusedEl = $mdUtil.getClosest(
this.$document[0].activeElement,
'MD-MENU'
);
if (!focusedEl) return -1;
var focusedIndex = this.getMenus().indexOf(focusedEl);
return focusedIndex;
};
MenuBarController.prototype.getOpenMenuIndex = function() {
var menus = this.getMenus();
for (var i = 0; i < menus.length; ++i) {
if (menus[i].classList.contains('md-open')) return i;
}
return -1;
};
MenuBarController.prototype.handleParentClick = function(event) {
var openMenu = this.querySelector('md-menu.md-open');
if (openMenu && !openMenu.contains(event.target)) {
angular.element(openMenu).controller('mdMenu').close(true, {
closeAll: true
});
}
};