Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2016
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {Injectable, Directive, ElementRef, Output, EventEmitter} from '@angular/core';
  2. import {NgModel} from '@angular/common';
  3. import {FormControl} from "@angular/forms";
  4.  
  5. declare var jQuery: any;
  6.  
  7. @Directive({
  8.     selector: '[mask]',
  9.     providers: [
  10.         NgModel
  11.     ],
  12.     host: {
  13.         '(input)': 'onInputChange($event)'
  14.     }
  15. })
  16.  
  17. @Injectable()
  18. export class INPUT_MASK_DIRECTIVE {
  19.  
  20.     public mask: any = ['(', /[0-9]/, /[0-9]/, /[0-9]/, ')', '-', /[0-9]/, /[0-9]/, /[0-9]/, '-', /[0-9]/, /[0-9]/, /[0-9]/, /[0-9]/];
  21.  
  22.     /**
  23.      *
  24.      * @type {number}
  25.      */
  26.     public lastPosition: number = 0;
  27.  
  28.  
  29.     /**
  30.      *
  31.      * @type {string}
  32.      */
  33.     public currentValue: string = '';
  34.  
  35.     /**
  36.      *
  37.      * @type {string}
  38.      */
  39.     public previousValue: string = '';
  40.  
  41.     /**
  42.      *
  43.      * @type {any}
  44.      */
  45.     public el: any;
  46.  
  47.     /**
  48.      *
  49.      * @type {FormControl}
  50.      */
  51.     public formControl = new FormControl();
  52.  
  53.     /**
  54.      *
  55.      * @param _elementRef
  56.      * @param _model
  57.      */
  58.     public constructor(private _elementRef: ElementRef,
  59.                        private _model: NgModel) {
  60.     }
  61.  
  62.     /**
  63.      *
  64.      * @param {any} event
  65.      */
  66.     onInputChange(event: any) {
  67.  
  68.         let value: any = event.target.value.split('');
  69.         let position: number = event.target.selectionStart;
  70.         let result: any = [];
  71.         let empty: boolean = true;
  72.  
  73.         for (let i = 0; i < this.mask.length; ++i) {
  74.  
  75.             if (typeof this.mask[i] == 'object') {
  76.                 if (value[i] && this.mask[i].test(value[i])) {
  77.                     result.push(value[i]);
  78.                     empty = false;
  79.                 } else {
  80.                     result.push(' ');
  81.                 }
  82.             } else {
  83.                 result.push(this.mask[i]);
  84.             }
  85.  
  86.         }
  87.  
  88.         if (position == 0 && empty) {
  89.             result = [];
  90.         }
  91.  
  92.         position = this.getPosition(position);
  93.         this.lastPosition = position;
  94.  
  95.         value = result.join('');
  96.  
  97.         this._model.viewToModelUpdate(value);
  98.         this._model.control.updateValue(value);
  99.  
  100.         this._model.valueAccessor.writeValue(value);
  101.  
  102.         event.target.setSelectionRange(position, position);
  103.     }
  104.  
  105.     /**
  106.      *
  107.      * @param {number} position
  108.      * @param {boolean} del
  109.      * @return {number}
  110.      */
  111.     public getPosition(position: number, del = false): number {
  112.         if (this.mask[position] && typeof this.mask[position] != 'object') {
  113.             if (position > this.lastPosition) {
  114.                 ++position;
  115.             } else {
  116.                 del = true;
  117.                 --position;
  118.             }
  119.             return this.getPosition(position, del);
  120.         } else {
  121.             if (del) {
  122.                 ++position;
  123.             }
  124.             return position;
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement