2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../../module';
|
|
|
|
import Input from '../../lib/input';
|
2017-04-28 13:04:29 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
2017-11-16 13:30:17 +00:00
|
|
|
export default class Textfield extends Input {
|
2018-09-14 07:10:30 +00:00
|
|
|
constructor($element, $scope, $attrs, vnTemplate) {
|
2018-02-12 12:16:49 +00:00
|
|
|
super($element, $scope);
|
2018-02-10 15:18:01 +00:00
|
|
|
vnTemplate.normalizeInputAttrs($attrs);
|
2017-09-12 10:45:29 +00:00
|
|
|
this._value = null;
|
2018-06-28 13:54:54 +00:00
|
|
|
this.type = $attrs.type;
|
2017-09-13 07:45:42 +00:00
|
|
|
this.showActions = false;
|
2017-11-16 13:30:17 +00:00
|
|
|
this.hasInfo = Boolean($attrs.info);
|
2017-09-27 10:27:18 +00:00
|
|
|
this.hasFocus = false;
|
|
|
|
this.hasMouseIn = false;
|
2018-06-28 13:54:54 +00:00
|
|
|
|
2018-07-18 07:31:45 +00:00
|
|
|
this.input.addEventListener('keyup', e => {
|
2019-02-26 16:33:51 +00:00
|
|
|
if (e.defaultPrevented || e.key != 'Escape')
|
|
|
|
return;
|
2018-09-14 07:10:30 +00:00
|
|
|
|
2019-02-26 16:33:51 +00:00
|
|
|
this.value = this.oldValue;
|
|
|
|
this.cancelled = true;
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
this.input.addEventListener('change', e => {
|
2018-11-26 08:53:34 +00:00
|
|
|
if (this.onChange && !this.cancelled && (this.oldValue != this.value)) {
|
2018-08-02 13:31:01 +00:00
|
|
|
this.onChange();
|
2018-11-26 08:53:34 +00:00
|
|
|
this.saveOldValue();
|
|
|
|
} else
|
2018-08-06 12:09:55 +00:00
|
|
|
this.cancelled = false;
|
2018-07-18 07:31:45 +00:00
|
|
|
});
|
2018-06-20 06:41:59 +00:00
|
|
|
}
|
2018-09-14 07:10:30 +00:00
|
|
|
|
2018-11-26 08:53:34 +00:00
|
|
|
$postLink() {
|
|
|
|
this.saveOldValue();
|
|
|
|
}
|
|
|
|
|
2018-07-18 07:31:45 +00:00
|
|
|
saveOldValue() {
|
2018-08-06 12:09:55 +00:00
|
|
|
this.oldValue = this.value;
|
2018-07-18 07:31:45 +00:00
|
|
|
}
|
2018-09-14 07:10:30 +00:00
|
|
|
|
2017-09-12 10:45:29 +00:00
|
|
|
set value(value) {
|
|
|
|
this._value = (value === undefined || value === '') ? null : value;
|
2017-09-13 07:45:42 +00:00
|
|
|
this.input.value = this._value;
|
2018-06-28 13:54:54 +00:00
|
|
|
this.hasValue = this._value !== null;
|
2018-06-21 07:12:35 +00:00
|
|
|
|
2018-06-28 13:54:54 +00:00
|
|
|
if (this.hasValue) this.element.classList.add('not-empty');
|
|
|
|
else this.element.classList.remove('not-empty');
|
|
|
|
}
|
2018-09-14 07:10:30 +00:00
|
|
|
|
2018-06-28 13:54:54 +00:00
|
|
|
get value() {
|
|
|
|
return this._value;
|
|
|
|
}
|
2018-09-14 07:10:30 +00:00
|
|
|
|
2018-06-28 13:54:54 +00:00
|
|
|
set type(value) {
|
|
|
|
this._type = value || 'text';
|
|
|
|
}
|
2018-09-14 07:10:30 +00:00
|
|
|
|
2018-06-28 13:54:54 +00:00
|
|
|
get type() {
|
|
|
|
return this._type;
|
|
|
|
}
|
2018-09-14 07:10:30 +00:00
|
|
|
|
2018-04-19 12:48:38 +00:00
|
|
|
set vnTabIndex(value) {
|
|
|
|
this.input.tabindex = value;
|
2018-01-16 12:54:21 +00:00
|
|
|
}
|
2018-09-14 07:10:30 +00:00
|
|
|
|
2017-09-13 07:45:42 +00:00
|
|
|
clear() {
|
2018-09-04 14:51:29 +00:00
|
|
|
this.saveOldValue();
|
2017-09-13 07:45:42 +00:00
|
|
|
this.value = null;
|
2018-09-13 09:27:38 +00:00
|
|
|
if (this.onClear) this.onClear();
|
2017-05-18 15:35:07 +00:00
|
|
|
this.input.focus();
|
|
|
|
}
|
2017-04-28 13:04:29 +00:00
|
|
|
}
|
2018-09-14 07:10:30 +00:00
|
|
|
Textfield.$inject = ['$element', '$scope', '$attrs', 'vnTemplate'];
|
2017-04-28 13:04:29 +00:00
|
|
|
|
2018-02-10 15:18:01 +00:00
|
|
|
ngModule.component('vnTextfield', {
|
2017-09-12 09:44:56 +00:00
|
|
|
template: require('./textfield.html'),
|
2018-06-28 13:54:54 +00:00
|
|
|
transclude: {
|
|
|
|
leftIcons: '?tLeftIcons',
|
|
|
|
rightIcons: '?tRightIcons'
|
|
|
|
},
|
2017-11-16 13:30:17 +00:00
|
|
|
controller: Textfield,
|
2017-09-12 10:45:29 +00:00
|
|
|
bindings: {
|
2017-09-12 13:02:14 +00:00
|
|
|
value: '=model',
|
2017-09-13 07:45:42 +00:00
|
|
|
label: '@?',
|
|
|
|
name: '@?',
|
|
|
|
disabled: '<?',
|
2017-09-21 11:10:30 +00:00
|
|
|
readonly: '<?',
|
2017-09-13 07:45:42 +00:00
|
|
|
rule: '@?',
|
2018-01-16 12:54:21 +00:00
|
|
|
type: '@?',
|
2018-06-28 13:54:54 +00:00
|
|
|
vnTabIndex: '@?',
|
2018-09-13 09:27:38 +00:00
|
|
|
onChange: '&',
|
2019-04-12 12:33:08 +00:00
|
|
|
onClear: '&',
|
|
|
|
info: '@?'
|
2017-09-12 10:45:29 +00:00
|
|
|
}
|
2017-09-13 07:45:42 +00:00
|
|
|
});
|