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

Commit a653122

Browse files
devversionThomasBurleson
authored andcommitted
fix(theming): theming should also watch for controller changes.
As the dialog inherits the controllers not at link time, the theming won't work. Tried a few possibilities: - Walking through the DOM after show and re-theme the elements - Recompile the template after show But the most elegant and performant solution is definitely watching for the controller change. Fixes #5899 Closes #7154
1 parent e126977 commit a653122

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

src/core/services/theming/theming.js

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -334,27 +334,61 @@ function ThemingProvider($mdColorPalette) {
334334
*/
335335
/* @ngInject */
336336
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+
};
337343

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;
340351

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');
341365
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);
350379
}
351380

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) {
353386
if (!theme) return;
354387
if (!registered(theme)) {
355388
$log.warn('Attempted to use unregistered theme \'' + theme + '\'. ' +
356389
'Register it with $mdThemingProvider.theme().');
357390
}
391+
358392
var oldTheme = el.data('$mdThemeName');
359393
if (oldTheme) el.removeClass('md-' + oldTheme +'-theme');
360394
el.addClass('md-' + theme + '-theme');
@@ -363,31 +397,8 @@ function ThemingProvider($mdColorPalette) {
363397
el.data('$mdThemeController', ctrl);
364398
}
365399
}
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;
378400
}
379401

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-
}
391402
}
392403
}
393404

0 commit comments

Comments
 (0)