Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. import { Member } from './../../../membership-models/member.model';
  2. import { MembershipApiService } from './../../../membership-serivce/membership.api';
  3. import { Application } from './../../../../nots-common/api/models/application.model';
  4. import { FormBuilder, FormArray, Validators, FormGroup, FormControl } from '@angular/forms';
  5. import { Loadable } from './../../../../nots-utils/xLoader/loadable.base';
  6. import { Component, OnInit } from '@angular/core';
  7. import { MembershipEnablementDetails } from 'app/nots-membership/membership-models/membershipEnablementDetails.model';
  8. import { ConnectionIPDetails } from 'app/nots-membership/membership-models/connectionIPDetails.model';
  9. import { EnablementValidators } from 'app/nots-membership/custom-validators/customValidator';
  10.  
  11. @Component({
  12. selector: 'app-membership-connection',
  13. templateUrl: './membership-connection.component.html',
  14. styleUrls: ['./membership-connection.component.css']
  15. })
  16. export class MembershipConnectionComponent extends Loadable implements OnInit {
  17. model: MembershipEnablementDetails;
  18. mode = 'new';
  19. connectionIPSForm: FormGroup;
  20. member: Member;
  21. invalidIpFormControls: Array<FormControl> = [];
  22.  
  23. constructor(private formBuilder: FormBuilder,
  24. private apiService: MembershipApiService,
  25. private enablementValidator: EnablementValidators) { super(); }
  26.  
  27. ngOnInit() {
  28. this.apiService.getMemberByID(this.model.member.id).then(response => {
  29. this.member = response;
  30. console.log(this.member);
  31. }).catch(err => {
  32. console.dir(err);
  33. });
  34. }
  35.  
  36. createIP(): FormGroup {
  37. const octet = '(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])';
  38. const ip = '(?:' + octet + '\\.){3}' + octet;
  39. const quad = '(?:\\[' + ip + '\\])|(?:' + ip + ')';
  40. const ipRE = new RegExp('(' + quad + ')');
  41. return this.formBuilder.group({
  42. connectionNumber: ['', Validators.required],
  43. ip: ['', [Validators.required, Validators.pattern(ipRE), this.uniqueIPS.bind(this)],
  44. this.enablementValidator.checkIP.bind(this)],
  45. location: ['', Validators.required]
  46. });
  47. }
  48.  
  49. uniqueIPS(control: FormControl): any {
  50. if (this.model.notsConnectionIDS.map(item => item.connectionIPS).filter(ip => ip === control.value).length > 1) {
  51. if (!this.invalidIpFormControls.includes(control)) {
  52. this.invalidIpFormControls.push(control);
  53. }
  54. return { 'error': true };
  55. }
  56. this.invalidIpFormControls.forEach(item => {
  57. if (item != control) {
  58. this.invalidIpFormControls = this.invalidIpFormControls.filter(item2 => item2 !== control);
  59. item.reset(item.value);
  60. }
  61. });
  62. return null;
  63. }
  64.  
  65. doMagic() {
  66. this.connectionIPSForm.reset(this.connectionIPSForm.value);
  67. }
  68. addFormDetail() {
  69. const control = <FormArray>this.connectionIPSForm.controls['connectionIPS'];
  70. control.push(this.createIP());
  71. this.model.notsConnectionIDS.push(new ConnectionIPDetails());
  72. }
  73.  
  74. removeFormDetail(i: number) {
  75. const control = <FormArray>this.connectionIPSForm.controls['connectionIPS'];
  76. control.removeAt(i);
  77. this.model.notsConnectionIDS.splice(i, 1);
  78. }
  79.  
  80. loadDataObject(data: Application, loaderId?: String) {
  81. this.model = data.memberEnablementDetails;
  82. this.connectionIPSForm = this.formBuilder.group({
  83. connectionIPS: this.formBuilder.array([])
  84. });
  85. }
  86.  
  87. onSave() {
  88. return new Promise((resolve, reject) => {
  89. resolve(this.connectionIPSForm.valid);
  90. });
  91. }
  92.  
  93. isValid() { return new Promise((resolve, reject) => { resolve(this.connectionIPSForm.valid); }); }
  94.  
  95. loaded(loaderId?: String) {
  96. if (this.model.notsConnectionIDS.length === 0) {
  97. this.model.notsConnectionIDS.push(new ConnectionIPDetails());
  98. }
  99.  
  100. const control = <FormArray>this.connectionIPSForm.controls['connectionIPS'];
  101. this.model.notsConnectionIDS.forEach(item => {
  102. control.push(this.createIP());
  103. });
  104.  
  105. console.log(control);
  106. }
  107.  
  108.  
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement