-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathcustom-angularComponentEditor.ts
187 lines (157 loc) · 5.89 KB
/
custom-angularComponentEditor.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import type { ComponentRef } from '@angular/core';
import type { Subscription } from 'rxjs';
import {
AngularUtilService,
type Column,
type ColumnEditor,
type Editor,
type EditorValidator,
type EditorValidationResult,
type GridOption,
type SlickGrid,
unsubscribeAllObservables,
} from './../modules/angular-slickgrid';
/*
* An example of a 'detached' editor.
* KeyDown events are also handled to provide handling for Tab, Shift-Tab, Esc and Ctrl-Enter.
*/
export class CustomAngularComponentEditor implements Editor {
private _subscriptions: Subscription[] = [];
/** Angular Component Reference */
componentRef!: ComponentRef<any>;
/** default item Id */
defaultId = '';
/** default item object */
defaultItem: any;
/** SlickGrid grid object */
grid: SlickGrid;
constructor(private args: any) {
this.grid = args?.grid;
this.init();
}
/** Angular Util Service (could be inside the Grid Options Params or the Editor Params ) */
get angularUtilService(): AngularUtilService {
let angularUtilService = this.gridOptions && this.gridOptions.params && this.gridOptions.params.angularUtilService;
if (!angularUtilService || !(angularUtilService instanceof AngularUtilService)) {
angularUtilService = this.columnEditor && this.columnEditor.params && this.columnEditor.params.angularUtilService;
}
return angularUtilService;
}
/** Get the Collection */
get collection(): any[] {
return this.columnDef?.editor!.collection ?? [];
}
/** Get Column Definition object */
get columnDef(): Column {
return this.args?.column ?? {};
}
/** Get Column Editor object */
get columnEditor(): ColumnEditor {
return this.columnDef?.editor ?? {};
}
/** Getter for the Grid Options pulled through the Grid Object */
get gridOptions(): GridOption {
return (this.grid?.getOptions() ?? {}) as GridOption;
}
get hasAutoCommitEdit(): boolean {
return this.gridOptions.autoCommitEdit ?? false;
}
/** Get the Validator function, can be passed in Editor property or Column Definition */
get validator(): EditorValidator | undefined {
return this.columnEditor.validator || this.columnDef.validator;
}
init() {
if (!this.columnEditor || !this.columnEditor.params.component || !(this.angularUtilService instanceof AngularUtilService)) {
throw new Error(`[Angular-Slickgrid] For Editor with Angular Component to work properly, you need to provide the "AngularUtilService" via the Editor "params" OR the Grid Options "params"
Example: this.columnDefs = [{ id: 'title', field: 'title', editor: { model: CustomEditor, collection: [...], params: { component: MyComponent, angularUtilService: this.angularUtilService }}];
OR this.columnDefs = [{ id: 'title', field: 'title', editor: { model: CustomEditor, collection: [...] }]; this.gridOptions = { params: { angularUtilService: this.angularUtilService }}`);
}
if (this.columnEditor?.params.component) {
const componentOutput = this.angularUtilService.createAngularComponentAppendToDom(
this.columnEditor.params.component,
this.args.container
);
this.componentRef = componentOutput?.componentRef;
// here we override the collection object of the Angular Component
// but technically you can pass any values you wish to your Component
Object.assign(this.componentRef.instance, { collection: this.collection });
// when our model (item object) changes, we'll call a save of the slickgrid editor
this._subscriptions.push(this.componentRef.instance.onItemChanged.subscribe((_item: any) => this.save()));
}
}
save() {
const validation = this.validate();
if (validation && validation.valid) {
if (this.hasAutoCommitEdit) {
this.args.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
}
}
cancel() {
this.componentRef.instance.selectedId = this.defaultId;
this.componentRef.instance.selectedItem = this.defaultItem;
if (this.args?.cancelChanges) {
this.args.cancelChanges();
}
}
/** optional, implement a hide method on your Angular Component */
hide() {
if (typeof this.componentRef?.instance.hide === 'function') {
this.componentRef.instance.hide();
}
}
/** optional, implement a show method on your Angular Component */
show() {
if (typeof this.componentRef?.instance.show === 'function') {
this.componentRef.instance.show();
}
}
/** destroy the Angular Component & Subscription */
destroy() {
if (this.componentRef?.destroy) {
this.componentRef.destroy();
}
// also unsubscribe all Angular Subscriptions
unsubscribeAllObservables(this._subscriptions);
}
/** optional, implement a focus method on your Angular Component */
focus() {
if (typeof this.componentRef?.instance.focus === 'function') {
this.componentRef.instance.focus();
}
}
applyValue(item: any, state: any) {
item[this.columnDef.field] = state;
}
getValue() {
return this.componentRef.instance.selectedId;
}
loadValue(item: any) {
const itemObject = item?.[this.columnDef.field];
this.componentRef.instance.selectedId = itemObject?.id || '';
this.componentRef.instance.selectedItem = itemObject;
}
serializeValue(): any {
return this.componentRef.instance.selectedItem;
}
isValueChanged() {
return (
!(this.componentRef.instance.selectedId === '' && (this.defaultId === null || this.defaultId === undefined)) &&
this.componentRef.instance.selectedId !== this.defaultId
);
}
validate(): EditorValidationResult {
if (this.validator) {
const value = this.componentRef.instance.selectedId;
return this.validator(value, this.args);
}
// by default the editor is always valid
// if user want it to be required, he would have to provide his own validator
return {
valid: true,
msg: null,
};
}
}