Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {Directive, HostListener, ElementRef}    from '@angular/core';
  2.  
  3. @Directive({
  4.     selector: '[myPhoneMask]',
  5. })
  6. export class PhoneMaskDirective {
  7.     ddd: string = '';
  8.     prefix: string = '';
  9.     end: string = '';
  10.     mobileDigit: string = '';
  11.     formattedPhone: string = '';
  12.     elementValue: string;
  13.     lastKnowLength: any = 0;
  14.  
  15.     constructor(_elRef: ElementRef) {}
  16.  
  17.     setDDD(inputLength) {
  18.         if (inputLength >= 2) {
  19.             this.ddd = this.elementValue.slice(0, 2);
  20.             return '(' + this.ddd + ') ';
  21.         }
  22.         return '';
  23.     }
  24.     setPrefix(inputLength) {
  25.         if (inputLength >= 6) {
  26.             this.prefix = this.elementValue.slice(2, 6);
  27.             return this.prefix;
  28.         }
  29.         return '';
  30.     }
  31.     setEnd(inputLength) {
  32.         if (inputLength === 10) {
  33.             this.end = this.elementValue.slice(6, 10);
  34.             this.formattedPhone += '-' + this.end;
  35.             this.formatPush(this.formattedPhone);
  36.         }
  37.         return;
  38.     }
  39.     setEndMobile(inputLength) {
  40.         if (inputLength === 11) {
  41.             this.mobileDigit = this.elementValue.slice(6, 7);
  42.             this.formattedPhone += this.mobileDigit + '-';
  43.             this.end = this.elementValue.slice(7, 11);
  44.             this.formattedPhone += this.end;
  45.             this.formatPush(this.formattedPhone);
  46.         }
  47.         return;
  48.     }
  49.  
  50.     @HostListener('keyup') onInputChange() {
  51.         // Receive the input value (Digits only)
  52.         this.elementValue = this.getModelValue();
  53.         // store the length to compare and allow the user to backspace
  54.         let inputLength: any = this.elementValue.length;
  55.         // set the DDD code
  56.         this.formattedPhone = this.setDDD(inputLength);
  57.         // set the Prefix number
  58.         this.formattedPhone += this.setPrefix(inputLength);
  59.  
  60.         /*
  61.          * inputLength 10 and 11 do the formatPush on them selves to display the effect
  62.          * of the number changing the position between the slash -
  63.          */
  64.         this.setEnd(inputLength);
  65.         // if it's mobile, in brazil it has 11 digits including DDD
  66.         this.setEndMobile(inputLength);
  67.         // If the user want to backspace, it'll compare if the lastKnowLength is bigger then the inputLength
  68.         if (this.lastKnowLength < inputLength) {
  69.             // this will format the field while user is typing
  70.             if (inputLength === 2 || inputLength === 6) {
  71.                 this.formatPush(this.formattedPhone);
  72.             }
  73.         }
  74.         // set the lastKnowLength
  75.         this.lastKnowLength = inputLength;
  76.     }
  77.  
  78.     formatPush(formattedValue) {
  79.         this._elRef.nativeElement.value = formattedValue;
  80.     }
  81.  
  82.     getModelValue() {
  83.         let elementValue = this._elRef.nativeElement.value;
  84.         if (/[a-z]/.test(elementValue)) {
  85.             this.formatPush(elementValue.replace(/\D/g, ''));
  86.         }
  87.         return elementValue.replace(/\D/g, '');
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement