salix/front/core/components/input-number/index.js

106 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-09-13 09:27:38 +00:00
import ngModule from '../../module';
import Textfield from '../textfield/textfield';
import './style.scss';
export default class InputNumber extends Textfield {
constructor($element, $scope, $attrs, vnTemplate, $transclude) {
super($element, $scope, $attrs, vnTemplate, $transclude);
this.displayControls = true;
2018-09-13 09:27:38 +00:00
this.input.addEventListener('change', () => {
this.validateValue();
});
}
get value() {
return this._value;
}
set value(value) {
this._value = value;
this.hasValue = this._value !== null;
if (this.hasValue) this.element.classList.add('not-empty');
else this.element.classList.remove('not-empty');
this.element.querySelector('.infix').classList.remove('invalid', 'validated');
}
get max() {
return this.input.max;
}
set max(value) {
if (value)
this.input.max = value;
}
get min() {
return this.input.min;
}
set min(value) {
if (!value) value = 0;
this.input.min = value;
}
get step() {
2018-09-19 13:05:07 +00:00
return parseFloat(this.input.step);
2018-09-13 09:27:38 +00:00
}
set step(value) {
this.input.step = value;
}
validateValue() {
if ((this.validate() !== undefined && !this.validate()) ||
(this.max && this.value > this.max) ||
(this.min && this.value < this.min) ||
(this.displayControls && (this.step && this.value % this.step != 0)))
2018-09-13 09:27:38 +00:00
this.$element[0].querySelector('.infix').classList.add('invalid', 'validated');
2019-02-25 09:03:50 +00:00
2018-09-13 09:27:38 +00:00
if (this.onChange)
this.onChange();
}
add() {
2019-02-25 09:03:50 +00:00
if (this.step && this.value % this.step != 0)
2018-09-13 09:27:38 +00:00
this.value += (this.step - this.value % this.step);
2019-02-25 09:03:50 +00:00
else
2018-09-13 09:27:38 +00:00
this.value += this.step;
2019-02-25 09:03:50 +00:00
2018-09-13 09:27:38 +00:00
this.validateValue();
}
remove() {
2019-02-25 09:03:50 +00:00
if (this.step && this.value % this.step != 0)
2018-09-13 09:27:38 +00:00
this.value -= (this.step + this.value % this.step);
2019-02-25 09:03:50 +00:00
else
2018-09-13 09:27:38 +00:00
this.value -= this.step;
2019-02-25 09:03:50 +00:00
2018-09-13 09:27:38 +00:00
this.validateValue();
}
}
InputNumber.$inject = ['$element', '$scope', '$attrs', 'vnTemplate', '$transclude'];
ngModule.component('vnInputNumber', {
template: require('./index.html'),
controller: InputNumber,
bindings: {
label: '@?',
name: '@?',
2018-09-13 09:27:38 +00:00
disabled: '<?',
min: '<?',
max: '<?',
step: '<?',
displayControls: '<?',
2018-09-13 09:27:38 +00:00
rule: '@?',
value: '=model',
validate: '&',
onChange: '&',
onClear: '&'
}
});