import { DeviceVersion } from '@business/entity/data';
|
import { Component, OnInit } from '@angular/core';
|
import { NzMessageService, NzModalSubject } from 'ng-zorro-antd';
|
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
|
import { _HttpClient } from '@delon/theme';
|
import { ResultBean } from '@business/entity/grid';
|
import { Observable } from 'rxjs/Observable';
|
import { ToolsService } from '@business/services/util/tools.service';
|
@Component({
|
selector: 'app-version-edit',
|
templateUrl: './version-edit.component.html',
|
styles: []
|
})
|
export class VersionEditComponent implements OnInit {
|
|
private originalData: DeviceVersion = {};
|
cols: DeviceVersion;
|
data: DeviceVersion;
|
isSaving = false;
|
validateForm: FormGroup;
|
constructor(
|
private subject: NzModalSubject,
|
private formBuilder: FormBuilder,
|
private http: _HttpClient
|
) { }
|
|
ngOnInit() {
|
if (this.data.createTime == null) {
|
this.data.createTime = new Date().getTime();
|
}
|
if (!this.data.version) {
|
this.setMaxVersionNo();
|
}
|
const validates: DeviceVersion = {
|
name: [this.data.name, [Validators.required] ],
|
version: [this.data.version, [Validators.required], [this.versionAsyncValidator] ],
|
createTime: [this.data.createTime, [Validators.required] ],
|
description: [this.data.description]
|
};
|
Object.assign(this.originalData, this.data);
|
this.validateForm = this.formBuilder.group(
|
validates
|
);
|
}
|
private setMaxVersionNo() {
|
this.isSaving = true;
|
this.http.get('device-version/get-maxverno').subscribe(
|
(res: ResultBean<number>) => {
|
if (!!res.code && res.data) {
|
ToolsService.setValueToControl(this.validateForm, 'version', res.data + 1);
|
}else {
|
this.data.version = 1;
|
}
|
this.isSaving = false;
|
}
|
);
|
}
|
close() {
|
this.subject.destroy();
|
}
|
save($event, value, valid) {
|
$event.preventDefault();
|
if (valid) {
|
for (const i in this.validateForm.controls) {
|
this.validateForm.controls[ i ].disable();
|
}
|
const isModified = Object.keys(value).some(
|
(key: string) => {
|
return this.data[key] !== value[key];
|
}
|
);
|
// 未作修改
|
if (!isModified) {
|
this.close();
|
return;
|
}
|
this.isSaving = true;
|
Object.keys(value).forEach( (key: string) => {
|
if ( value[key] != null ) {
|
this.data[key] = value[key];
|
}
|
} );
|
this.subject.next( this );
|
} else {
|
this.validate();
|
}
|
}
|
versionAsyncValidator = (control: FormControl): any => {
|
return Observable.create(observer => {
|
if (!!control && !!control.value) {
|
// 编辑状态,version未改变
|
if (!!this.originalData && this.originalData.version === control.value) {
|
observer.next(null);
|
observer.complete();
|
} else {
|
this.isSaving = true;
|
this.http.get('device-version/get-byversion', {version: control.value} ).subscribe(
|
(res: ResultBean<any>) => {
|
if (!!res.code && !!res.data) {
|
observer.next({ error: true, duplicated: true });
|
} else {
|
observer.next(null);
|
}
|
observer.complete();
|
this.isSaving = false;
|
}
|
);
|
}
|
}else {
|
observer.next(null);
|
observer.complete();
|
}
|
});
|
}
|
validate() {
|
for (const i in this.validateForm.controls) {
|
this.validateForm.controls[ i ].markAsDirty();
|
}
|
}
|
}
|