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

Commit

Permalink
feat(theming): add warnings when using unregistered themes
Browse files Browse the repository at this point in the history
  • Loading branch information
rschmukler committed Jan 27, 2015
1 parent 06c52b6 commit f6f56c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/core/services/theming/theming.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ function ThemingProvider($mdColorPalette) {
* @param {el=} element to apply theming to
*/
/* @ngInject */
function ThemingService($rootScope) {
function ThemingService($rootScope, $log) {
applyTheme.inherit = function(el, parent) {
var ctrl = parent.controller('mdTheme');

Expand All @@ -298,15 +298,26 @@ function ThemingProvider($mdColorPalette) {
}

function changeTheme(theme) {
if (!registered(name)) {
debugger;
$log.warn('attempted to use unregistered theme \'' + theme + '\'');
}
var oldTheme = el.data('$mdThemeName');
if (oldTheme) el.removeClass('md-' + oldTheme +'-theme');
el.addClass('md-' + theme + '-theme');
el.data('$mdThemeName', theme);
}
};

applyTheme.registered = registered;

return applyTheme;

function registered(theme) {
if (theme === undefined || theme === '') return true;
return THEMES[theme] !== undefined;
}

function applyTheme(scope, el) {
// Allow us to be invoked via a linking function signature.
if (el === undefined) {
Expand All @@ -321,13 +332,16 @@ function ThemingProvider($mdColorPalette) {
}
}

function ThemingDirective($interpolate) {
function ThemingDirective($mdTheming, $interpolate, $log) {
return {
priority: 100,
link: {
pre: function(scope, el, attrs) {
var ctrl = {
$setTheme: function(theme) {
if (!$mdTheming.registered(theme)) {
$log.warn('attempted to use unregistered theme \'' + theme + '\'');
}
ctrl.$mdTheme = theme;
}
};
Expand Down
10 changes: 10 additions & 0 deletions src/core/services/theming/theming.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('$mdThemingProvider', function() {
.backgroundPalette('testPalette');

testTheme = themingProvider.theme('test');

});
startAngular();
}
Expand Down Expand Up @@ -361,6 +362,15 @@ describe('md-theme directive', function() {
$rootScope.$apply('themey = "blue"');
expect(ctrl.$mdTheme).toBe('blue');
}));

it('warns when an unregistered theme is used', function() {
inject(function($log, $compile, $rootScope) {
spyOn($log, 'warn');
var el = $compile('<div md-theme="unregistered"></div>')($rootScope);
$rootScope.$apply();
expect($log.warn).toHaveBeenCalled();
});
});
});

describe('md-themable directive', function() {
Expand Down

0 comments on commit f6f56c9

Please sign in to comment.