2023-01-12 18:34:45 +01:00
|
|
|
import { ApplicationController } from './application_controller';
|
|
|
|
|
|
|
|
export class FormatController extends ApplicationController {
|
|
|
|
connect() {
|
|
|
|
const format = this.element.getAttribute('data-format');
|
|
|
|
switch (format) {
|
2024-08-21 17:59:30 +02:00
|
|
|
case 'deleteSpace':
|
|
|
|
this.on('change', (event) => {
|
|
|
|
const target = event.target as HTMLInputElement;
|
|
|
|
const value = this.deleteSpace(target.value);
|
|
|
|
replaceValue(target, value);
|
|
|
|
});
|
|
|
|
break;
|
2023-01-12 18:34:45 +01:00
|
|
|
case 'list':
|
|
|
|
this.on('change', (event) => {
|
|
|
|
const target = event.target as HTMLInputElement;
|
2023-10-16 10:58:11 +02:00
|
|
|
const value = this.formatList(target.value);
|
|
|
|
replaceValue(target, value);
|
2023-01-12 18:34:45 +01:00
|
|
|
});
|
|
|
|
break;
|
2024-07-16 11:25:11 +02:00
|
|
|
case 'siret':
|
|
|
|
this.on('input', (event) => {
|
|
|
|
const target = event.target as HTMLInputElement;
|
|
|
|
const value = this.formatSIRET(target.value);
|
|
|
|
replaceValue(target, value);
|
|
|
|
});
|
|
|
|
break;
|
2023-01-12 18:34:45 +01:00
|
|
|
case 'iban':
|
|
|
|
this.on('input', (event) => {
|
|
|
|
const target = event.target as HTMLInputElement;
|
2023-10-16 10:58:11 +02:00
|
|
|
const value = this.formatIBAN(target.value);
|
|
|
|
replaceValue(target, value);
|
2023-01-12 18:34:45 +01:00
|
|
|
});
|
2023-05-30 14:42:36 +02:00
|
|
|
break;
|
|
|
|
case 'integer':
|
|
|
|
this.on('input', (event) => {
|
|
|
|
const target = event.target as HTMLInputElement;
|
2023-10-16 10:58:11 +02:00
|
|
|
const value = this.formatInteger(target.value);
|
|
|
|
replaceValue(target, value);
|
2023-05-30 14:42:36 +02:00
|
|
|
});
|
2023-09-26 16:31:35 +02:00
|
|
|
break;
|
|
|
|
case 'decimal':
|
|
|
|
this.on('input', (event) => {
|
|
|
|
const target = event.target as HTMLInputElement;
|
2023-10-16 10:58:11 +02:00
|
|
|
const value = this.formatDecimal(target.value);
|
|
|
|
replaceValue(target, value);
|
2023-09-26 16:31:35 +02:00
|
|
|
});
|
|
|
|
break;
|
2023-01-12 18:34:45 +01:00
|
|
|
}
|
|
|
|
}
|
2024-08-21 17:59:30 +02:00
|
|
|
private deleteSpace(value: string) {
|
|
|
|
return value.replace(/\s*/g, '');
|
|
|
|
}
|
2023-01-12 18:34:45 +01:00
|
|
|
|
|
|
|
private formatList(value: string) {
|
|
|
|
return value.replace(/;/g, ',');
|
|
|
|
}
|
2024-08-21 17:59:30 +02:00
|
|
|
|
2024-07-16 11:25:11 +02:00
|
|
|
private formatSIRET(value: string) {
|
|
|
|
return value
|
|
|
|
.replace(/[^\d]/gi, '')
|
|
|
|
.replace(/^\s*(\d{3})\s*(\d{3})\s*(\d{3})\s*(\d{5})\s*$/gi, '$1 $2 $3 $4')
|
|
|
|
.trim();
|
|
|
|
}
|
2024-08-21 17:59:30 +02:00
|
|
|
|
2023-01-12 18:34:45 +01:00
|
|
|
private formatIBAN(value: string) {
|
|
|
|
return value
|
2023-07-17 16:42:37 +02:00
|
|
|
.replace(/[^\dA-Z]/gi, '')
|
2023-01-12 18:34:45 +01:00
|
|
|
.replace(/(.{4})/g, '$1 ')
|
2023-07-17 16:42:37 +02:00
|
|
|
.trim()
|
|
|
|
.toUpperCase();
|
2023-01-12 18:34:45 +01:00
|
|
|
}
|
2023-05-30 14:42:36 +02:00
|
|
|
|
|
|
|
private formatInteger(value: string) {
|
2023-10-06 14:27:50 +02:00
|
|
|
return value.replace(/[^-?\d]/g, '');
|
2023-05-30 14:42:36 +02:00
|
|
|
}
|
2023-09-26 16:31:35 +02:00
|
|
|
|
|
|
|
private formatDecimal(value: string) {
|
2023-10-23 16:46:07 +02:00
|
|
|
let formattedNumber = value;
|
|
|
|
const lastDotIndex = value.lastIndexOf('.');
|
|
|
|
const lastCommaIndex = value.lastIndexOf(',');
|
|
|
|
if (lastDotIndex != -1 && lastCommaIndex != -1) {
|
|
|
|
if (lastDotIndex < lastCommaIndex) {
|
|
|
|
formattedNumber = value.replace(/\./g, '');
|
|
|
|
} else {
|
|
|
|
formattedNumber = value.replace(/,/g, '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return formattedNumber
|
|
|
|
.replace(/,/g, '.')
|
|
|
|
.replace(new RegExp(`[^-?\\d.]`, 'g'), '');
|
2023-10-16 10:58:11 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-26 16:31:35 +02:00
|
|
|
|
2023-10-16 10:58:11 +02:00
|
|
|
function replaceValue(target: HTMLInputElement, value: string) {
|
|
|
|
const delta = target.value.length - value.length;
|
|
|
|
const start = target.selectionStart;
|
|
|
|
const end = target.selectionStart;
|
|
|
|
const dir = target.selectionDirection;
|
|
|
|
target.value = value;
|
|
|
|
target.selectionStart = start ? start - delta : 0;
|
|
|
|
target.selectionEnd = end ? end - delta : 0;
|
|
|
|
target.selectionDirection = dir;
|
|
|
|
}
|