This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
/
menuController.js
225 lines (195 loc) · 7.03 KB
/
menuController.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
angular
.module('material.components.menu')
.controller('mdMenuCtrl', MenuController);
/**
* @ngInject
*/
function MenuController($mdMenu, $attrs, $element, $scope, $mdUtil, $timeout, $rootScope, $q) {
var menuContainer;
var self = this;
var triggerElement;
this.nestLevel = parseInt($attrs.mdNestLevel, 10) || 0;
/**
* Called by our linking fn to provide access to the menu-content
* element removed during link
*/
this.init = function init(setMenuContainer, opts) {
opts = opts || {};
menuContainer = setMenuContainer;
// Default element for ARIA attributes has the ngClick or ngMouseenter expression
triggerElement = $element[0].querySelector('[ng-click],[ng-mouseenter]');
triggerElement.setAttribute('aria-expanded', 'false');
this.isInMenuBar = opts.isInMenuBar;
this.nestedMenus = $mdUtil.nodesToArray(menuContainer[0].querySelectorAll('.md-nested-menu'));
menuContainer.on('$mdInterimElementRemove', function() {
self.isOpen = false;
});
var menuContainerId = 'menu_container_' + $mdUtil.nextUid();
menuContainer.attr('id', menuContainerId);
angular.element(triggerElement).attr({
'aria-owns': menuContainerId,
'aria-haspopup': 'true'
});
$scope.$on('$destroy', this.disableHoverListener);
menuContainer.on('$destroy', function() {
$mdMenu.destroy();
});
};
var openMenuTimeout, menuItems, deregisterScopeListeners = [];
this.enableHoverListener = function() {
deregisterScopeListeners.push($rootScope.$on('$mdMenuOpen', function(event, el) {
if (menuContainer[0].contains(el[0])) {
self.currentlyOpenMenu = el.controller('mdMenu');
self.isAlreadyOpening = false;
self.currentlyOpenMenu.registerContainerProxy(self.triggerContainerProxy.bind(self));
}
}));
deregisterScopeListeners.push($rootScope.$on('$mdMenuClose', function(event, el) {
if (menuContainer[0].contains(el[0])) {
self.currentlyOpenMenu = undefined;
}
}));
menuItems = angular.element($mdUtil.nodesToArray(menuContainer[0].children[0].children));
menuItems.on('mouseenter', self.handleMenuItemHover);
menuItems.on('mouseleave', self.handleMenuItemMouseLeave);
};
this.disableHoverListener = function() {
while (deregisterScopeListeners.length) {
deregisterScopeListeners.shift()();
}
menuItems && menuItems.off('mouseenter', self.handleMenuItemHover);
menuItems && menuItems.off('mouseleave', self.handleMenuMouseLeave);
};
this.handleMenuItemHover = function(event) {
if (self.isAlreadyOpening) return;
var nestedMenu = (
event.target.querySelector('md-menu')
|| $mdUtil.getClosest(event.target, 'MD-MENU')
);
openMenuTimeout = $timeout(function() {
if (nestedMenu) {
nestedMenu = angular.element(nestedMenu).controller('mdMenu');
}
if (self.currentlyOpenMenu && self.currentlyOpenMenu != nestedMenu) {
var closeTo = self.nestLevel + 1;
self.currentlyOpenMenu.close(true, { closeTo: closeTo });
self.isAlreadyOpening = true;
nestedMenu.open();
} else if (nestedMenu && !nestedMenu.isOpen && nestedMenu.open) {
self.isAlreadyOpening = true;
nestedMenu.open();
}
}, nestedMenu ? 100 : 250);
var focusableTarget = event.currentTarget.querySelector('.md-button:not([disabled])');
focusableTarget && focusableTarget.focus();
};
this.handleMenuItemMouseLeave = function() {
if (openMenuTimeout) {
$timeout.cancel(openMenuTimeout);
openMenuTimeout = undefined;
}
};
/**
* Uses the $mdMenu interim element service to open the menu contents
*/
this.open = function openMenu(ev) {
ev && ev.stopPropagation();
ev && ev.preventDefault();
if (self.isOpen) return;
self.enableHoverListener();
self.isOpen = true;
triggerElement = triggerElement || (ev ? ev.target : $element[0]);
triggerElement.setAttribute('aria-expanded', 'true');
$scope.$emit('$mdMenuOpen', $element);
$mdMenu.show({
scope: $scope,
mdMenuCtrl: self,
nestLevel: self.nestLevel,
element: menuContainer,
target: triggerElement,
preserveElement: true,
parent: 'body'
}).finally(function() {
triggerElement.setAttribute('aria-expanded', 'false');
self.disableHoverListener();
});
};
// Expose a open function to the child scope for html to use
$scope.$mdOpenMenu = this.open;
$scope.$watch(function() { return self.isOpen; }, function(isOpen) {
if (isOpen) {
menuContainer.attr('aria-hidden', 'false');
$element[0].classList.add('_md-open');
angular.forEach(self.nestedMenus, function(el) {
el.classList.remove('_md-open');
});
} else {
menuContainer.attr('aria-hidden', 'true');
$element[0].classList.remove('_md-open');
}
$scope.$mdMenuIsOpen = self.isOpen;
});
this.focusMenuContainer = function focusMenuContainer() {
var focusTarget = menuContainer[0].querySelector('[md-menu-focus-target]');
if (!focusTarget) focusTarget = menuContainer[0].querySelector('.md-button');
focusTarget.focus();
};
this.registerContainerProxy = function registerContainerProxy(handler) {
this.containerProxy = handler;
};
this.triggerContainerProxy = function triggerContainerProxy(ev) {
this.containerProxy && this.containerProxy(ev);
};
this.destroy = function() {
return self.isOpen ? $mdMenu.destroy() : $q.when(false);
};
// Use the $mdMenu interim element service to close the menu contents
this.close = function closeMenu(skipFocus, closeOpts) {
if ( !self.isOpen ) return;
self.isOpen = false;
var eventDetails = angular.extend({}, closeOpts, { skipFocus: skipFocus });
$scope.$emit('$mdMenuClose', $element, eventDetails);
$mdMenu.hide(null, closeOpts);
if (!skipFocus) {
var el = self.restoreFocusTo || $element.find('button')[0];
if (el instanceof angular.element) el = el[0];
if (el) el.focus();
}
};
/**
* Build a nice object out of our string attribute which specifies the
* target mode for left and top positioning
*/
this.positionMode = function positionMode() {
var attachment = ($attrs.mdPositionMode || 'target').split(' ');
// If attachment is a single item, duplicate it for our second value.
// ie. 'target' -> 'target target'
if (attachment.length == 1) {
attachment.push(attachment[0]);
}
return {
left: attachment[0],
top: attachment[1]
};
};
/**
* Build a nice object out of our string attribute which specifies
* the offset of top and left in pixels.
*/
this.offsets = function offsets() {
var position = ($attrs.mdOffset || '0 0').split(' ').map(parseFloat);
if (position.length == 2) {
return {
left: position[0],
top: position[1]
};
} else if (position.length == 1) {
return {
top: position[0],
left: position[0]
};
} else {
throw Error('Invalid offsets specified. Please follow format <x, y> or <n>');
}
};
}