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

Commit

Permalink
fix(colors): parsed watched expression
Browse files Browse the repository at this point in the history
- Moved expression parsing inside the watch, this way the watch could really trigger changes when the interpolated expression had changed

fixes #8212

Closes #8235
  • Loading branch information
EladBezalel authored and ThomasBurleson committed May 11, 2016
1 parent d62bb5e commit e9a1d4f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
30 changes: 12 additions & 18 deletions src/components/colors/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* </hljs>
*
*/
function MdColorsService($mdTheming, $mdUtil, $parse, $log) {
function MdColorsService($mdTheming, $mdUtil, $log) {
colorPalettes = colorPalettes || Object.keys($mdTheming.PALETTES);

// Publish service instance
Expand All @@ -61,10 +61,8 @@
* Then calculate the rgba() values based on the theme color parts
*
* @param {DOMElement} element the element to apply the styles on.
* @param {scope} scope a scope is needed in case there are interpolated values in the expression.
* @param {string|object} colorExpression json object, keys are css properties and values are string of the wanted color,
* for example: `{color: 'red-A200-0.3'}`. Note that the color keys must be upperCamelCase instead of snake-case.
* e.g. `{background-color: 'grey-300'}` --> `{backgroundColor: 'grey-300'}`
* @param {object} colorExpression json object, keys are css properties and values are string of the wanted color,
* for example: `{color: 'red-A200-0.3'}`.
*
* @usage
* <hljs lang="js">
Expand All @@ -78,16 +76,10 @@
* });
* </hljs>
*/
function applyThemeColors(element, scope, colorExpression) {
function applyThemeColors(element, colorExpression) {
try {
// Json.parse() does not work because the keys are not quoted;
// use $parse to convert to a hash map
// NOTE: keys cannot be snake-case, upperCamelCase are required
// e.g. {background-color: 'grey-300'} --> {backgroundColor: 'grey-300'}
var themeColors = $parse(colorExpression)(scope);

// Assign the calculate RGBA color values directly as inline CSS
element.css(interpolateColors(themeColors));
element.css(interpolateColors(colorExpression));
} catch( e ) {
$log.error(e.message);
}
Expand Down Expand Up @@ -265,25 +257,27 @@
* </hljs>
*
*/
function MdColorsDirective($mdColors, $mdUtil, $log) {
function MdColorsDirective($mdColors, $mdUtil, $log, $parse) {
return {
restrict: 'A',
compile: function (tElem, tAttrs) {
var shouldWatch = shouldColorsWatch();

return function (scope, element, attrs) {
var colorExpression = function () {
return attrs.mdColors;
// Json.parse() does not work because the keys are not quoted;
// use $parse to convert to a hash map
return $parse(attrs.mdColors)(scope);
};

try {
if (shouldWatch) {
scope.$watch(colorExpression, angular.bind(this,
$mdColors.applyThemeColors, element, scope
));
$mdColors.applyThemeColors, element
), true);
}
else {
$mdColors.applyThemeColors(element, scope, colorExpression());
$mdColors.applyThemeColors(element, colorExpression());
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/components/colors/colors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ describe('md-colors', function () {
var color = $mdColorPalette['red']['200'].value;
var expectedRGB = supplant('rgb({0}, {1}, {2})', [color[0], color[1], color[2]]);

$mdColors.applyThemeColors(element, scope, '{background: \'red-200\'}');
$mdColors.applyThemeColors(element, { background: 'red-200' });
expect(element[0].style.background).toContain( expectedRGB );
}));

Expand Down

0 comments on commit e9a1d4f

Please sign in to comment.