diff --git a/karma.conf.ts b/karma.conf.ts index d5d3164..67eba88 100644 --- a/karma.conf.ts +++ b/karma.conf.ts @@ -3,7 +3,6 @@ import * as path from 'path'; import * as ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'; export default config => { - config.set({ basePath: './', frameworks: ['mocha'], diff --git a/src/currency-mask.config.ts b/src/currency-mask.config.ts index 6e978d5..1e7bd05 100644 --- a/src/currency-mask.config.ts +++ b/src/currency-mask.config.ts @@ -1,15 +1,15 @@ import {InjectionToken} from "@angular/core"; export interface CurrencyMaskConfig { - align: string; - allowNegative: boolean; - allowZero: boolean; - decimal: string; - precision: number; - prefix: string; - suffix: string; - thousands: string; - nullable: boolean; + align: string; + allowNegative: boolean; + allowZero: boolean; + decimal: string; + precision: number; + prefix: string; + suffix: string; + thousands: string; + nullable: boolean; } export let CURRENCY_MASK_CONFIG = new InjectionToken("currency.mask.config"); diff --git a/src/currency-mask.directive.ts b/src/currency-mask.directive.ts index 4b219eb..6ba0e4e 100644 --- a/src/currency-mask.directive.ts +++ b/src/currency-mask.directive.ts @@ -1,26 +1,26 @@ import { - AfterViewInit, - Directive, - DoCheck, - ElementRef, - forwardRef, - HostListener, - Inject, - KeyValueDiffer, - KeyValueDiffers, - Input, - OnInit, - Optional + AfterViewInit, + Directive, + DoCheck, + ElementRef, + forwardRef, + HostListener, + Inject, + KeyValueDiffer, + KeyValueDiffers, + Input, + OnInit, + Optional } from "@angular/core"; -import {ControlValueAccessor, NG_VALUE_ACCESSOR} from "@angular/forms"; +import {ControlValueAccessor, NG_VALUE_ACCESSOR} from "@angular/forms"; import {CurrencyMaskConfig, CURRENCY_MASK_CONFIG} from "./currency-mask.config"; import {InputHandler} from "./input.handler"; export const CURRENCYMASKDIRECTIVE_VALUE_ACCESSOR: any = { - provide: NG_VALUE_ACCESSOR, - useExisting: forwardRef(() => CurrencyMaskDirective), - multi: true + provide: NG_VALUE_ACCESSOR, + useExisting: forwardRef(() => CurrencyMaskDirective), + multi: true, }; @Directive({ @@ -29,103 +29,105 @@ export const CURRENCYMASKDIRECTIVE_VALUE_ACCESSOR: any = { }) export class CurrencyMaskDirective implements AfterViewInit, ControlValueAccessor, DoCheck, OnInit { - @Input() options: any = {}; - - public inputHandler: InputHandler; - public keyValueDiffer: KeyValueDiffer; - - public optionsTemplate = { - align: "right", - allowNegative: true, - allowZero: true, - decimal: ".", - precision: 2, - prefix: "$ ", - suffix: "", - thousands: ",", - nullable: false - }; - - constructor(@Optional() @Inject(CURRENCY_MASK_CONFIG) private currencyMaskConfig: CurrencyMaskConfig, private elementRef: ElementRef, private keyValueDiffers: KeyValueDiffers) { - if (currencyMaskConfig) { - this.optionsTemplate = currencyMaskConfig; - } - - this.keyValueDiffer = keyValueDiffers.find({}).create(); + @Input() options: any = {}; + + public inputHandler: InputHandler; + public keyValueDiffer: KeyValueDiffer; + + public optionsTemplate = { + align: "right", + allowNegative: true, + allowZero: true, + decimal: ".", + precision: 2, + prefix: "$ ", + suffix: "", + thousands: ",", + nullable: false + }; + + constructor(@Optional() @Inject(CURRENCY_MASK_CONFIG) private currencyMaskConfig: CurrencyMaskConfig, + private elementRef: ElementRef, + private keyValueDiffers: KeyValueDiffers) { + if (currencyMaskConfig) { + this.optionsTemplate = currencyMaskConfig; } - ngAfterViewInit() { - this.elementRef.nativeElement.style.textAlign = this.options.align ? this.options.align : this.optionsTemplate.align; - } + this.keyValueDiffer = keyValueDiffers.find({}).create(); + } - ngDoCheck() { - if (this.keyValueDiffer.diff(this.options)) { - this.elementRef.nativeElement.style.textAlign = this.options.align ? this.options.align : this.optionsTemplate.align; - this.inputHandler.updateOptions((Object).assign({}, this.optionsTemplate, this.options)); - } - } + ngAfterViewInit() { + this.elementRef.nativeElement.style.textAlign = this.options.align ? this.options.align : this.optionsTemplate.align; + } - ngOnInit() { - this.inputHandler = new InputHandler(this.elementRef.nativeElement, (Object).assign({}, this.optionsTemplate, this.options)); + ngDoCheck() { + if (this.keyValueDiffer.diff(this.options)) { + this.elementRef.nativeElement.style.textAlign = this.options.align ? this.options.align : this.optionsTemplate.align; + this.inputHandler.updateOptions((Object).assign({}, this.optionsTemplate, this.options)); } + } - @HostListener("blur", ["$event"]) - handleBlur(event: any) { - this.inputHandler.getOnModelTouched().apply(event); - } + ngOnInit() { + this.inputHandler = new InputHandler(this.elementRef.nativeElement, (Object).assign({}, this.optionsTemplate, this.options)); + } - @HostListener("cut", ["$event"]) - handleCut(event: any) { - if (!this.isChromeAndroid()) { - this.inputHandler.handleCut(event); - } - } + @HostListener("blur", ["$event"]) + handleBlur(event: any) { + this.inputHandler.getOnModelTouched().apply(event); + } - @HostListener("input", ["$event"]) - handleInput(event: any) { - if (this.isChromeAndroid()) { - this.inputHandler.handleInput(event); - } + @HostListener("cut", ["$event"]) + handleCut(event: any) { + if (!this.isChromeAndroid()) { + this.inputHandler.handleCut(event); } + } - @HostListener("keydown", ["$event"]) - handleKeydown(event: any) { - if (!this.isChromeAndroid()) { - this.inputHandler.handleKeydown(event); - } + @HostListener("input", ["$event"]) + handleInput(event: any) { + if (this.isChromeAndroid()) { + this.inputHandler.handleInput(event); } + } - @HostListener("keypress", ["$event"]) - handleKeypress(event: any) { - if (!this.isChromeAndroid()) { - this.inputHandler.handleKeypress(event); - } + @HostListener("keydown", ["$event"]) + handleKeydown(event: any) { + if (!this.isChromeAndroid()) { + this.inputHandler.handleKeydown(event); } + } - @HostListener("paste", ["$event"]) - handlePaste(event: any) { - if (!this.isChromeAndroid()) { - this.inputHandler.handlePaste(event); - } + @HostListener("keypress", ["$event"]) + handleKeypress(event: any) { + if (!this.isChromeAndroid()) { + this.inputHandler.handleKeypress(event); } + } - isChromeAndroid(): boolean { - return /chrome/i.test(navigator.userAgent) && /android/i.test(navigator.userAgent); + @HostListener("paste", ["$event"]) + handlePaste(event: any) { + if (!this.isChromeAndroid()) { + this.inputHandler.handlePaste(event); } + } - registerOnChange(callbackFunction: Function): void { - this.inputHandler.setOnModelChange(callbackFunction); - } + isChromeAndroid(): boolean { + return /chrome/i.test(navigator.userAgent) && /android/i.test(navigator.userAgent); + } - registerOnTouched(callbackFunction: Function): void { - this.inputHandler.setOnModelTouched(callbackFunction); - } + registerOnChange(callbackFunction: Function): void { + this.inputHandler.setOnModelChange(callbackFunction); + } - setDisabledState(value: boolean): void { - this.elementRef.nativeElement.disabled = value; - } + registerOnTouched(callbackFunction: Function): void { + this.inputHandler.setOnModelTouched(callbackFunction); + } - writeValue(value: number): void { - this.inputHandler.setValue(value); - } + setDisabledState(value: boolean): void { + this.elementRef.nativeElement.disabled = value; + } + + writeValue(value: number): void { + this.inputHandler.setValue(value); + } } diff --git a/src/currency-mask.module.ts b/src/currency-mask.module.ts index c2df17f..08b03ca 100644 --- a/src/currency-mask.module.ts +++ b/src/currency-mask.module.ts @@ -1,20 +1,11 @@ import {NgModule} from '@angular/core'; import {CommonModule} from '@angular/common'; import {FormsModule} from '@angular/forms'; - import {CurrencyMaskDirective} from "./currency-mask.directive"; @NgModule({ - imports: [ - CommonModule, - FormsModule - ], - declarations: [ - CurrencyMaskDirective - ], - exports: [ - CurrencyMaskDirective - ] + imports: [ CommonModule, FormsModule ], + declarations: [ CurrencyMaskDirective ], + exports: [ CurrencyMaskDirective ] }) -export class NgxCurrencyModule { -} +export class NgxCurrencyModule {} diff --git a/src/input.manager.ts b/src/input.manager.ts index 8b17a49..6782dfe 100644 --- a/src/input.manager.ts +++ b/src/input.manager.ts @@ -29,7 +29,8 @@ export class InputManager { let haventReachedMaxLength = !(this.rawValue.length >= this.htmlInputElement.maxLength && this.htmlInputElement.maxLength >= 0); let selectionStart = this.inputSelection.selectionStart; let selectionEnd = this.inputSelection.selectionEnd; - let haveNumberSelected = !!(selectionStart != selectionEnd && this.htmlInputElement.value.substring(selectionStart, selectionEnd).match(/[^0-9\u0660-\u0669\u06F0-\u06F9]/)); + let haveNumberSelected = !!(selectionStart != selectionEnd && + this.htmlInputElement.value.substring(selectionStart, selectionEnd).match(/[^0-9\u0660-\u0669\u06F0-\u06F9]/)); let startWithZero = (this.htmlInputElement.value.substring(0, 1) == "0"); return haventReachedMaxLength || haveNumberSelected || startWithZero; } diff --git a/src/input.service.ts b/src/input.service.ts index e75b2d5..d2f63a8 100644 --- a/src/input.service.ts +++ b/src/input.service.ts @@ -57,7 +57,11 @@ export class InputService { return ""; } - let integerPart = onlyNumbers.slice(0, onlyNumbers.length - precision).replace(/^\u0660*/g, "").replace(/^\u06F0*/g, "").replace(/^0*/g, "").replace(/\B(?=([0-9\u0660-\u0669\u06F0-\u06F9]{3})+(?![0-9\u0660-\u0669\u06F0-\u06F9]))/g, thousands); + let integerPart = onlyNumbers.slice(0, onlyNumbers.length - precision) + .replace(/^\u0660*/g, "") + .replace(/^\u06F0*/g, "") + .replace(/^0*/g, "") + .replace(/\B(?=([0-9\u0660-\u0669\u06F0-\u06F9]{3})+(?![0-9\u0660-\u0669\u06F0-\u06F9]))/g, thousands); if (integerPart.startsWith(thousands)) { integerPart = integerPart.substring(1);