Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit e0ecde8

Browse files
committed
feat(themes): register theme on the fly
- By using `defineTheme` of `$mdTheming` you can specify the name of the theme and the palettes, combined with `md-deferred-theme` directive to avoid warnings on not defined themes fixes #2965
1 parent 0cd2a59 commit e0ecde8

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/core/services/theming/theming.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
angular.module('material.core.theming', ['material.core.theming.palette', 'material.core.meta'])
1010
.directive('mdTheme', ThemingDirective)
11+
.directive('mdDeferredTheme', ThemingDirective)
1112
.directive('mdThemable', ThemableDirective)
1213
.directive('mdThemesDisabled', disableThemesDirective )
1314
.provider('$mdTheming', ThemingProvider)
@@ -552,6 +553,28 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
552553
applyTheme.registered = registered;
553554
applyTheme.defaultTheme = function() { return defaultTheme; };
554555
applyTheme.generateTheme = function(name) { generateTheme(THEMES[name], name, themeConfig.nonce); };
556+
applyTheme.defineTheme = function (name, options) {
557+
options = options || {};
558+
559+
var theme = registerTheme(name);
560+
561+
if (options.primary) {
562+
theme.primaryPalette(options.primary);
563+
}
564+
if (options.accent) {
565+
theme.accentPalette(options.accent);
566+
}
567+
if (options.warn) {
568+
theme.warnPalette(options.warn);
569+
}
570+
if (options.background) {
571+
theme.backgroundPalette(options.background);
572+
}
573+
574+
this.generateTheme(name);
575+
576+
return theme;
577+
};
555578
applyTheme.setBrowserColor = enableBrowserColor;
556579

557580
return applyTheme;
@@ -593,7 +616,7 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
593616
*/
594617
function updateThemeClass(theme) {
595618
if (!theme) return;
596-
if (!registered(theme)) {
619+
if (!ctrl.$isDeferredTheme && !registered(theme)) {
597620
$log.warn('Attempted to use unregistered theme \'' + theme + '\'. ' +
598621
'Register it with $mdThemingProvider.theme().');
599622
}
@@ -628,6 +651,7 @@ function ThemingDirective($mdTheming, $interpolate, $log) {
628651
priority: 100,
629652
link: {
630653
pre: function(scope, el, attrs) {
654+
var isDeferred = attrs.hasOwnProperty('mdDeferredTheme');
631655
var registeredCallbacks = [];
632656
var ctrl = {
633657
registerChanges: function (cb, context) {
@@ -646,19 +670,21 @@ function ThemingDirective($mdTheming, $interpolate, $log) {
646670
};
647671
},
648672
$setTheme: function (theme) {
649-
if (!$mdTheming.registered(theme)) {
673+
if (!isDeferred && !$mdTheming.registered(theme)) {
650674
$log.warn('attempted to use unregistered theme \'' + theme + '\'');
651675
}
652676
ctrl.$mdTheme = theme;
677+
ctrl.$isDeferredTheme = isDeferred;
653678

654679
registeredCallbacks.forEach(function (cb) {
655680
cb();
656681
});
657682
}
658683
};
659684
el.data('$mdThemeController', ctrl);
660-
ctrl.$setTheme($interpolate(attrs.mdTheme)(scope));
661-
attrs.$observe('mdTheme', ctrl.$setTheme);
685+
var directiveType = isDeferred ? 'mdDeferredTheme' : 'mdTheme';
686+
ctrl.$setTheme($interpolate(attrs[directiveType])(scope));
687+
attrs.$observe(directiveType, ctrl.$setTheme);
662688
}
663689
}
664690
};

0 commit comments

Comments
 (0)