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') {
75 if (!this.isInteger(value)){
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)
185 checkArea (area: any) {
186 if (area.every((item) => {return item.selected === ''})) {
189 if (area.some((item) => {return item.selected === ''})) {
196 return Object.prototype.toString.call(a)
199 // used to verify that each item is not empty in a object
201 let type = this.judgeType(target);
202 if (type === '[object Array]') {
203 for (let i = 0; i < target.length; i++) {
204 if (!this.deepCheck(target[i])) {
208 } else if (type === '[object Object]') {
209 for (const prop in target) {
210 if (target.hasOwnProperty(prop)) { // special handling for address
211 if (prop === 'an_coverage_area_ta_list' || prop ==='cn_coverage_area_ta_list') {
212 return target[prop].every((item) => {return this.deepCheck(item.split(';'))});
213 } else if (!this.deepCheck(target[prop])) {
219 if (!target && target!==0) {
228 isInteger (value: any) {
229 // for common string and undefined, eg '123a3'
232 } else if (isNaN(parseInt(value))) {
234 } else if (Number(value) >= 0 && Number(value)%1 !== 0){
241 endCheckBeforeSubmit () {
242 // check params of Endpoint
243 let result = [true, ''];
244 let formatedEndpoint = {};
245 this.EndpointInputs.forEach((item) => {
246 formatedEndpoint[Object.keys(item)[0]] = item[Object.keys(item)[0]];
248 if (this.title === 'An') {
249 for (let prop in formatedEndpoint) {
250 if (prop === 'an_ip_address') {
251 if (!this.regxpIP.test(formatedEndpoint[prop])) {
252 result = [false, 'Illegal IpAddress']
254 } else if (prop === 'an_logical_link') {
255 if (!this.isInteger(formatedEndpoint[prop])) {
256 result = [false, 'LogicalID can only be an integer']
260 } else if (this.title === 'Cn') {
261 for (let prop in formatedEndpoint) {
262 if (prop === 'cn_ip_address') {
263 if (!this.regxpIP.test(formatedEndpoint[prop])) {
264 result = [false, 'Illegal IpAddress']
266 } else if (prop === 'cn_logical_link') {
267 if (!this.isInteger(formatedEndpoint[prop])) {
268 result = [false, 'LogicalID can only be an integer']
277 // Verify that items of EndPoint is correct
278 let endCheckResult = this.endCheckBeforeSubmit()
279 if (!endCheckResult[0]) {
280 this.message.error(endCheckResult[1].toString());
284 if (this.title === 'An') {
285 const an_coverage_area_ta_list: string[] = [];
286 this.areaList.forEach( item => {
287 let str: string = '';
288 item.forEach( area => {
289 str += area.selected + ';';
291 an_coverage_area_ta_list.push(str.substring(0, str.length-1));
293 params = {...this.formData, an_coverage_area_ta_list};
295 params = {...this.formData};
297 // Verify that each item is not empty
298 if (this.deepCheck(params)) {
299 this.paramsDataChange.emit(params);
302 this.message.error('Please complete the form');