Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
nbfontana committed Sep 10, 2018
1 parent 32817ac commit 23b3d5d
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 119 deletions.
1 change: 0 additions & 1 deletion karma.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
18 changes: 9 additions & 9 deletions src/currency-mask.config.ts
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");
190 changes: 96 additions & 94 deletions src/currency-mask.directive.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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<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();
@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;
}

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((<any>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, (<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));
}
}

@HostListener("blur", ["$event"])
handleBlur(event: any) {
this.inputHandler.getOnModelTouched().apply(event);
}
ngOnInit() {
this.inputHandler = new InputHandler(this.elementRef.nativeElement, (<any>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);
}
}
17 changes: 4 additions & 13 deletions src/currency-mask.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
3 changes: 2 additions & 1 deletion src/input.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion src/input.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 23b3d5d

Please sign in to comment.