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 pathsidenav.js
597 lines (538 loc) · 19.6 KB
/
sidenav.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
/**
* @ngdoc module
* @name material.components.sidenav
*
* @description
* A Sidenav component.
*/
angular
.module('material.components.sidenav', [
'material.core',
'material.components.backdrop'
])
.factory('$mdSidenav', SidenavService)
.directive('mdSidenav', SidenavDirective)
.directive('mdSidenavFocus', SidenavFocusDirective)
.controller('$mdSidenavController', SidenavController);
/**
* @ngdoc service
* @name $mdSidenav
* @module material.components.sidenav
*
* @description
* `$mdSidenav` makes it easy to interact with multiple sidenavs in an app. When looking up a
* sidenav instance, you can either look it up synchronously or wait for it to be initialized
* asynchronously. This is done by passing the second argument to `$mdSidenav`.
*
* @usage
* <hljs lang="js">
* // Async lookup for sidenav instance; will resolve when the instance is available
* $mdSidenav(componentId, true).then(function(instance) {
* $log.debug( componentId + "is now ready" );
* });
* // Sync lookup for sidenav instance; this will resolve immediately.
* $mdSidenav(componentId).then(function(instance) {
* $log.debug( componentId + "is now ready" );
* });
* // Async toggle the given sidenav;
* // when instance is known ready and lazy lookup is not needed.
* $mdSidenav(componentId)
* .toggle()
* .then(function(){
* $log.debug('toggled');
* });
* // Async open the given sidenav
* $mdSidenav(componentId)
* .open()
* .then(function(){
* $log.debug('opened');
* });
* // Async close the given sidenav
* $mdSidenav(componentId)
* .close()
* .then(function(){
* $log.debug('closed');
* });
* // Async lookup for sidenav instance
* $mdSidenav(componentId, true).then(function(instance) {
* // On close callback to handle close, backdrop click, or escape key pressed.
* // Callback happens BEFORE the close action occurs.
* instance.onClose(function() {
* $log.debug('closing');
* });
* });
* // Sync check to see if the specified sidenav is set to be open
* $mdSidenav(componentId).isOpen();
* // Sync check to whether given sidenav is locked open
* // If this is true, the sidenav will be open regardless of close()
* $mdSidenav(componentId).isLockedOpen();
* </hljs>
*/
function SidenavService($mdComponentRegistry, $mdUtil, $q, $log) {
var errorMsg = "SideNav '{0}' is not available! Did you use md-component-id='{0}'?";
var service = {
find: findInstance, // sync - returns proxy API
waitFor: waitForInstance // async - returns promise
};
/**
* Service API that supports three (3) usages:
* $mdSidenav().find("left") // sync (must already exist) or returns undefined
* $mdSidenav("left").toggle(); // sync (must already exist) or returns reject promise;
* $mdSidenav("left",true).then(function(left) { // async returns instance when available
* left.toggle();
* });
*/
return function(handle, enableWait) {
if (angular.isUndefined(handle)) {
return service;
}
var shouldWait = enableWait === true;
var instance = service.find(handle, shouldWait);
return !instance && shouldWait ? service.waitFor(handle) :
!instance && angular.isUndefined(enableWait) ? addLegacyAPI(service, handle) : instance;
};
/**
* For failed instance/handle lookups, older-clients expect an response object with noops
* that include `rejected promise APIs`
* @param service
* @param handle
* @returns {Object}
*/
function addLegacyAPI(service, handle) {
var falseFn = function() {
return false;
};
var rejectFn = function() {
return $q.when($mdUtil.supplant(errorMsg, [handle || ""]));
};
return angular.extend({
isLockedOpen: falseFn,
isLockedClosed: falseFn,
isOpen: falseFn,
toggle: rejectFn,
open: rejectFn,
close: rejectFn,
onClose: angular.noop,
then: function(callback) {
return waitForInstance(handle).then(callback || angular.noop);
}
}, service);
}
/**
* Synchronously lookup the controller instance for the specified sidNav instance which has been
* registered with the markup `md-component-id`
*/
function findInstance(handle, shouldWait) {
var instance = $mdComponentRegistry.get(handle);
if (!instance && !shouldWait) {
// Report missing instance
$log.error($mdUtil.supplant(errorMsg, [handle || ""]));
// The component has not registered itself... most like NOT yet created
// return null to indicate that the Sidenav is not in the DOM
return undefined;
}
return instance;
}
/**
* Asynchronously wait for the component instantiation,
* Deferred lookup of component instance using $component registry
*/
function waitForInstance(handle) {
return $mdComponentRegistry.when(handle).catch($log.error);
}
}
/**
* @ngdoc directive
* @name mdSidenavFocus
* @module material.components.sidenav
*
* @restrict A
*
* @description
* `mdSidenavFocus` provides a way to specify the focused element when a sidenav opens.
* This is completely optional, as the sidenav itself is focused by default.
*
* @usage
* <hljs lang="html">
* <md-sidenav>
* <form>
* <md-input-container>
* <label for="testInput">Label</label>
* <input id="testInput" type="text" md-sidenav-focus>
* </md-input-container>
* </form>
* </md-sidenav>
* </hljs>
**/
function SidenavFocusDirective() {
return {
restrict: 'A',
require: '^mdSidenav',
link: function(scope, element, attr, sidenavCtrl) {
// @see $mdUtil.findFocusTarget(...)
}
};
}
/**
* @ngdoc directive
* @name mdSidenav
* @module material.components.sidenav
* @restrict E
*
* @description
* A Sidenav component that can be opened and closed programmatically.
*
* By default, upon opening it will slide out on top of the main content area.
*
* For keyboard and screen reader accessibility, focus is sent to the sidenav wrapper by default.
* It can be overridden with the `md-autofocus` directive on the child element you want focused.
*
* @usage
* <hljs lang="html">
* <div layout="row" ng-controller="MyController">
* <md-sidenav md-component-id="left" class="md-sidenav-left">
* Left Nav!
* </md-sidenav>
*
* <md-content>
* Center Content
* <md-button ng-click="openLeftMenu()">
* Open Left Menu
* </md-button>
* </md-content>
*
* <md-sidenav md-component-id="right"
* md-is-locked-open="$mdMedia('min-width: 333px')"
* class="md-sidenav-right">
* <form>
* <md-input-container>
* <label for="testInput">Test input</label>
* <input id="testInput" type="text"
* ng-model="data" md-autofocus>
* </md-input-container>
* </form>
* </md-sidenav>
* </div>
* </hljs>
*
* <hljs lang="js">
* var app = angular.module('myApp', ['ngMaterial']);
* app.controller('MyController', function($scope, $mdSidenav) {
* $scope.openLeftMenu = function() {
* $mdSidenav('left').toggle();
* };
* });
* </hljs>
*
* @param {expression=} md-is-open A model bound to whether the sidenav is opened.
* @param {boolean=} md-disable-backdrop When present in the markup, the sidenav will not show a
* backdrop.
* @param {boolean=} md-disable-close-events When present in the markup, clicking the backdrop or
* pressing the 'Escape' key will not close the sidenav.
* @param {string=} md-component-id componentId to use with $mdSidenav service.
* @param {expression=} md-is-locked-open When this expression evaluates to true,
* the sidenav 'locks open': it falls into the content's flow instead
* of appearing over it. This overrides the `md-is-open` attribute.
* @param {expression=} md-is-locked-closed When this expression evaluates to true,
* the sidenav 'locks closed': hides sidenav.
* @param {string=} md-disable-scroll-target Selector, pointing to an element, whose scrolling will
* be disabled when the sidenav is opened. By default this is the sidenav's direct parent.
*
* The $mdMedia() service is exposed to the is-locked-open and is-locked-closed attributes, which
* can be given a media query or one of the `sm`, `gt-sm`, `md`, `gt-md`, `lg` or `gt-lg` presets.
* Examples:
*
* - `<md-sidenav md-is-locked-open="shouldLockOpen"></md-sidenav>`
* - `<md-sidenav md-is-locked-open="$mdMedia('min-width: 1000px')"></md-sidenav>`
* - `<md-sidenav md-is-locked-open="$mdMedia('sm')"></md-sidenav>` (locks open on small screens)
* - `<md-sidenav md-is-locked-closed="$mdMedia('sm')"></md-sidenav>` (locks closed on small screens)
*/
function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $mdInteraction, $animate,
$compile, $parse, $log, $q, $document, $window, $$rAF) {
return {
restrict: 'E',
scope: {
isOpen: '=?mdIsOpen'
},
controller: '$mdSidenavController',
compile: function(element) {
element.addClass('md-closed').attr('tabIndex', '-1');
return postLink;
}
};
/**
* Directive Post Link function...
*/
function postLink(scope, element, attr, sidenavCtrl) {
var lastParentOverFlow;
var backdrop;
var disableScrollTarget = null;
var disableCloseEvents;
var triggeringInteractionType;
var triggeringElement = null;
var previousContainerStyles;
var promise = $q.when(true);
var isLockedOpenParsed = $parse(attr.mdIsLockedOpen);
var isLockedClosedParsed = $parse(attr.mdIsLockedClosed);
var ngWindow = angular.element($window);
var isLockedOpen = function() {
return isLockedOpenParsed(scope.$parent, {
$media: function(arg) {
$log.warn("$media is deprecated for is-locked-open. Use $mdMedia instead.");
return $mdMedia(arg);
},
$mdMedia: $mdMedia
});
};
var isLockedClosed = function() {
return isLockedClosedParsed(scope.$parent, {
$media: function(arg) {
$log.warn("$media is deprecated for is-locked-closed. Use $mdMedia instead.");
return $mdMedia(arg);
},
$mdMedia: $mdMedia
});
};
if (attr.mdDisableScrollTarget) {
disableScrollTarget = $document[0].querySelector(attr.mdDisableScrollTarget);
if (disableScrollTarget) {
disableScrollTarget = angular.element(disableScrollTarget);
} else {
$log.warn($mdUtil.supplant('mdSidenav: couldn\'t find element matching ' +
'selector "{selector}". Falling back to parent.',
{ selector: attr.mdDisableScrollTarget }));
}
}
if (!disableScrollTarget) {
disableScrollTarget = element.parent();
}
// Only create the backdrop if the backdrop isn't disabled.
if (!attr.hasOwnProperty('mdDisableBackdrop')) {
backdrop = $mdUtil.createBackdrop(scope, "md-sidenav-backdrop md-opaque ng-enter");
}
// If md-disable-close-events is set on the sidenav we will disable
// backdrop click and Escape key events
if (attr.hasOwnProperty('mdDisableCloseEvents')) {
disableCloseEvents = true;
}
element.addClass('_md'); // private md component indicator for styling
$mdTheming(element);
// The backdrop should inherit the sidenavs theme,
// because the backdrop will take its parent theme by default.
if (backdrop) $mdTheming.inherit(backdrop, element);
element.on('$destroy', function() {
backdrop && backdrop.remove();
sidenavCtrl.destroy();
});
scope.$on('$destroy', function(){
backdrop && backdrop.remove();
});
scope.$watch(isLockedOpen, updateIsLockedOpen);
scope.$watch(isLockedClosed, updateIsLockedClosed);
scope.$watch('isOpen', updateIsOpen);
// Publish special accessor for the Controller instance
sidenavCtrl.$toggleOpen = toggleOpen;
/**
* Toggle the DOM classes to indicate `locked`
* @param isLockedOpen
* @param oldValue
*/
function updateIsLockedOpen(isLockedOpen, oldValue) {
scope.isLockedOpen = isLockedOpen;
if (isLockedOpen === oldValue) {
element.toggleClass('md-locked-open', !!isLockedOpen);
} else {
$animate[isLockedOpen ? 'addClass' : 'removeClass'](element, 'md-locked-open');
}
if (backdrop) {
backdrop.toggleClass('md-locked-open', !!isLockedOpen);
}
}
/**
* Toggle the DOM classes to indicate `locked`
* @param isLockedClosed
* @param oldValue
*/
function updateIsLockedClosed(isLockedClosed, oldValue) {
scope.isLockedClosed = isLockedClosed;
if (true === !!isLockedClosed) {
sidenavCtrl.close();
}
}
/**
* Toggle the SideNav view and attach/detach listeners
* @param isOpen
*/
function updateIsOpen(isOpen) {
// Support deprecated md-sidenav-focus attribute as fallback
var focusEl = $mdUtil.findFocusTarget(element) ||
$mdUtil.findFocusTarget(element,'[md-sidenav-focus]') || element;
var parent = element.parent();
var restorePositioning;
// If the user hasn't set the disable close events property we are adding
// click and escape events to close the sidenav
if (!disableCloseEvents) {
parent[isOpen ? 'on' : 'off']('keydown', onKeyDown);
if (backdrop) backdrop[isOpen ? 'on' : 'off']('click', close);
}
restorePositioning = updateContainerPositions(parent, isOpen);
if (isOpen) {
// Capture upon opening..
triggeringElement = $document[0].activeElement;
triggeringInteractionType = $mdInteraction.getLastInteractionType();
}
disableParentScroll(isOpen);
return promise = $q.all([
isOpen && backdrop ? $animate.enter(backdrop, parent) : backdrop ?
$animate.leave(backdrop) : $q.when(true),
$animate[isOpen ? 'removeClass' : 'addClass'](element, 'md-closed')
]).then(function() {
// Perform focus when animations are ALL done...
if (scope.isOpen) {
$$rAF(function() {
// Notifies child components that the sidenav was opened. Should wait
// a frame in order to allow for the element height to be computed.
ngWindow.triggerHandler('resize');
});
focusEl && focusEl.focus();
}
// Restores the positioning on the sidenav and backdrop.
restorePositioning && restorePositioning();
});
}
function updateContainerPositions(parent, willOpen) {
var drawerEl = element[0];
var scrollTop = parent[0].scrollTop;
if (willOpen && scrollTop) {
previousContainerStyles = {
top: drawerEl.style.top,
bottom: drawerEl.style.bottom,
height: drawerEl.style.height
};
// When the parent is scrolled down, then we want to be able to show the sidenav at the
// current scroll position. We're moving the sidenav down to the correct scroll position
// and apply the height of the parent, to increase the performance. Using 100% as height,
// will impact the performance heavily.
var positionStyle = {
top: scrollTop + 'px',
bottom: 'auto',
height: parent[0].clientHeight + 'px'
};
// Apply the new position styles to the sidenav and backdrop.
element.css(positionStyle);
backdrop.css(positionStyle);
}
// When the sidenav is closing and we have previous defined container styles,
// then we return a restore function, which resets the sidenav and backdrop.
if (!willOpen && previousContainerStyles) {
return function() {
drawerEl.style.top = previousContainerStyles.top;
drawerEl.style.bottom = previousContainerStyles.bottom;
drawerEl.style.height = previousContainerStyles.height;
backdrop[0].style.top = null;
backdrop[0].style.bottom = null;
backdrop[0].style.height = null;
previousContainerStyles = null;
};
}
}
/**
* Prevent parent scrolling (when the SideNav is open)
*/
function disableParentScroll(disabled) {
if (disabled && !lastParentOverFlow) {
lastParentOverFlow = disableScrollTarget.css('overflow');
disableScrollTarget.css('overflow', 'hidden');
} else if (angular.isDefined(lastParentOverFlow)) {
disableScrollTarget.css('overflow', lastParentOverFlow);
lastParentOverFlow = undefined;
}
}
/**
* Toggle the sideNav view and publish a promise to be resolved when
* the view animation finishes.
* @param {boolean} isOpen true to open the sidenav, false to close it
* @returns {*} promise to be resolved when the view animation finishes
*/
function toggleOpen(isOpen) {
if (scope.isOpen === isOpen) {
return $q.when(true);
} else {
if (scope.isOpen && sidenavCtrl.onCloseCb) sidenavCtrl.onCloseCb();
return $q(function(resolve) {
// Toggle value to force an async `updateIsOpen()` to run
scope.isOpen = isOpen;
$mdUtil.nextTick(function() {
// When the current `updateIsOpen()` animation finishes
promise.then(function(result) {
if (!scope.isOpen && triggeringElement && triggeringInteractionType === 'keyboard') {
// reset focus to originating element (if available) upon close
triggeringElement.focus();
triggeringElement = null;
}
resolve(result);
});
});
});
}
}
/**
* Auto-close sideNav when the `escape` key is pressed.
* @param {KeyboardEvent} ev keydown event
*/
function onKeyDown(ev) {
var isEscape = (ev.keyCode === $mdConstant.KEY_CODE.ESCAPE);
return isEscape ? close(ev) : $q.when(true);
}
/**
* With backdrop `clicks` or `escape` key-press, immediately apply the CSS close transition...
* Then notify the controller to close() and perform its own actions.
* @param {Event} ev
* @returns {*}
*/
function close(ev) {
ev.preventDefault();
return sidenavCtrl.close();
}
}
}
/*
* @private
* @ngdoc controller
* @name SidenavController
* @module material.components.sidenav
*/
function SidenavController($scope, $attrs, $mdComponentRegistry, $q, $interpolate) {
var self = this;
// Use Default internal method until overridden by directive postLink
// Synchronous getters
self.isOpen = function() { return !!$scope.isOpen; };
self.isLockedOpen = function() { return !!$scope.isLockedOpen; };
self.isLockedClosed = function() { return !!$scope.isLockedClosed; };
// Synchronous setters
self.onClose = function (callback) {
self.onCloseCb = callback;
return self;
};
// Async actions
self.open = function() { return self.$toggleOpen(true); };
self.close = function() { return self.$toggleOpen(false); };
self.toggle = function() { return self.$toggleOpen(!$scope.isOpen); };
self.$toggleOpen = function(value) { return $q.when($scope.isOpen = value); };
// Evaluate the component id.
var rawId = $attrs.mdComponentId;
var hasDataBinding = rawId && rawId.indexOf($interpolate.startSymbol()) > -1;
var componentId = hasDataBinding ? $interpolate(rawId)($scope.$parent) : rawId;
// Register the component.
self.destroy = $mdComponentRegistry.register(self, componentId);
// Watch and update the component, if the id has changed.
if (hasDataBinding) {
$attrs.$observe('mdComponentId', function(id) {
if (id && id !== self.$$mdHandle) {
// `destroy` only deregisters the old component id so we can add the new one.
self.destroy();
self.destroy = $mdComponentRegistry.register(self, id);
}
});
}
}