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 ANEndpointInputs: object = {};
29 CNEndpointInputs: object = {};
30 ANkeyList: string[] = [];
31 CNkeyList: string[] = [];
32 EndpointEnable: boolean = true; // Whether to enable the three parameters of Endpoint
33 keyList: string[] = []; // keys of endPoint
34 specialParaTN: string[] = ['Resource Sharing Level', 'Connection Links', 'AN Endpoint', 'CN Endpoint'];
35 // Parameters not passed to the back end
36 notPassPara: string[] = ['tn_connection_links'];
37 // Comment: Above code
40 private message: NzMessageService,
50 this.formData = JSON.parse(JSON.stringify(this.detailData));
51 if (this.title === 'An' || this.title === 'Cn') {
52 this.coreFormItems = this.title === 'An'?CORE_FORM_ITEMS.An:this.title === 'Cn'?CORE_FORM_ITEMS.Cn:[];
54 else if (this.title === 'Tn') {
55 this.ANkeyList = this.transferFormItems.find((item) => {return item.title === 'AN Endpoint'}).options.map((val) => {return val.key})
56 this.CNkeyList = this.transferFormItems.find((item) => {return item.title === 'CN Endpoint'}).options.map((val) => {return val.key})
57 this.keyList = this.ANkeyList.concat(this.CNkeyList)
58 this.formData['tn_connection_links_option'].forEach((item) => { // add init selection status
59 if (typeof this.formData['tn_connection_links'] !== 'undefined' && this.formData['tn_connection_links'] !== '' && this.formData['tn_connection_links'] !== null && item.id === this.formData['tn_connection_links']) {
65 console.log(this.formData['tn_connection_links_option'])
66 this.judgeTn() // init judge
68 // If the endpoint related parameters from the back end are incomplete, delete the endpoint item
69 if(this.formData !==undefined && Object.keys(this.formData).length!==0) {
70 this.EndpointEnable = this.keyList.every((item) => {return this.formData.hasOwnProperty(item)})
72 if(this.EndpointEnable){
73 if (this.title === 'Tn') {
74 this.ANEndpointInputs = this.Util.pick(this.formData, this.ANkeyList)
75 this.CNEndpointInputs = this.Util.pick(this.formData, this.CNkeyList)
77 if (this.title === 'Tn') {
78 this.transferFormItems.map((item,index)=>{
79 if (item.title === 'AN Endpoint' || item.title === 'CN Endpoint') {
80 this.coreFormItems.splice(index,1)
85 //-------> Comment: Above code
86 if (this.title === 'An') {
87 this.AreaFormatting();
92 changeResourceShare () {
97 if (this.formData['sliceProfile_TN_resourceSharingLevel'] === 'non-shared') {
98 this.formData['tn_connection_links_option'].forEach ((item) => {
101 this.formData['tn_connection_links'] = null
102 this.notPassPara = ['tn_connection_links', 'tn_connection_links_option']
103 this.transferFormItems.forEach((item) => {
104 if (item.title === 'Connection Links') {
106 } else if (item.title === 'AN Endpoint' || item.title === 'CN Endpoint') {
111 } else if (this.formData['sliceProfile_TN_resourceSharingLevel'] === 'shared') {
112 this.transferFormItems.forEach((item) => {
113 if (item.title === 'Connection Links') {
115 } else if (item.title === 'AN Endpoint' || item.title === 'CN Endpoint') {
116 if (typeof this.formData['tn_connection_links'] !== 'undefined' && this.formData['tn_connection_links']!==null && this.formData['tn_connection_links'] !== '') {
118 item.required = false
119 this.notPassPara = ['tn_connection_links_option']
120 this.notPassPara = this.notPassPara.concat(this.ANkeyList, this.CNkeyList)
122 this.formData['tn_connection_links'] = ''
125 this.notPassPara = ['tn_connection_links_option']
132 validateEndPoint (key: string, value: any, required: boolean): string {
134 if (this.Util.isEmpty(value)) {
135 return 'can not be empty';
138 if (key === 'ip_address') {
139 if (!this.regxpIP.test(value)) {
141 return 'xxx.xxx.xxx.xxx';
148 } else if (key === 'logical_link') {
149 if (!this.Util.isInteger(value)){
151 return 'integer only'
163 changeLinkCheck (id: string) : void{ // update the selection state
164 this.formData['tn_connection_links_option'].forEach((item) => {
165 if (item.id === id) {
171 console.log(this.formData['tn_connection_links_option'])
172 this.formData['tn_connection_links'] = id // get the selected id
177 let areaList = [...this.formData.an_coverage_area_ta_list];
178 this.areaList = areaList.map ( (item: any) => {
179 let arr = item.split(';');
180 item = arr.map( (ite, index) => {
184 } else if (index === 1){
192 obj.options = [{name: ite, id: ite}]
199 creatAreaList (): void {
217 this.areaList.push(arr)
220 deleteAreaList (index: number): void {
221 this.areaList.splice(index,1);
224 handleChange(area: any[], areaItem: any): void{
225 if (areaItem.key === 'province' && areaItem.options.length <= 1) {
226 areaItem.options = ADDRESS;
227 } else if (areaItem.key === 'city' && areaItem.options.length <= 1) {
228 ADDRESS.forEach( item => {
229 if(item.name === area[0].selected) {
230 areaItem.options = item.city;
233 }else if (areaItem.key === 'district' && areaItem.options.length <= 1) {
234 ADDRESS.forEach( (item: any) => {
235 item.city.forEach(city => {
236 if (city.name === area[1].selected) {
237 areaItem.options = city.county;
244 handleChangeSelected(area: any[], areaItem: any) {
245 if (areaItem.key === 'province') {
246 area[1].selected = ''
247 area[1].options = [];
248 area[2].selected = '';
249 area[2].options = [];
250 } else if (areaItem.key === 'city') {
251 area[2].selected = '';
252 area[2].options = [];
256 handleCancel() : void{
257 this.showModel = false
258 this.cancel.emit(this.showModel)
261 // prompt text for each item of area_list
262 checkArea (area: any) : string{
263 if (area.every((item) => {return item.selected === ''})) {
266 if (area.some((item) => {return item.selected === ''})) {
272 // special handling for address
273 areaCheckBeforeSubmit (target: object) : Boolean{
274 for (const prop in target) {
275 if (target.hasOwnProperty(prop)) {
276 if (prop === 'an_coverage_area_ta_list' || prop ==='cn_coverage_area_ta_list') {
277 // if the vlaue is "shanghai;shanghai;", the input is incomplete
278 return target[prop].every((item) => {return this.Util.deepCheck(item.split(';'))});
285 endCheckBeforeSubmit (endpoint, required) : Array<any>{
286 // check params of Endpoint
287 let result: Array<any> = [true, ''];
289 endPointList = this.transferFormItems.find((item) => {return item.title === 'AN Endpoint'}).options
293 for (let item of endPointList) {
294 if (item.title === 'ip_address') {
296 } else if (item.title === 'logical_link') {
297 logicalKey = item.key
298 } else if (item.title === 'nexthop_info') {
302 for (let prop in endpoint) {
303 if (prop === ipKey) {
305 if (endpoint[prop] === '') {
306 result = [false, 'Endpoint can not be empty']
307 } else if (!this.regxpIP.test(endpoint[prop])) {
308 result = [false, 'Illegal IpAddress']
310 } else if (!this.regxpIP.test(endpoint[prop]) && endpoint[prop] !== '') {
311 result = [false, 'Illegal IpAddress']
313 } else if (prop === logicalKey) {
315 if (endpoint[prop] === '') {
316 result = [false, 'logical can not be empty']
317 } else if (!this.Util.isInteger(endpoint[prop])) {
318 result = [false, 'LogicalID can only be an integer']
320 } else if (!this.Util.isInteger(endpoint[prop]) && endpoint[prop] !== '') {
321 result = [false, 'LogicalID can only be an integer']
323 } else if (prop === nextKey) {
324 if (required && endpoint[prop] === '') {
325 result = [false, 'Endpoint can not be empty']
332 inputHolder (title: string): string {
333 const titleArr = title.split(' ')
334 if (titleArr.length > 1) {
335 return titleArr.slice(0, 2).join('');
341 labelStyle (required: boolean) : object{
344 style = {'margin-left': '18px', 'margin-right': '-18px'};
352 // Verify that items of EndPoint is correct
353 if (this.EndpointEnable) {
354 let endCheckResult = []
355 if (this.title === 'Tn') {
356 const ANendCheckResult = this.endCheckBeforeSubmit(this.ANEndpointInputs, this.transferFormItems.find((item) => {return item.title === 'AN Endpoint'}).required)
357 const CNendCheckResult = this.endCheckBeforeSubmit(this.CNEndpointInputs, this.transferFormItems.find((item) => {return item.title === 'CN Endpoint'}).required)
358 if (ANendCheckResult[0] && CNendCheckResult[0]) {
359 endCheckResult[0] = true
361 if (ANendCheckResult[0] === false) {
362 endCheckResult = ANendCheckResult
364 endCheckResult = CNendCheckResult
368 if (!endCheckResult[0]) {
369 this.message.error(endCheckResult[1].toString());
372 // replace the params about endPoint
373 for (let prop in this.formData) {
374 if (this.title === 'Tn' && typeof this.ANEndpointInputs[prop] !== 'undefined') {
375 this.formData[prop] = this.ANEndpointInputs[prop];
376 } else if (this.title === 'Tn' && typeof this.CNEndpointInputs[prop] !== 'undefined') {
377 this.formData[prop] = this.CNEndpointInputs[prop];
382 if (this.title === 'An') {
383 const an_coverage_area_ta_list: string[] = [];
384 this.areaList.forEach( item => {
385 let str: string = '';
386 item.forEach( area => {
387 str += area.selected + ';';
389 an_coverage_area_ta_list.push(str.substring(0, str.length-1));
391 params = {...this.formData, an_coverage_area_ta_list};
393 params = {...this.formData};
395 // Verify that each item exclude endpoint is not empty, include special handeling of area_list
396 let checkParams : object= params
397 let requireKeyList :string[] = []
398 let targetFormItems : any[] = []
399 if (this.title === 'An' || this.title === 'Cn') {
400 targetFormItems = this.coreFormItems
401 } else if (this.title = 'Tn') {
402 targetFormItems = this.transferFormItems
404 for (let item of targetFormItems) {
405 if (typeof item.required !== 'undefined' && item.required) {
406 if (typeof item.type !== 'undefined' && item.type !== 'endpoint')
408 requireKeyList.push(item.key)
412 console.log(requireKeyList)
413 checkParams = this.Util.pick(params, requireKeyList)
414 console.log(checkParams)
415 if (this.Util.deepCheck(checkParams) && this.areaCheckBeforeSubmit(params)) {
416 this.paramsDataChange.emit(params);
417 this.noPassParaChange.emit(this.notPassPara)
420 this.message.error('Please complete the form');