src/currency-mask.directive.ts
providers |
CURRENCYMASKDIRECTIVE_VALUE_ACCESSOR
|
selector | [currencyMask] |
options
|
Type: |
Defined in src/currency-mask.directive.ts:32
|
constructor(currencyMaskConfig: CurrencyMaskConfig, elementRef: any, keyValueDiffers: KeyValueDiffers)
|
Defined in src/currency-mask.directive.ts:47
|
ngAfterViewInit |
ngAfterViewInit()
|
Defined in src/currency-mask.directive.ts:59
|
Returns:
void
|
ngDoCheck |
ngDoCheck()
|
Defined in src/currency-mask.directive.ts:63
|
Returns:
void
|
ngOnInit |
ngOnInit()
|
Defined in src/currency-mask.directive.ts:70
|
Returns:
void
|
handleBlur |
handleBlur(event: any)
|
Defined in src/currency-mask.directive.ts:75
|
Returns:
void
|
handleCut |
handleCut(event: any)
|
Defined in src/currency-mask.directive.ts:80
|
Returns:
void
|
handleInput |
handleInput(event: any)
|
Defined in src/currency-mask.directive.ts:87
|
Returns:
void
|
handleKeydown |
handleKeydown(event: any)
|
Defined in src/currency-mask.directive.ts:94
|
Returns:
void
|
handleKeypress |
handleKeypress(event: any)
|
Defined in src/currency-mask.directive.ts:101
|
Returns:
void
|
handlePaste |
handlePaste(event: any)
|
Defined in src/currency-mask.directive.ts:108
|
Returns:
void
|
isChromeAndroid |
isChromeAndroid()
|
Defined in src/currency-mask.directive.ts:114
|
Returns:
boolean
|
registerOnChange |
registerOnChange(callbackFunction: Function)
|
Defined in src/currency-mask.directive.ts:118
|
Returns:
void
|
registerOnTouched |
registerOnTouched(callbackFunction: Function)
|
Defined in src/currency-mask.directive.ts:122
|
Returns:
void
|
setDisabledState |
setDisabledState(value: boolean)
|
Defined in src/currency-mask.directive.ts:126
|
Returns:
void
|
writeValue |
writeValue(value: number)
|
Defined in src/currency-mask.directive.ts:130
|
Returns:
void
|
Public inputHandler |
inputHandler: |
Defined in src/currency-mask.directive.ts:34
|
Public keyValueDiffer |
keyValueDiffer: |
Defined in src/currency-mask.directive.ts:35
|
Public optionsTemplate |
optionsTemplate: |
Defined in src/currency-mask.directive.ts:37
|
import {
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,
};
@Directive({
selector: "[currencyMask]",
providers: [CURRENCYMASKDIRECTIVE_VALUE_ACCESSOR]
})
export class CurrencyMaskDirective implements AfterViewInit, ControlValueAccessor, DoCheck, OnInit {
@Input() options: Partial<CurrencyMaskConfig> = {};
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();
}
ngAfterViewInit() {
this.elementRef.nativeElement.style.textAlign = this.options && 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));
}
}
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("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("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("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);
}
registerOnChange(callbackFunction: Function): void {
this.inputHandler.setOnModelChange(callbackFunction);
}
registerOnTouched(callbackFunction: Function): void {
this.inputHandler.setOnModelTouched(callbackFunction);
}
setDisabledState(value: boolean): void {
this.elementRef.nativeElement.disabled = value;
}
writeValue(value: number): void {
this.inputHandler.setValue(value);
}
}