#1152 updated td-editable
gitea/salix/dev This commit looks good Details

This commit is contained in:
Gerard 2019-02-22 12:27:56 +01:00
parent 35d4c99e53
commit 6364239d8a
4 changed files with 77 additions and 34 deletions

View File

@ -1,4 +1,2 @@
<vn-horizontal class="text-container">
<span class="text" ng-transclude="text" vn-one></span>
</vn-horizontal>
<div class="field"></div> <div class="field"></div>
<span class="text" ng-transclude="text"></span>

View File

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

View File

@ -1,16 +1,40 @@
@import "variables";
vn-td-editable { vn-td-editable {
cursor: pointer; cursor: pointer;
& > div.text-container{ outline: none;
width: 100%; position: relative;
}
&.selected { &.selected > .text {
& > .text-container{ visibility: hidden;
display: none; }
& > .field {
display: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
box-sizing: border-box;
align-items: center;
padding: .6em;
overflow: visible;
& > field {
flex: 1;
background-color: $color-bg-panel;
padding: .5em;
box-shadow: 0 0 .4em rgba(0, 0, 0, .2);
border-radius: .1em;
min-width: 6em;
& > * {
width: 100%;
max-width: initial;
}
} }
} }
&.selected > .field {
vn-icon { display: flex;
font-size: 1em;
} }
} }

View File

@ -1,5 +1,22 @@
import ngModule from '../module'; import ngModule from '../module';
export function focus(input) {
let selector = 'input, textarea, button, submit';
if (!input.matches(selector))
input = input.querySelector(selector);
if (!input) {
console.warn(`vnFocus: Can't find a focusable element`);
return;
}
input.focus();
if (input.select)
input.select();
}
/** /**
* Sets the focus and selects the text on the input. * Sets the focus and selects the text on the input.
* *
@ -8,24 +25,8 @@ import ngModule from '../module';
export function directive() { export function directive() {
return { return {
restrict: 'A', restrict: 'A',
link: function($scope, $element, $attrs) { link: function($scope, $element) {
$scope.$watch('', function() { $scope.$watch('', () => focus($element[0]));
let input = $element[0];
let selector = 'input, textarea, button, submit';
if (!input.matches(selector))
input = input.querySelector(selector);
if (!input) {
console.warn(`vnFocus: Can't find a focusable element`);
return;
}
input.focus();
if (input.select)
input.select();
});
} }
}; };
} }