@@ -606,20 +606,53 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
606
606
*/
607
607
608
608
/* @ngInject */
609
- function ThemingService ( $rootScope , $log ) {
609
+ function ThemingService ( $rootScope , $mdUtil , $q , $ log) {
610
610
// Allow us to be invoked via a linking function signature.
611
611
var applyTheme = function ( scope , el ) {
612
612
if ( el === undefined ) { el = scope ; scope = undefined ; }
613
613
if ( scope === undefined ) { scope = $rootScope ; }
614
614
applyTheme . inherit ( el , el ) ;
615
615
} ;
616
616
617
- applyTheme . THEMES = angular . extend ( { } , THEMES ) ;
618
- applyTheme . PALETTES = angular . extend ( { } , PALETTES ) ;
617
+ Object . defineProperty ( applyTheme , 'THEMES' , {
618
+ get : function ( ) {
619
+ return angular . extend ( { } , THEMES ) ;
620
+ }
621
+ } ) ;
622
+ Object . defineProperty ( applyTheme , 'PALETTES' , {
623
+ get : function ( ) {
624
+ return angular . extend ( { } , PALETTES ) ;
625
+ }
626
+ } ) ;
619
627
applyTheme . inherit = inheritTheme ;
620
628
applyTheme . registered = registered ;
621
629
applyTheme . defaultTheme = function ( ) { return defaultTheme ; } ;
622
630
applyTheme . generateTheme = function ( name ) { generateTheme ( THEMES [ name ] , name , themeConfig . nonce ) ; } ;
631
+ applyTheme . defineTheme = function ( name , options ) {
632
+ options = options || { } ;
633
+
634
+ var theme = registerTheme ( name ) ;
635
+
636
+ if ( options . primary ) {
637
+ theme . primaryPalette ( options . primary ) ;
638
+ }
639
+ if ( options . accent ) {
640
+ theme . accentPalette ( options . accent ) ;
641
+ }
642
+ if ( options . warn ) {
643
+ theme . warnPalette ( options . warn ) ;
644
+ }
645
+ if ( options . background ) {
646
+ theme . backgroundPalette ( options . background ) ;
647
+ }
648
+ if ( options . dark ) {
649
+ theme . dark ( ) ;
650
+ }
651
+
652
+ this . generateTheme ( name ) ;
653
+
654
+ return $q . resolve ( name ) ;
655
+ } ;
623
656
applyTheme . setBrowserColor = enableBrowserColor ;
624
657
625
658
return applyTheme ;
@@ -636,22 +669,31 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
636
669
* Get theme name for the element, then update with Theme CSS class
637
670
*/
638
671
function inheritTheme ( el , parent ) {
639
- var ctrl = parent . controller ( 'mdTheme' ) ;
640
- var attrThemeValue = el . attr ( 'md-theme-watch' ) ;
641
- var watchTheme = ( alwaysWatchTheme || angular . isDefined ( attrThemeValue ) ) && attrThemeValue != 'false' ;
672
+ var ctrl = parent . controller ( 'mdTheme' ) || el . data ( '$mdThemeController' ) ;
642
673
643
674
updateThemeClass ( lookupThemeName ( ) ) ;
644
675
645
- if ( ( alwaysWatchTheme && ! registerChangeCallback ( ) ) || ( ! alwaysWatchTheme && watchTheme ) ) {
646
- el . on ( '$destroy' , $rootScope . $watch ( lookupThemeName , updateThemeClass ) ) ;
676
+ if ( ctrl ) {
677
+ var watchTheme =
678
+ alwaysWatchTheme || ctrl . $shouldWatch || $mdUtil . parseAttributeBoolean ( el . attr ( 'md-theme-watch' ) ) ;
679
+
680
+ var unwatch = ctrl . registerChanges ( function ( name ) {
681
+ updateThemeClass ( name ) ;
682
+
683
+ if ( ! watchTheme ) {
684
+ unwatch ( ) ;
685
+ }
686
+ else {
687
+ el . on ( '$destroy' , unwatch ) ;
688
+ }
689
+ } ) ;
647
690
}
648
691
649
692
/**
650
693
* Find the theme name from the parent controller or element data
651
694
*/
652
695
function lookupThemeName ( ) {
653
696
// As a few components (dialog) add their controllers later, we should also watch for a controller init.
654
- ctrl = parent . controller ( 'mdTheme' ) || el . data ( '$mdThemeController' ) ;
655
697
return ctrl && ctrl . $mdTheme || ( defaultTheme == 'default' ? '' : defaultTheme ) ;
656
698
}
657
699
@@ -674,24 +716,12 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
674
716
el . data ( '$mdThemeController' , ctrl ) ;
675
717
}
676
718
}
677
-
678
- /**
679
- * Register change callback with parent mdTheme controller
680
- */
681
- function registerChangeCallback ( ) {
682
- var parentController = parent . controller ( 'mdTheme' ) ;
683
- if ( ! parentController ) return false ;
684
- el . on ( '$destroy' , parentController . registerChanges ( function ( ) {
685
- updateThemeClass ( lookupThemeName ( ) ) ;
686
- } ) ) ;
687
- return true ;
688
- }
689
719
}
690
720
691
721
}
692
722
}
693
723
694
- function ThemingDirective ( $mdTheming , $interpolate , $log ) {
724
+ function ThemingDirective ( $mdTheming , $interpolate , $parse , $mdUtil , $q , $ log) {
695
725
return {
696
726
priority : 100 ,
697
727
link : {
@@ -717,16 +747,39 @@ function ThemingDirective($mdTheming, $interpolate, $log) {
717
747
if ( ! $mdTheming . registered ( theme ) ) {
718
748
$log . warn ( 'attempted to use unregistered theme \'' + theme + '\'' ) ;
719
749
}
750
+
751
+
720
752
ctrl . $mdTheme = theme ;
721
753
722
- registeredCallbacks . forEach ( function ( cb ) {
723
- cb ( ) ;
754
+ // Iterating backwards to support unregistering during iteration
755
+ // http://stackoverflow.com/a/9882349/890293
756
+ registeredCallbacks . reverse ( ) . forEach ( function ( cb ) {
757
+ cb ( theme ) ;
724
758
} ) ;
725
- }
759
+ } ,
760
+ $shouldWatch : $mdUtil . parseAttributeBoolean ( el . attr ( 'md-theme-watch' ) )
726
761
} ;
762
+
727
763
el . data ( '$mdThemeController' , ctrl ) ;
728
- ctrl . $setTheme ( $interpolate ( attrs . mdTheme ) ( scope ) ) ;
729
- attrs . $observe ( 'mdTheme' , ctrl . $setTheme ) ;
764
+
765
+ var getThemeInterpolation = function ( ) { return $interpolate ( attrs . mdTheme ) ( scope ) ; } ;
766
+
767
+ var setParsedTheme = function ( interpolation ) {
768
+ var theme = $parse ( interpolation ) ( scope ) || interpolation ;
769
+
770
+ if ( typeof theme === 'string' ) {
771
+ return ctrl . $setTheme ( theme ) ;
772
+ }
773
+
774
+ $q . when ( ( typeof theme === 'function' ) ? theme ( ) : theme )
775
+ . then ( function ( name ) {
776
+ ctrl . $setTheme ( name )
777
+ } ) ;
778
+ } ;
779
+
780
+ setParsedTheme ( getThemeInterpolation ( ) ) ;
781
+
782
+ scope . $watch ( getThemeInterpolation , setParsedTheme ) ;
730
783
}
731
784
}
732
785
} ;
0 commit comments