import { MonitorPoint } from '@business/entity/data';
|
import { PageBean } from '@business/entity/grid';
|
import { OrganizationService } from '@business/services/http//organization.service';
|
import { AreacodeService } from '@business/services/http/areacode.service';
|
import { Component, OnInit } from '@angular/core';
|
import { NzMessageService, NzModalSubject } from 'ng-zorro-antd';
|
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
|
import { CascaderOption } from 'ng-zorro-antd/src/cascader/nz-cascader.component';
|
|
@Component({
|
selector: 'app-monitor-point-edit',
|
templateUrl: './monitor-point-edit.component.html',
|
styles: []
|
})
|
export class MonitorPointEditComponent implements OnInit {
|
|
orgOptions = [];
|
configMap: {organizationId: number};
|
data: MonitorPoint;
|
isSaving = false;
|
validateForm: FormGroup;
|
constructor(
|
private subject: NzModalSubject,
|
private formBuilder: FormBuilder,
|
private areacodeService: AreacodeService,
|
private organizationService: OrganizationService
|
) { }
|
|
ngOnInit() {
|
const data = this.data;
|
const areaNames = data.areaNames;
|
let _areas = null;
|
if (areaNames != null) {
|
_areas = {
|
label: Object.values(areaNames).filter(d=>d).join('/'),
|
value: data.areaCode
|
};
|
}
|
this.orgSelectChange(null);
|
if (!!this.configMap.organizationId && !data.organizationId) {
|
data.organizationId = this.configMap.organizationId;
|
}
|
const validates: MonitorPoint|object = {
|
name: [data.name, [Validators.required] ],
|
organizationId: [data.organizationId, [Validators.required]],
|
longitude: [data.longitude],
|
latitude: [data.latitude],
|
address: [data.address ],
|
_areas: [_areas, [Validators.required]],
|
description: [data.description ]
|
};
|
this.validateForm = this.formBuilder.group(
|
validates
|
);
|
this.validateForm.controls['organizationId'].valueChanges.subscribe(
|
value => {
|
this.configMap.organizationId = value;
|
}
|
);
|
}
|
close() {
|
this.subject.destroy();
|
}
|
save($event, value, valid) {
|
$event.preventDefault();
|
if (valid) {
|
for (const i in this.validateForm.controls) {
|
this.validateForm.controls[ i ].disable();
|
}
|
this.isSaving = true;
|
Object.keys(value).forEach( (key: string) => {
|
// '_'为前缀的为自定义属性
|
if (!key.startsWith('_') && value[key] != null) {
|
this.data[key] = value[key];
|
}
|
} );
|
this.subject.next( this );
|
} else {
|
this.validate();
|
}
|
}
|
validate() {
|
for (const i in this.validateForm.controls) {
|
this.validateForm.controls[ i ].markAsDirty();
|
}
|
}
|
areaLazyLoad(event: { option: CascaderOption, index: number, resolve: (children: CascaderOption[]) => void, reject: () => void }) {
|
// console.info(event);
|
const index = event['index'];
|
const option = event.option;
|
switch (index) {
|
case -1:
|
this.areacodeService.getProvinces().subscribe(
|
(res: {label: string, value: string}[]) => {
|
event.resolve( res );
|
}
|
); break;
|
case 0:
|
this.areacodeService.getCities(option.value).subscribe(
|
(res: {label: string, value: string}[]) => {
|
event.resolve( res );
|
}
|
); break;
|
case 1:
|
// 区不是叶节点 兼容旧组件
|
this.areacodeService.getAreas(option.value , false).subscribe(
|
(res: {label: string, value: string}[]) => {
|
event.resolve( res );
|
}
|
); break;
|
case 2:
|
this.areacodeService.getTowns(option.value).subscribe(
|
(res: {label: string, value: string}[]) => {
|
event.resolve( res );
|
}
|
); break;
|
case 3:
|
this.areacodeService.getVillages(option.value).subscribe(
|
(res: {label: string, value: string}[]) => {
|
event.resolve( res );
|
}
|
); break;
|
}
|
}
|
setAreaCodes(codes: string[]) {
|
this.data.provinceCode = codes[0];
|
this.data.cityCode = codes[1];
|
this.data.areaCode = codes[2];
|
this.data.townCode = codes[3];
|
this.data.villageCode = codes[4];
|
}
|
orgSelectChange(text) {
|
const pageBean: PageBean = {pageIndex: 0, pageSize: 20};
|
this.organizationService.getPagingList(pageBean, text).subscribe(
|
(res: PageBean) => {
|
if (res != null && res.data != null) {
|
this.orgOptions = res.data;
|
}
|
const organization = this.data.organization;
|
if (organization != null && text == null) {
|
const num: number = this.orgOptions.filter(
|
(item: any) => {
|
return item.id === organization.id;
|
}
|
).length;
|
if ( num === 0 ) {
|
this.orgOptions.push(organization);
|
}
|
}
|
}
|
);
|
}
|
}
|