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.formData = JSON.parse(JSON.stringify(this.detailData));
44 if (this.title === 'An' || this.title === 'Cn') {
45 this.coreFormItems = this.title === 'An'?CORE_FORM_ITEMS.An:this.title === 'Cn'?CORE_FORM_ITEMS.Cn:[];
46 this.keyList = this.coreFormItems.find((item) => {return item.title === 'Endpoint'}).options.map((val) => {return val.key});
47 if(this.formData !==undefined && Object.keys(this.formData).length!==0){
48 this.EndpointEnable = this.keyList.every((item) => {return this.formData.hasOwnProperty(item)})
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)
62 //-------> Comment: Above code
63 if (this.title === 'An') {
64 this.AreaFormatting();
67 validateEndPoint (key: string, value: any): string {
68 if (this.Util.isEmpty(value)) {
69 return 'can not be empty';
71 if (key === 'ip_address') {
72 if (!this.regxpIP.test(value)) {
73 return 'xxx.xxx.xxx.xxx';
77 } else if (key === 'logical_link') {
78 if (!this.Util.isInteger(value)){
88 // onInput ($event:any, title: string) {
92 // const target = $event.target;
93 // if (title === 'ip_address') {
94 // // only number and '.' can be inputted
95 // const regexp = /[^\d^\.]+/g;
96 // target.value = target.value.replace(regexp, '');
97 // } else if (title === 'logical_link') {
98 // // only number can be inputted
99 // const regxp = /[^\d]/g;
100 // target.value = target.value.replace(regxp, '');
104 let areaList = [...this.formData.an_coverage_area_ta_list];
105 this.areaList = areaList.map ( (item: any) => {
106 let arr = item.split(';');
107 item = arr.map( (ite, index) => {
111 } else if (index === 1){
119 obj.options = [{name: ite, id: ite}]
126 creatAreaList (): void {
144 this.areaList.push(arr)
147 deleteAreaList (index: number): void {
148 this.areaList.splice(index,1);
151 handleChange(area: any[], areaItem: any): void{
152 if (areaItem.key === 'province' && areaItem.options.length <= 1) {
153 areaItem.options = ADDRESS;
154 } else if (areaItem.key === 'city' && areaItem.options.length <= 1) {
155 ADDRESS.forEach( item => {
156 if(item.name === area[0].selected) {
157 areaItem.options = item.city;
160 }else if (areaItem.key === 'district' && areaItem.options.length <= 1) {
161 ADDRESS.forEach( (item: any) => {
162 item.city.forEach(city => {
163 if (city.name === area[1].selected) {
164 areaItem.options = city.county;
171 handleChangeSelected(area: any[], areaItem: any) {
172 if (areaItem.key === 'province') {
173 area[1].selected = ''
174 area[1].options = [];
175 area[2].selected = '';
176 area[2].options = [];
177 } else if (areaItem.key === 'city') {
178 area[2].selected = '';
179 area[2].options = [];
184 this.showModel = false
185 this.cancel.emit(this.showModel)
188 // prompt text for each item of area_list
189 checkArea (area: any) {
190 if (area.every((item) => {return item.selected === ''})) {
193 if (area.some((item) => {return item.selected === ''})) {
199 // special handling for address
200 areaCheckBeforeSubmit (target: object) : Boolean{
201 for (const prop in target) {
202 if (target.hasOwnProperty(prop)) {
203 if (prop === 'an_coverage_area_ta_list' || prop ==='cn_coverage_area_ta_list') {
204 // if the vlaue is "shanghai;shanghai;", the input is incomplete
205 return target[prop].every((item) => {return this.Util.deepCheck(item.split(';'))});
212 endCheckBeforeSubmit () : Array<any>{
213 // check params of Endpoint
214 let result: Array<any> = [true, ''];
215 const endPointList = this.coreFormItems&&this.coreFormItems.length!==0?this.coreFormItems.find((item) => {return item.title === 'Endpoint'}).options:{};
218 for (let item of endPointList) {
219 if (item.title === 'ip_address') {
221 } else if (item.title === 'logical_link') {
222 logicalKey = item.key
225 for (let prop in this.EndpointInputs) {
226 if (prop === ipKey) {
227 if (!this.regxpIP.test(this.EndpointInputs[prop])) {
228 result = [false, 'Illegal IpAddress']
230 } else if (prop === logicalKey) {
231 if (!this.Util.isInteger(this.EndpointInputs[prop])) {
232 result = [false, 'LogicalID can only be an integer']
239 inputHolder (title: string): string {
240 const titleArr = title.split(' ')
241 if (titleArr.length > 1) {
242 return titleArr.slice(0, 2).join('');
248 labelStyle (required: boolean) : object{
251 style = {'margin-left': '18px', 'margin-right': '-18px'};
259 // Verify that items of EndPoint is correct
260 let endCheckResult = this.endCheckBeforeSubmit()
261 if (!endCheckResult[0]) {
262 this.message.error(endCheckResult[1].toString());
265 // replace the params about endPoint
266 for (let prop in this.formData) {
267 if (typeof this.EndpointInputs[prop] !== 'undefined') {
268 this.formData[prop] = this.EndpointInputs[prop];
272 if (this.title === 'An') {
273 const an_coverage_area_ta_list: string[] = [];
274 this.areaList.forEach( item => {
275 let str: string = '';
276 item.forEach( area => {
277 str += area.selected + ';';
279 an_coverage_area_ta_list.push(str.substring(0, str.length-1));
281 params = {...this.formData, an_coverage_area_ta_list};
283 params = {...this.formData};
285 // Verify that each item is not empty, include special handeling of area_list
286 if (this.Util.deepCheck(params) && this.areaCheckBeforeSubmit(params)) {
287 this.paramsDataChange.emit(params);
290 this.message.error('Please complete the form');