Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn mask helper to MaskParsingService #6703

Merged
merged 1 commit into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Injectable } from '@angular/core';

/**
* @hidden
*/
Expand All @@ -19,7 +21,10 @@ export const KEYS = {
/**
* @hidden
*/
export class MaskHelper {
@Injectable({
providedIn: 'root'
})
export class MaskParsingService {
private _cursor;
public get cursor() {
return this._cursor;
Expand Down
31 changes: 12 additions & 19 deletions projects/igniteui-angular/src/lib/directives/mask/mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
PipeTransform
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { KEYS, MaskHelper } from './mask-helper';
import { KEYS, MaskParsingService } from './mask-parsing-service';
import { isIE, IBaseEventArgs } from '../../core/utils';

const noop = () => { };
Expand Down Expand Up @@ -185,11 +185,6 @@ export class IgxMaskDirective implements OnInit, ControlValueAccessor {

private _stopPropagation: boolean;

/**
*@hidden
*/
private maskHelper: MaskHelper;

/**
*@hidden
*/
Expand All @@ -200,9 +195,7 @@ export class IgxMaskDirective implements OnInit, ControlValueAccessor {
*/
private _onChangeCallback: (_: any) => void = noop;

constructor(private elementRef: ElementRef) {
this.maskHelper = new MaskHelper();
}
constructor(private elementRef: ElementRef, private maskParser: MaskParsingService) { }

/**
*@hidden
Expand Down Expand Up @@ -277,23 +270,23 @@ export class IgxMaskDirective implements OnInit, ControlValueAccessor {
this._paste = false;

const clipboardData = this.value.substring(this._cursorOnPaste, this.getCursorPosition());
this.value = this.maskHelper.parseValueByMaskUponCopyPaste(
this.value = this.maskParser.parseValueByMaskUponCopyPaste(
this._valOnPaste, this._maskOptions, this._cursorOnPaste, clipboardData, this._selection);

this.setCursorPosition(this.maskHelper.cursor);
this.setCursorPosition(this.maskParser.cursor);
} else {
const currentCursorPos = this.getCursorPosition();

this.maskHelper.data = (this._key === KEYS.BACKSPACE) || (this._key === KEYS.DELETE);
this.maskParser.data = (this._key === KEYS.BACKSPACE) || (this._key === KEYS.DELETE);

this.value = this._selection && this._selection !== 0 ?
this.maskHelper.parseValueByMaskUponSelection(this.value, this._maskOptions, currentCursorPos - 1, this._selection) :
this.maskHelper.parseValueByMask(this.value, this._maskOptions, currentCursorPos - 1);
this.maskParser.parseValueByMaskUponSelection(this.value, this._maskOptions, currentCursorPos - 1, this._selection) :
this.maskParser.parseValueByMask(this.value, this._maskOptions, currentCursorPos - 1);

this.setCursorPosition(this.maskHelper.cursor);
this.setCursorPosition(this.maskParser.cursor);
}

const rawVal = this.maskHelper.restoreValueFromMask(this.value, this._maskOptions);
const rawVal = this.maskParser.restoreValueFromMask(this.value, this._maskOptions);

this.dataValue = this.includeLiterals ? this.value : rawVal;
this._onChangeCallback(this.dataValue);
Expand All @@ -312,7 +305,7 @@ export class IgxMaskDirective implements OnInit, ControlValueAccessor {
}
this.value = this.focusedValuePipe.transform(value);
} else {
this.value = this.maskHelper.parseValueByMaskOnInit(this.value, this._maskOptions);
this.value = this.maskParser.parseValueByMaskOnInit(this.value, this._maskOptions);
}
}

Expand All @@ -323,7 +316,7 @@ export class IgxMaskDirective implements OnInit, ControlValueAccessor {
public onBlur(value) {
if (this.displayValuePipe) {
this.value = this.displayValuePipe.transform(value);
} else if (value === this.maskHelper.parseMask(this._maskOptions)) {
} else if (value === this.maskParser.parseMask(this._maskOptions)) {
this.value = '';
}
}
Expand All @@ -350,7 +343,7 @@ export class IgxMaskDirective implements OnInit, ControlValueAccessor {
this._maskOptions.promptChar = this.promptChar.substring(0, 1);
}

this.value = value ? this.maskHelper.parseValueByMaskOnInit(value, this._maskOptions) : '';
this.value = value ? this.maskParser.parseValueByMaskOnInit(value, this._maskOptions) : '';
if (this.displayValuePipe) {
this.value = this.displayValuePipe.transform(this.value);
}
Expand Down