salix/client/core/src/components/textfield/textfield.js

119 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-02-10 15:18:01 +00:00
import ngModule from '../../module';
import Input from '../../lib/input';
import './style.scss';
2017-11-16 13:30:17 +00:00
export default class Textfield extends Input {
2018-06-28 13:54:54 +00:00
constructor($element, $scope, $attrs, vnTemplate, $transclude) {
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);
this.info = $attrs.info || null;
2017-09-27 10:27:18 +00:00
this.hasFocus = false;
this.hasMouseIn = false;
2018-06-28 13:54:54 +00:00
if ($transclude) {
$transclude($scope.$parent, tClone => {
this.leftIcons = tClone[0];
}, null, 'leftIcons');
$transclude($scope.$parent, tClone => {
this.rightIcons = tClone[0];
}, null, 'rightIcons');
}
this.input.addEventListener('keyup', e => {
if (e.key == "Escape") {
this._cancel();
}
});
this.input.addEventListener('focus', () => this.saveOldValue());
this.input.addEventListener('keyup', e => {
if (e.key == "Enter") {
this._accept();
}
});
2018-06-20 06:41:59 +00:00
}
_accept() {
if (this.accept) {
this.input.blur();
this.accept();
}
2018-06-28 13:54:54 +00:00
}
_cancel() {
this.input.blur();
this.input.value = this.oldValue;
this.value = this.input.value;
this.$.$apply();
2018-06-28 13:54:54 +00:00
if (this.cancel) {
this.cancel();
}
}
saveOldValue() {
this.oldValue = this.input.value;
}
2018-06-28 13:54:54 +00:00
set leftIcons(value) {
for (let i = 0; i < value.children.length; i++) {
this.element.querySelector('.leftIcons').appendChild(value.children[i]);
}
}
set rightIcons(value) {
for (let i = 0; i < value.children.length; i++) {
this.element.querySelector('.rightIcons').appendChild(value.children[i]);
}
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');
this.mdlUpdate();
2018-06-28 13:54:54 +00:00
}
get value() {
return this._value;
}
set type(value) {
this._type = value || 'text';
}
get type() {
return this._type;
}
set vnTabIndex(value) {
this.input.tabindex = value;
}
mdlUpdate() {
let mdlElement = this.element.firstChild.MaterialTextfield;
if (mdlElement) mdlElement.updateClasses_();
}
2017-09-13 07:45:42 +00:00
clear() {
this.value = null;
2017-05-18 15:35:07 +00:00
this.input.focus();
}
}
2018-06-28 13:54:54 +00:00
Textfield.$inject = ['$element', '$scope', '$attrs', 'vnTemplate', '$transclude'];
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: {
value: '=model',
2017-09-13 07:45:42 +00:00
label: '@?',
name: '@?',
disabled: '<?',
readonly: '<?',
2017-09-13 07:45:42 +00:00
rule: '@?',
type: '@?',
2018-06-28 13:54:54 +00:00
vnTabIndex: '@?',
accept: '&',
cancel: '&',
2018-06-28 13:54:54 +00:00
unclearable: '<?'
2017-09-12 10:45:29 +00:00
}
2017-09-13 07:45:42 +00:00
});