salix/front/core/components/field/index.js

220 lines
4.7 KiB
JavaScript
Raw Normal View History

import ngModule from '../../module';
2019-10-09 22:47:29 +00:00
import FormInput from '../form-input';
import './style.scss';
2019-10-09 22:47:29 +00:00
export default class Field extends FormInput {
2019-10-28 20:40:24 +00:00
constructor($element, $scope) {
super($element, $scope);
this.prefix = null;
this.suffix = null;
2019-10-09 22:47:29 +00:00
this.container = this.element.querySelector('.container');
2020-04-01 14:05:43 +00:00
this.container.addEventListener('focusout', () => this.onFocus(false));
this.container.addEventListener('focusin', () => this.onFocus(true));
this.control = this.element.querySelector('.control');
}
$onInit() {
2019-10-09 22:47:29 +00:00
super.$onInit();
if (this.info) this.classList.add('has-icons');
this.input.addEventListener('change', () => this.onChange());
}
set field(value) {
2019-10-11 15:38:04 +00:00
if (value === this.field) return;
2019-10-09 22:47:29 +00:00
super.field = value;
this.classList.toggle('not-empty', value != null && value !== '');
this.validateValue();
}
get field() {
2019-10-09 22:47:29 +00:00
return super.field;
}
set input(value) {
2019-10-18 19:36:30 +00:00
if (this.input)
this.control.removeChild(this.input);
2019-10-09 22:47:29 +00:00
this._input = value;
2019-10-18 19:36:30 +00:00
if (value)
this.control.appendChild(value);
2019-10-09 22:47:29 +00:00
}
get input() {
return this._input;
}
set value(value) {
2019-11-11 15:32:03 +00:00
this.input.value = value;
}
get value() {
return this.input.value;
}
set type(value) {
this.input.type = value;
}
get type() {
return this.input.type;
}
set name(value) {
this.input.name = value;
}
get name() {
return this.input.name;
}
2019-10-09 22:47:29 +00:00
set placeholder(value) {
this.input.placeholder = value;
}
get placeholder() {
return this.input.placeholder;
}
set required(value) {
2019-10-18 19:36:30 +00:00
this._required = value;
let required = this.element.querySelector('.required');
display(required, this._required);
}
get required() {
return this._required;
}
set prefix(value) {
this._prefix = value;
this.refreshFix('.prefix', value);
}
get prefix() {
return this._prefix;
}
set suffix(value) {
this._suffix = value;
this.refreshFix('.suffix', value);
}
get suffix() {
return this._suffix;
}
set hint(value) {
this._hint = value;
this.refreshHint();
}
get hint() {
return this._hint;
}
set error(value) {
2019-10-11 15:38:04 +00:00
if (value === this.error) return;
this._error = value;
this.refreshHint();
}
get error() {
return this._error;
}
2019-10-11 15:38:04 +00:00
get shownError() {
return this.error || this.inputError || null;
}
2020-03-10 13:28:50 +00:00
get autocomplete() {
return this._autocomplete;
}
set autocomplete(value) {
this._autocomplete = value;
2020-03-10 13:28:50 +00:00
2020-03-10 13:28:50 +00:00
if (value === 'off')
this.input.setAttribute('autocomplete', 'off');
}
refreshHint() {
2019-10-11 15:38:04 +00:00
let error = this.shownError;
let hint = error || this.hint;
let hintEl = this.element.querySelector('.hint');
2019-10-11 15:38:04 +00:00
hintEl.innerText = hint || '';
hintEl.classList.toggle('filled', Boolean(hint));
2019-10-11 15:38:04 +00:00
this.classList.toggle('invalid', Boolean(error));
}
refreshFix(selector, text) {
let fix = this.element.querySelector(selector);
display(fix, text);
fix.innerText = text || '';
}
onFocus(hasFocus) {
this.classList.toggle('focused', hasFocus);
}
onClear(event) {
if (event.defaultPrevented) return;
event.preventDefault();
this.field = null;
this.input.dispatchEvent(new Event('change'));
}
buildInput(type) {
2019-10-11 15:38:04 +00:00
let template = `<input type="${type}" ng-model="$ctrl.field"></input>`;
this.input = this.$compile(template)(this.$)[0];
}
/**
* If input value is invalid, sets the error message as hint.
*/
validateValue() {
2019-10-11 15:38:04 +00:00
let error = this.input.checkValidity()
? null
: this.input.validationMessage;
2019-10-11 15:38:04 +00:00
if (error === this.inputError) return;
this.inputError = error;
this.refreshHint();
}
onChange() {
this.emit('change', {value: this.field});
}
}
2019-10-28 20:40:24 +00:00
Field.$inject = ['$element', '$scope'];
ngModule.vnComponent('vnField', {
template: require('./index.html'),
transclude: {
prepend: '?prepend',
append: '?append'
},
controller: Field,
bindings: {
type: '@?',
2020-03-10 13:28:50 +00:00
autocomplete: '@?',
2019-10-09 22:47:29 +00:00
placeholder: '@?',
value: '=?',
info: '@?',
required: '<?',
prefix: '@?',
suffix: '@?',
hint: '@?',
error: '<?',
2019-10-11 15:38:04 +00:00
rule: '@?'
}
});
function display(element, display) {
element.style.display = display ? 'initial' : 'none';
}