@@ -22,7 +22,9 @@ function MenuProvider($$interimElementProvider) {
22
22
} ) ;
23
23
24
24
/* @ngInject */
25
- function menuDefaultOptions ( $mdUtil , $mdTheming , $mdConstant , $document , $window , $q , $$rAF , $animateCss , $animate ) {
25
+ function menuDefaultOptions ( $mdUtil , $mdTheming , $mdConstant , $document , $window , $q , $$rAF ,
26
+ $animateCss , $animate , $log ) {
27
+
26
28
var prefixer = $mdUtil . prefixer ( ) ;
27
29
var animator = $mdUtil . dom . animator ;
28
30
@@ -74,10 +76,14 @@ function MenuProvider($$interimElementProvider) {
74
76
* and backdrop
75
77
*/
76
78
function onRemove ( scope , element , opts ) {
77
- opts . cleanupInteraction && opts . cleanupInteraction ( ) ;
79
+ opts . cleanupInteraction ( ) ;
80
+ opts . cleanupBackdrop ( ) ;
78
81
opts . cleanupResizing ( ) ;
79
82
opts . hideBackdrop ( ) ;
80
83
84
+ // Before the menu is closing remove the clickable class.
85
+ element . removeClass ( 'md-clickable' ) ;
86
+
81
87
// For navigation $destroy events, do a quick, non-animated removal,
82
88
// but for normal closes (from clicks, etc) animate the removal
83
89
@@ -109,9 +115,17 @@ function MenuProvider($$interimElementProvider) {
109
115
function onShow ( scope , element , opts ) {
110
116
sanitizeAndConfigure ( opts ) ;
111
117
112
- // Wire up theming on our menu element
113
- $mdTheming . inherit ( opts . menuContentEl , opts . target ) ;
114
-
118
+ if ( opts . menuContentEl [ 0 ] ) {
119
+ // Inherit the theme from the target element.
120
+ $mdTheming ( opts . menuContentEl , opts . target ) ;
121
+ } else {
122
+ $log . warn (
123
+ '$mdMenu: Menu elements should always contain a `md-menu-content` element,' +
124
+ 'otherwise interactivity features will not work properly.' ,
125
+ element
126
+ ) ;
127
+ }
128
+
115
129
// Register various listeners to move menu on resize/orientation change
116
130
opts . cleanupResizing = startRepositioningOnResize ( ) ;
117
131
opts . hideBackdrop = showBackdrop ( scope , element , opts ) ;
@@ -121,6 +135,11 @@ function MenuProvider($$interimElementProvider) {
121
135
. then ( function ( response ) {
122
136
opts . alreadyOpen = true ;
123
137
opts . cleanupInteraction = activateInteraction ( ) ;
138
+ opts . cleanupBackdrop = setupBackdrop ( ) ;
139
+
140
+ // Since the menu finished its animation, mark the menu as clickable.
141
+ element . addClass ( 'md-clickable' ) ;
142
+
124
143
return response ;
125
144
} ) ;
126
145
@@ -194,14 +213,40 @@ function MenuProvider($$interimElementProvider) {
194
213
}
195
214
196
215
/**
197
- * Activate interaction on the menu. Wire up keyboard listerns for
198
- * clicks, keypresses, backdrop closing, etc.
216
+ * Sets up the backdrop and listens for click elements.
217
+ * Once the backdrop will be clicked, the menu will automatically close.
218
+ * @returns {!Function } Function to remove the backdrop.
199
219
*/
200
- function activateInteraction ( ) {
201
- element . addClass ( 'md-clickable' ) ;
220
+ function setupBackdrop ( ) {
221
+ if ( ! opts . backdrop ) return angular . noop ;
222
+
223
+ opts . backdrop . on ( 'click' , onBackdropClick ) ;
202
224
203
- // close on backdrop click
204
- if ( opts . backdrop ) opts . backdrop . on ( 'click' , onBackdropClick ) ;
225
+ return function ( ) {
226
+ opts . backdrop . off ( 'click' , onBackdropClick ) ;
227
+ }
228
+ }
229
+
230
+ /**
231
+ * Function to be called whenever the backdrop is clicked.
232
+ * @param {!MouseEvent } event
233
+ */
234
+ function onBackdropClick ( event ) {
235
+ event . preventDefault ( ) ;
236
+ event . stopPropagation ( ) ;
237
+
238
+ scope . $apply ( function ( ) {
239
+ opts . mdMenuCtrl . close ( true , { closeAll : true } ) ;
240
+ } ) ;
241
+ }
242
+
243
+ /**
244
+ * Activate interaction on the menu. Resolves the focus target and closes the menu on
245
+ * escape or option click.
246
+ * @returns {!Function } Function to deactivate the interaction listeners.
247
+ */
248
+ function activateInteraction ( ) {
249
+ if ( ! opts . menuContentEl [ 0 ] ) return angular . noop ;
205
250
206
251
// Wire up keyboard listeners.
207
252
// - Close on escape,
@@ -232,8 +277,6 @@ function MenuProvider($$interimElementProvider) {
232
277
focusTarget && focusTarget . focus ( ) ;
233
278
234
279
return function cleanupInteraction ( ) {
235
- element . removeClass ( 'md-clickable' ) ;
236
- if ( opts . backdrop ) opts . backdrop . off ( 'click' , onBackdropClick ) ;
237
280
opts . menuContentEl . off ( 'keydown' , onMenuKeyDown ) ;
238
281
opts . menuContentEl [ 0 ] . removeEventListener ( 'click' , captureClickListener , true ) ;
239
282
} ;
0 commit comments