-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
64 lines (55 loc) · 2.23 KB
/
Copy pathindex.ts
File metadata and controls
64 lines (55 loc) · 2.23 KB
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
import { AdminForthPlugin, AdminForthDataTypes } from "adminforth";
import type { IAdminForth, AdminForthResource } from "adminforth";
import type { PluginOptions } from './types.js';
export default class JsonEditorPlugin extends AdminForthPlugin {
options: PluginOptions;
resourceConfig!: AdminForthResource;
constructor(options: PluginOptions) {
super(options, import.meta.url);
this.options = options;
}
instanceUniqueRepresentation(pluginOptions: any): string {
return pluginOptions.fieldName;
}
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
const column = resourceConfig.columns.find(c => c.name === this.options.fieldName);
if (!column) {
throw new Error(`[JsonEditorPlugin] Column "${this.options.fieldName}" not found in resource "${resourceConfig.label}"`);
}
const validTypes = [AdminForthDataTypes.JSON, AdminForthDataTypes.STRING, AdminForthDataTypes.TEXT];
if (!validTypes.includes(column.type!)) {
throw new Error(
`[JsonEditorPlugin] Column "${this.options.fieldName}" must be of type json, string, or text, but got "${column.type}"`
);
}
}
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
super.modifyResourceConfig(adminforth, resourceConfig);
this.resourceConfig = resourceConfig;
const column = resourceConfig.columns.find(c => c.name === this.options.fieldName);
if (!column) {
throw new Error(`[JsonEditorPlugin] Column "${this.options.fieldName}" not found in resource "${resourceConfig.label}"`);
}
if (!column.components) {
column.components = {};
}
const editorComponent = {
file: this.componentPath('JsonEditor.vue'),
meta: {
pluginInstanceId: this.pluginInstanceId,
columnName: this.options.fieldName,
},
};
const viewerComponent = {
file: this.componentPath('JsonViewer.vue'),
meta: {
pluginInstanceId: this.pluginInstanceId,
columnName: this.options.fieldName,
},
};
column.components.edit = editorComponent;
column.components.create = editorComponent;
column.components.show = viewerComponent;
column.components.list = viewerComponent;
}
}