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-08 21:57:02 +00:00
|
|
|
constructor($element, $scope, $compile) {
|
2019-10-04 10:33:16 +00:00
|
|
|
super($element, $scope);
|
2019-10-08 21:57:02 +00:00
|
|
|
this.$compile = $compile;
|
|
|
|
|
2019-10-04 10:33:16 +00:00
|
|
|
this.prefix = null;
|
|
|
|
this.suffix = null;
|
|
|
|
|
2019-10-08 21:57:02 +00:00
|
|
|
this.control = this.element.querySelector('.control');
|
2019-10-04 10:33:16 +00:00
|
|
|
this.classList = this.element.classList;
|
|
|
|
this.classList.add('vn-field');
|
2019-10-08 21:57:02 +00:00
|
|
|
this.element.addEventListener('click', e => this.onClick(e));
|
2019-10-04 10:33:16 +00:00
|
|
|
|
2019-10-09 22:47:29 +00:00
|
|
|
this.container = this.element.querySelector('.container');
|
|
|
|
this.container.addEventListener('mousedown', e => this.onMouseDown(e));
|
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');
|
2019-10-08 21:57:02 +00:00
|
|
|
|
|
|
|
this.input.addEventListener('focus', () => this.onFocus(true));
|
|
|
|
this.input.addEventListener('blur', () => this.onFocus(false));
|
|
|
|
this.input.addEventListener('change', e => {
|
|
|
|
this.emit('change', {event: e});
|
|
|
|
});
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set field(value) {
|
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) {
|
|
|
|
if (this.input) this.control.removeChild(this.input);
|
|
|
|
this._input = value;
|
|
|
|
this.control.appendChild(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
get input() {
|
|
|
|
return this._input;
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 21:57:02 +00:00
|
|
|
set value(value) {
|
|
|
|
this.field = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
set tabIndex(value) {
|
|
|
|
this.input.tabIndex = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get tabIndex() {
|
|
|
|
return this.input.tabIndex;
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:33:16 +00:00
|
|
|
set disabled(value) {
|
|
|
|
this._disabled = boolTag(value);
|
|
|
|
this.input.disabled = this._disabled;
|
|
|
|
this.classList.toggle('disabled', this._disabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 required(value) {
|
|
|
|
this._required = boolTag(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) {
|
|
|
|
this._error = value;
|
|
|
|
this.refreshHint();
|
|
|
|
this.classList.toggle('invalid', Boolean(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
get error() {
|
|
|
|
return this._error;
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshHint() {
|
|
|
|
let hint = this.error || this.hint || '';
|
|
|
|
let hintEl = this.element.querySelector('.hint');
|
|
|
|
hintEl.innerText = hint;
|
|
|
|
hintEl.classList.toggle('filled', Boolean(hint));
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshFix(selector, text) {
|
|
|
|
let fix = this.element.querySelector(selector);
|
|
|
|
display(fix, text);
|
|
|
|
fix.innerText = text || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
onClick() {
|
2019-10-08 21:57:02 +00:00
|
|
|
if (event.defaultPrevented) return;
|
|
|
|
event.preventDefault();
|
|
|
|
|
2019-10-04 10:33:16 +00:00
|
|
|
if (this.input !== document.activeElement)
|
2019-10-08 21:57:02 +00:00
|
|
|
this.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseDown(event) {
|
|
|
|
if (event.target == this.input) return;
|
|
|
|
event.preventDefault();
|
|
|
|
this.focus();
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onFocus(hasFocus) {
|
|
|
|
this.classList.toggle('focused', hasFocus);
|
|
|
|
}
|
|
|
|
|
2019-10-08 21:57:02 +00:00
|
|
|
onClear(event) {
|
|
|
|
if (event.defaultPrevented) return;
|
|
|
|
event.preventDefault();
|
|
|
|
this.field = null;
|
2019-10-04 10:33:16 +00:00
|
|
|
this.input.dispatchEvent(new Event('change'));
|
|
|
|
}
|
|
|
|
|
|
|
|
focus() {
|
|
|
|
this.input.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
select() {
|
|
|
|
this.input.select();
|
|
|
|
}
|
2019-10-08 21:57:02 +00:00
|
|
|
|
|
|
|
buildInput(type) {
|
2019-10-09 22:47:29 +00:00
|
|
|
let template = `<input ng-model="$ctrl.field"></input>`;
|
2019-10-08 21:57:02 +00:00
|
|
|
this.input = this.$compile(template)(this.$)[0];
|
2019-10-09 22:47:29 +00:00
|
|
|
this.type = type;
|
2019-10-08 21:57:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If input value is invalid, sets the error message as hint.
|
|
|
|
*/
|
|
|
|
validateValue() {
|
|
|
|
this.error = this.input.checkValidity()
|
|
|
|
? null
|
|
|
|
: this.input.validationMessage;
|
|
|
|
}
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
2019-10-08 21:57:02 +00:00
|
|
|
Field.$inject = ['$element', '$scope', '$compile'];
|
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: '@?',
|
2019-10-09 22:47:29 +00:00
|
|
|
placeholder: '@?',
|
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-09 22:47:29 +00:00
|
|
|
tabIndex: '<?',
|
|
|
|
onChange: '&?', // FIXME: Not implemented, nesessary?
|
|
|
|
rule: '@?' // FIXME: Not implemented
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function boolTag(value) {
|
|
|
|
return Boolean(value || value === '');
|
|
|
|
}
|
|
|
|
|
|
|
|
function display(element, display) {
|
|
|
|
element.style.display = display ? 'initial' : 'none';
|
|
|
|
}
|