fengxiang
2018-01-10 3517e796f650b8aed52165c1a5905456f54033ef
src/app/routes/devices/monitor-point/monitor-point-edit/monitor-point-edit.component.ts
@@ -1,4 +1,11 @@
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',
@@ -7,9 +14,116 @@
})
export class MonitorPointEditComponent implements OnInit {
  constructor() { }
   orgOptions = [];
  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).join('/'),
       value: data.areaCode
      };
  }
     this.OrgSelectChange(null);
     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
     );
  }
  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 }) {
    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).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];
  }
  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);
                 }
             }
        }
     );
  }
}