3
3
* @name material.components.sidenav
4
4
*
5
5
* @description
6
- * A Sidenav QP component.
6
+ * A Sidenav component.
7
7
*/
8
8
angular
9
9
. module ( 'material.components.sidenav' , [
@@ -22,10 +22,9 @@ angular
22
22
* @module material.components.sidenav
23
23
*
24
24
* @description
25
- * `$mdSidenav` makes it easy to interact with multiple sidenavs
26
- * in an app. When looking up a sidenav instance, you can either look
27
- * it up synchronously or wait for it to be initializied asynchronously.
28
- * This is done by passing the second argument to `$mdSidenav`.
25
+ * `$mdSidenav` makes it easy to interact with multiple sidenavs in an app. When looking up a
26
+ * sidenav instance, you can either look it up synchronously or wait for it to be initialized
27
+ * asynchronously. This is done by passing the second argument to `$mdSidenav`.
29
28
*
30
29
* @usage
31
30
* <hljs lang="js">
@@ -74,77 +73,84 @@ angular
74
73
function SidenavService ( $mdComponentRegistry , $mdUtil , $q , $log ) {
75
74
var errorMsg = "SideNav '{0}' is not available! Did you use md-component-id='{0}'?" ;
76
75
var service = {
77
- find : findInstance , // sync - returns proxy API
78
- waitFor : waitForInstance // async - returns promise
79
- } ;
76
+ find : findInstance , // sync - returns proxy API
77
+ waitFor : waitForInstance // async - returns promise
78
+ } ;
80
79
81
80
/**
82
81
* Service API that supports three (3) usages:
83
- * $mdSidenav().find("left") // sync (must already exist) or returns undefined
84
- * $mdSidenav("left").toggle(); // sync (must already exist) or returns reject promise;
85
- * $mdSidenav("left",true).then( function(left){ // async returns instance when available
86
- * left.toggle();
87
- * });
82
+ * $mdSidenav().find("left") // sync (must already exist) or returns undefined
83
+ * $mdSidenav("left").toggle(); // sync (must already exist) or returns reject promise;
84
+ * $mdSidenav("left",true).then(function(left) { // async returns instance when available
85
+ * left.toggle();
86
+ * });
88
87
*/
89
88
return function ( handle , enableWait ) {
90
- if ( angular . isUndefined ( handle ) ) return service ;
89
+ if ( angular . isUndefined ( handle ) ) {
90
+ return service ;
91
+ }
91
92
92
93
var shouldWait = enableWait === true ;
93
94
var instance = service . find ( handle , shouldWait ) ;
94
- return ! instance && shouldWait ? service . waitFor ( handle ) :
95
- ! instance && angular . isUndefined ( enableWait ) ? addLegacyAPI ( service , handle ) : instance ;
95
+ return ! instance && shouldWait ? service . waitFor ( handle ) :
96
+ ! instance && angular . isUndefined ( enableWait ) ? addLegacyAPI ( service , handle ) : instance ;
96
97
} ;
97
98
98
99
/**
99
100
* For failed instance/handle lookups, older-clients expect an response object with noops
100
101
* that include `rejected promise APIs`
102
+ * @param service
103
+ * @param handle
104
+ * @returns {Object }
101
105
*/
102
106
function addLegacyAPI ( service , handle ) {
103
- var falseFn = function ( ) { return false ; } ;
104
- var rejectFn = function ( ) {
105
- return $q . when ( $mdUtil . supplant ( errorMsg , [ handle || "" ] ) ) ;
106
- } ;
107
-
108
- return angular . extend ( {
109
- isLockedOpen : falseFn ,
110
- isOpen : falseFn ,
111
- toggle : rejectFn ,
112
- open : rejectFn ,
113
- close : rejectFn ,
114
- onClose : angular . noop ,
115
- then : function ( callback ) {
116
- return waitForInstance ( handle )
117
- . then ( callback || angular . noop ) ;
118
- }
119
- } , service ) ;
120
- }
121
- /**
122
- * Synchronously lookup the controller instance for the specified sidNav instance which has been
123
- * registered with the markup `md-component-id`
124
- */
125
- function findInstance ( handle , shouldWait ) {
126
- var instance = $mdComponentRegistry . get ( handle ) ;
107
+ var falseFn = function ( ) {
108
+ return false ;
109
+ } ;
110
+ var rejectFn = function ( ) {
111
+ return $q . when ( $mdUtil . supplant ( errorMsg , [ handle || "" ] ) ) ;
112
+ } ;
127
113
128
- if ( ! instance && ! shouldWait ) {
114
+ return angular . extend ( {
115
+ isLockedOpen : falseFn ,
116
+ isOpen : falseFn ,
117
+ toggle : rejectFn ,
118
+ open : rejectFn ,
119
+ close : rejectFn ,
120
+ onClose : angular . noop ,
121
+ then : function ( callback ) {
122
+ return waitForInstance ( handle ) . then ( callback || angular . noop ) ;
123
+ }
124
+ } , service ) ;
125
+ }
129
126
130
- // Report missing instance
131
- $log . error ( $mdUtil . supplant ( errorMsg , [ handle || "" ] ) ) ;
127
+ /**
128
+ * Synchronously lookup the controller instance for the specified sidNav instance which has been
129
+ * registered with the markup `md-component-id`
130
+ */
131
+ function findInstance ( handle , shouldWait ) {
132
+ var instance = $mdComponentRegistry . get ( handle ) ;
132
133
133
- // The component has not registered itself... most like NOT yet created
134
- // return null to indicate that the Sidenav is not in the DOM
135
- return undefined ;
136
- }
137
- return instance ;
138
- }
134
+ if ( ! instance && ! shouldWait ) {
135
+ // Report missing instance
136
+ $log . error ( $mdUtil . supplant ( errorMsg , [ handle || "" ] ) ) ;
139
137
140
- /**
141
- * Asynchronously wait for the component instantiation,
142
- * Deferred lookup of component instance using $component registry
143
- */
144
- function waitForInstance ( handle ) {
145
- return $mdComponentRegistry . when ( handle ) . catch ( $log . error ) ;
138
+ // The component has not registered itself... most like NOT yet created
139
+ // return null to indicate that the Sidenav is not in the DOM
140
+ return undefined ;
146
141
}
142
+ return instance ;
143
+ }
144
+
145
+ /**
146
+ * Asynchronously wait for the component instantiation,
147
+ * Deferred lookup of component instance using $component registry
148
+ */
149
+ function waitForInstance ( handle ) {
150
+ return $mdComponentRegistry . when ( handle ) . catch ( $log . error ) ;
151
+ }
147
152
}
153
+
148
154
/**
149
155
* @ngdoc directive
150
156
* @name mdSidenavFocus
@@ -177,15 +183,15 @@ function SidenavFocusDirective() {
177
183
}
178
184
} ;
179
185
}
186
+
180
187
/**
181
188
* @ngdoc directive
182
189
* @name mdSidenav
183
190
* @module material.components.sidenav
184
191
* @restrict E
185
192
*
186
193
* @description
187
- *
188
- * A Sidenav component that can be opened and closed programatically.
194
+ * A Sidenav component that can be opened and closed programmatically.
189
195
*
190
196
* By default, upon opening it will slide out on top of the main content area.
191
197
*
@@ -230,8 +236,10 @@ function SidenavFocusDirective() {
230
236
* </hljs>
231
237
*
232
238
* @param {expression= } md-is-open A model bound to whether the sidenav is opened.
233
- * @param {boolean= } md-disable-backdrop When present in the markup, the sidenav will not show a backdrop.
234
- * @param {boolean= } md-disable-close-events When present in the markup, clicking the backdrop or pressing the 'Escape' key will not close the sidenav.
239
+ * @param {boolean= } md-disable-backdrop When present in the markup, the sidenav will not show a
240
+ * backdrop.
241
+ * @param {boolean= } md-disable-close-events When present in the markup, clicking the backdrop or
242
+ * pressing the 'Escape' key will not close the sidenav.
235
243
* @param {string= } md-component-id componentId to use with $mdSidenav service.
236
244
* @param {expression= } md-is-locked-open When this expression evaluates to true,
237
245
* the sidenav 'locks open': it falls into the content's flow instead
@@ -268,6 +276,7 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $mdInterac
268
276
var lastParentOverFlow ;
269
277
var backdrop ;
270
278
var disableScrollTarget = null ;
279
+ var disableCloseEvents ;
271
280
var triggeringInteractionType ;
272
281
var triggeringElement = null ;
273
282
var previousContainerStyles ;
@@ -291,7 +300,8 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $mdInterac
291
300
disableScrollTarget = angular . element ( disableScrollTarget ) ;
292
301
} else {
293
302
$log . warn ( $mdUtil . supplant ( 'mdSidenav: couldn\'t find element matching ' +
294
- 'selector "{selector}". Falling back to parent.' , { selector : attr . mdDisableScrollTarget } ) ) ;
303
+ 'selector "{selector}". Falling back to parent.' ,
304
+ { selector : attr . mdDisableScrollTarget } ) ) ;
295
305
}
296
306
}
297
307
@@ -307,7 +317,7 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $mdInterac
307
317
// If md-disable-close-events is set on the sidenav we will disable
308
318
// backdrop click and Escape key events
309
319
if ( attr . hasOwnProperty ( 'mdDisableCloseEvents' ) ) {
310
- var disableCloseEvents = true ;
320
+ disableCloseEvents = true ;
311
321
}
312
322
313
323
element . addClass ( '_md' ) ; // private md component indicator for styling
@@ -336,6 +346,7 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $mdInterac
336
346
/**
337
347
* Toggle the DOM classes to indicate `locked`
338
348
* @param isLocked
349
+ * @param oldValue
339
350
*/
340
351
function updateIsLocked ( isLocked , oldValue ) {
341
352
scope . isLockedOpen = isLocked ;
@@ -355,19 +366,21 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $mdInterac
355
366
*/
356
367
function updateIsOpen ( isOpen ) {
357
368
// Support deprecated md-sidenav-focus attribute as fallback
358
- var focusEl = $mdUtil . findFocusTarget ( element ) || $mdUtil . findFocusTarget ( element , '[md-sidenav-focus]' ) || element ;
369
+ var focusEl = $mdUtil . findFocusTarget ( element ) ||
370
+ $mdUtil . findFocusTarget ( element , '[md-sidenav-focus]' ) || element ;
359
371
var parent = element . parent ( ) ;
372
+ var restorePositioning ;
360
373
361
374
// If the user hasn't set the disable close events property we are adding
362
375
// click and escape events to close the sidenav
363
- if ( ! disableCloseEvents ) {
376
+ if ( ! disableCloseEvents ) {
364
377
parent [ isOpen ? 'on' : 'off' ] ( 'keydown' , onKeyDown ) ;
365
378
if ( backdrop ) backdrop [ isOpen ? 'on' : 'off' ] ( 'click' , close ) ;
366
379
}
367
380
368
- var restorePositioning = updateContainerPositions ( parent , isOpen ) ;
381
+ restorePositioning = updateContainerPositions ( parent , isOpen ) ;
369
382
370
- if ( isOpen ) {
383
+ if ( isOpen ) {
371
384
// Capture upon opening..
372
385
triggeringElement = $document [ 0 ] . activeElement ;
373
386
triggeringInteractionType = $mdInteraction . getLastInteractionType ( ) ;
@@ -407,9 +420,10 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $mdInterac
407
420
height : drawerEl . style . height
408
421
} ;
409
422
410
- // When the parent is scrolled down, then we want to be able to show the sidenav at the current scroll
411
- // position. We're moving the sidenav down to the correct scroll position and apply the height of the
412
- // parent, to increase the performance. Using 100% as height, will impact the performance heavily.
423
+ // When the parent is scrolled down, then we want to be able to show the sidenav at the
424
+ // current scroll position. We're moving the sidenav down to the correct scroll position
425
+ // and apply the height of the parent, to increase the performance. Using 100% as height,
426
+ // will impact the performance heavily.
413
427
var positionStyle = {
414
428
top : scrollTop + 'px' ,
415
429
bottom : 'auto' ,
@@ -454,19 +468,16 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $mdInterac
454
468
/**
455
469
* Toggle the sideNav view and publish a promise to be resolved when
456
470
* the view animation finishes.
457
- *
458
- * @param isOpen
459
- * @returns {* }
471
+ * @param {boolean } isOpen true to open the sidenav, false to close it
472
+ * @returns {* } promise to be resolved when the view animation finishes
460
473
*/
461
- function toggleOpen ( isOpen ) {
462
- if ( scope . isOpen == isOpen ) {
463
-
474
+ function toggleOpen ( isOpen ) {
475
+ if ( scope . isOpen === isOpen ) {
464
476
return $q . when ( true ) ;
465
-
466
477
} else {
467
478
if ( scope . isOpen && sidenavCtrl . onCloseCb ) sidenavCtrl . onCloseCb ( ) ;
468
479
469
- return $q ( function ( resolve ) {
480
+ return $q ( function ( resolve ) {
470
481
// Toggle value to force an async `updateIsOpen()` to run
471
482
scope . isOpen = isOpen ;
472
483
@@ -483,32 +494,30 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $mdInterac
483
494
resolve ( result ) ;
484
495
} ) ;
485
496
} ) ;
486
-
487
497
} ) ;
488
-
489
498
}
490
499
}
491
500
492
501
/**
493
502
* Auto-close sideNav when the `escape` key is pressed.
494
- * @param evt
503
+ * @param { KeyboardEvent } ev keydown event
495
504
*/
496
505
function onKeyDown ( ev ) {
497
506
var isEscape = ( ev . keyCode === $mdConstant . KEY_CODE . ESCAPE ) ;
498
507
return isEscape ? close ( ev ) : $q . when ( true ) ;
499
508
}
500
509
501
510
/**
502
- * With backdrop `clicks` or `escape` key-press, immediately
503
- * apply the CSS close transition... Then notify the controller
504
- * to close() and perform its own actions.
511
+ * With backdrop `clicks` or `escape` key-press, immediately apply the CSS close transition...
512
+ * Then notify the controller to close() and perform its own actions.
513
+ * @param {Event } ev
514
+ * @returns {* }
505
515
*/
506
516
function close ( ev ) {
507
517
ev . preventDefault ( ) ;
508
518
509
519
return sidenavCtrl . close ( ) ;
510
520
}
511
-
512
521
}
513
522
}
514
523
@@ -519,7 +528,6 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $mdInterac
519
528
* @module material.components.sidenav
520
529
*/
521
530
function SidenavController ( $scope , $attrs , $mdComponentRegistry , $q , $interpolate ) {
522
-
523
531
var self = this ;
524
532
525
533
// Use Default internal method until overridden by directive postLink
@@ -552,7 +560,8 @@ function SidenavController($scope, $attrs, $mdComponentRegistry, $q, $interpolat
552
560
if ( hasDataBinding ) {
553
561
$attrs . $observe ( 'mdComponentId' , function ( id ) {
554
562
if ( id && id !== self . $$mdHandle ) {
555
- self . destroy ( ) ; // `destroy` only deregisters the old component id so we can add the new one.
563
+ // `destroy` only deregisters the old component id so we can add the new one.
564
+ self . destroy ( ) ;
556
565
self . destroy = $mdComponentRegistry . register ( self , id ) ;
557
566
}
558
567
} ) ;
0 commit comments