import { ChangeDetectorRef, ElementRef } from '@angular/core'; import { NgControl } from '@angular/forms'; import { InputCommandsAction, InputStringEnum } from '../enums'; import { ICaInput } from '../config'; import { MethodsCalculationsHelper } from '../../../utils/helpers/methods-calculations.helper'; export abstract class CaInputBaseHelpersComponent { abstract input: ElementRef; abstract lastCursorSpacePosition: number; abstract superControl: NgControl; abstract _inputConfig: ICaInput; abstract cdRef: ChangeDetectorRef; // Number of points public numberOfConsecutivelyPoints: number = 0; public numberOfPoints: number = 0; public oneSpaceOnlyCounter: number = 0; public inputCommandsTimeout!: ReturnType | number; // #region DisableConsecutivelySpaces public disableConsecutivelySpaces(event: any) { const inputElement = this.input.nativeElement as HTMLInputElement; // Get cursor position const cursorPosition = inputElement.selectionStart as number; // If the cursor is in the middle of a word, allow spaces if ( cursorPosition > 0 && !/\s/.test(inputElement.value[cursorPosition - 1]) && !/\s/.test(inputElement.value[cursorPosition]) ) { return; } // If the user is typing a space and the previous character is a space, prevent the default behavior if ( event.key === ' ' && /\s/.test(inputElement.value[cursorPosition - 1]) ) { event.preventDefault(); } // Save the cursor position for the next input event this.lastCursorSpacePosition = cursorPosition; } // #endregion // #region DisableConsecutivelyPoints public disableConsecutivelyPoints(event: KeyboardEvent): void | boolean { if (/^[.]*$/.test(String.fromCharCode(event.charCode))) { this.numberOfConsecutivelyPoints++; if (this.numberOfConsecutivelyPoints > 1) { event.preventDefault(); return false; } } else { this.numberOfConsecutivelyPoints = 0; } } // #endregion // #region DisableMultiplePoints public disableMultiplePoints(event: KeyboardEvent): void | boolean { if (/^[.]*$/.test(String.fromCharCode(event.charCode))) { if (!this.superControl.control?.value) { event.preventDefault(); return false; } if (this.superControl.control?.value?.toString().includes('.')) { event.preventDefault(); return false; } this.numberOfPoints++; if (this.numberOfPoints > 1) { event.preventDefault(); return false; } } else { this.numberOfPoints = 0; } } // endregion // #region EnableOneSpaceOnly public enableOneSpaceOnly(event: any) { if ( /^\s*$/.test(String.fromCharCode(event.charCode)) || this.superControl.control?.value?.includes(' ') ) { this.oneSpaceOnlyCounter++; } if ( this.oneSpaceOnlyCounter > 1 && /^\s*$/.test(String.fromCharCode(event.charCode)) ) { event.preventDefault(); return false; } return true; } // #endregion // #region HandlePmIncrementDecrement public handlePmIncrementDecrement(action: string): void { const value = MethodsCalculationsHelper.convertThousanSepInNumber( this.superControl.control?.value ); switch (action) { case InputCommandsAction.DECREMENT: this.decrementPmValue(value); break; case InputCommandsAction.INCREMENT: this.incrementPmValue(value); break; case InputCommandsAction.RESET: this.resetPmValue(); break; default: break; } this.setInputCursorAtTheEnd(this.input.nativeElement); } // #endregion // #region DecrementPmValue private decrementPmValue(value: number): void { if (value >= 10000 && value < 20000) { this.superControl.control?.patchValue( MethodsCalculationsHelper.convertNumberInThousandSep( value - 1000 ) ); } else if (value >= 20001 && value < 50000) { this.superControl.control?.patchValue( MethodsCalculationsHelper.convertNumberInThousandSep( value - 3000 ) ); } else if (value >= 50001 && value < 100000) { this.superControl.control?.patchValue( MethodsCalculationsHelper.convertNumberInThousandSep( value - 5000 ) ); } else if (value >= 10000) { this.superControl.control?.patchValue( MethodsCalculationsHelper.convertNumberInThousandSep( value - 10000 ) ); } else if (value >= 1000) { this.superControl.control?.patchValue( MethodsCalculationsHelper.convertNumberInThousandSep( value - 500 ) ); } } // #endregion // #region ResetPmValue private resetPmValue(): void { if (this._inputConfig?.defaultValue) { this.superControl.control?.patchValue( this._inputConfig?.defaultValue ); } } // #endregion // #region ResetPmValue private incrementPmValue(value: number): void { if (value > 10000 && value < 20000) { this.superControl.control?.patchValue( MethodsCalculationsHelper.convertNumberInThousandSep( value + 1000 ) ); } else if (value >= 20001 && value < 50000) { this.superControl.control?.patchValue( MethodsCalculationsHelper.convertNumberInThousandSep( value + 3000 ) ); } else if (value >= 50001 && value < 100000) { this.superControl.control?.patchValue( MethodsCalculationsHelper.convertNumberInThousandSep( value + 5000 ) ); } else if (value >= 10000) { this.superControl.control?.patchValue( MethodsCalculationsHelper.convertNumberInThousandSep( value + 10000 ) ); } else { this.superControl.control?.patchValue( MethodsCalculationsHelper.convertNumberInThousandSep( value + 500 ) ); } } // #endregion // #region SetInputCursorAtTheEnd public setInputCursorAtTheEnd(input: any, time: number = 120): void { const selectionEnd = input.selectionEnd; if (input.setSelectionRange) { input.setSelectionRange(selectionEnd, selectionEnd); } setTimeout(() => { input.focus(); }, time); } // #endregion // #region HandleIncrementDecrement public handleIncrementDecrement(action: string): void { clearTimeout(this.inputCommandsTimeout); switch (action) { case InputCommandsAction.DECREMENT: this.superControl.control?.patchValue( (this.superControl.control?.value ? parseInt(this.superControl.control?.value) : 0) - 1 ); break; case InputCommandsAction.INCREMENT: this.superControl.control?.patchValue( (this.superControl.control?.value ? parseInt(this.superControl.control?.value) : 0) + 1 ); break; default: break; } this.setInputCursorAtTheEnd(this.input.nativeElement); } // #endregion // #region public handleMonths(action: string): void { clearTimeout(this.inputCommandsTimeout); switch (action) { case InputCommandsAction.MINUS: this.decrementMonth(); break; case InputCommandsAction.PLUS: this.incrementMonth(); break; default: break; } if ( parseInt(this.superControl.control?.value) < 1 || parseInt(this.superControl.control?.value) > 12 ) { this.superControl.control?.setErrors({ invalid: true }); } else { this.superControl.control?.setErrors(null); } this.setInputCursorAtTheEnd(this.input.nativeElement); } // #endregion // #region DecrementMonth private decrementMonth(): void { if ( parseInt(this.superControl.control?.value) === 1 || !this.superControl.control?.value ) { this.blurOnCommands(); return; } this.superControl.control?.patchValue( (this.superControl.control?.value ? parseInt(this.superControl.control?.value) : 0) - 1 ); } // #endregion // #region IncrementMonth private incrementMonth(): void { if (parseInt(this.superControl.control?.value) === 12) { this.blurOnCommands(); return; } this.superControl.control?.patchValue( (this.superControl.control?.value ? parseInt(this.superControl.control?.value) : 0) + 1 ); } // #endregion // #region public blurOnCommands(): void { this.inputCommandsTimeout = setTimeout(() => { this._inputConfig.commands!.active = false; // this.setIsVisibleCommands(false); this._inputConfig.blackInput = false; this.cdRef.detectChanges(); }, 200); } // #endregion // #region CheckNameArray public checkNameArray(array: string[], name: string): boolean { return array.includes(name); } // #endregion }