webspresso 0.0.97 → 0.0.99
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/ADMIN.md +163 -5
- package/.agents/AGENTS.md +1 -1
- package/.agents/ARCHITECTURE.md +10 -0
- package/.agents/AUTH.md +169 -0
- package/.agents/CLI.md +195 -1
- package/.agents/ORM.md +88 -4
- package/.agents/PLUGINS.md +221 -9
- package/.agents/QUEUE.md +127 -0
- package/.agents/ROUTING.md +127 -7
- package/AGENTS.md +1 -0
- package/bin/commands/add-esbuild.js +80 -0
- package/bin/commands/add-tailwind.js +76 -31
- package/bin/commands/analytics-prune.js +57 -0
- package/bin/commands/api.js +207 -15
- package/bin/commands/build.js +99 -0
- package/bin/commands/dev.js +64 -35
- package/bin/commands/doctor.js +26 -0
- package/bin/commands/email-prune.js +47 -0
- package/bin/commands/make-job.js +86 -0
- package/bin/commands/make-model.js +250 -0
- package/bin/commands/new.js +45 -28
- package/bin/commands/openapi.js +102 -0
- package/bin/commands/page.js +6 -3
- package/bin/commands/queue-prune.js +59 -0
- package/bin/commands/routes.js +52 -0
- package/bin/commands/service.js +6 -3
- package/bin/commands/typegen.js +106 -0
- package/bin/commands/upload-prune.js +79 -0
- package/bin/webspresso.js +22 -0
- package/core/applySchema.js +13 -6
- package/core/auth/hash.js +21 -12
- package/core/auth/index.js +60 -0
- package/core/auth/manager.js +134 -0
- package/core/auth/middleware.js +88 -0
- package/core/auth/oauth.js +517 -0
- package/core/auth/qrcode.js +499 -0
- package/core/auth/totp.js +504 -0
- package/core/compression/middleware.js +23 -17
- package/core/openapi/build-from-api-routes.js +160 -29
- package/core/orm/cache/fingerprint.js +15 -1
- package/core/orm/cache/memory-provider.js +27 -2
- package/core/orm/eager-loader.js +144 -10
- package/core/orm/index.js +20 -7
- package/core/orm/model.js +53 -7
- package/core/orm/query-builder.js +90 -4
- package/core/orm/repository.js +44 -3
- package/core/orm/search.js +213 -0
- package/core/queue/adapters/database.js +94 -0
- package/core/queue/adapters/memory.js +62 -0
- package/core/queue/adapters/redis.js +80 -0
- package/core/queue/cron.js +331 -0
- package/core/queue/index.js +8 -1
- package/core/queue/manager.js +205 -1
- package/core/queue/scheduler.js +359 -0
- package/core/sse/index.js +19 -0
- package/core/sse/stream.js +430 -0
- package/core/ssr/stream.js +11 -0
- package/index.d.ts +1188 -10
- package/index.js +88 -96
- package/package.json +1 -1
- package/plugins/admin-panel/api.js +279 -17
- package/plugins/admin-panel/app.js +114 -22
- package/plugins/admin-panel/client/parts/01-state-api-breadcrumb.js +314 -9
- package/plugins/admin-panel/client/parts/04-field-renderers.js +264 -67
- package/plugins/admin-panel/client/parts/05-rich-text-file-helpers.js +333 -36
- package/plugins/admin-panel/client/parts/06-login-setup-forms.js +119 -28
- package/plugins/admin-panel/client/parts/07-model-list.js +49 -8
- package/plugins/admin-panel/client/parts/08-record-list.js +248 -23
- package/plugins/admin-panel/client/parts/09-record-form.js +59 -12
- package/plugins/admin-panel/client/parts/10-export-registry.js +23 -0
- package/plugins/admin-panel/client/verify-spa-parts.js +2 -0
- package/plugins/admin-panel/core/admin-module.js +13 -7
- package/plugins/admin-panel/core/api-extensions.js +30 -2
- package/plugins/admin-panel/core/registry.js +96 -16
- package/plugins/admin-panel/field-renderers/file-upload.js +187 -104
- package/plugins/admin-panel/field-renderers/index.js +2 -0
- package/plugins/admin-panel/index.js +96 -0
- package/plugins/admin-panel/lib/login-rate-limiter.js +119 -0
- package/plugins/admin-panel/modules/admin-users.js +16 -1
- package/plugins/admin-panel/modules/bulk-actions.js +13 -5
- package/plugins/admin-panel/modules/custom-pages.js +492 -3
- package/plugins/admin-panel/modules/dashboard.js +283 -0
- package/plugins/admin-panel/modules/menu.js +56 -18
- package/plugins/admin-panel/modules/user-management.js +24 -6
- package/plugins/api-key/crypto.js +101 -0
- package/plugins/api-key/index.js +288 -0
- package/plugins/api-key/middleware.js +461 -0
- package/plugins/api-key/migration-template.js +48 -0
- package/plugins/api-key/model.js +50 -0
- package/plugins/api-key/scopes.js +125 -0
- package/plugins/audit-log/admin-component.js +487 -59
- package/plugins/audit-log/api-handlers.js +148 -0
- package/plugins/audit-log/index.js +153 -3
- package/plugins/audit-log/purge.js +60 -10
- package/plugins/dashboard/app.js +152 -27
- package/plugins/dashboard/index.js +143 -39
- package/plugins/dashboard/styles.js +78 -0
- package/plugins/data-exchange/export-xlsx.js +9 -2
- package/plugins/data-exchange/import.js +9 -2
- package/plugins/data-exchange/index.js +6 -3
- package/plugins/email/index.js +132 -2
- package/plugins/email/purge.js +60 -10
- package/plugins/file-manager/admin-component.js +20 -4
- package/plugins/file-manager/index.js +21 -4
- package/plugins/index.js +10 -0
- package/plugins/oauth/index.js +338 -0
- package/plugins/orm-cache-admin/index.js +4 -2
- package/plugins/queue/index.js +15 -0
- package/plugins/schema-explorer/admin-component.js +714 -0
- package/plugins/schema-explorer/relations-graph.js +205 -0
- package/plugins/schema-explorer.js +248 -69
- package/plugins/site-analytics/admin-component.js +23 -0
- package/plugins/site-analytics/api-handlers.js +42 -0
- package/plugins/site-analytics/index.js +147 -2
- package/plugins/site-analytics/purge.js +168 -0
- package/plugins/swagger.js +261 -13
- package/plugins/upload/index.js +26 -0
- package/plugins/upload/orphan-pruner.js +292 -0
- package/src/api/api-loader.js +4 -1
- package/src/bundle/index.js +517 -0
- package/src/bundle/manifest.js +165 -0
- package/src/dev/banner.js +296 -0
- package/src/dev/colors.js +70 -0
- package/src/dev/index.js +62 -0
- package/src/dev/profiler.js +354 -0
- package/src/dev/typegen.js +497 -0
- package/src/dev-reload/index.js +611 -0
- package/src/discovery/file-route-parser.js +44 -1
- package/src/discovery/scan-routes.js +2 -2
- package/src/file-router.js +347 -162
- package/src/helpers.js +967 -98
- package/src/njk-frontmatter.js +12 -0
- package/src/pages/page-loader.js +61 -11
- package/src/routing/cascading.js +378 -0
- package/src/routing/middleware-resolver.js +92 -0
- package/src/server.js +635 -63
|
@@ -1,83 +1,231 @@
|
|
|
1
1
|
// Field Renderers - render appropriate input based on column type
|
|
2
2
|
|
|
3
|
-
// BelongsTo relation dropdown
|
|
3
|
+
// BelongsTo relation searchable combobox dropdown component
|
|
4
4
|
const BelongsToField = {
|
|
5
5
|
oninit: (vnode) => {
|
|
6
|
-
const { col } = vnode.attrs;
|
|
7
|
-
const customField = col.customField || {};
|
|
8
|
-
const relationName = customField.relation || col.name.replace(/_id$/, '');
|
|
9
|
-
const modelName = m.route.param('model');
|
|
10
6
|
vnode.state.loading = true;
|
|
7
|
+
vnode.state.loadingMore = false;
|
|
11
8
|
vnode.state.options = [];
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
9
|
+
vnode.state.isOpen = false;
|
|
10
|
+
vnode.state.search = '';
|
|
11
|
+
vnode.state.page = 1;
|
|
12
|
+
vnode.state.hasMore = false;
|
|
13
|
+
vnode.state.total = 0;
|
|
14
|
+
vnode.state.displayField = null;
|
|
15
|
+
vnode.state.primaryKey = 'id';
|
|
16
|
+
vnode.state._searchTimer = null;
|
|
17
|
+
|
|
18
|
+
vnode.state.fetchOptions = (vn, opts = {}) => {
|
|
19
|
+
const page = opts.page || 1;
|
|
20
|
+
const append = opts.append === true;
|
|
21
|
+
const search = opts.search !== undefined ? opts.search : (vn.state.search || '');
|
|
22
|
+
const { col, value } = vn.attrs;
|
|
23
|
+
const customField = col.customField || {};
|
|
24
|
+
const relationName = customField.relation || col.name.replace(/_id$/, '');
|
|
25
|
+
const modelName = m.route.param('model');
|
|
26
|
+
|
|
27
|
+
let url = '/models/' + modelName + '/relations/' + relationName + '?page=' + page + '&limit=50';
|
|
28
|
+
if (search) url += '&search=' + encodeURIComponent(search);
|
|
29
|
+
if (value !== null && value !== undefined && value !== '') {
|
|
30
|
+
url += '&selected=' + encodeURIComponent(value);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (append) {
|
|
34
|
+
vn.state.loadingMore = true;
|
|
35
|
+
} else {
|
|
36
|
+
vn.state.loading = true;
|
|
37
|
+
}
|
|
38
|
+
m.redraw();
|
|
39
|
+
|
|
40
|
+
return api.get(url)
|
|
41
|
+
.then(result => {
|
|
42
|
+
const incoming = result.data || [];
|
|
43
|
+
const pk = result.primaryKey || vn.state.primaryKey || 'id';
|
|
44
|
+
if (append) {
|
|
45
|
+
const existingIds = new Set(vn.state.options.map(o => String(o[pk])));
|
|
46
|
+
const newItems = incoming.filter(o => !existingIds.has(String(o[pk])));
|
|
47
|
+
vn.state.options = vn.state.options.concat(newItems);
|
|
48
|
+
} else {
|
|
49
|
+
vn.state.options = incoming;
|
|
50
|
+
}
|
|
51
|
+
if (result.displayField) vn.state.displayField = result.displayField;
|
|
52
|
+
if (result.primaryKey) vn.state.primaryKey = result.primaryKey;
|
|
53
|
+
if (result.pagination) {
|
|
54
|
+
vn.state.page = result.pagination.page;
|
|
55
|
+
vn.state.hasMore = result.pagination.hasMore;
|
|
56
|
+
vn.state.total = result.pagination.total;
|
|
57
|
+
} else {
|
|
58
|
+
vn.state.hasMore = false;
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
.catch(err => {
|
|
62
|
+
console.error('Failed to load relation data:', err);
|
|
63
|
+
})
|
|
64
|
+
.finally(() => {
|
|
65
|
+
vn.state.loading = false;
|
|
66
|
+
vn.state.loadingMore = false;
|
|
67
|
+
m.redraw();
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
vnode.state.fetchOptions(vnode);
|
|
72
|
+
},
|
|
73
|
+
onremove: (vnode) => {
|
|
74
|
+
if (vnode.state._searchTimer) {
|
|
75
|
+
clearTimeout(vnode.state._searchTimer);
|
|
76
|
+
vnode.state._searchTimer = null;
|
|
77
|
+
}
|
|
24
78
|
},
|
|
25
79
|
view: (vnode) => {
|
|
26
80
|
const { col, value, onChange, readonly, error } = vnode.attrs;
|
|
27
81
|
const customField = col.customField || {};
|
|
28
82
|
const label = col.ui?.label || formatColumnLabel(col.name);
|
|
29
83
|
const hint = col.ui?.hint || '';
|
|
30
|
-
const valueKey = customField.valueKey || 'id';
|
|
31
|
-
const displayKey = customField.displayKey ||
|
|
84
|
+
const valueKey = customField.valueKey || vnode.state.primaryKey || 'id';
|
|
85
|
+
const displayKey = customField.displayKey || vnode.state.displayField ||
|
|
32
86
|
(vnode.state.options.length > 0 ? ['name', 'title', 'label', 'username', 'email'].find(k => k in vnode.state.options[0]) : null) ||
|
|
33
87
|
'id';
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
])
|
|
40
|
-
|
|
88
|
+
|
|
89
|
+
const selectedItem = (value !== null && value !== undefined && value !== '')
|
|
90
|
+
? vnode.state.options.find(item => String(item[valueKey]) === String(value))
|
|
91
|
+
: null;
|
|
92
|
+
const selectedDisplay = selectedItem
|
|
93
|
+
? (selectedItem[displayKey] || String(selectedItem[valueKey]))
|
|
94
|
+
: (value !== null && value !== undefined && value !== '' ? String(value) : '-- Select --');
|
|
95
|
+
const hasSelection = value !== null && value !== undefined && value !== '';
|
|
96
|
+
|
|
97
|
+
const selectItem = (item) => {
|
|
98
|
+
const selectedValue = item ? item[valueKey] : null;
|
|
99
|
+
const typedValue = (col.type === 'integer' || col.type === 'bigint' || col.type === 'float' || col.type === 'decimal') && selectedValue !== null
|
|
100
|
+
? Number(selectedValue)
|
|
101
|
+
: selectedValue;
|
|
102
|
+
onChange(typedValue);
|
|
103
|
+
vnode.state.isOpen = false;
|
|
104
|
+
vnode.state.search = '';
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const clearSelection = (e) => {
|
|
108
|
+
e.stopPropagation();
|
|
109
|
+
onChange(null);
|
|
110
|
+
vnode.state.search = '';
|
|
111
|
+
};
|
|
41
112
|
|
|
42
113
|
const borderClass = error
|
|
43
114
|
? 'border-red-500 dark:border-red-500 focus:ring-red-500'
|
|
44
115
|
: 'border-gray-300 dark:border-slate-600 focus:ring-blue-500 dark:focus:ring-blue-400';
|
|
45
|
-
|
|
116
|
+
|
|
46
117
|
return m('.mb-4', [
|
|
47
118
|
m('label.block.text-sm.font-medium.text-gray-700.dark:text-slate-300.mb-1', { for: col.name }, [
|
|
48
119
|
label,
|
|
49
|
-
!col.nullable && !readonly ? m('span.text-red-500', ' *') : null
|
|
120
|
+
!col.nullable && !readonly ? m('span.text-red-500', ' *') : null,
|
|
50
121
|
]),
|
|
51
|
-
m('
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
122
|
+
m('.relative', [
|
|
123
|
+
// Backdrop to close popover when clicked outside
|
|
124
|
+
vnode.state.isOpen ? m('.fixed.inset-0.z-30', {
|
|
125
|
+
onclick: () => { vnode.state.isOpen = false; },
|
|
126
|
+
}) : null,
|
|
127
|
+
|
|
128
|
+
// Trigger Button / Combobox Display
|
|
129
|
+
m('button.w-full.flex.items-center.justify-between.px-3.py-2.border.rounded-md.bg-white.dark:bg-slate-900/70.text-left.text-sm.transition-colors.relative.z-30', {
|
|
130
|
+
id: col.name,
|
|
131
|
+
type: 'button',
|
|
132
|
+
disabled: readonly,
|
|
133
|
+
class: (readonly ? 'bg-gray-100 dark:bg-slate-800 cursor-not-allowed text-gray-500 ' : 'hover:border-gray-400 dark:hover:border-slate-500 ') + borderClass,
|
|
134
|
+
onclick: () => {
|
|
135
|
+
if (!readonly) {
|
|
136
|
+
vnode.state.isOpen = !vnode.state.isOpen;
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
}, [
|
|
140
|
+
m('span.truncate', {
|
|
141
|
+
class: hasSelection ? 'text-gray-900 dark:text-slate-100 font-medium' : 'text-gray-400 dark:text-slate-500',
|
|
142
|
+
}, selectedDisplay),
|
|
143
|
+
m('.flex.items-center.gap-1.ml-2', [
|
|
144
|
+
hasSelection && !readonly ? m('span.text-gray-400.hover:text-red-500.cursor-pointer.px-1', {
|
|
145
|
+
title: 'Clear',
|
|
146
|
+
onclick: clearSelection,
|
|
147
|
+
}, '✕') : null,
|
|
148
|
+
m('svg.w-4.h-4.text-gray-400.transition-transform', {
|
|
149
|
+
class: vnode.state.isOpen ? 'rotate-180' : '',
|
|
150
|
+
fill: 'none',
|
|
151
|
+
stroke: 'currentColor',
|
|
152
|
+
viewBox: '0 0 24 24',
|
|
153
|
+
}, m('path', { strokeLinecap: 'round', strokeLinejoin: 'round', strokeWidth: 2, d: 'M19 9l-7 7-7-7' })),
|
|
154
|
+
]),
|
|
155
|
+
]),
|
|
156
|
+
|
|
157
|
+
// Dropdown Popover
|
|
158
|
+
vnode.state.isOpen ? m('.absolute.left-0.right-0.top-full.mt-1.bg-white.dark:bg-slate-900.border.border-gray-200.dark:border-slate-700.rounded-md.shadow-lg.z-40.overflow-hidden', [
|
|
159
|
+
// Search input bar
|
|
160
|
+
m('.p-2.border-b.border-gray-100.dark:border-slate-800.bg-gray-50/50.dark:bg-slate-900/50', [
|
|
161
|
+
m('input.w-full.px-2.5.py-1.5.text-xs.border.border-gray-300.dark:border-slate-700.rounded.bg-white.dark:bg-slate-800.text-gray-900.dark:text-slate-100.focus:outline-none.focus:ring-1.focus:ring-blue-500', {
|
|
162
|
+
type: 'text',
|
|
163
|
+
placeholder: 'Search ' + label.toLowerCase() + '...',
|
|
164
|
+
value: vnode.state.search,
|
|
165
|
+
oncreate: (v) => {
|
|
166
|
+
if (v.dom && typeof v.dom.focus === 'function') v.dom.focus();
|
|
167
|
+
},
|
|
168
|
+
oninput: (e) => {
|
|
169
|
+
const term = e.target.value;
|
|
170
|
+
vnode.state.search = term;
|
|
171
|
+
clearTimeout(vnode.state._searchTimer);
|
|
172
|
+
vnode.state._searchTimer = setTimeout(() => {
|
|
173
|
+
vnode.state.fetchOptions(vnode, { page: 1, append: false, search: term });
|
|
174
|
+
}, 250);
|
|
175
|
+
},
|
|
176
|
+
}),
|
|
177
|
+
]),
|
|
178
|
+
|
|
179
|
+
// Options list
|
|
180
|
+
m('.max-h-60.overflow-y-auto.py-1', [
|
|
181
|
+
vnode.state.loading ? m('.py-4.text-center.text-xs.text-gray-400', 'Loading...') : null,
|
|
182
|
+
|
|
183
|
+
// Clear / None option
|
|
184
|
+
col.nullable ? m('.px-3.py-2.text-xs.text-gray-500.hover:bg-gray-100.dark:hover:bg-slate-800.cursor-pointer', {
|
|
185
|
+
onclick: () => selectItem(null),
|
|
186
|
+
}, '-- None --') : null,
|
|
187
|
+
|
|
188
|
+
// Item options
|
|
189
|
+
!vnode.state.loading && vnode.state.options.length === 0 ? m('.py-4.text-center.text-xs.text-gray-400', 'No results found') : null,
|
|
190
|
+
|
|
191
|
+
vnode.state.options.map(item => {
|
|
192
|
+
const itemValue = item[valueKey];
|
|
193
|
+
const itemDisplay = item[displayKey] || String(itemValue);
|
|
194
|
+
const isSelected = String(value) === String(itemValue);
|
|
195
|
+
return m('.px-3.py-2.text-xs.flex.items-center.justify-between.cursor-pointer.transition-colors', {
|
|
196
|
+
class: isSelected
|
|
197
|
+
? 'bg-blue-50 dark:bg-blue-900/30 text-blue-600 dark:text-blue-300 font-medium'
|
|
198
|
+
: 'text-gray-800 dark:text-slate-200 hover:bg-gray-100 dark:hover:bg-slate-800',
|
|
199
|
+
onclick: () => selectItem(item),
|
|
200
|
+
}, [
|
|
201
|
+
m('span.truncate', itemDisplay),
|
|
202
|
+
isSelected ? m('span.text-blue-600.dark:text-blue-400.font-bold.ml-2', '✓') : null,
|
|
203
|
+
]);
|
|
204
|
+
}),
|
|
205
|
+
]),
|
|
206
|
+
|
|
207
|
+
// Pagination Load More
|
|
208
|
+
vnode.state.hasMore ? m('.p-2.border-t.border-gray-100.dark:border-slate-800.text-center.bg-gray-50/50.dark:bg-slate-900/50', [
|
|
209
|
+
m('button.text-xs.text-blue-600.dark:text-blue-400.hover:underline.font-medium.py-1.px-2', {
|
|
210
|
+
type: 'button',
|
|
211
|
+
disabled: vnode.state.loadingMore,
|
|
212
|
+
onclick: () => {
|
|
213
|
+
vnode.state.fetchOptions(vnode, {
|
|
214
|
+
page: vnode.state.page + 1,
|
|
215
|
+
append: true,
|
|
216
|
+
search: vnode.state.search,
|
|
217
|
+
});
|
|
218
|
+
},
|
|
219
|
+
}, vnode.state.loadingMore ? 'Loading more...' : 'Load more (' + (vnode.state.total - vnode.state.options.length) + ' remaining)'),
|
|
220
|
+
]) : null,
|
|
221
|
+
]) : null,
|
|
74
222
|
]),
|
|
75
223
|
error ? m('p.text-xs.text-red-600.dark:text-red-400.mt-1.font-medium', error) : (hint ? m('p.text-xs.text-gray-500.dark:text-slate-400.mt-1', hint) : null),
|
|
76
224
|
]);
|
|
77
225
|
}
|
|
78
226
|
};
|
|
79
227
|
|
|
80
|
-
// HasMany relation multi-select checkbox component
|
|
228
|
+
// HasMany relation multi-select checkbox component with filter & select-all
|
|
81
229
|
const HasManyField = {
|
|
82
230
|
oninit: (vnode) => {
|
|
83
231
|
const { col } = vnode.attrs;
|
|
@@ -86,10 +234,13 @@ const HasManyField = {
|
|
|
86
234
|
const modelName = m.route.param('model');
|
|
87
235
|
vnode.state.loading = true;
|
|
88
236
|
vnode.state.options = [];
|
|
237
|
+
vnode.state.filter = '';
|
|
89
238
|
|
|
90
|
-
api.get('/models/' + modelName + '/relations/' + relationName)
|
|
239
|
+
api.get('/models/' + modelName + '/relations/' + relationName + '?all=true')
|
|
91
240
|
.then(result => {
|
|
92
241
|
vnode.state.options = result.data || [];
|
|
242
|
+
if (result.displayField) vnode.state.displayField = result.displayField;
|
|
243
|
+
if (result.primaryKey) vnode.state.primaryKey = result.primaryKey;
|
|
93
244
|
})
|
|
94
245
|
.catch(err => {
|
|
95
246
|
console.error('Failed to load relation data:', err);
|
|
@@ -104,12 +255,38 @@ const HasManyField = {
|
|
|
104
255
|
const customField = col.customField || {};
|
|
105
256
|
const label = col.ui?.label || formatColumnLabel(col.name);
|
|
106
257
|
const hint = col.ui?.hint || '';
|
|
107
|
-
const valueKey = customField.valueKey || 'id';
|
|
108
|
-
const displayKey = customField.displayKey ||
|
|
258
|
+
const valueKey = customField.valueKey || vnode.state.primaryKey || 'id';
|
|
259
|
+
const displayKey = customField.displayKey || vnode.state.displayField ||
|
|
109
260
|
(vnode.state.options.length > 0 ? ['name', 'title', 'label', 'username', 'email'].find(k => k in vnode.state.options[0]) : null) ||
|
|
110
261
|
'id';
|
|
111
262
|
const selectedIds = Array.isArray(value) ? value.map(v => String(v)) : [];
|
|
112
|
-
|
|
263
|
+
|
|
264
|
+
const filterTerm = (vnode.state.filter || '').trim().toLowerCase();
|
|
265
|
+
const visibleOptions = vnode.state.options.filter(item => {
|
|
266
|
+
if (!filterTerm) return true;
|
|
267
|
+
const text = String(item[displayKey] || item[valueKey] || '').toLowerCase();
|
|
268
|
+
return text.includes(filterTerm);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
const updateSelection = (newSelected) => {
|
|
272
|
+
const typedSelected = col.type === 'array' || (Array.isArray(value) && typeof value[0] === 'string')
|
|
273
|
+
? newSelected
|
|
274
|
+
: newSelected.map(Number);
|
|
275
|
+
onChange(typedSelected);
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
const selectAll = () => {
|
|
279
|
+
const visibleIds = visibleOptions.map(item => String(item[valueKey]));
|
|
280
|
+
const merged = Array.from(new Set([...selectedIds, ...visibleIds]));
|
|
281
|
+
updateSelection(merged);
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
const deselectAll = () => {
|
|
285
|
+
const visibleIds = new Set(visibleOptions.map(item => String(item[valueKey])));
|
|
286
|
+
const filtered = selectedIds.filter(id => !visibleIds.has(id));
|
|
287
|
+
updateSelection(filtered);
|
|
288
|
+
};
|
|
289
|
+
|
|
113
290
|
if (vnode.state.loading) {
|
|
114
291
|
return m('.mb-4', [
|
|
115
292
|
m('label.block.text-sm.font-medium.text-gray-700.dark:text-slate-300.mb-1', label),
|
|
@@ -120,14 +297,39 @@ const HasManyField = {
|
|
|
120
297
|
const borderClass = error
|
|
121
298
|
? 'border-red-500 dark:border-red-500'
|
|
122
299
|
: 'border-gray-300 dark:border-slate-600';
|
|
123
|
-
|
|
300
|
+
|
|
124
301
|
return m('.mb-4', [
|
|
125
|
-
m('
|
|
126
|
-
label,
|
|
127
|
-
|
|
302
|
+
m('.flex.items-center.justify-between.mb-1', [
|
|
303
|
+
m('label.block.text-sm.font-medium.text-gray-700.dark:text-slate-300', [
|
|
304
|
+
label,
|
|
305
|
+
!col.nullable && !readonly ? m('span.text-red-500', ' *') : null,
|
|
306
|
+
]),
|
|
307
|
+
m('.text-xs.text-gray-500.dark:text-slate-400', selectedIds.length + ' of ' + vnode.state.options.length + ' selected'),
|
|
128
308
|
]),
|
|
309
|
+
|
|
310
|
+
// Search & select all controls
|
|
311
|
+
vnode.state.options.length > 5 ? m('.flex.items-center.gap-2.mb-2', [
|
|
312
|
+
m('input.flex-1.px-2.5.py-1.text-xs.border.border-gray-300.dark:border-slate-700.rounded.bg-white.dark:bg-slate-800.text-gray-900.dark:text-slate-100.focus:outline-none.focus:ring-1.focus:ring-blue-500', {
|
|
313
|
+
type: 'text',
|
|
314
|
+
placeholder: 'Filter ' + label.toLowerCase() + '...',
|
|
315
|
+
value: vnode.state.filter,
|
|
316
|
+
oninput: (e) => { vnode.state.filter = e.target.value; },
|
|
317
|
+
}),
|
|
318
|
+
!readonly ? m('.flex.items-center.gap-1', [
|
|
319
|
+
m('button.px-2.py-1.text-xs.border.border-gray-300.dark:border-slate-700.rounded.hover:bg-gray-100.dark:hover:bg-slate-800.text-gray-700.dark:text-slate-300', {
|
|
320
|
+
type: 'button',
|
|
321
|
+
onclick: selectAll,
|
|
322
|
+
}, 'All'),
|
|
323
|
+
m('button.px-2.py-1.text-xs.border.border-gray-300.dark:border-slate-700.rounded.hover:bg-gray-100.dark:hover:bg-slate-800.text-gray-700.dark:text-slate-300', {
|
|
324
|
+
type: 'button',
|
|
325
|
+
onclick: deselectAll,
|
|
326
|
+
}, 'None'),
|
|
327
|
+
]) : null,
|
|
328
|
+
]) : null,
|
|
329
|
+
|
|
129
330
|
m('.border.rounded-md.p-3.max-h-48.overflow-y-auto.bg-white.dark:bg-slate-900/70', { class: borderClass }, [
|
|
130
|
-
|
|
331
|
+
visibleOptions.length === 0 ? m('.py-2.text-center.text-xs.text-gray-400', 'No matching items') : null,
|
|
332
|
+
visibleOptions.map(item => {
|
|
131
333
|
const itemValue = String(item[valueKey]);
|
|
132
334
|
const itemDisplay = item[displayKey] || itemValue;
|
|
133
335
|
const isSelected = selectedIds.includes(itemValue);
|
|
@@ -146,12 +348,7 @@ const HasManyField = {
|
|
|
146
348
|
} else {
|
|
147
349
|
newSelected = newSelected.filter(id => id !== itemValue);
|
|
148
350
|
}
|
|
149
|
-
|
|
150
|
-
// Convert type if needed
|
|
151
|
-
const typedSelected = col.type === 'array' || typeof value[0] === 'string'
|
|
152
|
-
? newSelected
|
|
153
|
-
: newSelected.map(Number);
|
|
154
|
-
onChange(typedSelected);
|
|
351
|
+
updateSelection(newSelected);
|
|
155
352
|
}
|
|
156
353
|
}),
|
|
157
354
|
m('span', itemDisplay),
|