Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. import { Directive, Input, OnInit, HostListener } from '@angular/core';
  2. import { NgControl } from '@angular/forms';
  3. import { CustomerService } from '../../core/services/customer.service';
  4. import { Customer } from '../../core/models/customer.model';
  5. @Directive({
  6. selector: '[phoneMasking]'
  7. })
  8. export class PhoneMaskingDirective implements OnInit {
  9. country: string = 'US';
  10. private PNF = require('google-libphonenumber').PhoneNumberFormat;
  11. private phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();
  12. private AsYouTypeFormatter = require('google-libphonenumber').AsYouTypeFormatter;
  13. private formatter = new this.AsYouTypeFormatter(this.country);
  14. formattedPhone: string;
  15. customer: Customer;
  16. addPlus: boolean = false;
  17.  
  18. constructor(
  19. private control: NgControl,
  20. private customerService: CustomerService
  21. ) {}
  22. ngOnInit() {
  23. if (this.control.control.value) {
  24. this.formattedPhone = this.control.control.value;
  25. let newFormatted = this.phoneUtil.parse(this.formattedPhone, this.country);
  26. }
  27. this.customerService.getUserInfo().subscribe(customer => this.customer = customer);
  28. if(this.customer !== null) {
  29. this.country = this.customer.mainCountry;
  30. }
  31. }
  32. @HostListener('change') onChange() {
  33. this.formattedPhone = this.control.control.value;
  34. let newFormatted = this.phoneUtil.parse(this.formattedPhone, this.country);
  35. this.control.control.setValue(this.phoneUtil.format(newFormatted, this.country));
  36. this.formattedPhone = this.control.control.value;
  37. this.control.control.markAsDirty();
  38. }
  39. @HostListener('keydown', ['$event']) onkeydown(event) {
  40. this.control.control.markAsDirty();
  41. //only runs if a number key without shift key held down is clicked or '+' key
  42. if (((event.keyCode >= 48 && event.shiftKey === false) && (event.keyCode <= 57 && event.shiftKey === false))
  43. || ((event.keyCode >= 96 && event.shiftKey === false) && (event.keyCode <= 105 && event.shiftKey === false))
  44. || (event.keyCode == 187 && event.shiftKey === true) || event.keyCode == 107) {
  45. if (this.formattedPhone !== undefined) {
  46. event.preventDefault();
  47. }
  48. this.formattedPhone = this.formatter.inputDigit(event.key);
  49. this.formattedPhone.indexOf('+') > -1 ? this.addPlus = true : this.addPlus = false;
  50. if (this.formattedPhone.length <= 1) {
  51. event.preventDefault();
  52. }
  53. this.control.control.setValue(this.formattedPhone);
  54. //only runs if backspace or delete is clicked
  55. } else if (event.keyCode == 8 || event.keyCode == 46) {
  56. event.preventDefault();
  57. this.formattedPhone = this.formattedPhone.substring(0, this.formattedPhone.length - 1);
  58. this.formattedPhone.indexOf('+') > -1 ? this.addPlus = true : this.addPlus = false;
  59. this.formatter.clear();
  60. let phoneArray = this.formattedPhone.replace(/\D+/g, '').split('');
  61. if (this.addPlus === true) {
  62. this.formatter.inputDigit('+');
  63. }
  64. phoneArray.map(digit => {
  65. this.formattedPhone = this.formatter.inputDigit(digit);
  66. })
  67. this.control.control.setValue(this.formattedPhone);
  68. //runs when ctrl + v
  69. } else if ((event.keyCode == 86 && event.ctrlKey === true) || event.keyCode == 9) {
  70. return;
  71. //runs when anything else is clicked
  72. } else {
  73. event.preventDefault();
  74. }
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement