Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
125 lines (108 loc) · 4.32 KB
/
Copy pathindex.ts
File metadata and controls
125 lines (108 loc) · 4.32 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
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
import type {
AdminForthResource,
IAdminForth,
IHttpServer,
} from "adminforth";
import clone from 'clone';
import { AdminForthPlugin, AdminForthResourcePages, suggestIfTypo } from "adminforth";
import { PluginOptions } from "./types.js";
import { interpretResource, ActionCheckSource } from "adminforth";
export default class ForeignInlineShowPlugin extends AdminForthPlugin {
foreignResource: AdminForthResource;
options: PluginOptions;
adminforth: IAdminForth;
constructor(options: PluginOptions) {
super(options, import.meta.url);
this.options = options;
}
instanceUniqueRepresentation(pluginOptions: any) : string {
return `${pluginOptions.foreignResourceId}`;
}
setupEndpoints(server: IHttpServer) {
process.env.HEAVY_DEBUG && console.log(`🪲 ForeignInlineShowPlugin.setupEndpoints, registering: '/plugin/${this.pluginInstanceId}/get_resource'`);
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/get_resource`,
handler: async ({ body, adminUser }) => {
const resource = this.adminforth.config.resources.find((res) => this.options.foreignResourceId === res.resourceId);
if (!resource) {
return { error: `Resource ${this.options.foreignResourceId} not found` };
}
const resourceCopy = clone({ ...resource, plugins: undefined });
if (this.options.modifyTableResourceConfig) {
this.options.modifyTableResourceConfig(resourceCopy);
}
const { allowedActions } = await interpretResource(adminUser, resourceCopy, {}, ActionCheckSource.DisplayButtons, this.adminforth);
return {
resource: {
...resourceCopy,
options: {
...resourceCopy.options,
allowedActions,
},
}
};
}
});
}
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
super.modifyResourceConfig(adminforth, resourceConfig);
this.adminforth = adminforth;
this.foreignResource = adminforth.config.resources.find((resource) => resource.resourceId === this.options.foreignResourceId);
if (!this.foreignResource) {
const similar = suggestIfTypo(adminforth.config.resources.map((res) => res.resourceId), this.options.foreignResourceId);
throw new Error(`ForeignInlineShowPlugin: Resource with ID "${this.options.foreignResourceId}" not found. ${similar ? `Did you mean "${similar}"?` : ''}`);
}
const newColumn = {
name: `foreignInlineShow_${this.foreignResource.resourceId}`,
label: 'Foreign Inline Show',
virtual: true,
showIn: {
show: true,
list: false,
edit: false,
create: false,
filter: false,
},
components: {
showRow: {
file: this.componentPath('InlineShow.vue'),
meta: {
...this.options,
pluginInstanceId: this.pluginInstanceId
}
}
},
};
if (this.options.placeInGroup?.name) {
const fieldGroupTypes = [
'fieldGroups',
'createFieldGroups',
'editFieldGroups',
'showFieldGroups'
] as const;
let columnAdded = false;
for (const groupType of fieldGroupTypes) {
const targetGroup = resourceConfig.options?.[groupType]?.find(
group => group.groupName === this.options.placeInGroup?.name
);
if (targetGroup) {
if (this.options.placeInGroup.position < 0 || this.options.placeInGroup.position > targetGroup.columns.length) {
throw new Error(`ForeignInlineShowPlugin: Invalid position ${this.options.placeInGroup?.position}. Must be between 0 and ${targetGroup.columns.length} for group "${this.options.placeInGroup?.name}"`);
}
if (!columnAdded) {
const beforeColumnName = targetGroup.columns[this.options.placeInGroup.position - 1];
const beforeColumnIndex = resourceConfig.columns.findIndex(
col => col.name === beforeColumnName
);
resourceConfig.columns.splice(beforeColumnIndex + 1, 0, newColumn);
columnAdded = true;
}
targetGroup.columns.splice(this.options.placeInGroup.position, 0, newColumn.name);
}
}
} else {
resourceConfig.columns.push(newColumn);
}
}
}