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

70 lines
2.0 KiB
JavaScript

import ngModule from '../../module';
import Component from '../../lib/component';
import {focus} from '../../directives/focus';
import './style.scss';
export default class Controller extends Component {
constructor($element, $scope, $transclude, $timeout) {
super($element, $scope);
this.$timeout = $timeout;
let element = $element[0];
element.tabIndex = 0;
element.addEventListener('focus', () => {
if (this.field || this.disabled) return;
$transclude((tClone, tScope) => {
this.field = tClone;
this.tScope = tScope;
this.element.querySelector('.field').appendChild(this.field[0]);
element.tabIndex = -1;
this.timer = $timeout(() => {
this.timer = null;
focus(this.field[0]);
});
}, null, 'field');
element.classList.add('selected');
});
element.addEventListener('focusout', event => {
if (!this.field || this.disabled) return;
// this.destroyTimer();
this.lastEvent = event;
let target = event.relatedTarget;
while (target && target != element)
target = target.parentNode;
if (!target) {
this.tScope.$destroy();
this.field.remove();
this.field = null;
element.classList.remove('selected');
element.tabIndex = 0;
}
});
}
destroyTimer() {
if (this.timer) {
this.$timeout.cancel(this.timer);
this.timer = null;
}
}
$onDestroy() {
this.destroyTimer();
}
}
Controller.$inject = ['$element', '$scope', '$transclude', '$timeout'];
ngModule.component('vnTdEditable', {
template: require('./index.html'),
controller: Controller,
bindings: {
disabled: '<?'
},
transclude: {
text: 'text',
field: '?field'
}
});