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";
4 import { stringify } from '@angular/core/src/util';
5 import { Util } from '../../../../../../shared/utils/utils';
8 selector: 'app-subnet-params-model',
9 templateUrl: './subnet-params-model.component.html',
10 styleUrls: ['./subnet-params-model.component.less']
12 export class SubnetParamsModelComponent implements OnInit {
14 @Input() showModel: boolean;
15 @Input() detailData: any;
16 @Input() title: string;
17 @Output() cancel = new EventEmitter<boolean>();
18 @Output() paramsDataChange = new EventEmitter<any>();
20 transferFormItems = TRANSFRER_FORM_ITEMS;
21 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
23 coreFormItems : any = [];
25 // 2020.08.17 Add 3 parameters for Endpoint, Comment: The following code
26 NexthopInfoOptions = NexthopInfo_Options;
27 EndpointInputs: object = {};
28 EndpointEnable: boolean = true; // Whether to enable the three parameters of Endpoint
29 keyList: string[] = []; // keys of endPoint
30 // Comment: Above code
33 private message: NzMessageService,
43 this.coreFormItems = this.title === 'An'?CORE_FORM_ITEMS.An:this.title === 'Cn'?CORE_FORM_ITEMS.Cn:[];
44 this.formData = JSON.parse(JSON.stringify(this.detailData));
45 this.keyList = this.coreFormItems.find((item) => {return item.title === 'Endpoint'}).options.map((val) => {return val.key});
46 if(this.formData !==undefined && Object.keys(this.formData).length!==0){
47 this.EndpointEnable = this.keyList.every((item) => {return this.formData.hasOwnProperty(item)})
49 // -------> 2020.08.17 Add 3 parameters for Endpoint, Comment: The following code
50 if(this.EndpointEnable){
51 this.EndpointInputs = this.Util.pick(this.formData, this.keyList)// no?
53 this.coreFormItems.map((item,index)=>{
54 if(item.title === 'Endpoint'){
55 this.coreFormItems.splice(index,1)
60 //-------> Comment: Above code
61 if (this.title === 'An') {
62 this.AreaFormatting();
65 validateEndPoint (key: string, value: any): string {
66 if (this.Util.isEmpty(value)) {
67 return 'can not be empty';
69 if (key === 'ip_address') {
70 if (!this.regxpIP.test(value)) {
71 return 'xxx.xxx.xxx.xxx';
75 } else if (key === 'logical_link') {
76 if (!this.Util.isInteger(value)){
86 // onInput ($event:any, title: string) {
90 // const target = $event.target;
91 // if (title === 'ip_address') {
92 // // only number and '.' can be inputted
93 // const regexp = /[^\d^\.]+/g;
94 // target.value = target.value.replace(regexp, '');
95 // } else if (title === 'logical_link') {
96 // // only number can be inputted
97 // const regxp = /[^\d]/g;
98 // target.value = target.value.replace(regxp, '');
102 let areaList = [...this.formData.an_coverage_area_ta_list];
103 this.areaList = areaList.map ( (item: any) => {
104 let arr = item.split(';');
105 item = arr.map( (ite, index) => {
109 } else if (index === 1){
117 obj.options = [{name: ite, id: ite}]
124 creatAreaList (): void {
142 this.areaList.push(arr)
145 deleteAreaList (index: number): void {
146 this.areaList.splice(index,1);
149 handleChange(area: any[], areaItem: any): void{
150 if (areaItem.key === 'province' && areaItem.options.length <= 1) {
151 areaItem.options = ADDRESS;
152 } else if (areaItem.key === 'city' && areaItem.options.length <= 1) {
153 ADDRESS.forEach( item => {
154 if(item.name === area[0].selected) {
155 areaItem.options = item.city;
158 }else if (areaItem.key === 'district' && areaItem.options.length <= 1) {
159 ADDRESS.forEach( (item: any) => {
160 item.city.forEach(city => {
161 if (city.name === area[1].selected) {
162 areaItem.options = city.county;
169 handleChangeSelected(area: any[], areaItem: any) {
170 if (areaItem.key === 'province') {
171 area[1].selected = ''
172 area[1].options = [];
173 area[2].selected = '';
174 area[2].options = [];
175 } else if (areaItem.key === 'city') {
176 area[2].selected = '';
177 area[2].options = [];
182 this.showModel = false
183 this.cancel.emit(this.showModel)
186 // prompt text for each item of area_list
187 checkArea (area: any) {
188 if (area.every((item) => {return item.selected === ''})) {
191 if (area.some((item) => {return item.selected === ''})) {
197 // special handling for address
198 areaCheckBeforeSubmit (target: object) : Boolean{
199 for (const prop in target) {
200 if (target.hasOwnProperty(prop)) {
201 if (prop === 'an_coverage_area_ta_list' || prop ==='cn_coverage_area_ta_list') {
202 // if the vlaue is "shanghai;shanghai;", the input is incomplete
203 return target[prop].every((item) => {return this.Util.deepCheck(item.split(';'))});
210 endCheckBeforeSubmit () : Array<any>{
211 // check params of Endpoint
212 let result: Array<any> = [true, ''];
213 const endPointList = this.coreFormItems.find((item) => {return item.title === 'Endpoint'}).options;
216 for (let item of endPointList) {
217 if (item.title === 'ip_address') {
219 } else if (item.title === 'logical_link') {
220 logicalKey = item.key
223 for (let prop in this.EndpointInputs) {
224 if (prop === ipKey) {
225 if (!this.regxpIP.test(this.EndpointInputs[prop])) {
226 result = [false, 'Illegal IpAddress']
228 } else if (prop === logicalKey) {
229 if (!this.Util.isInteger(this.EndpointInputs[prop])) {
230 result = [false, 'LogicalID can only be an integer']
237 inputHolder (title: string): string {
238 const titleArr = title.split(' ')
239 if (titleArr.length > 1) {
240 return titleArr.slice(0, 2).join('');
246 labelStyle (required: boolean) : object{
249 style = {'margin-left': '18px', 'margin-right': '-18px'};
257 // Verify that items of EndPoint is correct
258 let endCheckResult = this.endCheckBeforeSubmit()
259 if (!endCheckResult[0]) {
260 this.message.error(endCheckResult[1].toString());
263 // replace the params about endPoint
264 for (let prop in this.formData) {
265 if (typeof this.EndpointInputs[prop] !== 'undefined') {
266 this.formData[prop] = this.EndpointInputs[prop];
270 if (this.title === 'An') {
271 const an_coverage_area_ta_list: string[] = [];
272 this.areaList.forEach( item => {
273 let str: string = '';
274 item.forEach( area => {
275 str += area.selected + ';';
277 an_coverage_area_ta_list.push(str.substring(0, str.length-1));
279 params = {...this.formData, an_coverage_area_ta_list};
281 params = {...this.formData};
283 // Verify that each item is not empty, include special handeling of area_list
284 if (this.Util.deepCheck(params) && this.areaCheckBeforeSubmit(params)) {
285 this.paramsDataChange.emit(params);
288 this.message.error('Please complete the form');