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-28 16:31:33 +00:00
|
|
|
this.classList.add(...this.constructor.$classNames);
|
2019-10-18 19:36:30 +00:00
|
|
|
}
|
|
|
|
|
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.classList.toggle('disabled', this._disabled);
|
2019-10-23 15:38:35 +00:00
|
|
|
if (this.input)
|
|
|
|
this.input.disabled = this._disabled;
|
2019-10-18 19:36:30 +00:00
|
|
|
this.refreshTabIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
get disabled() {
|
|
|
|
return this._disabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
set readonly(value) {
|
|
|
|
this._readonly = boolTag(value);
|
|
|
|
this.classList.toggle('readonly', this._readonly);
|
2019-10-23 15:38:35 +00:00
|
|
|
if (this.input)
|
|
|
|
this.input.readOnly = this._readonly;
|
2019-10-18 19:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get readonly() {
|
|
|
|
return this._readonly;
|
|
|
|
}
|
|
|
|
|
|
|
|
set tabIndex(value) {
|
|
|
|
this._tabIndex = value;
|
|
|
|
this.refreshTabIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
get tabIndex() {
|
|
|
|
return this._tabIndex;
|
|
|
|
}
|
|
|
|
|
2019-10-23 15:38:35 +00:00
|
|
|
get inputEl() {
|
|
|
|
return this.input || this.element;
|
|
|
|
}
|
|
|
|
|
|
|
|
get editable() {
|
|
|
|
return !(this.readonly || this.disabled);
|
|
|
|
}
|
|
|
|
|
2019-10-18 19:36:30 +00:00
|
|
|
select() {
|
2019-10-24 08:17:32 +00:00
|
|
|
if (this.inputEl.select)
|
|
|
|
this.inputEl.select();
|
2019-10-18 19:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
focus() {
|
2019-10-23 15:38:35 +00:00
|
|
|
this.inputEl.focus();
|
2019-10-18 19:36:30 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 08:17:32 +00:00
|
|
|
initTabIndex() {
|
|
|
|
if (!this.element.hasAttribute('tabindex'))
|
|
|
|
this.element.tabIndex = 0;
|
|
|
|
}
|
|
|
|
|
2019-10-18 19:36:30 +00:00
|
|
|
refreshTabIndex() {
|
2019-10-23 15:38:35 +00:00
|
|
|
this.inputEl.tabIndex = this.disabled ? -1 : this.tabIndex;
|
2019-10-18 19:36:30 +00:00
|
|
|
}
|
2019-10-28 16:31:33 +00:00
|
|
|
|
|
|
|
change(value) {
|
|
|
|
this.field = value;
|
|
|
|
this.element.dispatchEvent(new Event('change'));
|
|
|
|
this.emit('change', {value: this.field});
|
|
|
|
}
|
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 === '');
|
|
|
|
}
|