@@ -334,27 +334,61 @@ function ThemingProvider($mdColorPalette) {
334
334
*/
335
335
/* @ngInject */
336
336
function ThemingService ( $rootScope , $log ) {
337
+ // Allow us to be invoked via a linking function signature.
338
+ var applyTheme = function ( scope , el ) {
339
+ if ( el === undefined ) { el = scope ; scope = undefined ; }
340
+ if ( scope === undefined ) { scope = $rootScope ; }
341
+ applyTheme . inherit ( el , el ) ;
342
+ } ;
337
343
338
- applyTheme . inherit = function ( el , parent ) {
339
- var ctrl = parent . controller ( 'mdTheme' ) ;
344
+ applyTheme . THEMES = angular . extend ( { } , THEMES ) ;
345
+ applyTheme . inherit = inheritTheme ;
346
+ applyTheme . registered = registered ;
347
+ applyTheme . defaultTheme = function ( ) { return defaultTheme ; } ;
348
+ applyTheme . generateTheme = function ( name ) { generateTheme ( name , nonce ) ; } ;
349
+
350
+ return applyTheme ;
340
351
352
+ /**
353
+ * Determine is specified theme name is a valid, registered theme
354
+ */
355
+ function registered ( themeName ) {
356
+ if ( themeName === undefined || themeName === '' ) return true ;
357
+ return applyTheme . THEMES [ themeName ] !== undefined ;
358
+ }
359
+
360
+ /**
361
+ * Get theme name for the element, then update with Theme CSS class
362
+ */
363
+ function inheritTheme ( el , parent ) {
364
+ var ctrl = parent . controller ( 'mdTheme' ) ;
341
365
var attrThemeValue = el . attr ( 'md-theme-watch' ) ;
342
- if ( ( alwaysWatchTheme || angular . isDefined ( attrThemeValue ) ) && attrThemeValue != 'false' ) {
343
- var deregisterWatch = $rootScope . $watch ( function ( ) {
344
- return ctrl && ctrl . $mdTheme || ( defaultTheme == 'default' ? '' : defaultTheme ) ;
345
- } , changeTheme ) ;
346
- el . on ( '$destroy' , deregisterWatch ) ;
347
- } else {
348
- var theme = ctrl && ctrl . $mdTheme || ( defaultTheme == 'default' ? '' : defaultTheme ) ;
349
- changeTheme ( theme ) ;
366
+ var watchTheme = ( alwaysWatchTheme || angular . isDefined ( attrThemeValue ) ) && attrThemeValue != 'false' ;
367
+
368
+ updateThemeClass ( lookupThemeName ( ) ) ;
369
+
370
+ el . on ( '$destroy' , watchTheme ? $rootScope . $watch ( lookupThemeName , updateThemeClass ) : angular . noop ) ;
371
+
372
+ /**
373
+ * Find the theme name from the parent controller or element data
374
+ */
375
+ function lookupThemeName ( ) {
376
+ // As a few components (dialog) add their controllers later, we should also watch for a controller init.
377
+ ctrl = parent . controller ( 'mdTheme' ) || el . data ( '$mdThemeController' ) ;
378
+ return ctrl && ctrl . $mdTheme || ( defaultTheme == 'default' ? '' : defaultTheme ) ;
350
379
}
351
380
352
- function changeTheme ( theme ) {
381
+ /**
382
+ * Remove old theme class and apply a new one
383
+ * NOTE: if not a valid theme name, then the current name is not changed
384
+ */
385
+ function updateThemeClass ( theme ) {
353
386
if ( ! theme ) return ;
354
387
if ( ! registered ( theme ) ) {
355
388
$log . warn ( 'Attempted to use unregistered theme \'' + theme + '\'. ' +
356
389
'Register it with $mdThemingProvider.theme().' ) ;
357
390
}
391
+
358
392
var oldTheme = el . data ( '$mdThemeName' ) ;
359
393
if ( oldTheme ) el . removeClass ( 'md-' + oldTheme + '-theme' ) ;
360
394
el . addClass ( 'md-' + theme + '-theme' ) ;
@@ -363,31 +397,8 @@ function ThemingProvider($mdColorPalette) {
363
397
el . data ( '$mdThemeController' , ctrl ) ;
364
398
}
365
399
}
366
- } ;
367
-
368
- applyTheme . THEMES = angular . extend ( { } , THEMES ) ;
369
- applyTheme . defaultTheme = function ( ) { return defaultTheme ; } ;
370
- applyTheme . registered = registered ;
371
- applyTheme . generateTheme = function ( name ) { generateTheme ( name , nonce ) ; } ;
372
-
373
- return applyTheme ;
374
-
375
- function registered ( themeName ) {
376
- if ( themeName === undefined || themeName === '' ) return true ;
377
- return applyTheme . THEMES [ themeName ] !== undefined ;
378
400
}
379
401
380
- function applyTheme ( scope , el ) {
381
- // Allow us to be invoked via a linking function signature.
382
- if ( el === undefined ) {
383
- el = scope ;
384
- scope = undefined ;
385
- }
386
- if ( scope === undefined ) {
387
- scope = $rootScope ;
388
- }
389
- applyTheme . inherit ( el , el ) ;
390
- }
391
402
}
392
403
}
393
404
0 commit comments