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>();
19 @Output() noPassParaChange = new EventEmitter<any>();
21 transferFormItems: any[] = TRANSFRER_FORM_ITEMS;
22 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
24 coreFormItems : any = [];
26 // 2020.08.17 Add 3 parameters for Endpoint, Comment: The following code
27 NexthopInfoOptions = NexthopInfo_Options;
28 EndpointInputs: object = {};
29 ANEndpointInputs: object = {};
30 CNEndpointInputs: object = {};
31 ANkeyList: string[] = [];
32 CNkeyList: string[] = [];
33 EndpointEnable: boolean = true; // Whether to enable the three parameters of Endpoint
34 keyList: string[] = []; // keys of endPoint
35 specialParaTN: string[] = ['Resource Sharing Level', 'Connection Links', 'AN Endpoint', 'CN Endpoint'];
36 // Parameters not passed to the back end
37 notPassPara: string[] = ['tn_connection_links'];
38 // Comment: Above code
41 private message: NzMessageService,
51 this.formData = JSON.parse(JSON.stringify(this.detailData));
52 if (this.title === 'An' || this.title === 'Cn') {
53 this.coreFormItems = this.title === 'An'?CORE_FORM_ITEMS.An:this.title === 'Cn'?CORE_FORM_ITEMS.Cn:[];
54 this.keyList = this.coreFormItems.find((item) => {return item.title === 'Endpoint'}).options.map((val) => {return val.key});
56 else if (this.title === 'Tn') {
57 this.ANkeyList = this.transferFormItems.find((item) => {return item.title === 'AN Endpoint'}).options.map((val) => {return val.key})
58 this.CNkeyList = this.transferFormItems.find((item) => {return item.title === 'CN Endpoint'}).options.map((val) => {return val.key})
59 this.keyList = this.ANkeyList.concat(this.CNkeyList)
60 this.formData['tn_connection_links_option'].forEach((item) => { // add init selection status
61 if (this.formData['tn_connection_links']!== null && this.formData['tn_connection_links'].indexOf(item.id) !== -1) {
67 this.judgeTn() // init judge
69 // If the endpoint related parameters from the back end are incomplete, delete the endpoint item
70 if(this.formData !==undefined && Object.keys(this.formData).length!==0) {
71 this.EndpointEnable = this.keyList.every((item) => {return this.formData.hasOwnProperty(item)})
73 if(this.EndpointEnable){
74 if (this.title === 'An' || this.title === 'Cn') {
75 this.EndpointInputs = this.Util.pick(this.formData, this.keyList)
76 } else if (this.title === 'Tn') {
77 this.ANEndpointInputs = this.Util.pick(this.formData, this.ANkeyList)
78 this.CNEndpointInputs = this.Util.pick(this.formData, this.CNkeyList)
80 if (this.title === 'An' || this.title === 'Cn') {
81 this.coreFormItems.map((item,index)=>{
82 if (item.title === 'Endpoint') {
83 this.coreFormItems.splice(index,1)
86 } else if (this.title === 'Tn') {
87 this.transferFormItems.map((item,index)=>{
88 if (item.title === 'AN Endpoint' || item.title === 'CN Endpoint') {
89 this.coreFormItems.splice(index,1)
94 //-------> Comment: Above code
95 if (this.title === 'An') {
96 this.AreaFormatting();
101 changeResourceShare () {
106 if (this.formData['sliceProfile_TN_resourceSharingLevel'] === 'non-shared') {
107 this.formData['tn_connection_links_option'].forEach ((item) => {
110 this.formData['tn_connection_links'] = null
111 this.transferFormItems.forEach((item) => {
112 if (item.title === 'Connection Links') {
114 this.notPassPara = ['tn_connection_links', 'tn_connection_links_option']
115 } else if (item.title === 'AN Endpoint' || item.title === 'CN Endpoint') {
120 } else if (this.formData['sliceProfile_TN_resourceSharingLevel'] === 'shared') {
121 this.transferFormItems.forEach((item) => {
122 if (item.title === 'Connection Links') {
124 } else if (item.title === 'AN Endpoint' || item.title === 'CN Endpoint') {
125 if (this.formData['tn_connection_links']!==null && this.formData['tn_connection_links'].length !== 0) {
127 item.required = false
128 this.notPassPara = ['tn_connection_links_option']
129 this.notPassPara = this.notPassPara.concat(this.ANkeyList, this.CNkeyList)
130 console.log('not', this.notPassPara)
131 } else if (this.formData['tn_connection_links']!==null && this.formData['tn_connection_links'].length === 0) {
140 validateEndPoint (key: string, value: any, required: boolean): string {
142 if (this.Util.isEmpty(value)) {
143 return 'can not be empty';
146 if (key === 'ip_address') {
147 if (!this.regxpIP.test(value)) {
149 return 'xxx.xxx.xxx.xxx';
156 } else if (key === 'logical_link') {
157 if (!this.Util.isInteger(value)){
159 return 'integer only'
171 changeLinkCheck (id: string, e: boolean) : void{ // update the selection state
172 this.formData['tn_connection_links_option'].find((item) => {
173 return item.id === id
175 const checkedList = this.formData['tn_connection_links_option'].filter((item) => {return item.checked === true})
176 this.formData['tn_connection_links'] = checkedList.map((item) => {return item.id}) // get the selected id
181 let areaList = [...this.formData.an_coverage_area_ta_list];
182 this.areaList = areaList.map ( (item: any) => {
183 let arr = item.split(';');
184 item = arr.map( (ite, index) => {
188 } else if (index === 1){
196 obj.options = [{name: ite, id: ite}]
203 creatAreaList (): void {
221 this.areaList.push(arr)
224 deleteAreaList (index: number): void {
225 this.areaList.splice(index,1);
228 handleChange(area: any[], areaItem: any): void{
229 if (areaItem.key === 'province' && areaItem.options.length <= 1) {
230 areaItem.options = ADDRESS;
231 } else if (areaItem.key === 'city' && areaItem.options.length <= 1) {
232 ADDRESS.forEach( item => {
233 if(item.name === area[0].selected) {
234 areaItem.options = item.city;
237 }else if (areaItem.key === 'district' && areaItem.options.length <= 1) {
238 ADDRESS.forEach( (item: any) => {
239 item.city.forEach(city => {
240 if (city.name === area[1].selected) {
241 areaItem.options = city.county;
248 handleChangeSelected(area: any[], areaItem: any) {
249 if (areaItem.key === 'province') {
250 area[1].selected = ''
251 area[1].options = [];
252 area[2].selected = '';
253 area[2].options = [];
254 } else if (areaItem.key === 'city') {
255 area[2].selected = '';
256 area[2].options = [];
260 handleCancel() : void{
261 this.showModel = false
262 this.cancel.emit(this.showModel)
265 // prompt text for each item of area_list
266 checkArea (area: any) : string{
267 if (area.every((item) => {return item.selected === ''})) {
270 if (area.some((item) => {return item.selected === ''})) {
276 // special handling for address
277 areaCheckBeforeSubmit (target: object) : Boolean{
278 for (const prop in target) {
279 if (target.hasOwnProperty(prop)) {
280 if (prop === 'an_coverage_area_ta_list' || prop ==='cn_coverage_area_ta_list') {
281 // if the vlaue is "shanghai;shanghai;", the input is incomplete
282 return target[prop].every((item) => {return this.Util.deepCheck(item.split(';'))});
289 endCheckBeforeSubmit (endpoint, required) : Array<any>{
290 // check params of Endpoint
291 console.log('end', endpoint)
292 let result: Array<any> = [true, ''];
294 if (this.title === 'An' || this.title === 'Cn') {
295 endPointList = this.coreFormItems.find((item) => {return item.title === 'Endpoint'}).options
297 endPointList = this.transferFormItems.find((item) => {return item.title === 'AN Endpoint'}).options
302 for (let item of endPointList) {
303 if (item.title === 'ip_address') {
305 } else if (item.title === 'logical_link') {
306 logicalKey = item.key
307 } else if (item.title === 'nexthop_info') {
311 for (let prop in endpoint) {
312 if (prop === ipKey) {
314 if (endpoint[prop] === '') {
315 result = [false, 'Endpoint can not be empty']
316 } else if (!this.regxpIP.test(endpoint[prop])) {
317 result = [false, 'Illegal IpAddress']
319 } else if (!this.regxpIP.test(endpoint[prop]) && endpoint[prop] !== '') {
320 result = [false, 'Illegal IpAddress']
322 } else if (prop === logicalKey) {
324 if (endpoint[prop] === '') {
325 result = [false, 'logical can not be empty']
326 } else if (!this.Util.isInteger(endpoint[prop])) {
327 result = [false, 'LogicalID can only be an integer']
329 } else if (!this.Util.isInteger(endpoint[prop]) && endpoint[prop] !== '') {
330 result = [false, 'LogicalID can only be an integer']
332 } else if (prop === nextKey) {
333 if (required && endpoint[prop] === '') {
334 result = [false, 'Endpoint can not be empty']
341 inputHolder (title: string): string {
342 const titleArr = title.split(' ')
343 if (titleArr.length > 1) {
344 return titleArr.slice(0, 2).join('');
350 labelStyle (required: boolean) : object{
353 style = {'margin-left': '18px', 'margin-right': '-18px'};
361 // Verify that items of EndPoint is correct
362 if (this.EndpointEnable) {
363 let endCheckResult = []
364 if (this.title === 'An' || this.title === 'Cn') {
365 endCheckResult = this.endCheckBeforeSubmit(this.EndpointInputs, this.coreFormItems.find((item) => {return item.title === 'Endpoint'}).required)
366 } else if (this.title === 'Tn') {
367 const ANendCheckResult = this.endCheckBeforeSubmit(this.ANEndpointInputs, this.transferFormItems.find((item) => {return item.title === 'AN Endpoint'}).required)
368 const CNendCheckResult = this.endCheckBeforeSubmit(this.CNEndpointInputs, this.transferFormItems.find((item) => {return item.title === 'CN Endpoint'}).required)
369 if (ANendCheckResult[0] && CNendCheckResult[0]) {
370 endCheckResult[0] = true
372 if (ANendCheckResult[0] === false) {
373 endCheckResult = ANendCheckResult
375 endCheckResult = CNendCheckResult
379 if (!endCheckResult[0]) {
380 this.message.error(endCheckResult[1].toString());
383 // replace the params about endPoint
384 for (let prop in this.formData) {
385 if ((this.title === 'An' || this.title === 'Cn') && typeof this.EndpointInputs[prop] !== 'undefined') {
386 this.formData[prop] = this.EndpointInputs[prop];
387 } else if (this.title === 'Tn' && typeof this.ANEndpointInputs[prop] !== 'undefined') {
388 this.formData[prop] = this.ANEndpointInputs[prop];
389 } else if (this.title === 'Tn' && typeof this.CNEndpointInputs[prop] !== 'undefined') {
390 this.formData[prop] = this.CNEndpointInputs[prop];
395 if (this.title === 'An') {
396 const an_coverage_area_ta_list: string[] = [];
397 this.areaList.forEach( item => {
398 let str: string = '';
399 item.forEach( area => {
400 str += area.selected + ';';
402 an_coverage_area_ta_list.push(str.substring(0, str.length-1));
404 params = {...this.formData, an_coverage_area_ta_list};
406 params = {...this.formData};
408 // Verify that each item exclude endpoint is not empty, include special handeling of area_list
409 let checkParams = params
410 if (this.title === 'An' || this.title === 'Cn') {
411 checkParams = this.coreFormItems.filter((item) => {
412 return item.required === true
414 } else if (this.title = 'Tn') {
415 checkParams = this.transferFormItems.filter((item) => {
416 return item.required === true
419 if (this.Util.deepCheck(checkParams) && this.areaCheckBeforeSubmit(params)) {
420 this.paramsDataChange.emit(params);
421 this.noPassParaChange.emit(this.notPassPara)
424 this.message.error('Please complete the form');