import { Constructor } from '../../../models/mixin.model'; import { InputCommandsAction, InputCommandsType } from '../enums'; import { InputBase } from '../models/base.model'; export function InputCommandMixin>(Base: T) { return class extends Base { // #region OnCommands public onCommands(e: any): void { const { event, type, action } = e; event.stopPropagation(); event.preventDefault(); switch (type) { case InputCommandsType.PM_INCREMENT_DECREMENT: this.handlePmIncrementDecrement(action); break; case InputCommandsType.CONFIRM_CANCEL: this.handleConfirmCancel(action); break; case InputCommandsType.MONTHS: this.handleMonths(action); break; default: break; } } // #region public handleMonths(action: string): void { //clearTimeout(this.inputCommandsTimeout); switch (action) { case InputCommandsAction.MINUS: this.handleChangeInputAndUpdateControl( (this.inputElement.nativeElement.value ? +this.inputElement.nativeElement.value : 0) - 1 ); break; case InputCommandsAction.PLUS: this.handleChangeInputAndUpdateControl( (this.inputElement.nativeElement.value ? +this.inputElement.nativeElement.value : 0) + 1 ); break; default: break; } } // #endregion public handleConfirmCancel(action: string): void { switch (action) { case InputCommandsAction.CONFIRM: this.confirmCommand(); break; case InputCommandsAction.CANCEL: this.cancelCommand(); break; default: break; } // this.superControl.control?.setErrors(null); // this._inputConfig.dropdownLabelNew = false; // this._inputConfig.commands!.active = false; // this._inputConfig.blackInput = false; // this.isEditInput = false; // this.isVisibleCommands = false // this.isFocusInput = false; } public cancelCommand(): void { //this.setCommandEvent.next({ action: InputCommandsAction.CANCEL }); } public confirmCommand(): void { // this.setCommandEvent.next({ // data: this.superControl.control?.value, // action: InputCommandsAction.CONFIRM, // mode: // !this._inputConfig.dropdownLabelNew && // this._inputConfig.name !== 'Input Dropdown Bank Name' // ? 'edit' // : 'new', // }); } // #region HandlePmIncrementDecrement public handlePmIncrementDecrement(action: string): void { const numericValue = this.inputElement.nativeElement.value; // Remove thousands separators (`,`) before parsing let value = parseFloat(numericValue.replace(/,/g, '')); switch (action) { case InputCommandsAction.DECREMENT: case InputCommandsAction.INCREMENT: const incdecValue = this.decrementIncrementDecrementPmValue( value, action ); this.handleChangeInputAndUpdateControl( action === InputCommandsAction.INCREMENT ? value + incdecValue : value - incdecValue ); break; case InputCommandsAction.RESET: this.resetPmValue(); break; default: break; } } // #region DecrementPmValue public decrementIncrementDecrementPmValue( value: number, action: InputCommandsAction ): number { if (value >= 10000 && value < 20000) { return 1000; } else if (value >= 20000 && value < 50000) { return 5000; } else if (value >= 50000 && value < 100000) { return 10000; } else if ( value >= 500000 && action === InputCommandsAction.INCREMENT ) { return 0; } else if (value >= 100000) { return 20000; } else if ( value <= 5000 && action === InputCommandsAction.DECREMENT ) { return 0; } return 500; } // #endregion // #region ResetPmValue public resetPmValue(): void { if (this._inputConfig?.defaultValue) this.handleChangeInputAndUpdateControl( this._inputConfig?.defaultValue! ); } // #endregion }; }