2019-10-04 10:33:16 +00:00
|
|
|
import ngModule from '../../module';
|
2019-10-09 22:47:29 +00:00
|
|
|
import FormInput from '../form-input';
|
2019-10-04 10:33:16 +00:00
|
|
|
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) {
|
2019-10-04 10:33:16 +00:00
|
|
|
super($element, $scope);
|
2019-10-08 21:57:02 +00:00
|
|
|
|
2019-10-04 10:33:16 +00:00
|
|
|
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');
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$onInit() {
|
2019-10-09 22:47:29 +00:00
|
|
|
super.$onInit();
|
|
|
|
|
2019-10-04 10:33:16 +00:00
|
|
|
if (this.info) this.classList.add('has-icons');
|
2021-01-26 06:21:05 +00:00
|
|
|
this.input.addEventListener('change', event =>
|
|
|
|
this.onChange(event));
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-10-04 10:33:16 +00:00
|
|
|
this.classList.toggle('not-empty', value != null && value !== '');
|
2019-10-08 21:57:02 +00:00
|
|
|
this.validateValue();
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 21:57:02 +00:00
|
|
|
set value(value) {
|
2019-11-11 15:32:03 +00:00
|
|
|
this.input.value = value;
|
2019-10-08 21:57:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get value() {
|
|
|
|
return this.input.value;
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:33:16 +00:00
|
|
|
set type(value) {
|
|
|
|
this.input.type = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get type() {
|
|
|
|
return this.input.type;
|
|
|
|
}
|
|
|
|
|
2019-10-08 21:57:02 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:33:16 +00:00
|
|
|
set required(value) {
|
2019-10-18 19:36:30 +00:00
|
|
|
this._required = value;
|
2019-10-04 10:33:16 +00:00
|
|
|
let required = this.element.querySelector('.required');
|
|
|
|
display(required, this._required);
|
2021-03-10 08:01:32 +00:00
|
|
|
|
|
|
|
this.$.$applyAsync(() =>
|
|
|
|
this.input.setAttribute('required', value));
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-10-04 10:33:16 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:33:16 +00:00
|
|
|
refreshHint() {
|
2019-10-11 15:38:04 +00:00
|
|
|
let error = this.shownError;
|
|
|
|
let hint = error || this.hint;
|
|
|
|
|
2019-10-04 10:33:16 +00:00
|
|
|
let hintEl = this.element.querySelector('.hint');
|
2019-10-11 15:38:04 +00:00
|
|
|
hintEl.innerText = hint || '';
|
2019-10-04 10:33:16 +00:00
|
|
|
hintEl.classList.toggle('filled', Boolean(hint));
|
2019-10-11 15:38:04 +00:00
|
|
|
|
|
|
|
this.classList.toggle('invalid', Boolean(error));
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
refreshFix(selector, text) {
|
|
|
|
let fix = this.element.querySelector(selector);
|
|
|
|
display(fix, text);
|
|
|
|
fix.innerText = text || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
onFocus(hasFocus) {
|
|
|
|
this.classList.toggle('focused', hasFocus);
|
|
|
|
}
|
|
|
|
|
2019-10-08 21:57:02 +00:00
|
|
|
onClear(event) {
|
|
|
|
if (event.defaultPrevented) return;
|
2019-10-28 16:31:33 +00:00
|
|
|
event.preventDefault();
|
2019-10-08 21:57:02 +00:00
|
|
|
this.field = null;
|
2019-10-04 10:33:16 +00:00
|
|
|
this.input.dispatchEvent(new Event('change'));
|
|
|
|
}
|
|
|
|
|
2019-10-08 21:57:02 +00:00
|
|
|
buildInput(type) {
|
2019-10-11 15:38:04 +00:00
|
|
|
let template = `<input type="${type}" ng-model="$ctrl.field"></input>`;
|
2019-10-08 21:57:02 +00:00
|
|
|
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()
|
2019-10-08 21:57:02 +00:00
|
|
|
? null
|
|
|
|
: this.input.validationMessage;
|
2019-10-11 15:38:04 +00:00
|
|
|
|
|
|
|
if (error === this.inputError) return;
|
|
|
|
this.inputError = error;
|
|
|
|
this.refreshHint();
|
2019-10-08 21:57:02 +00:00
|
|
|
}
|
2019-10-28 16:31:33 +00:00
|
|
|
|
2021-01-26 06:21:05 +00:00
|
|
|
onChange($event) {
|
2021-01-11 07:48:53 +00:00
|
|
|
// Changes doesn't reflect until appling async
|
|
|
|
this.$.$applyAsync(() => {
|
2021-01-26 06:21:05 +00:00
|
|
|
this.emit('change', {
|
|
|
|
value: this.field,
|
|
|
|
$event: $event
|
|
|
|
});
|
2021-01-11 07:48:53 +00:00
|
|
|
});
|
2019-10-28 16:31:33 +00:00
|
|
|
}
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
2019-10-28 20:40:24 +00:00
|
|
|
Field.$inject = ['$element', '$scope'];
|
2019-10-04 10:33:16 +00:00
|
|
|
|
2019-10-08 21:57:02 +00:00
|
|
|
ngModule.vnComponent('vnField', {
|
2019-10-04 10:33:16 +00:00
|
|
|
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: '@?',
|
2020-06-05 11:14:14 +00:00
|
|
|
clearDisabled: '<?',
|
2019-10-08 21:57:02 +00:00
|
|
|
value: '=?',
|
2019-10-04 10:33:16 +00:00
|
|
|
info: '@?',
|
2019-10-08 21:57:02 +00:00
|
|
|
required: '<?',
|
2019-10-04 10:33:16 +00:00
|
|
|
prefix: '@?',
|
|
|
|
suffix: '@?',
|
|
|
|
hint: '@?',
|
2019-10-08 21:57:02 +00:00
|
|
|
error: '<?',
|
2019-10-11 15:38:04 +00:00
|
|
|
rule: '@?'
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function display(element, display) {
|
|
|
|
element.style.display = display ? 'initial' : 'none';
|
|
|
|
}
|