2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../../module';
|
2019-10-09 22:47:29 +00:00
|
|
|
import Field from '../field';
|
2017-04-28 13:04:29 +00:00
|
|
|
|
2019-10-09 22:47:29 +00:00
|
|
|
export default class Textfield extends Field {
|
|
|
|
constructor($element, $scope, $compile) {
|
|
|
|
super($element, $scope, $compile);
|
|
|
|
this.buildInput('text');
|
2022-10-03 11:48:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set maxLength(value) {
|
|
|
|
this.input.maxLength = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get maxLength() {
|
|
|
|
return this.input.maxLength;
|
|
|
|
}
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
set insertable(value) {
|
|
|
|
if (this._insertable === value)
|
|
|
|
return;
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
if (this._insertable)
|
|
|
|
this.input.removeEventListener('keypress', this.keyPressListener);
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
if (value) {
|
|
|
|
this.keyPressListener = async e => await this.onKeyPress(e);
|
|
|
|
this.input.addEventListener('keypress', this.keyPressListener);
|
|
|
|
}
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
this._insertable = value;
|
|
|
|
}
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
get insertable() {
|
|
|
|
return this._insertable;
|
|
|
|
}
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
async onKeyPress(e) {
|
|
|
|
if (e.key == 'Enter')
|
|
|
|
return; // If the enter key is pressed dismiss it
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
let maxLength = this.maxLength;
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
// Call the function that obtains the current cursor position
|
|
|
|
let pointerPosition = getCaretPosition(e.target);
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
// If the cursor position is on the last allowed character,
|
|
|
|
// prevent any keystroke from doing anything
|
|
|
|
if (pointerPosition >= maxLength) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return;
|
|
|
|
}
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
// In case by any ways the input is longer than the max especified size, cut it to it.
|
|
|
|
let currentArrValue = e.target.value.slice(0, maxLength);
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
// Transform said input to a array with each character on one position
|
|
|
|
currentArrValue = currentArrValue.split('');
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
// Cut the array in 2 parts, one with everything right of the caret,
|
|
|
|
// and one with everything left to it
|
|
|
|
let rightToTheCaret = currentArrValue.slice(pointerPosition);
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
let leftToTheCaret = currentArrValue.slice(0, pointerPosition);
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
// Remove the first number on the array that was right of the caret
|
|
|
|
rightToTheCaret.shift();
|
2022-10-03 06:48:04 +00:00
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
// The part that was left to the caret is not modified in any way and is put back into the textField
|
|
|
|
e.target.value = leftToTheCaret.join('');
|
|
|
|
|
|
|
|
// Add one millisecond of delay to give the UI time to update,
|
|
|
|
// so that it detects the changes on the textField
|
|
|
|
await new Promise(r => setTimeout(r, 1));
|
|
|
|
|
|
|
|
// Add the values that should be right to the Caret back in the textField
|
|
|
|
e.target.value = e.target.value + rightToTheCaret.join('');
|
|
|
|
|
|
|
|
// Update the current pointer position so that it moves 1 position to the right
|
|
|
|
pointerPosition++;
|
|
|
|
e.target.setSelectionRange(pointerPosition, pointerPosition);
|
|
|
|
|
|
|
|
return false;
|
2017-05-18 15:35:07 +00:00
|
|
|
}
|
2017-04-28 13:04:29 +00:00
|
|
|
}
|
2022-10-03 11:48:35 +00:00
|
|
|
|
2022-10-03 06:48:04 +00:00
|
|
|
function getCaretPosition(targetElement) {
|
|
|
|
let caretPosition = 0;
|
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
if (targetElement.selectionStart || targetElement.selectionStart == 0) // Firefox Support
|
2022-10-03 06:48:04 +00:00
|
|
|
caretPosition = targetElement.selectionStart;
|
|
|
|
|
2022-10-03 11:48:35 +00:00
|
|
|
return caretPosition;
|
2022-10-03 06:48:04 +00:00
|
|
|
}
|
2017-04-28 13:04:29 +00:00
|
|
|
|
2019-10-09 22:47:29 +00:00
|
|
|
ngModule.vnComponent('vnTextfield', {
|
2022-10-03 06:48:04 +00:00
|
|
|
controller: Textfield,
|
|
|
|
bindings: {
|
|
|
|
maxLength: '<?',
|
|
|
|
insertable: '<?'
|
|
|
|
}
|
2017-09-13 07:45:42 +00:00
|
|
|
});
|
2022-10-03 11:48:35 +00:00
|
|
|
|