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

111 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-10-09 22:47:29 +00:00
import ngModule from '../../module';
import Component from '../../lib/component';
/**
* Base component for form inputs.
*
* @property {String} label Label to display along the component
* @property {any} field The value with which the element is linked
* @property {Boolean} disabled Put component in disabled mode
*/
export default class FormInput extends Component {
2019-10-18 19:36:30 +00:00
constructor($element, $scope) {
super($element, $scope);
this.classList = this.element.classList;
}
2019-10-09 22:47:29 +00:00
$onInit() {
// XXX: Compatibility with old inputs
let attrs = this.$element[0].attributes;
if (!this.name && attrs['ng-model']) {
let split = attrs['ng-model'].nodeValue.split('.');
this.name = split[split.length - 1];
}
if (!this.ngModel) return;
this.ngModel.$render = () => {
this.field = this.ngModel.$viewValue;
};
}
set field(value) {
this._field = value;
if (this.ngModel)
this.ngModel.$setViewValue(value);
}
get field() {
return this._field;
}
set name(value) {
this.element.setAttribute('name', value);
}
get name() {
return this.element.getAttribute('name');
}
2019-10-18 19:36:30 +00:00
set disabled(value) {
this._disabled = boolTag(value);
this.input.disabled = this._disabled;
this.classList.toggle('disabled', this._disabled);
this.refreshTabIndex();
}
get disabled() {
return this._disabled;
}
set readonly(value) {
this._readonly = boolTag(value);
this.input.readOnly = this._readonly;
this.classList.toggle('readonly', this._readonly);
}
get readonly() {
return this._readonly;
}
set tabIndex(value) {
this._tabIndex = value;
this.refreshTabIndex();
}
get tabIndex() {
return this._tabIndex;
}
select() {
this.input.select();
}
focus() {
this.input.focus();
}
refreshTabIndex() {
this.element.tabIndex = this.disabled ? -1 : this.tabIndex;
}
2019-10-09 22:47:29 +00:00
}
ngModule.vnComponent('vnFormInput', {
controller: FormInput,
bindings: {
label: '@?',
field: '=?',
name: '@?',
disabled: '<?',
2019-10-18 19:36:30 +00:00
readonly: '<?',
tabIndex: '<?'
2019-10-09 22:47:29 +00:00
},
require: {
ngModel: '?ngModel'
}
});
2019-10-18 19:36:30 +00:00
function boolTag(value) {
return Boolean(value || value === '');
}