Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support color values also in the middle of the variable string #256

Merged
merged 1 commit into from
Nov 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});