($(element) as any).keydown(function(e) {
const {
target: {
selectionStart,
selectionEnd
},
keyCode
} = e;
const htmlNode = $(this);
const inputValue = htmlNode.val();
const selectedValue = inputValue.substring(selectionStart, selectionEnd);
const splittedTime = splitTime(inputValue);
if (selectedValue) {
if (e.which === 37) { // if left
e.preventDefault();
if (selectionStart === 0 && selectionEnd === 2) {
setTimeout(() => {
this.setSelectionRange(selectionStart, selectionEnd);
}, 1);
} else if (selectionStart === 3 && selectionEnd === 5) {
setTimeout(() => {
this.setSelectionRange(0, 2);
}, 1);
} else if (selectionStart === 6 && selectionEnd === 10) {
setTimeout(() => {
this.setSelectionRange(3, 5);
}, 1);
}
} else if (e.which === 39) {
e.preventDefault();
if (selectionStart === 0 && selectionEnd === 2) {
setTimeout(() => {
this.setSelectionRange(3, 5);
}, 1);
} else if (selectionStart === 3 && selectionEnd === 5) {
setTimeout(() => {
this.setSelectionRange(6, 10);
}, 1);
} else if (selectionStart === 6 && selectionEnd === 10) {
setTimeout(() => {
this.setSelectionRange(selectionStart, selectionEnd);
}, 1);
}
} else if (keyCode === 38 || keyCode === 40) {
e.preventDefault();
const action = keyCode === 38 ? Enums.ValueActions.increment : Enums.ValueActions.decrement;
changeAppendix(splittedTime, selectedValue, htmlNode);
adjustTime(splittedTime, htmlNode, action, selectionStart, selectedValue);
this.setSelectionRange(selectionStart, selectionEnd);
}
}
});
($(element) as any).keydown(e => {
const {
target: {
selectionStart,
selectionEnd
},
keyCode: direction
} = e;
const htmlNode = $(this);
const inputValue = htmlNode.val();
const splittedTime = splitTime(inputValue);
const selectedValue = inputValue.substring(selectionStart, selectionEnd);
if (!selectedValue) { return; }
const Point = (x, y) => ({x, y});
const points = [Point(0, 2), Point(3, 5), Point(6, 10)];
const targetPoint = Point(selectionStart, selectionEnd);
const LEFT = 37;
const UP = 38;
const RIGHT = 39;
const DOWN = 40;
if ((direction === LEFT) || (direction === RIGHT)) {
e.preventDefault();
const index = points.findIndex(p => ((p.x === targetPoint.x) && (p.y === targetPoint.y)));
if (index === -1) { return; }
index += ((direction === LEFT) ? points.length : 1);
points.push(targetPoint);
const point = points[index % points.length];
this.setSelectionRange(point.x, point.y);
}
if ((direction === DOWN) || (direction === UP)) {
e.preventDefault();
const Actions = Enums.ValueActions;
const action = ((direction === DOWN) ? Actions.decrement : Actions. increment);
changeAppendix(splittedTime, selectedValue, htmlNode);
adjustTime(splittedTime, htmlNode, action, selectionStart, selectedValue);
this.setSelectionRange(selectionStart, selectionEnd);
}
});
e.preventDefault();
, при беглом осмотре я увидел это при всех условиях.более лаконичнее и читабельнее
более лаконичным и читабельным