Skip to content

Commit

Permalink
Support color values in the middle of the variable string
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannu Pelkonen committed Nov 24, 2014
1 parent 60a9bc4 commit e7bad9f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
24 changes: 21 additions & 3 deletions lib/app/js/directives/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,28 @@ angular.module('sgApp')
restrict: 'A',
templateUrl: 'views/partials/variable.html',
link: function(scope) {
scope.isColor = function(value) {
if (/^(#|rgb)/.test(value)) return true;
return false;
var colorRegex = /#[0-9a-f]{3,6}/i;
scope.color = {};
scope.hasColor = function(value) {
return colorRegex.test(value);
}

// Parse first color from the string
scope.$watch(function() {
return scope.variable.value;
}, function(newVal, oldVal) {
var match = colorRegex.exec(newVal);
if (match) {
scope.color.value = match[0];
}
});

// Set changed color back to the string
scope.$watch(function() {
return scope.color.value;
}, function(newVal, oldVal) {
scope.variable.value = scope.variable.value.replace(colorRegex, newVal);
});
}
};
});
2 changes: 1 addition & 1 deletion lib/app/views/partials/variable.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
<label>{{ variable.name }}<i ng-if="variable.dirty" class="fa fa-asterisk"></i></label>
<input class="sg" type="color" ng-if="isColor(variable.value)" ng-model="variable.value">
<input class="sg" type="color" ng-if="hasColor(variable.value)" ng-model="color.value">
<input class="sg" type="text" ng-model="variable.value">
</div>
54 changes: 54 additions & 0 deletions test/angular/directives/variable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
describe('Variable directive', function() {
var $scope, scope, elem, directive, linkFn, html;

beforeEach(module('sgApp'));

beforeEach(function() {
module(function($provide) {
$provide.value('Styleguide', {});
$provide.value('Variables', {
init: function() {}
});
});
});

beforeEach(function() {
html = '<div sg-variable></div>';

inject(function($compile, $rootScope, $templateCache) {
$templateCache.put('views/partials/variable.html', '<div></div>');
$scope = $rootScope.$new();
$scope.variable = {
value: '#000000'
};
elem = angular.element(html);
$compile(elem)($scope);
$scope.$apply();
});

});

it('should have correct default value', function() {
expect($scope.color.value).to.eql('#000000');
});

it('should parse color value correctly from the string', function() {
$scope.variable.value = 'test #ff0000 !default';
$scope.$apply();
expect($scope.color.value).to.eql('#ff0000');
});

it('should parse only the first color if string has multiple colors', function() {
$scope.variable.value = 'test #ff0000 #cccccc !default';
$scope.$apply();
expect($scope.color.value).to.eql('#ff0000');
});

it('replace color in the original string correctly', function() {
$scope.variable.value = 'test #ff0000 !default';
$scope.$apply();
$scope.color.value = '#cccccc';
$scope.$apply();
expect($scope.variable.value).to.eql('test #cccccc !default');
});
});

0 comments on commit e7bad9f

Please sign in to comment.