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 pathmenuServiceProvider.js
626 lines (543 loc) · 21.2 KB
/
menuServiceProvider.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
angular
.module('material.components.menu')
.provider('$mdMenu', MenuProvider);
/** Handles behavior for a menu while it is open, including:
*
* - handling animating the menu opening/closing
* - handling key/mouse events on the menu element
* - handling enabling/disabling scroll while the menu is open
* - handling redrawing during resizes and orientation changes
*/
/**
* @ngdoc service
* @name $mdMenu
* @module material.components.menu
*
* @description
* `$mdMenu` is a service that is available within the scope of an `md-menu` directive.
*
* ## Usage
*
* <hljs lang="html">
* <md-menu>
* <!--Clicking the button will open the menu-->
* <md-button ng-click="$mdMenu.open($event)">
* Open
* </md-button>
* <!--Moving the mouse off will close the menu-->
* <md-menu-content ng-mouseleave="$mdMenu.close()">
* <md-menu-item>
* <md-button ng-click="$mdMenu.close()">Clicking me will also close the menu</md-button>
* </md-menu-item>
* </md-menu-content>
* </md-menu>
* </hljs>
*/
/**
* @ngdoc method
* @name $mdMenu#open
* @param {$event} $event By passing $event as argument, the corresponding event is stopped from propagating up the
* DOM-tree
*
* @description
* Opens the menu within the `md-menu` scope
*/
/**
* @ngdoc method
* @name $mdMenu#close
*
* @description
* Closes the menu within the `md-menu` scope
*/
function MenuProvider($$interimElementProvider) {
var MENU_EDGE_MARGIN = 8;
return $$interimElementProvider('$mdMenu')
.setDefaults({
methods: ['target'],
options: menuDefaultOptions
});
/* @ngInject */
function menuDefaultOptions($mdUtil, $mdTheming, $mdConstant, $document, $window, $q, $$rAF,
$animateCss, $animate, $log) {
var prefixer = $mdUtil.prefixer();
var animator = $mdUtil.dom.animator;
return {
parent: 'body',
onShow: onShow,
onRemove: onRemove,
hasBackdrop: true,
disableParentScroll: true,
skipCompile: true,
preserveScope: true,
multiple: true,
themable: true
};
/**
* Show modal backdrop element...
* @returns {function(): void} A function that removes this backdrop
*/
function showBackdrop(scope, element, options) {
if (options.nestLevel) return angular.noop;
// If we are not within a dialog...
if (options.disableParentScroll && !$mdUtil.getClosest(options.target, 'MD-DIALOG')) {
// !! DO this before creating the backdrop; since disableScrollAround()
// configures the scroll offset; which is used by mdBackDrop postLink()
options.restoreScroll = $mdUtil.disableScrollAround(options.element, options.parent);
} else {
options.disableParentScroll = false;
}
if (options.hasBackdrop) {
options.backdrop = $mdUtil.createBackdrop(scope, "md-menu-backdrop md-click-catcher");
$animate.enter(options.backdrop, $document[0].body);
}
/**
* Hide and destroys the backdrop created by showBackdrop()
*/
return function hideBackdrop() {
if (options.backdrop) options.backdrop.remove();
if (options.disableParentScroll) options.restoreScroll();
};
}
/**
* Removing the menu element from the DOM and remove all associated event listeners
* and backdrop
*/
function onRemove(scope, element, opts) {
opts.cleanupInteraction();
opts.cleanupBackdrop();
opts.cleanupResizing();
opts.hideBackdrop();
// Before the menu is closing remove the clickable class.
element.removeClass('md-clickable');
// For navigation $destroy events, do a quick, non-animated removal,
// but for normal closes (from clicks, etc) animate the removal
return (opts.$destroy === true) ? detachAndClean() : animateRemoval().then(detachAndClean);
/**
* For normal closes, animate the removal.
* For forced closes (like $destroy events), skip the animations
*/
function animateRemoval() {
return $animateCss(element, {addClass: 'md-leave'}).start();
}
/**
* Detach the element
*/
function detachAndClean() {
element.removeClass('md-active');
detachElement(element, opts);
opts.alreadyOpen = false;
}
}
/**
* Inserts and configures the staged Menu element into the DOM, positioning it,
* and wiring up various interaction events
*/
function onShow(scope, element, opts) {
sanitizeAndConfigure(opts);
if (opts.menuContentEl[0]) {
// Inherit the theme from the target element.
$mdTheming.inherit(opts.menuContentEl, opts.target);
} else {
$log.warn(
'$mdMenu: Menu elements should always contain a `md-menu-content` element,' +
'otherwise interactivity features will not work properly.',
element
);
}
// Register various listeners to move menu on resize/orientation change
opts.cleanupResizing = startRepositioningOnResize();
opts.hideBackdrop = showBackdrop(scope, element, opts);
// Return the promise for when our menu is done animating in
return showMenu()
.then(function(response) {
opts.alreadyOpen = true;
opts.cleanupInteraction = activateInteraction();
opts.cleanupBackdrop = setupBackdrop();
// Since the menu finished its animation, mark the menu as clickable.
element.addClass('md-clickable');
return response;
});
/**
* Place the menu into the DOM and call positioning related functions
*/
function showMenu() {
opts.parent.append(element);
element[0].style.display = '';
return $q(function(resolve) {
var position = calculateMenuPosition(element, opts);
element.removeClass('md-leave');
// Animate the menu scaling, and opacity [from its position origin (default == top-left)]
// to normal scale.
$animateCss(element, {
addClass: 'md-active',
from: animator.toCss(position),
to: animator.toCss({transform: ''})
})
.start()
.then(resolve);
});
}
/**
* Check for valid opts and set some sane defaults
*/
function sanitizeAndConfigure() {
if (!opts.target) {
throw Error(
'$mdMenu.show() expected a target to animate from in options.target'
);
}
angular.extend(opts, {
alreadyOpen: false,
isRemoved: false,
target: angular.element(opts.target), // make sure it's not a naked DOM node
parent: angular.element(opts.parent),
menuContentEl: angular.element(element[0].querySelector('md-menu-content'))
});
}
/**
* Configure various resize listeners for screen changes
*/
function startRepositioningOnResize() {
var repositionMenu = (function(target, options) {
return $$rAF.throttle(function() {
if (opts.isRemoved) return;
var position = calculateMenuPosition(target, options);
target.css(animator.toCss(position));
});
})(element, opts);
$window.addEventListener('resize', repositionMenu);
$window.addEventListener('orientationchange', repositionMenu);
return function stopRepositioningOnResize() {
// Disable resizing handlers
$window.removeEventListener('resize', repositionMenu);
$window.removeEventListener('orientationchange', repositionMenu);
};
}
/**
* Sets up the backdrop and listens for click elements.
* Once the backdrop will be clicked, the menu will automatically close.
* @returns {!Function} Function to remove the backdrop.
*/
function setupBackdrop() {
if (!opts.backdrop) return angular.noop;
opts.backdrop.on('click', onBackdropClick);
return function() {
opts.backdrop.off('click', onBackdropClick);
};
}
/**
* Function to be called whenever the backdrop is clicked.
* @param {!MouseEvent} event
*/
function onBackdropClick(event) {
event.preventDefault();
event.stopPropagation();
scope.$apply(function() {
opts.mdMenuCtrl.close(true, { closeAll: true });
});
}
/**
* Activate interaction on the menu. Resolves the focus target and closes the menu on
* escape or option click.
* @returns {!Function} Function to deactivate the interaction listeners.
*/
function activateInteraction() {
if (!opts.menuContentEl[0]) return angular.noop;
// Wire up keyboard listeners.
// - Close on escape,
// - focus next item on down arrow,
// - focus prev item on up
opts.menuContentEl.on('keydown', onMenuKeyDown);
opts.menuContentEl[0].addEventListener('click', captureClickListener, true);
// kick off initial focus in the menu on the first enabled element
var focusTarget = opts.menuContentEl[0]
.querySelector(prefixer.buildSelector(['md-menu-focus-target', 'md-autofocus']));
if (!focusTarget) {
var childrenLen = opts.menuContentEl[0].children.length;
for (var childIndex = 0; childIndex < childrenLen; childIndex++) {
var child = opts.menuContentEl[0].children[childIndex];
focusTarget = child.querySelector('.md-button:not([disabled])');
if (focusTarget) {
break;
}
if (child.firstElementChild && !child.firstElementChild.disabled) {
focusTarget = child.firstElementChild;
break;
}
}
}
focusTarget && focusTarget.focus();
return function cleanupInteraction() {
opts.menuContentEl.off('keydown', onMenuKeyDown);
opts.menuContentEl[0].removeEventListener('click', captureClickListener, true);
};
// ************************************
// internal functions
// ************************************
function onMenuKeyDown(ev) {
var handled;
switch (ev.keyCode) {
case $mdConstant.KEY_CODE.ESCAPE:
opts.mdMenuCtrl.close(false, { closeAll: true });
handled = true;
break;
case $mdConstant.KEY_CODE.TAB:
opts.mdMenuCtrl.close(false, { closeAll: true });
// Don't prevent default or stop propagation on this event as we want tab
// to move the focus to the next focusable element on the page.
handled = false;
break;
case $mdConstant.KEY_CODE.UP_ARROW:
if (!focusMenuItem(ev, opts.menuContentEl, opts, -1) && !opts.nestLevel) {
opts.mdMenuCtrl.triggerContainerProxy(ev);
}
handled = true;
break;
case $mdConstant.KEY_CODE.DOWN_ARROW:
if (!focusMenuItem(ev, opts.menuContentEl, opts, 1) && !opts.nestLevel) {
opts.mdMenuCtrl.triggerContainerProxy(ev);
}
handled = true;
break;
case $mdConstant.KEY_CODE.LEFT_ARROW:
if (opts.nestLevel) {
opts.mdMenuCtrl.close();
} else {
opts.mdMenuCtrl.triggerContainerProxy(ev);
}
handled = true;
break;
case $mdConstant.KEY_CODE.RIGHT_ARROW:
var parentMenu = $mdUtil.getClosest(ev.target, 'MD-MENU');
if (parentMenu && parentMenu != opts.parent[0]) {
ev.target.click();
} else {
opts.mdMenuCtrl.triggerContainerProxy(ev);
}
handled = true;
break;
}
if (handled) {
ev.preventDefault();
ev.stopImmediatePropagation();
}
}
function onBackdropClick(e) {
e.preventDefault();
e.stopPropagation();
scope.$apply(function() {
opts.mdMenuCtrl.close(true, { closeAll: true });
});
}
// Close menu on menu item click, if said menu-item is not disabled
function captureClickListener(e) {
var target = e.target;
// Traverse up the event until we get to the menuContentEl to see if
// there is an ng-click and that the ng-click is not disabled
do {
if (target == opts.menuContentEl[0]) return;
if ((hasAnyAttribute(target, ['ng-click', 'ng-href', 'ui-sref']) ||
target.nodeName == 'BUTTON' || target.nodeName == 'MD-BUTTON') && !hasAnyAttribute(target, ['md-prevent-menu-close'])) {
var closestMenu = $mdUtil.getClosest(target, 'MD-MENU');
if (!target.hasAttribute('disabled') && (!closestMenu || closestMenu == opts.parent[0])) {
close();
}
break;
}
} while (target = target.parentNode);
function close() {
scope.$apply(function() {
opts.mdMenuCtrl.close(true, { closeAll: true });
});
}
function hasAnyAttribute(target, attrs) {
if (!target) return false;
for (var i = 0, attr; attr = attrs[i]; ++i) {
if (prefixer.hasAttribute(target, attr)) {
return true;
}
}
return false;
}
}
}
}
/**
* Takes a keypress event and focuses the next/previous menu
* item from the emitting element
* @param {event} e - The origin keypress event
* @param {angular.element} menuEl - The menu element
* @param {object} opts - The interim element options for the mdMenu
* @param {number} direction - The direction to move in (+1 = next, -1 = prev)
*/
function focusMenuItem(e, menuEl, opts, direction) {
var currentItem = $mdUtil.getClosest(e.target, 'MD-MENU-ITEM');
var items = $mdUtil.nodesToArray(menuEl[0].children);
var currentIndex = items.indexOf(currentItem);
// Traverse through our elements in the specified direction (+/-1) and try to
// focus them until we find one that accepts focus
var didFocus;
for (var i = currentIndex + direction; i >= 0 && i < items.length; i = i + direction) {
var focusTarget = items[i].querySelector('.md-button');
didFocus = attemptFocus(focusTarget);
if (didFocus) {
break;
}
}
return didFocus;
}
/**
* Attempts to focus an element. Checks whether that element is the currently
* focused element after attempting.
* @param {HTMLElement} el - the element to attempt focus on
* @returns {boolean} - whether the element was successfully focused
*/
function attemptFocus(el) {
if (el && el.getAttribute('tabindex') != -1) {
el.focus();
return ($document[0].activeElement == el);
}
}
/**
* Use browser to remove this element without triggering a $destroy event
*/
function detachElement(element, opts) {
if (!opts.preserveElement) {
if (toNode(element).parentNode === toNode(opts.parent)) {
toNode(opts.parent).removeChild(toNode(element));
}
} else {
toNode(element).style.display = 'none';
}
}
/**
* Computes menu position and sets the style on the menu container
* @param {HTMLElement} el - the menu container element
* @param {object} opts - the interim element options object
*/
function calculateMenuPosition(el, opts) {
var containerNode = el[0],
openMenuNode = el[0].firstElementChild,
openMenuNodeRect = openMenuNode.getBoundingClientRect(),
boundryNode = $document[0].body,
boundryNodeRect = boundryNode.getBoundingClientRect();
var menuStyle = $window.getComputedStyle(openMenuNode);
var originNode = opts.target[0].querySelector(prefixer.buildSelector('md-menu-origin')) || opts.target[0],
originNodeRect = originNode.getBoundingClientRect();
var bounds = {
left: boundryNodeRect.left + MENU_EDGE_MARGIN,
top: Math.max(boundryNodeRect.top, 0) + MENU_EDGE_MARGIN,
bottom: Math.max(boundryNodeRect.bottom, Math.max(boundryNodeRect.top, 0) + boundryNodeRect.height) - MENU_EDGE_MARGIN,
right: boundryNodeRect.right - MENU_EDGE_MARGIN
};
var alignTarget, alignTargetRect = { top:0, left : 0, right:0, bottom:0 }, existingOffsets = { top:0, left : 0, right:0, bottom:0 };
var positionMode = opts.mdMenuCtrl.positionMode();
if (positionMode.top === 'target' || positionMode.left === 'target' || positionMode.left === 'target-right') {
alignTarget = firstVisibleChild();
if (alignTarget) {
// TODO: Allow centering on an arbitrary node, for now center on first menu-item's child
alignTarget = alignTarget.firstElementChild || alignTarget;
alignTarget = alignTarget.querySelector(prefixer.buildSelector('md-menu-align-target')) || alignTarget;
alignTargetRect = alignTarget.getBoundingClientRect();
existingOffsets = {
top: parseFloat(containerNode.style.top || 0),
left: parseFloat(containerNode.style.left || 0)
};
}
}
var position = {};
var transformOrigin = 'top ';
switch (positionMode.top) {
case 'target':
position.top = existingOffsets.top + originNodeRect.top - alignTargetRect.top;
break;
case 'cascade':
position.top = originNodeRect.top - parseFloat(menuStyle.paddingTop) - originNode.style.top;
break;
case 'bottom':
position.top = originNodeRect.top + originNodeRect.height;
break;
default:
throw new Error('Invalid target mode "' + positionMode.top + '" specified for md-menu on Y axis.');
}
var rtl = $mdUtil.isRtl(el);
switch (positionMode.left) {
case 'target':
position.left = existingOffsets.left + originNodeRect.left - alignTargetRect.left;
transformOrigin += rtl ? 'right' : 'left';
break;
case 'target-left':
position.left = originNodeRect.left;
transformOrigin += 'left';
break;
case 'target-right':
position.left = originNodeRect.right - openMenuNodeRect.width + (openMenuNodeRect.right - alignTargetRect.right);
transformOrigin += 'right';
break;
case 'cascade':
var willFitRight = rtl ? (originNodeRect.left - openMenuNodeRect.width) < bounds.left : (originNodeRect.right + openMenuNodeRect.width) < bounds.right;
position.left = willFitRight ? originNodeRect.right - originNode.style.left : originNodeRect.left - originNode.style.left - openMenuNodeRect.width;
transformOrigin += willFitRight ? 'left' : 'right';
break;
case 'right':
if (rtl) {
position.left = originNodeRect.right - originNodeRect.width;
transformOrigin += 'left';
} else {
position.left = originNodeRect.right - openMenuNodeRect.width;
transformOrigin += 'right';
}
break;
case 'left':
if (rtl) {
position.left = originNodeRect.right - openMenuNodeRect.width;
transformOrigin += 'right';
} else {
position.left = originNodeRect.left;
transformOrigin += 'left';
}
break;
default:
throw new Error('Invalid target mode "' + positionMode.left + '" specified for md-menu on X axis.');
}
var offsets = opts.mdMenuCtrl.offsets();
position.top += offsets.top;
position.left += offsets.left;
clamp(position);
var scaleX = Math.round(100 * Math.min(originNodeRect.width / containerNode.offsetWidth, 1.0)) / 100;
var scaleY = Math.round(100 * Math.min(originNodeRect.height / containerNode.offsetHeight, 1.0)) / 100;
return {
top: Math.round(position.top),
left: Math.round(position.left),
// Animate a scale out if we aren't just repositioning
transform: !opts.alreadyOpen ? $mdUtil.supplant('scale({0},{1})', [scaleX, scaleY]) : undefined,
transformOrigin: transformOrigin
};
/**
* Clamps the repositioning of the menu within the confines of
* bounding element (often the screen/body)
*/
function clamp(pos) {
pos.top = Math.max(Math.min(pos.top, bounds.bottom - containerNode.offsetHeight), bounds.top);
pos.left = Math.max(Math.min(pos.left, bounds.right - containerNode.offsetWidth), bounds.left);
}
/**
* Gets the first visible child in the openMenuNode
* Necessary incase menu nodes are being dynamically hidden
*/
function firstVisibleChild() {
for (var i = 0; i < openMenuNode.children.length; ++i) {
if ($window.getComputedStyle(openMenuNode.children[i]).display != 'none') {
return openMenuNode.children[i];
}
}
}
}
}
function toNode(el) {
if (el instanceof angular.element) {
el = el[0];
}
return el;
}
}