Skip to content

Commit

Permalink
fix: throw an error if a directive is used without ng-model
Browse files Browse the repository at this point in the history
  • Loading branch information
assisrafael committed Apr 7, 2015
1 parent 8154c77 commit 31c7d51
Show file tree
Hide file tree
Showing 20 changed files with 30 additions and 68 deletions.
6 changes: 1 addition & 5 deletions src/br/cep/cep.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ angular.module('ui.utils.masks.br.cep', [])

return {
restrict: 'A',
require: '?ngModel',
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
if (!ctrl) {
return;
}

ctrl.$formatters.push(function(value) {
return applyCepMask(value, ctrl);
});
Expand Down
4 changes: 2 additions & 2 deletions src/br/cep/cep.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
describe('ui-br-cep-mask', function() {
beforeEach(module('ui.utils.masks.br.cep'));

it('should not throw an error if used without ng-model', function() {
it('should throw an error if used without ng-model', function() {
expect(function() {
TestUtil.compile('<input ui-br-cep-mask>');
}).not.toThrow();
}).toThrow();
});

it('should register a $parser and a $formatter', function() {
Expand Down
4 changes: 2 additions & 2 deletions src/br/cpf-cnpj/cnpj.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
describe('ui-br-cnpj', function() {
beforeEach(module('ui.utils.masks.br.cpfCnpj'));

it('should not throw an error if used without ng-model', function() {
it('should throw an error if used without ng-model', function() {
expect(function() {
TestUtil.compile('<input ui-br-cnpj-mask>');
}).not.toThrow();
}).toThrow();
});

it('should register a $parser and a $formatter', function() {
Expand Down
18 changes: 3 additions & 15 deletions src/br/cpf-cnpj/cpf-cnpj.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@

return {
restrict: 'A',
require: '?ngModel',
require: 'ngModel',
link: function (scope, element, attrs, ctrl) {
if (!ctrl) {
return;
}

ctrl.$formatters.push(function(value) {
return applyCpfMask(validateCPF(ctrl, value));
});
Expand Down Expand Up @@ -80,12 +76,8 @@
}
return {
restrict: 'A',
require: '?ngModel',
require: 'ngModel',
link: function (scope, element, attrs, ctrl) {
if (!ctrl) {
return;
}

ctrl.$formatters.push(function(value) {
return applyCnpjMask(validateCNPJ(ctrl, value));
});
Expand Down Expand Up @@ -128,12 +120,8 @@
}
return {
restrict: 'A',
require: '?ngModel',
require: 'ngModel',
link: function (scope, element, attrs, ctrl) {
if (!ctrl) {
return;
}

ctrl.$formatters.push(function(value) {
return applyCpfCnpjMask(validateCPForCNPJ(ctrl, value));
});
Expand Down
4 changes: 2 additions & 2 deletions src/br/cpf-cnpj/cpf-cnpj.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
describe('ui-br-cpfcnpj', function() {
beforeEach(module('ui.utils.masks.br.cpfCnpj'));

it('should not throw an error if used without ng-model', function() {
it('should throw an error if used without ng-model', function() {
expect(function() {
TestUtil.compile('<input ui-br-cpfcnpj-mask>');
}).not.toThrow();
}).toThrow();
});

it('should register a $parser and a $formatter', function() {
Expand Down
4 changes: 2 additions & 2 deletions src/br/cpf-cnpj/cpf.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
describe('ui-br-cpf', function() {
beforeEach(module('ui.utils.masks.br.cpfCnpj'));

it('should not throw an error if used without ng-model', function() {
it('should throw an error if used without ng-model', function() {
expect(function() {
TestUtil.compile('<input ui-br-cpf-mask>');
}).not.toThrow();
}).toThrow();
});

it('should register a $parser and a $formatter', function() {
Expand Down
6 changes: 1 addition & 5 deletions src/br/inscricao-estadual/ie.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,10 @@ angular.module('ui.utils.masks.br.ie', [])

return {
restrict: 'A',
require: '?ngModel',
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
var state = $parse(attrs.uiBrIeMask)(scope);

if (!ctrl) {
return;
}

scope.$watch(attrs.uiBrIeMask, function(newState) {
state = newState;
applyIEMask(ctrl.$viewValue, state, ctrl);
Expand Down
4 changes: 2 additions & 2 deletions src/br/inscricao-estadual/ie.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
describe('ui-br-ie-mask', function() {
beforeEach(module('ui.utils.masks.br.ie'));

it('should not throw an error if used without ng-model', function() {
it('should throw an error if used without ng-model', function() {
expect(function() {
TestUtil.compile('<input ui-br-ie-mask="\'MG\'">');
}).not.toThrow();
}).toThrow();
});

it('should register a $parser and a $formatter', function() {
Expand Down
6 changes: 1 addition & 5 deletions src/br/phone/br-phone.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@ angular.module('ui.utils.masks.br.phone', [])

return {
restrict: 'A',
require: '?ngModel',
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
if (!ctrl) {
return;
}

ctrl.$formatters.push(function(value) {
return applyPhoneMask(PhoneValidators.brPhoneNumber(ctrl, value));
});
Expand Down
2 changes: 1 addition & 1 deletion src/br/phone/br-phone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('ui-br-phone-number', function() {
it('should throw an error if used without ng-model', function() {
expect(function() {
TestUtil.compile('<input ui-br-phone-number>');
}).not.toThrow();
}).toThrow();
});

it('should register a $parser and a $formatter', function() {
Expand Down
2 changes: 1 addition & 1 deletion src/global/date/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ angular.module('ui.utils.masks.global.date', dependencies)

return {
restrict: 'A',
require: '?ngModel',
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
var dateMask = new StringMask(dateFormat.replace(/[YMD]/g,'0'));

Expand Down
7 changes: 2 additions & 5 deletions src/global/money/money.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,21 @@ angular.module('ui.utils.masks.global.money', [
function ($locale, $parse, PreFormatters, NumberValidators) {
return {
restrict: 'A',
require: '?ngModel',
require: 'ngModel',
link: function (scope, element, attrs, ctrl) {
var decimalDelimiter = $locale.NUMBER_FORMATS.DECIMAL_SEP,
thousandsDelimiter = $locale.NUMBER_FORMATS.GROUP_SEP,
currencySym = $locale.NUMBER_FORMATS.CURRENCY_SYM,
decimals = parseInt(attrs.uiMoneyMask);

if (!ctrl) {
return;
}

if (angular.isDefined(attrs.uiHideGroupSep)){
thousandsDelimiter = '';
}

if(isNaN(decimals)) {
decimals = 2;
}

var decimalsPattern = decimals > 0 ? decimalDelimiter + new Array(decimals + 1).join('0') : '';
var maskPattern = currencySym+' #'+thousandsDelimiter+'##0'+decimalsPattern;
var moneyMask = new StringMask(maskPattern, {reverse: true});
Expand Down
2 changes: 1 addition & 1 deletion src/global/money/money.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('ui-money-mask', function() {
it('should throw an error if used without ng-model', function() {
expect(function() {
TestUtil.compile('<input ui-money-mask>');
}).not.toThrow();
}).toThrow();
});

it('should register a $parser and a $formatter', function() {
Expand Down
7 changes: 2 additions & 5 deletions src/global/number/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@ angular.module('ui.utils.masks.global.number', [
function ($locale, $parse, PreFormatters, NumberMasks, NumberValidators) {
return {
restrict: 'A',
require: '?ngModel',
require: 'ngModel',
link: function (scope, element, attrs, ctrl) {
var decimalDelimiter = $locale.NUMBER_FORMATS.DECIMAL_SEP,
thousandsDelimiter = $locale.NUMBER_FORMATS.GROUP_SEP,
decimals = $parse(attrs.uiNumberMask)(scope);

if (!ctrl) {
return;
}

if (angular.isDefined(attrs.uiHideGroupSep)){
thousandsDelimiter = '';
}

if(isNaN(decimals)) {
decimals = 2;
}

var viewMask = NumberMasks.viewMask(decimals, decimalDelimiter, thousandsDelimiter),
modelMask = NumberMasks.modelMask(decimals);

Expand Down
2 changes: 1 addition & 1 deletion src/global/number/number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('ui-number-mask', function() {
it('should throw an error if used without ng-model', function() {
expect(function() {
TestUtil.compile('<input ui-number-mask>');
}).not.toThrow();
}).toThrow();
});

it('should register a $parser and a $formatter', function() {
Expand Down
6 changes: 1 addition & 5 deletions src/global/percentage/percentage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ angular.module('ui.utils.masks.global.percentage', [

return {
restrict: 'A',
require: '?ngModel',
require: 'ngModel',
link: function (scope, element, attrs, ctrl) {
var decimalDelimiter = $locale.NUMBER_FORMATS.DECIMAL_SEP,
thousandsDelimiter = $locale.NUMBER_FORMATS.GROUP_SEP,
decimals = parseInt(attrs.uiPercentageMask);

if (!ctrl) {
return;
}

if (angular.isDefined(attrs.uiHideGroupSep)){
thousandsDelimiter = '';
}
Expand Down
2 changes: 1 addition & 1 deletion src/global/percentage/percentage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('ui-percentage-mask', function() {
it('should throw an error if used without ng-model', function() {
expect(function() {
TestUtil.compile('<input ui-percentage-mask>');
}).not.toThrow();
}).toThrow();
});

it('should register a $parser and a $formatter', function() {
Expand Down
2 changes: 1 addition & 1 deletion src/global/time/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ angular.module('ui.utils.masks.global.time', [])

return {
restrict: 'A',
require: '?ngModel',
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
var unformattedValueLength = 6,
formattedValueLength = 8,
Expand Down
6 changes: 1 addition & 5 deletions src/us/phone/us-phone.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ angular.module('ui.utils.masks.us.phone', [])

return {
restrict: 'A',
require: '?ngModel',
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
if (!ctrl) {
return;
}

ctrl.$formatters.push(function(value) {
return applyPhoneMask(usPhoneValidators.usPhoneNumber(ctrl, value));
});
Expand Down
4 changes: 2 additions & 2 deletions src/us/phone/us-phone.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
describe('ui-us-phone-mask', function() {
beforeEach(module('ui.utils.masks.us.phone'));

it('should not throw an error if used without ng-model', function() {
it('should throw an error if used without ng-model', function() {
expect(function() {
TestUtil.compile('<input ui-us-phone-number>');
}).not.toThrow();
}).toThrow();
});

it('should register a $formatter', function() {
Expand Down

0 comments on commit 31c7d51

Please sign in to comment.