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

Commit

Permalink
Merge branch 'release' of github.com:angular-ui/angular-ui into release
Browse files Browse the repository at this point in the history
* 'release' of github.com:angular-ui/angular-ui:
  Make maps test-script cross-browser
  Adding tests for uiAnimate global options and fixing bugs
  Fixing IE8 issues for uiAnimate
  Tests and fixes for multiple validators support in uiValidate
  uiAnimate cleanup
  • Loading branch information
Dean Sofer committed Aug 6, 2012
2 parents 0ff546f + d29774d commit e77cdfa
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 98 deletions.
12 changes: 5 additions & 7 deletions modules/directives/animate/animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,25 @@
angular.module('ui.directives').directive('uiAnimate', ['ui.config', '$timeout', function(uiConfig, $timeout) {
var options = {};
if (angular.isString(uiConfig.animate)) {
options.class = uiConfig.animate;
options['class'] = uiConfig.animate;
} else if (uiConfig.animate) {
options = uiConfig.animate;
}
return {
restrict: 'A', // supports using directive as element, attribute and class
link: function($scope, element, attrs) {
var opts = {
'class': 'ui-animate' // the CSS class to add during animation
};
var opts = {};
if (attrs.uiAnimate) {
opts = $scope.$eval(attrs.uiAnimate);
if (angular.isString(opts)) {
opts = {'class': opts};
}
}
opts = angular.extend({}, options, opts);
opts = angular.extend({'class': 'ui-animate'}, options, opts);

element.addClass(opts.class);
element.addClass(opts['class']);
$timeout(function(){
element.removeClass(opts.class);
element.removeClass(opts['class']);
}, false);
}
};
Expand Down
23 changes: 12 additions & 11 deletions modules/directives/animate/test/animateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('uiAnimate', function() {
// inject in angular constructs. Injector knows about leading/trailing underscores and does the right thing
// otherwise, you would need to inject these into each test
beforeEach(inject(function(_$rootScope_, _$compile_, _$timeout_) {
$rootScope = _$rootScope_;
$rootScope = _$rootScope_;
$compile = _$compile_;
$timeout = _$timeout_;
}));
Expand Down Expand Up @@ -46,24 +46,25 @@ describe('uiAnimate', function() {
});

});
xdescribe('global', function() {
describe('global', function() {

var uiConfig;
beforeEach(inject(function($injector){
uiConfig = $injector.get('ui.config');
}));

it('should use a string as the class', function() {
uiConfig.value('ui.config', {
'animate' : 'ui-hide-global'
});
it('should use a string as the class', function(){
uiConfig.animate = 'ui-hide-global';
var element = $compile('<div ui-animate></div>')($rootScope);
expect(element.hasClass('ui-hide-global')).toBeTruthy();
});

it('should use an object\'s class attribute as the class', function() {
uiConfig.value('ui.config', {
'animate' : { 'class' : 'ui-hide-global' }
});
uiConfig.animate = { 'class' : 'ui-hide-global' };
var element = $compile('<div ui-animate></div>')($rootScope);
expect(element.hasClass('ui-hide-global')).toBeTruthy();
});

});
});

Expand Down
54 changes: 32 additions & 22 deletions modules/directives/validate/test/validateSpec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
describe('uiValidate', function ($compile) {
var scope, compileAndDigest;

var trueValidator = function () {
return true;
};

var falseValidator = function () {
return false;
};

var passedValueValidator = function (valueToValidate) {
return valueToValidate;
};

beforeEach(module('ui'));
beforeEach(inject(function ($rootScope, $compile) {

scope = $rootScope.$new();
compileAndDigest = function (inputHtml, scope) {

var inputElm = angular.element(inputHtml);
var formElm = angular.element('<form name="form"></form>');
formElm.append(inputElm);
Expand All @@ -21,19 +32,15 @@ describe('uiValidate', function ($compile) {

it('should mark input as valid if initial model is valid', inject(function () {

scope.validate = function () {
return true
};
scope.validate = trueValidator;
compileAndDigest('<input name="input" ng-model="value" ui-validate="validate">', scope);
expect(scope.form.input.$valid).toBeTruthy();
expect(scope.form.input.$error).toEqual({validator : false});
}));

it('should mark input as invalid if initial model is invalid', inject(function () {

scope.validate = function () {
return false
};
scope.validate = falseValidator;
compileAndDigest('<input name="input" ng-model="value" ui-validate="validate">', scope);
expect(scope.form.input.$valid).toBeFalsy();
expect(scope.form.input.$error).toEqual({ validator : true });
Expand All @@ -45,9 +52,7 @@ describe('uiValidate', function ($compile) {
it('should change valid state in response to model changes', inject(function () {

scope.value = false;
scope.validate = function (valueToValidate) {
return valueToValidate;
};
scope.validate = passedValueValidator;
compileAndDigest('<input name="input" ng-model="value" ui-validate="validate">', scope);
expect(scope.form.input.$valid).toBeFalsy();

Expand All @@ -63,20 +68,32 @@ describe('uiValidate', function ($compile) {
sniffer = $sniffer;
}));

it('should change valid state in response to element events', inject(function () {
it('should change valid state in response to element events', function () {

scope.value = false;
scope.validate = function (valueToValidate) {
return valueToValidate;
};
scope.validate = passedValueValidator;
var inputElm = compileAndDigest('<input name="input" ng-model="value" ui-validate="validate">', scope);
expect(scope.form.input.$valid).toBeFalsy();

inputElm.val('true');
inputElm.trigger((sniffer.hasEvent('input') ? 'input' : 'change'));

expect(scope.form.input.$valid).toBeTruthy();
}));
});
});

describe('multiple validators with custom keys', function(){

it('should support multiple validators with custom keys', function(){

scope.validate1 = trueValidator;
scope.validate2 = falseValidator;

compileAndDigest('<input name="input" ng-model="value" ui-validate="{key1 : validate1, key2 : validate2}">', scope);
expect(scope.form.input.$valid).toBeFalsy();
expect(scope.form.input.$error.key1).toBeFalsy();
expect(scope.form.input.$error.key2).toBeTruthy();
});
});

describe('error cases', function () {
Expand All @@ -85,13 +102,6 @@ describe('uiValidate', function ($compile) {
compileAndDigest('<input name="input" ui-validate="validate">', scope);
}).toThrow(new Error('No controller: ngModel'));
}));

it('should fail with an exception if validate expression is not a function', inject(function () {
expect(function () {
compileAndDigest('<input name="input" ng-model="value" ui-validate="value">', scope);
}).toThrow(new Error('uiValidate expression "value" is not a function.'));
}));

it('should have no effect if validate expression is empty', inject(function () {
compileAndDigest('<input ng-model="value" ui-validate="">', scope);
}));
Expand Down
21 changes: 10 additions & 11 deletions modules/directives/validate/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
* General-purpose validator for ngModel.
* angular.js comes with several built-in validation mechanism for input fields (ngRequired, ngPattern etc.) but using
* an arbitrary validation function requires creation of a custom formatters and / or parsers.
* The ui-validate directive makes it easy to use any function defined in scope as a validator function. The validator
* function will trigger validation on both model and input changes.
* The ui-validate directive makes it easy to use any function(s) defined in scope as a validator function(s).
* A validator function will trigger validation on both model and input changes.
*
* @example <input ui-validate="myValidatorFunction">
* @example <input ui-validate="{foo : validateFoo, bar : validateBar}">
*
* @param ui-validate {string} The name of a function to be used as a validator. The function will get a value to be
* validates as its argument and should return true/false indicating a validation result.
* @param ui-validate {string|object literal} If strings is passed it should be a scope's function to be used as a validator.
* If an object literal is passed a key denotes a validation error key while a value should be a validator function.
* In both cases validator function should take a value to validate as its argument and should return true/false indicating a validation result.
*/
angular.module('ui.directives').directive('uiValidate', function () {

Expand All @@ -18,9 +20,8 @@ angular.module('ui.directives').directive('uiValidate', function () {
link:function (scope, elm, attrs, ctrl) {

var validateFn, validateExpr = attrs.uiValidate;

validateExpr = scope.$eval(validateExpr);

if (!validateExpr) {
return;
}
Expand All @@ -29,14 +30,12 @@ angular.module('ui.directives').directive('uiValidate', function () {
validateExpr = { validator: validateExpr };
}

angular.forEach(validateExpr, function(validator, key){
angular.forEach(validateExpr, function(validatorFn, key){
validateFn = function (valueToValidate) {
if (validateExpr(valueToValidate)) {
// it is valid
if (validatorFn(valueToValidate)) {
ctrl.$setValidity(key, true);
return valueToValidate;
} else {
// it is invalid, return undefined (no model update)
ctrl.$setValidity(key, false);
return undefined;
}
Expand All @@ -45,5 +44,5 @@ angular.module('ui.directives').directive('uiValidate', function () {
ctrl.$parsers.push(validateFn);
});
}
}
};
});
Loading

0 comments on commit e77cdfa

Please sign in to comment.