@@ -538,20 +538,53 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
538
538
* @param {el= } element to apply theming to
539
539
*/
540
540
/* @ngInject */
541
- function ThemingService ( $rootScope , $log ) {
541
+ function ThemingService ( $rootScope , $log , $mdUtil ) {
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
- applyTheme . THEMES = angular . extend ( { } , THEMES ) ;
550
- applyTheme . PALETTES = angular . extend ( { } , PALETTES ) ;
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
+ } ) ;
551
559
applyTheme . inherit = inheritTheme ;
552
560
applyTheme . registered = registered ;
553
561
applyTheme . defaultTheme = function ( ) { return defaultTheme ; } ;
554
562
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 theme ;
587
+ } ;
555
588
applyTheme . setBrowserColor = enableBrowserColor ;
556
589
557
590
return applyTheme ;
@@ -568,22 +601,30 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
568
601
* Get theme name for the element, then update with Theme CSS class
569
602
*/
570
603
function inheritTheme ( el , parent ) {
571
- var ctrl = parent . controller ( 'mdTheme' ) ;
572
- var attrThemeValue = el . attr ( 'md-theme-watch' ) ;
573
- var watchTheme = ( alwaysWatchTheme || angular . isDefined ( attrThemeValue ) ) && attrThemeValue != 'false' ;
604
+ var ctrl = parent . controller ( 'mdTheme' ) || el . data ( '$mdThemeController' ) ;
574
605
575
606
updateThemeClass ( lookupThemeName ( ) ) ;
576
607
577
- if ( ( alwaysWatchTheme && ! registerChangeCallback ( ) ) || ( ! alwaysWatchTheme && watchTheme ) ) {
578
- el . on ( '$destroy' , $rootScope . $watch ( lookupThemeName , updateThemeClass ) ) ;
608
+ if ( ctrl ) {
609
+ var watchTheme = alwaysWatchTheme || ctrl . $shouldWatch || $mdUtil . parseAttributeBoolean ( el . attr ( 'md-theme-watch' ) ) ;
610
+
611
+ var unwatch = ctrl . registerChanges ( function ( name ) {
612
+ updateThemeClass ( name ) ;
613
+
614
+ if ( ! watchTheme ) {
615
+ unwatch ( ) ;
616
+ }
617
+ else {
618
+ el . on ( '$destroy' , unwatch ) ;
619
+ }
620
+ } ) ;
579
621
}
580
622
581
623
/**
582
624
* Find the theme name from the parent controller or element data
583
625
*/
584
626
function lookupThemeName ( ) {
585
627
// 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' ) ;
587
628
return ctrl && ctrl . $mdTheme || ( defaultTheme == 'default' ? '' : defaultTheme ) ;
588
629
}
589
630
@@ -606,24 +647,12 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
606
647
el . data ( '$mdThemeController' , ctrl ) ;
607
648
}
608
649
}
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
- }
621
650
}
622
651
623
652
}
624
653
}
625
654
626
- function ThemingDirective ( $mdTheming , $interpolate , $log ) {
655
+ function ThemingDirective ( $mdTheming , $interpolate , $parse , $mdUtil , $q , $ log) {
627
656
return {
628
657
priority : 100 ,
629
658
link : {
@@ -649,16 +678,39 @@ function ThemingDirective($mdTheming, $interpolate, $log) {
649
678
if ( ! $mdTheming . registered ( theme ) ) {
650
679
$log . warn ( 'attempted to use unregistered theme \'' + theme + '\'' ) ;
651
680
}
681
+
682
+
652
683
ctrl . $mdTheme = theme ;
653
684
654
- registeredCallbacks . forEach ( function ( cb ) {
655
- cb ( ) ;
685
+ // Iterating backwards to support unregistering during iteration
686
+ // http://stackoverflow.com/a/9882349/890293
687
+ registeredCallbacks . reverse ( ) . forEach ( function ( cb ) {
688
+ cb ( theme ) ;
656
689
} ) ;
657
- }
690
+ } ,
691
+ $shouldWatch : $mdUtil . parseAttributeBoolean ( el . attr ( 'md-theme-watch' ) )
658
692
} ;
693
+
659
694
el . data ( '$mdThemeController' , ctrl ) ;
660
- ctrl . $setTheme ( $interpolate ( attrs . mdTheme ) ( scope ) ) ;
661
- attrs . $observe ( 'mdTheme' , ctrl . $setTheme ) ;
695
+
696
+ var getThemeInterpolation = function ( ) { return $interpolate ( attrs . mdTheme ) ( scope ) ; } ;
697
+
698
+ var setParsedTheme = function ( interpolation ) {
699
+ var theme = $parse ( interpolation ) ( scope ) || interpolation ;
700
+
701
+ if ( typeof theme === 'string' ) {
702
+ return ctrl . $setTheme ( theme ) ;
703
+ }
704
+
705
+ $q . when ( ( typeof theme === 'function' ) ? theme ( ) : theme )
706
+ . then ( function ( name ) {
707
+ ctrl . $setTheme ( name )
708
+ } ) ;
709
+ } ;
710
+
711
+ setParsedTheme ( getThemeInterpolation ( ) ) ;
712
+
713
+ scope . $watch ( getThemeInterpolation , setParsedTheme ) ;
662
714
}
663
715
}
664
716
} ;
0 commit comments