salix/front/core/components/td-editable/index.js

108 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2019-02-08 14:08:36 +00:00
import ngModule from '../../module';
import Component from '../../lib/component';
2019-10-18 19:36:30 +00:00
import FormInput from '../form-input';
2019-02-08 14:08:36 +00:00
import './style.scss';
export default class Controller extends Component {
2019-02-22 11:27:56 +00:00
constructor($element, $scope, $transclude, $timeout) {
2019-02-08 14:08:36 +00:00
super($element, $scope);
2019-02-22 11:27:56 +00:00
this.$timeout = $timeout;
this.element.tabIndex = 0;
2019-02-08 14:08:36 +00:00
this.element.addEventListener('focus', () => {
2019-02-25 09:10:44 +00:00
if (this.field || this.disabled) return;
2019-02-08 14:08:36 +00:00
$transclude((tClone, tScope) => {
this.field = tClone;
this.tScope = tScope;
this.element.querySelector('.field').appendChild(this.field[0]);
this.element.tabIndex = -1;
2019-02-08 14:08:36 +00:00
}, null, 'field');
this.element.classList.add('selected');
2019-02-08 14:08:36 +00:00
});
this.element.addEventListener('focusout', event => this.hideField(event));
this.element.addEventListener('keyup', event => {
if (event.key === 'Enter')
this.hideField(event);
});
this.element.addEventListener('click', event => {
if (this.disabled) return;
let target = event.target;
while (target) {
if (target == this.field[0]) return;
2019-02-08 14:08:36 +00:00
target = target.parentNode;
}
2019-02-08 14:08:36 +00:00
let inputCtrl = this.field[0].firstElementChild.$ctrl;
2019-10-18 19:36:30 +00:00
if (inputCtrl instanceof FormInput) {
let evt = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
inputCtrl.input.dispatchEvent(evt);
2019-02-08 14:08:36 +00:00
}
});
}
hideField(event) {
if (!this.field || !this.tScope) return;
this.lastEvent = event;
let target = event.relatedTarget;
while (target && target != this.element)
target = target.parentNode;
if (!target) {
this.tScope.$destroy();
this.tScope = null;
this.field.remove();
this.field = null;
this.element.classList.remove('selected');
this.element.tabIndex = 0;
}
}
2019-02-22 11:27:56 +00:00
destroyTimer() {
if (this.timer) {
this.$timeout.cancel(this.timer);
this.timer = null;
}
}
2019-02-08 14:08:36 +00:00
2019-02-22 11:27:56 +00:00
$onDestroy() {
this.destroyTimer();
}
get disabled() {
return this._disabled;
}
set disabled(value) {
this._disabled = value;
const classList = this.element.classList;
if (value)
classList.add('disabled');
else
classList.remove('disabled');
}
2019-02-22 11:27:56 +00:00
}
Controller.$inject = ['$element', '$scope', '$transclude', '$timeout'];
2019-02-08 14:08:36 +00:00
ngModule.vnComponent('vnTdEditable', {
2019-02-08 14:08:36 +00:00
template: require('./index.html'),
controller: Controller,
2019-02-25 08:50:51 +00:00
bindings: {
disabled: '<?'
},
2019-02-08 14:08:36 +00:00
transclude: {
text: 'text',
field: '?field'
}
});