-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathdialog-edit-renderer.ts
155 lines (141 loc) · 6.76 KB
/
dialog-edit-renderer.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { IGrid } from '../base/interface';
import { Column } from '../models/column';
import { Dialog, DialogModel } from '@syncfusion/ej2-popups';
import { remove, extend, updateBlazorTemplate, isBlazor } from '@syncfusion/ej2-base';
import { L10n } from '@syncfusion/ej2-base';
import { ServiceLocator } from '../services/service-locator';
import * as events from '../base/constant';
import { appendChildren } from '../base/util';
/**
* Edit render module is used to render grid edit row.
*/
export class DialogEditRender {
//Internal variables
//Module declarations
private parent: IGrid;
private l10n: L10n;
private isEdit: boolean;
private serviceLocator: ServiceLocator;
private dialog: HTMLElement;
private dialogObj: Dialog;
/**
* Constructor for render module
*/
constructor(parent?: IGrid, serviceLocator?: ServiceLocator) {
this.parent = parent;
this.serviceLocator = serviceLocator;
if (this.parent.isDestroyed) { return; }
this.parent.on(events.dialogDestroy, this.destroy, this);
this.parent.on(events.destroy, this.destroy, this);
}
private setLocaleObj(): void {
this.l10n = this.serviceLocator.getService<L10n>('localization');
}
public addNew(elements: Element[], args: { primaryKeyValue?: string[] }): void {
this.isEdit = false;
this.createDialog(elements, args);
}
public update(elements: Element[], args: { primaryKeyValue?: string[] }): void {
this.isEdit = true;
this.createDialog(elements, args);
}
private createDialog(elements: Element[], args: { primaryKeyValue?: string[], rowData?: Object,
dialog?: DialogModel, target?: HTMLElement }): void {
let gObj: IGrid = this.parent;
this.dialog = this.parent.createElement('div', { id: gObj.element.id + '_dialogEdit_wrapper', styles: 'width: auto' });
gObj.element.appendChild(this.dialog);
this.setLocaleObj();
// let position: PositionDataModel = this.parent.element.getBoundingClientRect().height < 400 ?
// { X: 'center', Y: 'top' } : { X: 'center', Y: 'center' };
this.dialogObj = new Dialog(extend(
{
header: this.isEdit ? this.l10n.getConstant('EditFormTitle') + args.primaryKeyValue[0] :
this.l10n.getConstant('AddFormTitle'), isModal: true, visible: true, cssClass: 'e-edit-dialog',
content: this.getEditElement(elements, args) as HTMLElement,
showCloseIcon: true,
allowDragging: true,
// position: position,
close: this.dialogClose.bind(this),
closeOnEscape: true, width: gObj.editSettings.template ? 'auto' : '330px',
target: args.target ? args.target : document.body, animationSettings: { effect: 'None' },
buttons: [{
click: this.btnClick.bind(this),
buttonModel: { content: this.l10n.getConstant('SaveButton'), cssClass: 'e-primary', isPrimary: true }
},
{ click: this.btnClick.bind(this), buttonModel: { cssClass: 'e-flat', content: this.l10n.getConstant('CancelButton') } }]
},
gObj.editSettings.dialog.params
));
if (!isBlazor()) {
args.dialog = this.dialogObj;
}
let isStringTemplate: string = 'isStringTemplate';
this.dialogObj[isStringTemplate] = true;
this.dialogObj.appendTo(this.dialog);
this.parent.applyBiggerTheme(this.dialogObj.element.parentElement);
}
private btnClick(e: MouseEvent): void {
if (this.l10n.getConstant('CancelButton').toLowerCase() === (e.target as HTMLInputElement).innerText.trim().toLowerCase()) {
this.dialogClose();
} else {
this.parent.endEdit();
}
}
private dialogClose(): void {
this.parent.closeEdit();
this.destroy();
}
private destroy(args?: { requestType: string }): void {
let editTemplateID: string = this.parent.element.id + 'editSettingsTemplate';
updateBlazorTemplate(editTemplateID, 'Template', this.parent.editSettings);
this.parent.notify(events.destroyForm, {});
this.parent.isEdit = false;
this.parent.notify(events.toolbarRefresh, {});
if (this.dialog && !this.dialogObj.isDestroyed) {
this.dialogObj.destroy();
remove(this.dialog);
}
}
private getEditElement(elements: Object, args: {rowData?: Object, form?: Element}): Element {
let gObj: IGrid = this.parent;
let div: Element = this.parent.createElement('div', { className: this.isEdit ? 'e-editedrow' : 'e-insertedrow' });
let form: HTMLFormElement = args.form =
this.parent.createElement('form', { id: gObj.element.id + 'EditForm', className: 'e-gridform' }) as HTMLFormElement;
if (this.parent.editSettings.template) {
let editTemplateID: string = this.parent.element.id + 'editSettingsTemplate';
let dummyData: Object = extend({}, args.rowData, { isAdd: !this.isEdit }, true);
appendChildren(form, this.parent.getEditTemplate()(dummyData, this.parent, 'editSettingsTemplate', editTemplateID));
updateBlazorTemplate(editTemplateID, 'Template', this.parent.editSettings);
div.appendChild(form);
return div;
}
let table: Element = this.parent.createElement('table', { className: 'e-table', attrs: { cellspacing: '6px' } });
let tbody: Element = this.parent.createElement('tbody');
let cols: Column[] = gObj.getColumns() as Column[];
for (let i: number = 0; i < cols.length; i++) {
if (this.parent.editModule.checkColumnIsGrouped(cols[i]) || cols[i].commands || cols[i].commandsTemplate) {
continue;
}
let tr: Element = this.parent.createElement('tr');
let dataCell: HTMLElement = this.parent.createElement('td', {
className: 'e-rowcell', attrs: {
style: 'text-align:' + (this.parent.enableRtl ? 'right' : 'left') + ';width:190px'
}
});
let label: Element = this.parent.createElement('label', { innerHTML: cols[i].field });
elements[cols[i].uid].classList.remove('e-input');
dataCell.appendChild(elements[cols[i].uid]);
tr.appendChild(dataCell);
tbody.appendChild(tr);
}
table.appendChild(tbody);
form.appendChild(table);
div.appendChild(form);
return div;
}
public removeEventListener(): void {
if (this.parent.isDestroyed) { return; }
this.parent.off(events.dialogDestroy, this.destroy);
this.parent.off(events.destroy, this.destroy);
}
}