1 import { Component, OnInit, Input, Output, EventEmitter, ElementRef } from '@angular/core';
2 import { TRANSFRER_FORM_ITEMS, CORE_FORM_ITEMS, ADDRESS , NexthopInfo_Options } from '@src/constants/constants';
3 import { NzMessageService } from "ng-zorro-antd";
6 selector: 'app-subnet-params-model',
7 templateUrl: './subnet-params-model.component.html',
8 styleUrls: ['./subnet-params-model.component.less']
10 export class SubnetParamsModelComponent implements OnInit {
12 @Input() showModel: boolean;
13 @Input() detailData: any;
14 @Input() title: string;
15 @Output() cancel = new EventEmitter<boolean>();
16 @Output() paramsDataChange = new EventEmitter<any>();
18 transferFormItems = TRANSFRER_FORM_ITEMS;
19 regxpIP = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/; // check for correct ip address
21 coreFormItems : any = [];
23 // 2020.08.17 Add 3 parameters for Endpoint, Comment: The following code
24 NexthopInfoOptions = NexthopInfo_Options;
25 EndpointInputs: any[] = [];
26 EndpointEnable: boolean = true; // Whether to enable the three parameters of Endpoint
27 // Comment: Above code
30 private message: NzMessageService,
39 this.coreFormItems = this.title === 'An'?CORE_FORM_ITEMS.An:this.title === 'Cn'?CORE_FORM_ITEMS.Cn:[];
40 this.formData = JSON.parse(JSON.stringify(this.detailData));
41 if(this.formData !==undefined && Object.keys(this.formData).length!==0){
42 this.EndpointEnable = (this.formData.hasOwnProperty("an_Endpoint") && this.formData['an_Endpoint'].length!==0) || (this.formData.hasOwnProperty("cn_Endpoint") && this.formData['cn_Endpoint'].length!==0)
44 // -------> 2020.08.17 Add 3 parameters for Endpoint, Comment: The following code
45 if(this.EndpointEnable){
46 this.EndpointInputs = this.title === 'An'
47 ?this.formData["an_Endpoint"]
49 ?this.formData["cn_Endpoint"]
52 this.coreFormItems.map((item,index)=>{
53 if(item.title === 'Endpoint'){
54 this.coreFormItems.splice(index,1)
59 //-------> Comment: Above code
60 if (this.title === 'An') {
61 this.AreaFormatting();
64 validateEndPoint (key: string, value: any): string {
66 return 'can not be empty';
68 if (key === 'ip_address') {
69 if (!this.regxpIP.test(value)) {
70 return 'xxx.xxx.xxx.xxx';
74 } else if (key === 'logical_link') {
85 onInput ($event:any, title: string) {
89 const target = $event.target;
90 if (title === 'ip_address') {
91 // only number and '.' can be inputted
92 const regexp = /[^\d^\.]+/g;
93 target.value = target.value.replace(regexp, '');
94 } else if (title === 'logical_link') {
95 // only number can be inputted
96 const regxp = /[^\d]/g;
97 target.value = target.value.replace(regxp, '');
101 let areaList = [...this.formData.an_coverage_area_ta_list];
102 this.areaList = areaList.map ( (item: any) => {
103 let arr = item.split(';');
104 item = arr.map( (ite, index) => {
108 } else if (index === 1){
116 obj.options = [{name: ite, id: ite}]
123 creatAreaList (): void {
141 this.areaList.push(arr)
144 deleteAreaList (index: number): void {
145 this.areaList.splice(index,1);
148 handleChange(area: any[], areaItem: any): void{
149 if (areaItem.key === 'province' && areaItem.options.length <= 1) {
150 areaItem.options = ADDRESS;
151 } else if (areaItem.key === 'city' && areaItem.options.length <= 1) {
152 ADDRESS.forEach( item => {
153 if(item.name === area[0].selected) {
154 areaItem.options = item.city;
157 }else if (areaItem.key === 'district' && areaItem.options.length <= 1) {
158 ADDRESS.forEach( (item: any) => {
159 item.city.forEach(city => {
160 if (city.name === area[1].selected) {
161 areaItem.options = city.county;
168 handleChangeSelected(area: any[], areaItem: any) {
169 if (areaItem.key === 'province') {
170 area[1].selected = ''
171 area[1].options = [];
172 area[2].selected = '';
173 area[2].options = [];
174 } else if (areaItem.key === 'city') {
175 area[2].selected = '';
176 area[2].options = [];
181 this.showModel = false
182 this.cancel.emit(this.showModel)
187 this.areaList.forEach((item) => {
188 if (item.some((val) => {return val['selected'] === ''})) {
193 return 'can not be empty!';
200 return Object.prototype.toString.call(a)
203 // used to verify that each item is not empty in a object
205 let type = this.judgeType(target);
206 if (type === '[object Array]') {
207 for (let i = 0; i < target.length; i++) {
208 if (!this.deepCheck(target[i])) {
212 } else if (type === '[object Object]') {
213 for (const prop in target) {
214 if (target.hasOwnProperty(prop)) { // special handling for address
215 if (prop === 'an_coverage_area_ta_list' || prop ==='cn_coverage_area_ta_list') {
216 return target[prop].every((item) => {return this.deepCheck(item.split(';'))});
217 } else if (!this.deepCheck(target[prop])) {
223 if (!target && target!==0) {
233 endCheckBeforeSubmit () {
234 // check params of Endpoint
235 let result = [true, ''];
236 let formatedEndpoint = {};
237 this.EndpointInputs.forEach((item) => {
238 formatedEndpoint[Object.keys(item)[0]] = item[Object.keys(item)[0]];
240 if (this.title === 'An') {
241 for (let prop in formatedEndpoint) {
242 if (prop === 'an_ip_address') {
243 if (!this.regxpIP.test(formatedEndpoint[prop])) {
244 result = [false, 'Illegal IpAddress']
246 } else if (prop === 'an_logical_link') {
247 if (isNaN(formatedEndpoint[prop])) {
248 result = [false, 'LogicalID can only be a number']
252 } else if (this.title === 'Cn') {
253 for (let prop in formatedEndpoint) {
254 if (prop === 'cn_ip_address') {
255 if (!this.regxpIP.test(formatedEndpoint[prop])) {
256 result = [false, 'Illegal IpAddress']
258 } else if (prop === 'cn_logical_link') {
259 if (isNaN(formatedEndpoint[prop])) {
260 result = [false, 'LogicalID can only be a number']
269 // Verify that items of EndPoint is correct
270 let endCheckResult = this.endCheckBeforeSubmit()
271 if (!endCheckResult[0]) {
272 this.message.error(endCheckResult[1].toString());
276 if (this.title === 'An') {
277 const an_coverage_area_ta_list: string[] = [];
278 this.areaList.forEach( item => {
279 let str: string = '';
280 item.forEach( area => {
281 str += area.selected + ';';
283 an_coverage_area_ta_list.push(str.substring(0, str.length-1));
285 params = {...this.formData, an_coverage_area_ta_list};
287 params = {...this.formData};
289 // Verify that each item is not empty
290 if (this.deepCheck(params)) {
291 this.paramsDataChange.emit(params);
294 this.message.error('Please complete the form');