/
propertypanel.ts
108 lines (91 loc) · 2.87 KB
/
propertypanel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { PropertyItem, getInstanceType, getTypeFullName } from "../../base";
import { getForm } from "../../q";
import { FormKeyAttribute } from "../../types/attributes";
import { Decorators } from "../../types/decorators";
import { PropertyGrid, PropertyGridOptions } from "./propertygrid";
import { BasePanel } from "./basepanel";
import { WidgetProps } from "./widget";
@Decorators.registerClass('Serenity.PropertyPanel')
export class PropertyPanel<TItem, P> extends BasePanel<P> {
private _entity: TItem;
private _entityId: any;
constructor(props: WidgetProps<P>) {
super(props);
this.initPropertyGrid();
this.loadInitialEntity();
}
destroy() {
if (this.propertyGrid) {
this.propertyGrid.destroy();
this.propertyGrid = null;
}
if (this.validator) {
this.byId('Form').remove();
this.validator = null;
}
super.destroy();
}
protected initPropertyGrid() {
var pgDiv = this.findById('PropertyGrid');
if (!pgDiv)
return;
var pgOptions = this.getPropertyGridOptions();
this.propertyGrid = (new PropertyGrid({ element: pgDiv, ...pgOptions })).init();
}
protected loadInitialEntity(): void {
if (this.propertyGrid) {
this.propertyGrid.load(new Object());
}
}
protected getFormKey(): string {
var attr = this.getCustomAttribute(FormKeyAttribute);
if (attr) {
return attr.value;
}
var name = getTypeFullName(getInstanceType(this));
var px = name.indexOf('.');
if (px >= 0) {
name = name.substring(px + 1);
}
else if (name.endsWith('Panel')) {
name = name.substring(0, name.length - 5);
}
return name;
}
protected getPropertyGridOptions(): PropertyGridOptions {
return {
idPrefix: this.idPrefix,
items: this.getPropertyItems(),
mode: 1,
useCategories: false,
localTextPrefix: 'Forms.' + this.getFormKey() + '.'
};
}
protected getPropertyItems(): PropertyItem[] {
var formKey = this.getFormKey();
return getForm(formKey);
}
protected getSaveEntity(): TItem {
var entity = new Object();
if (this.propertyGrid) {
this.propertyGrid.save(entity);
}
return entity as TItem;
}
public get entity(): TItem {
return this._entity;
}
public get entityId(): any {
return this._entityId;
}
protected set entity(value: TItem) {
this._entity = value ?? new Object() as any;
}
protected set entityId(value: any) {
this._entityId = value;
}
protected validateBeforeSave(): boolean {
return this.validator.form();
}
protected propertyGrid: PropertyGrid;
}