diff --git a/front/core/components/textarea/index.js b/front/core/components/textarea/index.js index 8fcae7b0f..1954b12c6 100644 --- a/front/core/components/textarea/index.js +++ b/front/core/components/textarea/index.js @@ -16,11 +16,21 @@ export default class Textarea extends Field { get rows() { return this.input.rows; } + + set maxlength(value) { + let length = typeof value == 'number' && value > 1 ? value : 50; + this.input.setAttribute('maxlength', length); + } + + get maxlength() { + return this.input.getAttribute('maxlength', length); + } } ngModule.vnComponent('vnTextarea', { controller: Textarea, bindings: { - rows: ' { expect($ctrl.rows).toEqual(3); }); }); + + describe('maxlength() setter', () => { + it(`should set maxlength property of the element to the given value if it's a valid number`, () => { + $ctrl.maxlength = 100; + + expect($ctrl.maxlength).toEqual('100'); + }); + + it(`should set maxlength property of the element to 3 if the given value if it's null`, () => { + $ctrl.maxlength = null; + + expect($ctrl.maxlength).toEqual('50'); + }); + + it(`should set maxlength property of the element to 3 if the given value if it's not a valid number`, () => { + $ctrl.maxlength = 'a'; + + expect($ctrl.maxlength).toEqual('50'); + }); + }); }); diff --git a/modules/client/front/sms/index.html b/modules/client/front/sms/index.html index d968c1d05..ac7a20651 100644 --- a/modules/client/front/sms/index.html +++ b/modules/client/front/sms/index.html @@ -13,12 +13,19 @@ + + + {{'Characters remaining' | translate}}: {{$ctrl.charactersRemaining()}} + + diff --git a/modules/client/front/sms/index.js b/modules/client/front/sms/index.js index 580a02d27..b68171ce5 100644 --- a/modules/client/front/sms/index.js +++ b/modules/client/front/sms/index.js @@ -16,6 +16,16 @@ class Controller extends Component { this.$scope.SMSDialog.show(); } + charactersRemaining() { + let elementMaxLength; + let textAreaLength; + const element = this.$scope.message; + + textAreaLength = element.input.textLength; + elementMaxLength = element.maxlength; + return elementMaxLength - textAreaLength; + } + onResponse(response) { if (response === 'accept') { this.$http.post(`Sms/send`, this.sms).then(res => { diff --git a/modules/client/front/sms/index.spec.js b/modules/client/front/sms/index.spec.js index bda1cc013..6018825e9 100644 --- a/modules/client/front/sms/index.spec.js +++ b/modules/client/front/sms/index.spec.js @@ -8,10 +8,11 @@ describe('Client', () => { beforeEach(ngModule('client')); - beforeEach(angular.mock.inject(($componentController, _$httpBackend_) => { + beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_) => { $httpBackend = _$httpBackend_; + let $scope = $rootScope.$new(); $element = angular.element(''); - controller = $componentController('vnClientSms', {$element}); + controller = $componentController('vnClientSms', {$element, $scope}); controller.client = {id: 101}; })); @@ -30,5 +31,20 @@ describe('Client', () => { expect(controller.vnApp.showMessage).toHaveBeenCalledWith('SMS sent!'); }); }); + + describe('charactersRemaining()', () => { + it('should return the characters remaining in a element', () => { + controller.$scope.message = { + input: { + textLength: 50 + }, + maxlength: 150 + }; + + let result = controller.charactersRemaining(); + + expect(result).toEqual(100); + }); + }); }); }); diff --git a/modules/client/front/sms/locale/es.yml b/modules/client/front/sms/locale/es.yml index 10247b926..f26c8ba24 100644 --- a/modules/client/front/sms/locale/es.yml +++ b/modules/client/front/sms/locale/es.yml @@ -1,4 +1,5 @@ Send SMS: Enviar SMS Destination: Destinatario Message: Mensaje -SMS sent!: ¡SMS enviado! \ No newline at end of file +SMS sent!: ¡SMS enviado! +Characters remaining: Carácteres restantes \ No newline at end of file