-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support persian and arabic number
- Loading branch information
Showing
6 changed files
with
465 additions
and
426 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CurrencyMaskConfig>("currency.mask.config"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,131 @@ | ||
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 {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({ | ||
selector: "[currencyMask]", | ||
providers: [CURRENCYMASKDIRECTIVE_VALUE_ACCESSOR] | ||
selector: "[currencyMask]", | ||
providers: [CURRENCYMASKDIRECTIVE_VALUE_ACCESSOR] | ||
}) | ||
export class CurrencyMaskDirective implements AfterViewInit, ControlValueAccessor, DoCheck, OnInit { | ||
|
||
@Input() options: any = {}; | ||
|
||
public inputHandler: InputHandler; | ||
public keyValueDiffer: KeyValueDiffer<any, any>; | ||
|
||
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; | ||
@Input() options: any = {}; | ||
|
||
public inputHandler: InputHandler; | ||
public keyValueDiffer: KeyValueDiffer<any, any>; | ||
|
||
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(); | ||
} | ||
|
||
this.keyValueDiffer = keyValueDiffers.find({}).create(null); | ||
} | ||
|
||
ngAfterViewInit() { | ||
this.elementRef.nativeElement.style.textAlign = this.options.align ? this.options.align : this.optionsTemplate.align; | ||
} | ||
ngAfterViewInit() { | ||
this.elementRef.nativeElement.style.textAlign = this.options.align ? this.options.align : this.optionsTemplate.align; | ||
} | ||
|
||
ngDoCheck() { | ||
if (this.keyValueDiffer.diff(this.options)) { | ||
this.elementRef.nativeElement.style.textAlign = this.options.align ? this.options.align : this.optionsTemplate.align; | ||
this.inputHandler.updateOptions((<any>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((<any>Object).assign({}, this.optionsTemplate, this.options)); | ||
} | ||
} | ||
} | ||
|
||
ngOnInit() { | ||
this.inputHandler = new InputHandler(this.elementRef.nativeElement, (<any>Object).assign({}, this.optionsTemplate, this.options)); | ||
} | ||
ngOnInit() { | ||
this.inputHandler = new InputHandler(this.elementRef.nativeElement, (<any>Object).assign({}, this.optionsTemplate, this.options)); | ||
} | ||
|
||
@HostListener("blur", ["$event"]) | ||
handleBlur(event: any) { | ||
this.inputHandler.getOnModelTouched().apply(event); | ||
} | ||
@HostListener("blur", ["$event"]) | ||
handleBlur(event: any) { | ||
this.inputHandler.getOnModelTouched().apply(event); | ||
} | ||
|
||
@HostListener("cut", ["$event"]) | ||
handleCut(event: any) { | ||
if (!this.isChromeAndroid()) { | ||
this.inputHandler.handleCut(event); | ||
@HostListener("cut", ["$event"]) | ||
handleCut(event: any) { | ||
if (!this.isChromeAndroid()) { | ||
this.inputHandler.handleCut(event); | ||
} | ||
} | ||
} | ||
|
||
@HostListener("input", ["$event"]) | ||
handleInput(event: any) { | ||
if (this.isChromeAndroid()) { | ||
this.inputHandler.handleInput(event); | ||
@HostListener("input", ["$event"]) | ||
handleInput(event: any) { | ||
if (this.isChromeAndroid()) { | ||
this.inputHandler.handleInput(event); | ||
} | ||
} | ||
} | ||
|
||
@HostListener("keydown", ["$event"]) | ||
handleKeydown(event: any) { | ||
if (!this.isChromeAndroid()) { | ||
this.inputHandler.handleKeydown(event); | ||
@HostListener("keydown", ["$event"]) | ||
handleKeydown(event: any) { | ||
if (!this.isChromeAndroid()) { | ||
this.inputHandler.handleKeydown(event); | ||
} | ||
} | ||
} | ||
|
||
@HostListener("keypress", ["$event"]) | ||
handleKeypress(event: any) { | ||
if (!this.isChromeAndroid()) { | ||
this.inputHandler.handleKeypress(event); | ||
@HostListener("keypress", ["$event"]) | ||
handleKeypress(event: any) { | ||
if (!this.isChromeAndroid()) { | ||
this.inputHandler.handleKeypress(event); | ||
} | ||
} | ||
} | ||
|
||
@HostListener("paste", ["$event"]) | ||
handlePaste(event: any) { | ||
if (!this.isChromeAndroid()) { | ||
this.inputHandler.handlePaste(event); | ||
@HostListener("paste", ["$event"]) | ||
handlePaste(event: any) { | ||
if (!this.isChromeAndroid()) { | ||
this.inputHandler.handlePaste(event); | ||
} | ||
} | ||
} | ||
|
||
isChromeAndroid(): boolean { | ||
return /chrome/i.test(navigator.userAgent) && /android/i.test(navigator.userAgent); | ||
} | ||
isChromeAndroid(): boolean { | ||
return /chrome/i.test(navigator.userAgent) && /android/i.test(navigator.userAgent); | ||
} | ||
|
||
registerOnChange(callbackFunction: Function): void { | ||
this.inputHandler.setOnModelChange(callbackFunction); | ||
} | ||
registerOnChange(callbackFunction: Function): void { | ||
this.inputHandler.setOnModelChange(callbackFunction); | ||
} | ||
|
||
registerOnTouched(callbackFunction: Function): void { | ||
this.inputHandler.setOnModelTouched(callbackFunction); | ||
} | ||
registerOnTouched(callbackFunction: Function): void { | ||
this.inputHandler.setOnModelTouched(callbackFunction); | ||
} | ||
|
||
setDisabledState(value: boolean): void { | ||
this.elementRef.nativeElement.disabled = value; | ||
} | ||
setDisabledState(value: boolean): void { | ||
this.elementRef.nativeElement.disabled = value; | ||
} | ||
|
||
writeValue(value: number): void { | ||
this.inputHandler.setValue(value); | ||
} | ||
writeValue(value: number): void { | ||
this.inputHandler.setValue(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { FormsModule } from '@angular/forms'; | ||
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 { | ||
} |
Oops, something went wrong.