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

65 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-02-08 14:08:36 +00:00
import ngModule from '../../module';
import Component from '../../lib/component';
2019-02-22 11:27:56 +00:00
import {focus} from '../../directives/focus';
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;
2019-02-08 14:08:36 +00:00
let element = $element[0];
element.tabIndex = 0;
element.addEventListener('focus', () => {
2019-02-22 11:27:56 +00:00
if (this.field) 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]);
2019-02-22 11:27:56 +00:00
element.tabIndex = -1;
this.timer = $timeout(() => {
this.timer = null;
focus(this.field[0]);
});
2019-02-08 14:08:36 +00:00
}, null, 'field');
element.classList.add('selected');
});
element.addEventListener('focusout', event => {
2019-02-22 11:27:56 +00:00
this.destroyTimer();
2019-02-08 14:08:36 +00:00
this.lastEvent = event;
let target = event.relatedTarget;
2019-02-22 11:27:56 +00:00
while (target && target != element)
2019-02-08 14:08:36 +00:00
target = target.parentNode;
if (!target) {
this.tScope.$destroy();
this.field.remove();
2019-02-22 11:27:56 +00:00
this.field = null;
2019-02-08 14:08:36 +00:00
element.classList.remove('selected');
2019-02-22 11:27:56 +00:00
element.tabIndex = 0;
2019-02-08 14:08:36 +00:00
}
});
}
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();
}
}
Controller.$inject = ['$element', '$scope', '$transclude', '$timeout'];
2019-02-08 14:08:36 +00:00
ngModule.component('vnTdEditable', {
template: require('./index.html'),
controller: Controller,
transclude: {
text: 'text',
field: '?field'
}
});