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: any[] = [];
28 EndpointEnable: boolean = true; // Whether to enable the three parameters of Endpoint
29 // Comment: Above code
32 private message: NzMessageService,
42 this.coreFormItems = this.title === 'An'?CORE_FORM_ITEMS.An:this.title === 'Cn'?CORE_FORM_ITEMS.Cn:[];
43 this.formData = JSON.parse(JSON.stringify(this.detailData));
44 if(this.formData !==undefined && Object.keys(this.formData).length!==0){
45 this.EndpointEnable = (this.formData.hasOwnProperty("an_Endpoint") && this.formData['an_Endpoint'].length!==0) || (this.formData.hasOwnProperty("cn_Endpoint") && this.formData['cn_Endpoint'].length!==0)
47 // -------> 2020.08.17 Add 3 parameters for Endpoint, Comment: The following code
48 if(this.EndpointEnable){
49 this.EndpointInputs = this.title === 'An'
50 ?this.formData["an_Endpoint"]
52 ?this.formData["cn_Endpoint"]
55 this.coreFormItems.map((item,index)=>{
56 if(item.title === 'Endpoint'){
57 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 let formatedEndpoint = {};
216 this.EndpointInputs.forEach((item) => {
217 formatedEndpoint[Object.keys(item)[0]] = item[Object.keys(item)[0]];
219 if (this.title === 'An') {
220 for (let prop in formatedEndpoint) {
221 if (prop === 'an_ip_address') {
222 if (!this.regxpIP.test(formatedEndpoint[prop])) {
223 result = [false, 'Illegal IpAddress']
225 } else if (prop === 'an_logical_link') {
226 if (!this.Util.isInteger(formatedEndpoint[prop])) {
227 result = [false, 'LogicalID can only be an integer']
231 } else if (this.title === 'Cn') {
232 for (let prop in formatedEndpoint) {
233 if (prop === 'cn_ip_address') {
234 if (!this.regxpIP.test(formatedEndpoint[prop])) {
235 result = [false, 'Illegal IpAddress']
237 } else if (prop === 'cn_logical_link') {
238 if (!this.Util.isInteger(formatedEndpoint[prop])) {
239 result = [false, 'LogicalID can only be an integer']
247 inputHolder (title: string): string {
248 const titleArr = title.split(' ')
249 if (titleArr.length > 1) {
250 return titleArr.slice(0, 2).join('');
256 labelStyle (required: boolean) : object{
259 style = {'margin-left': '18px', 'margin-right': '-18px'};
267 // Verify that items of EndPoint is correct
268 let endCheckResult = this.endCheckBeforeSubmit()
269 if (!endCheckResult[0]) {
270 this.message.error(endCheckResult[1].toString());
274 if (this.title === 'An') {
275 const an_coverage_area_ta_list: string[] = [];
276 this.areaList.forEach( item => {
277 let str: string = '';
278 item.forEach( area => {
279 str += area.selected + ';';
281 an_coverage_area_ta_list.push(str.substring(0, str.length-1));
283 params = {...this.formData, an_coverage_area_ta_list};
285 params = {...this.formData};
287 // Verify that each item is not empty, include special handeling of area_list
288 if (this.Util.deepCheck(params) && this.areaCheckBeforeSubmit(params)) {
289 this.paramsDataChange.emit(params);
292 this.message.error('Please complete the form');