@@ -538,53 +538,20 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
538
538
* @param {el= } element to apply theming to
539
539
*/
540
540
/* @ngInject */
541
- function ThemingService ( $rootScope , $mdUtil , $q , $ log) {
541
+ function ThemingService ( $rootScope , $log ) {
542
542
// Allow us to be invoked via a linking function signature.
543
543
var applyTheme = function ( scope , el ) {
544
544
if ( el === undefined ) { el = scope ; scope = undefined ; }
545
545
if ( scope === undefined ) { scope = $rootScope ; }
546
546
applyTheme . inherit ( el , el ) ;
547
547
} ;
548
548
549
- Object . defineProperty ( applyTheme , 'THEMES' , {
550
- get : function ( ) {
551
- return angular . extend ( { } , THEMES ) ;
552
- }
553
- } ) ;
554
- Object . defineProperty ( applyTheme , 'PALETTES' , {
555
- get : function ( ) {
556
- return angular . extend ( { } , PALETTES ) ;
557
- }
558
- } ) ;
549
+ applyTheme . THEMES = angular . extend ( { } , THEMES ) ;
550
+ applyTheme . PALETTES = angular . extend ( { } , PALETTES ) ;
559
551
applyTheme . inherit = inheritTheme ;
560
552
applyTheme . registered = registered ;
561
553
applyTheme . defaultTheme = function ( ) { return defaultTheme ; } ;
562
554
applyTheme . generateTheme = function ( name ) { generateTheme ( THEMES [ name ] , name , themeConfig . nonce ) ; } ;
563
- applyTheme . defineTheme = function ( name , options ) {
564
- options = options || { } ;
565
-
566
- var theme = registerTheme ( name ) ;
567
-
568
- if ( options . primary ) {
569
- theme . primaryPalette ( options . primary ) ;
570
- }
571
- if ( options . accent ) {
572
- theme . accentPalette ( options . accent ) ;
573
- }
574
- if ( options . warn ) {
575
- theme . warnPalette ( options . warn ) ;
576
- }
577
- if ( options . background ) {
578
- theme . backgroundPalette ( options . background ) ;
579
- }
580
- if ( options . dark ) {
581
- theme . dark ( ) ;
582
- }
583
-
584
- this . generateTheme ( name ) ;
585
-
586
- return $q . resolve ( name ) ;
587
- } ;
588
555
applyTheme . setBrowserColor = enableBrowserColor ;
589
556
590
557
return applyTheme ;
@@ -601,31 +568,22 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
601
568
* Get theme name for the element, then update with Theme CSS class
602
569
*/
603
570
function inheritTheme ( el , parent ) {
604
- var ctrl = parent . controller ( 'mdTheme' ) || el . data ( '$mdThemeController' ) ;
571
+ var ctrl = parent . controller ( 'mdTheme' ) ;
572
+ var attrThemeValue = el . attr ( 'md-theme-watch' ) ;
573
+ var watchTheme = ( alwaysWatchTheme || angular . isDefined ( attrThemeValue ) ) && attrThemeValue != 'false' ;
605
574
606
575
updateThemeClass ( lookupThemeName ( ) ) ;
607
576
608
- if ( ctrl ) {
609
- var watchTheme =
610
- alwaysWatchTheme || ctrl . $shouldWatch || $mdUtil . parseAttributeBoolean ( el . attr ( 'md-theme-watch' ) ) ;
611
-
612
- var unwatch = ctrl . registerChanges ( function ( name ) {
613
- updateThemeClass ( name ) ;
614
-
615
- if ( ! watchTheme ) {
616
- unwatch ( ) ;
617
- }
618
- else {
619
- el . on ( '$destroy' , unwatch ) ;
620
- }
621
- } ) ;
577
+ if ( ( alwaysWatchTheme && ! registerChangeCallback ( ) ) || ( ! alwaysWatchTheme && watchTheme ) ) {
578
+ el . on ( '$destroy' , $rootScope . $watch ( lookupThemeName , updateThemeClass ) ) ;
622
579
}
623
580
624
581
/**
625
582
* Find the theme name from the parent controller or element data
626
583
*/
627
584
function lookupThemeName ( ) {
628
585
// As a few components (dialog) add their controllers later, we should also watch for a controller init.
586
+ ctrl = parent . controller ( 'mdTheme' ) || el . data ( '$mdThemeController' ) ;
629
587
return ctrl && ctrl . $mdTheme || ( defaultTheme == 'default' ? '' : defaultTheme ) ;
630
588
}
631
589
@@ -648,12 +606,24 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
648
606
el . data ( '$mdThemeController' , ctrl ) ;
649
607
}
650
608
}
609
+
610
+ /**
611
+ * Register change callback with parent mdTheme controller
612
+ */
613
+ function registerChangeCallback ( ) {
614
+ var parentController = parent . controller ( 'mdTheme' ) ;
615
+ if ( ! parentController ) return false ;
616
+ el . on ( '$destroy' , parentController . registerChanges ( function ( ) {
617
+ updateThemeClass ( lookupThemeName ( ) ) ;
618
+ } ) ) ;
619
+ return true ;
620
+ }
651
621
}
652
622
653
623
}
654
624
}
655
625
656
- function ThemingDirective ( $mdTheming , $interpolate , $parse , $mdUtil , $q , $ log) {
626
+ function ThemingDirective ( $mdTheming , $interpolate , $log ) {
657
627
return {
658
628
priority : 100 ,
659
629
link : {
@@ -679,39 +649,16 @@ function ThemingDirective($mdTheming, $interpolate, $parse, $mdUtil, $q, $log) {
679
649
if ( ! $mdTheming . registered ( theme ) ) {
680
650
$log . warn ( 'attempted to use unregistered theme \'' + theme + '\'' ) ;
681
651
}
682
-
683
-
684
652
ctrl . $mdTheme = theme ;
685
653
686
- // Iterating backwards to support unregistering during iteration
687
- // http://stackoverflow.com/a/9882349/890293
688
- registeredCallbacks . reverse ( ) . forEach ( function ( cb ) {
689
- cb ( theme ) ;
654
+ registeredCallbacks . forEach ( function ( cb ) {
655
+ cb ( ) ;
690
656
} ) ;
691
- } ,
692
- $shouldWatch : $mdUtil . parseAttributeBoolean ( el . attr ( 'md-theme-watch' ) )
693
- } ;
694
-
695
- el . data ( '$mdThemeController' , ctrl ) ;
696
-
697
- var getThemeInterpolation = function ( ) { return $interpolate ( attrs . mdTheme ) ( scope ) ; } ;
698
-
699
- var setParsedTheme = function ( interpolation ) {
700
- var theme = $parse ( interpolation ) ( scope ) || interpolation ;
701
-
702
- if ( typeof theme === 'string' ) {
703
- return ctrl . $setTheme ( theme ) ;
704
657
}
705
-
706
- $q . when ( ( typeof theme === 'function' ) ? theme ( ) : theme )
707
- . then ( function ( name ) {
708
- ctrl . $setTheme ( name )
709
- } ) ;
710
658
} ;
711
-
712
- setParsedTheme ( getThemeInterpolation ( ) ) ;
713
-
714
- scope . $watch ( getThemeInterpolation , setParsedTheme ) ;
659
+ el . data ( '$mdThemeController' , ctrl ) ;
660
+ ctrl . $setTheme ( $interpolate ( attrs . mdTheme ) ( scope ) ) ;
661
+ attrs . $observe ( 'mdTheme' , ctrl . $setTheme ) ;
715
662
}
716
663
}
717
664
} ;
0 commit comments