webspresso 0.0.108 → 0.0.110
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/LICENSE +21 -0
- package/README.md +3 -3
- package/bin/commands/add-esbuild.js +7 -3
- package/bin/commands/add-tailwind.js +9 -5
- package/bin/commands/build.js +20 -4
- package/bin/commands/dev.js +57 -7
- package/bin/commands/doctor.js +42 -1
- package/bin/commands/favicon-generate.js +10 -3
- package/bin/commands/new.js +8 -3
- package/bin/commands/start.js +3 -1
- package/bin/utils/add-command.js +16 -0
- package/core/app.js +45 -3
- package/core/auth/authenticate.js +3 -3
- package/core/auth/hash.js +167 -43
- package/core/auth/index.js +13 -1
- package/core/auth/jwt.js +21 -2
- package/core/auth/manager.js +63 -14
- package/core/auth/middleware.js +4 -2
- package/core/auth/oauth.js +5 -1
- package/core/auth/tokens.js +7 -2
- package/core/auth/totp.js +42 -6
- package/core/auth/user-agent.js +2 -0
- package/core/client/adapter/datastar.js +1401 -0
- package/core/client/api.js +1337 -0
- package/core/client/controller.js +1706 -0
- package/core/client/debug.js +367 -0
- package/core/client/devtool.js +1134 -0
- package/core/client/forms.js +1164 -0
- package/core/client/fragments.js +894 -0
- package/core/client/index.d.ts +59 -0
- package/core/client/index.js +391 -0
- package/core/client/lite.js +175 -0
- package/core/client/server-fragments.js +868 -0
- package/core/client/storage.js +339 -0
- package/core/client/utils.js +281 -0
- package/core/compression/middleware.js +277 -26
- package/core/context/app-context.js +25 -3
- package/core/context/request.js +32 -4
- package/core/errors/middleware.js +14 -0
- package/core/events/dispatcher.js +33 -0
- package/core/forms/flash.js +136 -102
- package/core/health/middleware.js +36 -6
- package/core/http/pipeline.js +45 -6
- package/core/index.js +25 -7
- package/core/kernel/view.js +41 -10
- package/core/metrics/middleware.js +30 -6
- package/core/metrics/registry.js +13 -5
- package/core/orm/cache/fingerprint.js +32 -11
- package/core/orm/cache/layer.js +19 -2
- package/core/orm/hidden.js +145 -0
- package/core/orm/model.js +48 -2
- package/core/orm/query-builder.js +343 -6
- package/core/orm/query-filters.js +14 -2
- package/core/orm/repository.js +64 -13
- package/core/orm/schema-helpers.js +9 -6
- package/core/orm/transaction.js +50 -6
- package/core/orm/types.js +4 -0
- package/core/queue/adapters/database.js +184 -2
- package/core/queue/adapters/memory.js +153 -5
- package/core/queue/adapters/redis.js +59 -4
- package/core/queue/job.js +14 -1
- package/core/queue/manager.js +115 -0
- package/core/resilience/circuit-breaker.js +27 -8
- package/core/services/builtins/exchange.js +174 -7
- package/core/services/builtins/file-manager.js +36 -0
- package/core/services/builtins/mail.js +27 -0
- package/core/services/builtins/queue.js +144 -0
- package/core/services/builtins/security.js +118 -0
- package/core/services/builtins/webhook.js +110 -0
- package/core/services/executor.js +38 -16
- package/core/services/index.js +4 -1
- package/core/sse/stream.js +28 -5
- package/core/utils/timeout.js +8 -1
- package/core/validation/compiler.js +10 -7
- package/index.d.ts +1320 -74
- package/index.js +128 -8
- package/package.json +75 -27
- package/plugins/admin-panel/client/parts/08-record-list.js +717 -70
- package/plugins/admin-panel/index.js +19 -8
- package/plugins/csrf/index.js +3 -3
- package/plugins/data-exchange/export.js +214 -0
- package/plugins/data-exchange/import.js +48 -5
- package/plugins/data-exchange/index.js +70 -28
- package/plugins/data-exchange/preview.js +266 -0
- package/plugins/data-exchange/template.js +242 -0
- package/plugins/email/admin-component.js +783 -141
- package/plugins/email/api-handlers.js +51 -1
- package/plugins/email/index.js +5 -0
- package/plugins/email/service.js +48 -0
- package/plugins/file-manager/admin-component.js +1546 -70
- package/plugins/file-manager/api-handlers.js +166 -1
- package/plugins/file-manager/core.js +598 -8
- package/plugins/file-manager/index.js +8 -0
- package/plugins/index.js +7 -0
- package/plugins/mcp/auth.js +3 -3
- package/plugins/oauth/index.js +36 -3
- package/plugins/queue/admin-component.js +1092 -0
- package/plugins/queue/api-handlers.js +248 -0
- package/plugins/queue/index.js +69 -0
- package/plugins/rest-resources/index.js +7 -2
- package/plugins/security/admin-component.js +689 -0
- package/plugins/security/api-handlers.js +169 -0
- package/plugins/security/audit.js +153 -0
- package/plugins/security/bruteforce.js +244 -0
- package/plugins/security/firewall.js +459 -0
- package/plugins/security/headers.js +214 -0
- package/plugins/security/honeypot.js +234 -0
- package/plugins/security/incident-recorder.js +137 -0
- package/plugins/security/index.js +211 -0
- package/plugins/seo-checker/analyzer.js +20 -7
- package/plugins/upload/index.js +36 -0
- package/plugins/webhooks/admin-component.js +702 -175
- package/plugins/webhooks/api-handlers.js +75 -1
- package/plugins/webhooks/dispatcher.js +290 -11
- package/plugins/webhooks/index.js +37 -2
- package/plugins/webhooks/sender.js +56 -0
- package/plugins/xlsx/engine.js +19 -4
- package/src/api/api-loader.js +19 -1
- package/src/bundle/index.js +443 -17
- package/src/client-runtime/bootstrap-swup.js +9 -0
- package/src/client-runtime/mount.js +15 -20
- package/src/client-runtime/resolve.js +3 -2
- package/src/dev/api-typegen.js +1300 -0
- package/src/dev/banner.js +10 -1
- package/src/dev/colors.js +1 -1
- package/src/dev/index.js +7 -1
- package/src/dev/typegen.js +173 -17
- package/src/dev-reload/index.js +1 -1
- package/src/discovery/file-route-parser.js +17 -3
- package/src/discovery/scan-routes.js +4 -1
- package/src/file-router.js +253 -69
- package/src/helpers.js +46 -26
- package/src/njk-frontmatter.js +30 -4
- package/src/routing/cascading.js +26 -8
- package/src/routing/mount-discovered-routes.js +76 -0
- package/src/server.js +231 -70
- package/views/partials/webspresso-client-runtime.njk +1 -9
- package/.agents/ADMIN.md +0 -426
- package/.agents/AGENTS.md +0 -96
- package/.agents/ARCHITECTURE.md +0 -163
- package/.agents/AUTH.md +0 -581
- package/.agents/CACHE.md +0 -148
- package/.agents/CLI.md +0 -649
- package/.agents/COMPRESSION.md +0 -59
- package/.agents/CONTEXT.md +0 -152
- package/.agents/CONVENTIONS.md +0 -50
- package/.agents/EARLY_HINTS.md +0 -120
- package/.agents/EMAIL.md +0 -223
- package/.agents/ERRORS.md +0 -260
- package/.agents/EVENTS.md +0 -179
- package/.agents/FLAGS.md +0 -118
- package/.agents/HEALTH.md +0 -118
- package/.agents/LIMITS.md +0 -109
- package/.agents/LOGGING.md +0 -93
- package/.agents/MCP.md +0 -103
- package/.agents/METRICS.md +0 -127
- package/.agents/MIGRATION_JOBS.md +0 -95
- package/.agents/ORM.md +0 -700
- package/.agents/PLUGINS.md +0 -729
- package/.agents/QUEUE.md +0 -290
- package/.agents/RATE_LIMIT.md +0 -147
- package/.agents/REALTIME.md +0 -213
- package/.agents/RESILIENCE.md +0 -235
- package/.agents/ROUTING.md +0 -605
- package/.agents/SCALE_OUT.md +0 -109
- package/.agents/SECRETS.md +0 -117
- package/.agents/SERVICES.md +0 -341
- package/.agents/SHUTDOWN.md +0 -74
- package/.agents/STREAMING.md +0 -108
- package/.agents/WORKFLOW.md +0 -27
- package/.agents/internal/DOCUMENTATION_REVIEW.md +0 -485
- package/.agents/internal/TECHNICAL_REVIEW.md +0 -428
- package/.agents/skills/webspresso-admin-and-plugins/SKILL.md +0 -122
- package/.agents/skills/webspresso-framework/SKILL.md +0 -89
- package/.agents/skills/webspresso-orm-and-database/SKILL.md +0 -127
- package/.agents/skills/webspresso-routing-and-api/SKILL.md +0 -130
- package/.agents/skills/webspresso-services/SKILL.md +0 -134
- package/.cursorrules +0 -14
- package/AGENTS.md +0 -203
- package/CLAUDE.md +0 -54
- package/core/kernel/run-demo.js +0 -58
- package/plugins/admin-panel/styles.js +0 -9
- package/plugins/data-exchange/export-xlsx.js +0 -119
- package/src/client-runtime/bootstrap-alpine-swup.js +0 -34
|
@@ -183,6 +183,715 @@ async function executeBulkAction(action, modelName, modelMeta) {
|
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
+
// --- Data Exchange Visual Wizard State & Components ---
|
|
187
|
+
var dataExchangeState = {
|
|
188
|
+
importOpen: false,
|
|
189
|
+
exportOpen: false,
|
|
190
|
+
modelName: '',
|
|
191
|
+
modelMeta: null,
|
|
192
|
+
step: 1, // 1: upload & settings, 2: column mapping & preview, 3: results
|
|
193
|
+
file: null,
|
|
194
|
+
fileName: '',
|
|
195
|
+
fileSize: '',
|
|
196
|
+
mode: 'insert',
|
|
197
|
+
upsertKey: 'id',
|
|
198
|
+
atomic: false,
|
|
199
|
+
loading: false,
|
|
200
|
+
error: null,
|
|
201
|
+
previewData: null,
|
|
202
|
+
columnMapping: {},
|
|
203
|
+
results: null,
|
|
204
|
+
|
|
205
|
+
// Export
|
|
206
|
+
exportFormat: 'xlsx',
|
|
207
|
+
exportColumns: {},
|
|
208
|
+
exportScope: 'all',
|
|
209
|
+
selectedRecords: [],
|
|
210
|
+
exportLoading: false,
|
|
211
|
+
exportError: null,
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
function openDataExchangeImport(modelName, modelMeta, file) {
|
|
215
|
+
dataExchangeState.importOpen = true;
|
|
216
|
+
dataExchangeState.exportOpen = false;
|
|
217
|
+
dataExchangeState.modelName = modelName;
|
|
218
|
+
dataExchangeState.modelMeta = modelMeta;
|
|
219
|
+
dataExchangeState.step = 1;
|
|
220
|
+
dataExchangeState.file = file || null;
|
|
221
|
+
dataExchangeState.fileName = file ? file.name : '';
|
|
222
|
+
dataExchangeState.fileSize = file ? (file.size / 1024).toFixed(1) + ' KB' : '';
|
|
223
|
+
dataExchangeState.mode = 'insert';
|
|
224
|
+
dataExchangeState.upsertKey = modelMeta?.primaryKey || 'id';
|
|
225
|
+
dataExchangeState.atomic = false;
|
|
226
|
+
dataExchangeState.loading = false;
|
|
227
|
+
dataExchangeState.error = null;
|
|
228
|
+
dataExchangeState.previewData = null;
|
|
229
|
+
dataExchangeState.columnMapping = {};
|
|
230
|
+
dataExchangeState.results = null;
|
|
231
|
+
|
|
232
|
+
if (file) {
|
|
233
|
+
fetchImportPreview();
|
|
234
|
+
} else {
|
|
235
|
+
m.redraw();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function openDataExchangeExport(modelName, modelMeta, selectedRecords) {
|
|
240
|
+
dataExchangeState.exportOpen = true;
|
|
241
|
+
dataExchangeState.importOpen = false;
|
|
242
|
+
dataExchangeState.modelName = modelName;
|
|
243
|
+
dataExchangeState.modelMeta = modelMeta;
|
|
244
|
+
dataExchangeState.exportFormat = 'xlsx';
|
|
245
|
+
const sel = selectedRecords ? (selectedRecords instanceof Set ? Array.from(selectedRecords) : selectedRecords) : [];
|
|
246
|
+
dataExchangeState.selectedRecords = sel;
|
|
247
|
+
dataExchangeState.exportScope = sel.length > 0 ? 'selected' : 'all';
|
|
248
|
+
dataExchangeState.exportLoading = false;
|
|
249
|
+
dataExchangeState.exportError = null;
|
|
250
|
+
|
|
251
|
+
// Initialize columns
|
|
252
|
+
dataExchangeState.exportColumns = {};
|
|
253
|
+
if (modelMeta?.columns) {
|
|
254
|
+
const hidden = new Set(modelMeta.hidden || []);
|
|
255
|
+
const cols = modelMeta.columns instanceof Map ? Array.from(modelMeta.columns.keys()) : Object.keys(modelMeta.columns);
|
|
256
|
+
for (const c of cols) {
|
|
257
|
+
if (!hidden.has(c)) {
|
|
258
|
+
dataExchangeState.exportColumns[c] = true;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
m.redraw();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function downloadExchangeTemplate(format) {
|
|
266
|
+
const adminPath = window.__ADMIN_PATH__ || '/_admin';
|
|
267
|
+
const url = adminPath + '/api/data-exchange/template/' + dataExchangeState.modelName +
|
|
268
|
+
'?format=' + format + '&mode=' + dataExchangeState.mode;
|
|
269
|
+
window.open(url, '_blank');
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function fetchImportPreview() {
|
|
273
|
+
if (!dataExchangeState.file) return;
|
|
274
|
+
dataExchangeState.loading = true;
|
|
275
|
+
dataExchangeState.error = null;
|
|
276
|
+
m.redraw();
|
|
277
|
+
|
|
278
|
+
const adminPath = window.__ADMIN_PATH__ || '/_admin';
|
|
279
|
+
const fd = new FormData();
|
|
280
|
+
fd.append('file', dataExchangeState.file);
|
|
281
|
+
|
|
282
|
+
const fetchFn = (typeof fetchWithTimeout === 'function' ? fetchWithTimeout : fetch);
|
|
283
|
+
fetchFn(adminPath + '/api/data-exchange/preview/' + dataExchangeState.modelName, {
|
|
284
|
+
method: 'POST',
|
|
285
|
+
body: fd,
|
|
286
|
+
credentials: 'include',
|
|
287
|
+
timeout: 30000,
|
|
288
|
+
}).then(function(res) {
|
|
289
|
+
return res.json().then(function(body) {
|
|
290
|
+
if (!res.ok) throw new Error(body.error || 'Preview failed');
|
|
291
|
+
return body;
|
|
292
|
+
});
|
|
293
|
+
}).then(function(data) {
|
|
294
|
+
dataExchangeState.loading = false;
|
|
295
|
+
dataExchangeState.previewData = data;
|
|
296
|
+
dataExchangeState.columnMapping = Object.assign({}, data.suggestedMapping || {});
|
|
297
|
+
dataExchangeState.step = 2;
|
|
298
|
+
m.redraw();
|
|
299
|
+
}).catch(function(err) {
|
|
300
|
+
dataExchangeState.loading = false;
|
|
301
|
+
dataExchangeState.error = err.message || 'Failed to analyze file';
|
|
302
|
+
m.redraw();
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function executeImport() {
|
|
307
|
+
dataExchangeState.loading = true;
|
|
308
|
+
dataExchangeState.error = null;
|
|
309
|
+
m.redraw();
|
|
310
|
+
|
|
311
|
+
const adminPath = window.__ADMIN_PATH__ || '/_admin';
|
|
312
|
+
const fd = new FormData();
|
|
313
|
+
fd.append('file', dataExchangeState.file);
|
|
314
|
+
fd.append('mode', dataExchangeState.mode);
|
|
315
|
+
fd.append('upsertKey', dataExchangeState.upsertKey);
|
|
316
|
+
fd.append('atomic', dataExchangeState.atomic ? 'true' : 'false');
|
|
317
|
+
fd.append('columnMapping', JSON.stringify(dataExchangeState.columnMapping));
|
|
318
|
+
|
|
319
|
+
const fetchFn = (typeof fetchWithTimeout === 'function' ? fetchWithTimeout : fetch);
|
|
320
|
+
fetchFn(adminPath + '/api/data-exchange/import/' + dataExchangeState.modelName +
|
|
321
|
+
'?mode=' + encodeURIComponent(dataExchangeState.mode) +
|
|
322
|
+
'&upsertKey=' + encodeURIComponent(dataExchangeState.upsertKey) +
|
|
323
|
+
'&atomic=' + (dataExchangeState.atomic ? 'true' : 'false'), {
|
|
324
|
+
method: 'POST',
|
|
325
|
+
body: fd,
|
|
326
|
+
credentials: 'include',
|
|
327
|
+
timeout: 120000,
|
|
328
|
+
}).then(function(res) {
|
|
329
|
+
return res.json().then(function(body) {
|
|
330
|
+
if (!res.ok) throw new Error(body.error || 'Import failed');
|
|
331
|
+
return body;
|
|
332
|
+
});
|
|
333
|
+
}).then(function(data) {
|
|
334
|
+
dataExchangeState.loading = false;
|
|
335
|
+
dataExchangeState.results = data;
|
|
336
|
+
dataExchangeState.step = 3;
|
|
337
|
+
m.redraw();
|
|
338
|
+
}).catch(function(err) {
|
|
339
|
+
dataExchangeState.loading = false;
|
|
340
|
+
dataExchangeState.error = err.message || 'Import failed';
|
|
341
|
+
m.redraw();
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function executeExport() {
|
|
346
|
+
dataExchangeState.exportLoading = true;
|
|
347
|
+
dataExchangeState.exportError = null;
|
|
348
|
+
m.redraw();
|
|
349
|
+
|
|
350
|
+
const adminPath = window.__ADMIN_PATH__ || '/_admin';
|
|
351
|
+
const chosenCols = Object.keys(dataExchangeState.exportColumns).filter(function(k) {
|
|
352
|
+
return dataExchangeState.exportColumns[k];
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
let q = '?format=' + dataExchangeState.exportFormat;
|
|
356
|
+
if (chosenCols.length > 0) {
|
|
357
|
+
q += '&columns=' + encodeURIComponent(chosenCols.join(','));
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (dataExchangeState.exportScope === 'selected' && dataExchangeState.selectedRecords.length > 0) {
|
|
361
|
+
const pk = dataExchangeState.modelMeta?.primaryKey || 'id';
|
|
362
|
+
const ids = dataExchangeState.selectedRecords.map(function(r) { return r[pk]; }).filter(Boolean);
|
|
363
|
+
if (ids.length > 0) {
|
|
364
|
+
q += '&ids=' + encodeURIComponent(ids.join(','));
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const fetchFn = (typeof fetchWithTimeout === 'function' ? fetchWithTimeout : fetch);
|
|
369
|
+
fetchFn(adminPath + '/api/data-exchange/export/' + dataExchangeState.modelName + q, {
|
|
370
|
+
method: 'GET',
|
|
371
|
+
credentials: 'include',
|
|
372
|
+
timeout: 60000,
|
|
373
|
+
}).then(function(res) {
|
|
374
|
+
if (!res.ok) {
|
|
375
|
+
return res.json().then(function(b) { throw new Error(b.error || 'Export failed'); });
|
|
376
|
+
}
|
|
377
|
+
return res.blob();
|
|
378
|
+
}).then(function(blob) {
|
|
379
|
+
const ext = dataExchangeState.exportFormat === 'csv' ? 'csv' : 'xlsx';
|
|
380
|
+
const filename = dataExchangeState.modelName + '_export.' + ext;
|
|
381
|
+
const url = URL.createObjectURL(blob);
|
|
382
|
+
const a = document.createElement('a');
|
|
383
|
+
a.style.display = 'none';
|
|
384
|
+
a.href = url;
|
|
385
|
+
a.download = filename;
|
|
386
|
+
document.body.appendChild(a);
|
|
387
|
+
a.click();
|
|
388
|
+
setTimeout(function() {
|
|
389
|
+
if (a.parentNode) a.parentNode.removeChild(a);
|
|
390
|
+
URL.revokeObjectURL(url);
|
|
391
|
+
}, 1000);
|
|
392
|
+
dataExchangeState.exportLoading = false;
|
|
393
|
+
dataExchangeState.exportOpen = false;
|
|
394
|
+
notifySuccess('Export complete! File download started.');
|
|
395
|
+
m.redraw();
|
|
396
|
+
}).catch(function(err) {
|
|
397
|
+
dataExchangeState.exportLoading = false;
|
|
398
|
+
dataExchangeState.exportError = err.message || 'Export failed';
|
|
399
|
+
notifyError('Export failed: ' + err.message);
|
|
400
|
+
m.redraw();
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// Visual Data Exchange Import Modal
|
|
405
|
+
var DataExchangeImportModal = {
|
|
406
|
+
view: function() {
|
|
407
|
+
if (!dataExchangeState.importOpen) return null;
|
|
408
|
+
const modelLabel = dataExchangeState.modelMeta?.label || dataExchangeState.modelName;
|
|
409
|
+
|
|
410
|
+
return m('div.fixed.inset-0.z-50.flex.items-center.justify-center.p-4', [
|
|
411
|
+
m('div.fixed.inset-0.bg-black/60.backdrop-blur-sm', {
|
|
412
|
+
onclick: function() {
|
|
413
|
+
if (!dataExchangeState.loading) dataExchangeState.importOpen = false;
|
|
414
|
+
}
|
|
415
|
+
}),
|
|
416
|
+
m('div.relative.bg-white.dark:bg-slate-900.rounded-2xl.shadow-2xl.border.border-gray-200.dark:border-slate-800.max-w-3xl.w-full.max-h-[90vh].flex.flex-col.overflow-hidden.z-10', [
|
|
417
|
+
// Modal Header
|
|
418
|
+
m('div.px-6.py-4.border-b.border-gray-100.dark:border-slate-800.flex.items-center.justify-between.bg-gray-50/50.dark:bg-slate-800/40', [
|
|
419
|
+
m('div.flex.items-center.gap-3', [
|
|
420
|
+
m('div.w-9.h-9.rounded-xl.bg-indigo-100.dark:bg-indigo-950/70.text-indigo-600.dark:text-indigo-400.flex.items-center.justify-center.font-bold', '📥'),
|
|
421
|
+
m('div', [
|
|
422
|
+
m('h3.text-base.font-bold.text-gray-900.dark:text-slate-100', 'Spreadsheet Import Wizard'),
|
|
423
|
+
m('p.text-xs.text-gray-500.dark:text-slate-400', 'Target Model: ' + modelLabel)
|
|
424
|
+
])
|
|
425
|
+
]),
|
|
426
|
+
m('button.text-gray-400.hover:text-gray-600.dark:hover:text-slate-200.p-1.5.rounded-lg', {
|
|
427
|
+
onclick: function() { dataExchangeState.importOpen = false; }
|
|
428
|
+
}, '✕')
|
|
429
|
+
]),
|
|
430
|
+
|
|
431
|
+
// Stepper Navigation
|
|
432
|
+
m('div.px-6.py-3.bg-gray-100/60.dark:bg-slate-800/60.border-b.border-gray-200.dark:border-slate-700/60.flex.items-center.justify-between.text-xs.font-semibold', [
|
|
433
|
+
m('div.flex.items-center.gap-2', {
|
|
434
|
+
class: dataExchangeState.step === 1 ? 'text-indigo-600 dark:text-indigo-400' : 'text-gray-500 dark:text-slate-400'
|
|
435
|
+
}, [
|
|
436
|
+
m('span.w-5.h-5.rounded-full.flex.items-center.justify-center.text-xs', {
|
|
437
|
+
class: dataExchangeState.step === 1 ? 'bg-indigo-600 text-white' : 'bg-gray-200 dark:bg-slate-700 text-gray-700 dark:text-slate-300'
|
|
438
|
+
}, '1'),
|
|
439
|
+
'Upload & Settings'
|
|
440
|
+
]),
|
|
441
|
+
m('span.text-gray-400', '➔'),
|
|
442
|
+
m('div.flex.items-center.gap-2', {
|
|
443
|
+
class: dataExchangeState.step === 2 ? 'text-indigo-600 dark:text-indigo-400' : 'text-gray-500 dark:text-slate-400'
|
|
444
|
+
}, [
|
|
445
|
+
m('span.w-5.h-5.rounded-full.flex.items-center.justify-center.text-xs', {
|
|
446
|
+
class: dataExchangeState.step === 2 ? 'bg-indigo-600 text-white' : 'bg-gray-200 dark:bg-slate-700 text-gray-700 dark:text-slate-300'
|
|
447
|
+
}, '2'),
|
|
448
|
+
'Column Mapping & Preview'
|
|
449
|
+
]),
|
|
450
|
+
m('span.text-gray-400', '➔'),
|
|
451
|
+
m('div.flex.items-center.gap-2', {
|
|
452
|
+
class: dataExchangeState.step === 3 ? 'text-indigo-600 dark:text-indigo-400' : 'text-gray-500 dark:text-slate-400'
|
|
453
|
+
}, [
|
|
454
|
+
m('span.w-5.h-5.rounded-full.flex.items-center.justify-center.text-xs', {
|
|
455
|
+
class: dataExchangeState.step === 3 ? 'bg-indigo-600 text-white' : 'bg-gray-200 dark:bg-slate-700 text-gray-700 dark:text-slate-300'
|
|
456
|
+
}, '3'),
|
|
457
|
+
'Execution Results'
|
|
458
|
+
]),
|
|
459
|
+
]),
|
|
460
|
+
|
|
461
|
+
// Modal Content
|
|
462
|
+
m('div.p-6.overflow-y-auto.flex-1.space-y-6', [
|
|
463
|
+
dataExchangeState.error ? m('div.p-4.rounded-xl.bg-red-50.dark:bg-red-950/40.border.border-red-200.dark:border-red-800.text-xs.text-red-700.dark:text-red-300', [
|
|
464
|
+
m('strong.font-semibold', 'Error: '), dataExchangeState.error
|
|
465
|
+
]) : null,
|
|
466
|
+
|
|
467
|
+
// STEP 1: Upload & Templates
|
|
468
|
+
dataExchangeState.step === 1 ? [
|
|
469
|
+
// Template Download Card
|
|
470
|
+
m('div.p-4.rounded-xl.bg-indigo-50/70.dark:bg-indigo-950/40.border.border-indigo-100.dark:border-indigo-900/60.flex.flex-col.sm:flex-row.sm:items-center.justify-between.gap-3', [
|
|
471
|
+
m('div', [
|
|
472
|
+
m('h4.text-xs.font-bold.text-indigo-900.dark:text-indigo-200', 'Need a Starter Spreadsheet Template?'),
|
|
473
|
+
m('p.text-xs.text-indigo-700/80.dark:text-indigo-300/80.mt-0.5', 'Download a pre-formatted template with model headers and sample data types:')
|
|
474
|
+
]),
|
|
475
|
+
m('div.flex.items-center.gap-2', [
|
|
476
|
+
m('button.px-3.py-1.5.text-xs.font-semibold.text-indigo-700.bg-white.dark:bg-slate-800.dark:text-indigo-300.rounded-lg.border.border-indigo-200.dark:border-indigo-800.hover:bg-indigo-50.shadow-sm', {
|
|
477
|
+
onclick: function() { downloadExchangeTemplate('csv'); }
|
|
478
|
+
}, '📄 Sample CSV'),
|
|
479
|
+
m('button.px-3.py-1.5.text-xs.font-semibold.text-emerald-700.bg-white.dark:bg-slate-800.dark:text-emerald-300.rounded-lg.border.border-emerald-200.dark:border-emerald-800.hover:bg-emerald-50.shadow-sm', {
|
|
480
|
+
onclick: function() { downloadExchangeTemplate('xlsx'); }
|
|
481
|
+
}, '📗 Sample Excel')
|
|
482
|
+
])
|
|
483
|
+
]),
|
|
484
|
+
|
|
485
|
+
// Dropzone
|
|
486
|
+
m('div.border-2.border-dashed.border-gray-300.dark:border-slate-700.rounded-2xl.p-8.text-center.hover:border-indigo-500.transition-colors.cursor-pointer.relative', {
|
|
487
|
+
ondragover: function(e) { e.preventDefault(); },
|
|
488
|
+
ondrop: function(e) {
|
|
489
|
+
e.preventDefault();
|
|
490
|
+
const file = e.dataTransfer && e.dataTransfer.files[0];
|
|
491
|
+
if (file) {
|
|
492
|
+
dataExchangeState.file = file;
|
|
493
|
+
dataExchangeState.fileName = file.name;
|
|
494
|
+
dataExchangeState.fileSize = (file.size / 1024).toFixed(1) + ' KB';
|
|
495
|
+
m.redraw();
|
|
496
|
+
}
|
|
497
|
+
},
|
|
498
|
+
onclick: function() {
|
|
499
|
+
const el = document.getElementById('data-exchange-wizard-file');
|
|
500
|
+
if (el) el.click();
|
|
501
|
+
}
|
|
502
|
+
}, [
|
|
503
|
+
m('input#data-exchange-wizard-file[type=file]', {
|
|
504
|
+
style: 'display:none',
|
|
505
|
+
accept: '.csv,.xlsx,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,text/csv',
|
|
506
|
+
onchange: function(e) {
|
|
507
|
+
const file = e.target.files && e.target.files[0];
|
|
508
|
+
if (file) {
|
|
509
|
+
dataExchangeState.file = file;
|
|
510
|
+
dataExchangeState.fileName = file.name;
|
|
511
|
+
dataExchangeState.fileSize = (file.size / 1024).toFixed(1) + ' KB';
|
|
512
|
+
m.redraw();
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}),
|
|
516
|
+
m('div.w-12.h-12.rounded-full.bg-indigo-50.dark:bg-indigo-950/60.text-indigo-600.dark:text-indigo-400.flex.items-center.justify-center.mx-auto.mb-3', {
|
|
517
|
+
style: 'font-size: 24px;'
|
|
518
|
+
}, '📁'),
|
|
519
|
+
dataExchangeState.file ? [
|
|
520
|
+
m('p.text-sm.font-bold.text-gray-900.dark:text-slate-100', dataExchangeState.fileName),
|
|
521
|
+
m('p.text-xs.text-emerald-600.dark:text-emerald-400.font-medium.mt-1', '✓ Ready to analyze (' + dataExchangeState.fileSize + ')')
|
|
522
|
+
] : [
|
|
523
|
+
m('p.text-sm.font-semibold.text-gray-800.dark:text-slate-200', 'Drag and drop your spreadsheet here, or click to browse'),
|
|
524
|
+
m('p.text-xs.text-gray-500.dark:text-slate-400.mt-1', 'Supports CSV (.csv) and Microsoft Excel (.xlsx)')
|
|
525
|
+
]
|
|
526
|
+
]),
|
|
527
|
+
|
|
528
|
+
// Settings Grid
|
|
529
|
+
m('div.grid.grid-cols-1.sm:grid-cols-2.gap-4.bg-gray-50.dark:bg-slate-800/40.p-4.rounded-xl.border.border-gray-200.dark:border-slate-800', [
|
|
530
|
+
m('div', [
|
|
531
|
+
m('label.text-xs.font-bold.text-gray-700.dark:text-slate-300.block.mb-1.5', 'Import Strategy'),
|
|
532
|
+
m('div.space-y-2', [
|
|
533
|
+
m('label.flex.items-center.gap-2.text-xs.text-gray-700.dark:text-slate-300.cursor-pointer', [
|
|
534
|
+
m('input[type=radio][name=import_mode]', {
|
|
535
|
+
value: 'insert',
|
|
536
|
+
checked: dataExchangeState.mode === 'insert',
|
|
537
|
+
onchange: function() { dataExchangeState.mode = 'insert'; }
|
|
538
|
+
}),
|
|
539
|
+
m('span', [
|
|
540
|
+
m('strong', 'Insert Only'),
|
|
541
|
+
m('span.text-gray-500.dark:text-slate-400.block', 'Create new records (auto-generates primary key IDs)')
|
|
542
|
+
])
|
|
543
|
+
]),
|
|
544
|
+
m('label.flex.items-center.gap-2.text-xs.text-gray-700.dark:text-slate-300.cursor-pointer', [
|
|
545
|
+
m('input[type=radio][name=import_mode]', {
|
|
546
|
+
value: 'upsert',
|
|
547
|
+
checked: dataExchangeState.mode === 'upsert',
|
|
548
|
+
onchange: function() { dataExchangeState.mode = 'upsert'; }
|
|
549
|
+
}),
|
|
550
|
+
m('span', [
|
|
551
|
+
m('strong', 'Upsert'),
|
|
552
|
+
m('span.text-gray-500.dark:text-slate-400.block', 'Update existing rows by unique key, insert if new')
|
|
553
|
+
])
|
|
554
|
+
])
|
|
555
|
+
])
|
|
556
|
+
]),
|
|
557
|
+
|
|
558
|
+
m('div.space-y-3', [
|
|
559
|
+
dataExchangeState.mode === 'upsert' ? m('div', [
|
|
560
|
+
m('label.text-xs.font-bold.text-gray-700.dark:text-slate-300.block.mb-1', 'Match Unique Key'),
|
|
561
|
+
m('select.w-full.px-3.py-2.text-xs.rounded-lg.border.border-gray-300.dark:border-slate-700.bg-white.dark:bg-slate-900.text-gray-800.dark:text-slate-200', {
|
|
562
|
+
value: dataExchangeState.upsertKey,
|
|
563
|
+
onchange: function(e) { dataExchangeState.upsertKey = e.target.value; }
|
|
564
|
+
}, (function() {
|
|
565
|
+
const cols = dataExchangeState.modelMeta?.columns instanceof Map
|
|
566
|
+
? Array.from(dataExchangeState.modelMeta.columns.keys())
|
|
567
|
+
: Object.keys(dataExchangeState.modelMeta?.columns || {});
|
|
568
|
+
return cols.map(function(c) {
|
|
569
|
+
return m('option', { value: c }, c + (c === dataExchangeState.modelMeta?.primaryKey ? ' (Primary Key)' : ''));
|
|
570
|
+
});
|
|
571
|
+
})())
|
|
572
|
+
]) : null,
|
|
573
|
+
|
|
574
|
+
m('div.pt-1', [
|
|
575
|
+
m('label.flex.items-center.gap-2.text-xs.text-gray-700.dark:text-slate-300.cursor-pointer', [
|
|
576
|
+
m('input[type=checkbox]', {
|
|
577
|
+
checked: dataExchangeState.atomic,
|
|
578
|
+
onchange: function(e) { dataExchangeState.atomic = e.target.checked; }
|
|
579
|
+
}),
|
|
580
|
+
m('span', [
|
|
581
|
+
m('strong', 'Atomic Transaction'),
|
|
582
|
+
m('span.text-gray-500.dark:text-slate-400.block', 'Roll back all changes if any single row fails')
|
|
583
|
+
])
|
|
584
|
+
])
|
|
585
|
+
])
|
|
586
|
+
])
|
|
587
|
+
])
|
|
588
|
+
] : null,
|
|
589
|
+
|
|
590
|
+
// STEP 2: Column Mapping & Preview
|
|
591
|
+
dataExchangeState.step === 2 && dataExchangeState.previewData ? [
|
|
592
|
+
m('div.p-3.rounded-xl.bg-blue-50.dark:bg-blue-950/40.border.border-blue-200.dark:border-blue-900.flex.items-center.justify-between.text-xs.text-blue-800.dark:text-blue-300', [
|
|
593
|
+
m('span', 'Detected ' + dataExchangeState.previewData.headers.length + ' file columns across ' + dataExchangeState.previewData.totalRows + ' rows. Verify column mapping:'),
|
|
594
|
+
m('span.font-semibold', dataExchangeState.fileName)
|
|
595
|
+
]),
|
|
596
|
+
|
|
597
|
+
// Mapping Table
|
|
598
|
+
m('div.border.border-gray-200.dark:border-slate-800.rounded-xl.overflow-hidden', [
|
|
599
|
+
m('table.w-full.text-left.text-xs', [
|
|
600
|
+
m('thead.bg-gray-100.dark:bg-slate-800.text-gray-600.dark:text-slate-300', [
|
|
601
|
+
m('tr', [
|
|
602
|
+
m('th.px-4.py-2.5', 'Spreadsheet Header'),
|
|
603
|
+
m('th.px-4.py-2.5', 'Sample Value (Row 1)'),
|
|
604
|
+
m('th.px-4.py-2.5.text-center', '➔'),
|
|
605
|
+
m('th.px-4.py-2.5', 'Database Model Column'),
|
|
606
|
+
m('th.px-4.py-2.5.text-center', 'Status'),
|
|
607
|
+
])
|
|
608
|
+
]),
|
|
609
|
+
m('tbody.divide-y.divide-gray-100.dark:divide-slate-800', dataExchangeState.previewData.headers.map(function(fh, idx) {
|
|
610
|
+
const sampleVal = dataExchangeState.previewData.sampleRows[0] ? dataExchangeState.previewData.sampleRows[0][idx] : '—';
|
|
611
|
+
const currentMapped = dataExchangeState.columnMapping[fh] || '';
|
|
612
|
+
|
|
613
|
+
return m('tr.hover:bg-gray-50/50.dark:hover:bg-slate-800/30', [
|
|
614
|
+
m('td.px-4.py-2.5.font-bold.text-gray-900.dark:text-slate-100', fh),
|
|
615
|
+
m('td.px-4.py-2.5.text-gray-500.dark:text-slate-400.font-mono.truncate.max-w-[150px]', sampleVal || '—'),
|
|
616
|
+
m('td.px-4.py-2.5.text-center.text-gray-400', '➔'),
|
|
617
|
+
m('td.px-4.py-2.5', [
|
|
618
|
+
m('select.w-full.px-2.5.py-1.5.text-xs.rounded-lg.border.border-gray-300.dark:border-slate-700.bg-white.dark:bg-slate-900.text-gray-800.dark:text-slate-200', {
|
|
619
|
+
value: currentMapped,
|
|
620
|
+
onchange: function(e) {
|
|
621
|
+
dataExchangeState.columnMapping[fh] = e.target.value || null;
|
|
622
|
+
}
|
|
623
|
+
}, [
|
|
624
|
+
m('option', { value: '' }, '— Do Not Import / Skip —'),
|
|
625
|
+
m('optgroup', { label: 'Model Columns' }, dataExchangeState.previewData.modelColumns.map(function(mc) {
|
|
626
|
+
return m('option', { value: mc.name }, mc.name + ' (' + mc.type + (mc.required ? ', required *' : '') + ')');
|
|
627
|
+
}))
|
|
628
|
+
])
|
|
629
|
+
]),
|
|
630
|
+
m('td.px-4.py-2.5.text-center', [
|
|
631
|
+
currentMapped
|
|
632
|
+
? m('span.px-2.py-0.5.text-2xs.font-semibold.rounded-full.bg-emerald-100.text-emerald-800.dark:bg-emerald-950/60.dark:text-emerald-300', '✓ Mapped')
|
|
633
|
+
: m('span.px-2.py-0.5.text-2xs.font-semibold.rounded-full.bg-gray-100.text-gray-600.dark:bg-slate-800.dark:text-slate-400', '— Skipped')
|
|
634
|
+
])
|
|
635
|
+
]);
|
|
636
|
+
}))
|
|
637
|
+
])
|
|
638
|
+
]),
|
|
639
|
+
|
|
640
|
+
// Sample Rows Preview Table
|
|
641
|
+
m('div.space-y-2', [
|
|
642
|
+
m('h5.text-xs.font-bold.text-gray-700.dark:text-slate-300', 'Live Data Preview (First 5 Rows Mapped):'),
|
|
643
|
+
m('div.border.border-gray-200.dark:border-slate-800.rounded-xl.overflow-x-auto.max-h-48', [
|
|
644
|
+
m('table.w-full.text-left.text-xs', [
|
|
645
|
+
m('thead.bg-gray-100.dark:bg-slate-800.text-gray-600.dark:text-slate-300', [
|
|
646
|
+
m('tr', dataExchangeState.previewData.headers.map(function(fh) {
|
|
647
|
+
const target = dataExchangeState.columnMapping[fh];
|
|
648
|
+
return m('th.px-3.py-2.whitespace-nowrap', [
|
|
649
|
+
m('span.text-gray-900.dark:text-slate-100.font-bold', target || '(skipped)'),
|
|
650
|
+
m('span.text-gray-400.text-2xs.block', 'from ' + fh)
|
|
651
|
+
]);
|
|
652
|
+
}))
|
|
653
|
+
]),
|
|
654
|
+
m('tbody.divide-y.divide-gray-100.dark:divide-slate-800', dataExchangeState.previewData.sampleRows.slice(0, 5).map(function(row) {
|
|
655
|
+
return m('tr', row.map(function(cell) {
|
|
656
|
+
return m('td.px-3.py-1.5.text-gray-600.dark:text-slate-300.whitespace-nowrap.truncate.max-w-[200px]', cell || '—');
|
|
657
|
+
}));
|
|
658
|
+
}))
|
|
659
|
+
])
|
|
660
|
+
])
|
|
661
|
+
])
|
|
662
|
+
] : null,
|
|
663
|
+
|
|
664
|
+
// STEP 3: Execution Results
|
|
665
|
+
dataExchangeState.step === 3 && dataExchangeState.results ? [
|
|
666
|
+
m('div.text-center.py-4', [
|
|
667
|
+
m('div.w-16.h-16.rounded-full.flex.items-center.justify-center.mx-auto.mb-3', {
|
|
668
|
+
class: dataExchangeState.results.failed === 0
|
|
669
|
+
? 'bg-emerald-100 dark:bg-emerald-950/60 text-emerald-600 dark:text-emerald-400'
|
|
670
|
+
: 'bg-amber-100 dark:bg-amber-950/60 text-amber-600 dark:text-amber-400',
|
|
671
|
+
style: 'font-size: 32px;'
|
|
672
|
+
}, dataExchangeState.results.failed === 0 ? '✓' : '⚠'),
|
|
673
|
+
m('h3.text-lg.font-bold.text-gray-900.dark:text-slate-100',
|
|
674
|
+
dataExchangeState.results.failed === 0 ? 'Import Completed Successfully!' : 'Import Completed with Failures'),
|
|
675
|
+
m('p.text-xs.text-gray-500.dark:text-slate-400.mt-1',
|
|
676
|
+
'Finished processing ' + (dataExchangeState.results.totalRows || (dataExchangeState.results.created + dataExchangeState.results.updated + dataExchangeState.results.failed)) + ' rows into ' + modelLabel + '.')
|
|
677
|
+
]),
|
|
678
|
+
|
|
679
|
+
// Results KPI Grid
|
|
680
|
+
m('div.grid.grid-cols-4.gap-3', [
|
|
681
|
+
m('div.p-4.rounded-xl.bg-gray-50.dark:bg-slate-800/50.border.border-gray-200.dark:border-slate-800.text-center', [
|
|
682
|
+
m('div.text-xs.font-semibold.text-gray-500.dark:text-slate-400', 'Total Rows'),
|
|
683
|
+
m('div.text-xl.font-extrabold.text-gray-900.dark:text-slate-100.mt-1', dataExchangeState.results.totalRows || (dataExchangeState.results.created + dataExchangeState.results.updated + dataExchangeState.results.failed))
|
|
684
|
+
]),
|
|
685
|
+
m('div.p-4.rounded-xl.bg-emerald-50.dark:bg-emerald-950/40.border.border-emerald-200.dark:border-emerald-900/60.text-center', [
|
|
686
|
+
m('div.text-xs.font-semibold.text-emerald-700.dark:text-emerald-300', 'Created'),
|
|
687
|
+
m('div.text-xl.font-extrabold.text-emerald-600.dark:text-emerald-400.mt-1', dataExchangeState.results.created)
|
|
688
|
+
]),
|
|
689
|
+
m('div.p-4.rounded-xl.bg-blue-50.dark:bg-blue-950/40.border.border-blue-200.dark:border-blue-900/60.text-center', [
|
|
690
|
+
m('div.text-xs.font-semibold.text-blue-700.dark:text-blue-300', 'Updated'),
|
|
691
|
+
m('div.text-xl.font-extrabold.text-blue-600.dark:text-blue-400.mt-1', dataExchangeState.results.updated || 0)
|
|
692
|
+
]),
|
|
693
|
+
m('div.p-4.rounded-xl.bg-red-50.dark:bg-red-950/40.border.border-red-200.dark:border-red-900/60.text-center', [
|
|
694
|
+
m('div.text-xs.font-semibold.text-red-700.dark:text-red-300', 'Failed'),
|
|
695
|
+
m('div.text-xl.font-extrabold.text-red-600.dark:text-red-400.mt-1', dataExchangeState.results.failed)
|
|
696
|
+
]),
|
|
697
|
+
]),
|
|
698
|
+
|
|
699
|
+
// Error Log Table if failed > 0
|
|
700
|
+
dataExchangeState.results.errors && dataExchangeState.results.errors.length > 0 ? [
|
|
701
|
+
m('div.space-y-2', [
|
|
702
|
+
m('h5.text-xs.font-bold.text-red-600.dark:text-red-400', 'Row Validation & Execution Errors (' + dataExchangeState.results.errors.length + '):'),
|
|
703
|
+
m('div.border.border-red-200.dark:border-red-900/60.rounded-xl.overflow-hidden.max-h-48.overflow-y-auto', [
|
|
704
|
+
m('table.w-full.text-left.text-xs', [
|
|
705
|
+
m('thead.bg-red-50.dark:bg-red-950/40.text-red-800.dark:text-red-300', [
|
|
706
|
+
m('tr', [
|
|
707
|
+
m('th.px-3.py-2', 'Row'),
|
|
708
|
+
m('th.px-3.py-2', 'Column'),
|
|
709
|
+
m('th.px-3.py-2', 'Value'),
|
|
710
|
+
m('th.px-3.py-2', 'Reason'),
|
|
711
|
+
])
|
|
712
|
+
]),
|
|
713
|
+
m('tbody.divide-y.divide-red-100.dark:divide-red-900/40', dataExchangeState.results.errors.map(function(errItem) {
|
|
714
|
+
return m('tr', [
|
|
715
|
+
m('td.px-3.py-1.5.font-bold.text-red-700.dark:text-red-400', '#' + errItem.row),
|
|
716
|
+
m('td.px-3.py-1.5.text-gray-600.dark:text-slate-300', errItem.column || '—'),
|
|
717
|
+
m('td.px-3.py-1.5.font-mono.text-gray-500.dark:text-slate-400', errItem.value !== undefined && errItem.value !== null ? String(errItem.value) : '—'),
|
|
718
|
+
m('td.px-3.py-1.5.text-red-600.dark:text-red-300', errItem.message),
|
|
719
|
+
]);
|
|
720
|
+
}))
|
|
721
|
+
])
|
|
722
|
+
])
|
|
723
|
+
])
|
|
724
|
+
] : null,
|
|
725
|
+
] : null,
|
|
726
|
+
]),
|
|
727
|
+
|
|
728
|
+
// Modal Footer
|
|
729
|
+
m('div.px-6.py-4.border-t.border-gray-100.dark:border-slate-800.bg-gray-50/50.dark:bg-slate-800/40.flex.items-center.justify-between', [
|
|
730
|
+
dataExchangeState.step === 1 ? [
|
|
731
|
+
m('button.px-4.py-2.text-xs.font-semibold.text-gray-700.dark:text-slate-300.hover:bg-gray-100.dark:hover:bg-slate-800.rounded-lg', {
|
|
732
|
+
onclick: function() { dataExchangeState.importOpen = false; }
|
|
733
|
+
}, 'Cancel'),
|
|
734
|
+
m('button.px-5.py-2.text-xs.font-bold.text-white.bg-indigo-600.hover:bg-indigo-700.rounded-lg.shadow-sm.flex.items-center.gap-2', {
|
|
735
|
+
disabled: !dataExchangeState.file || dataExchangeState.loading,
|
|
736
|
+
onclick: fetchImportPreview
|
|
737
|
+
}, dataExchangeState.loading ? 'Analyzing File...' : 'Next: Preview & Map Columns ➔')
|
|
738
|
+
] : null,
|
|
739
|
+
|
|
740
|
+
dataExchangeState.step === 2 ? [
|
|
741
|
+
m('button.px-4.py-2.text-xs.font-semibold.text-gray-700.dark:text-slate-300.hover:bg-gray-100.dark:hover:bg-slate-800.rounded-lg', {
|
|
742
|
+
onclick: function() { dataExchangeState.step = 1; }
|
|
743
|
+
}, '⬅ Back to Upload'),
|
|
744
|
+
m('button.px-5.py-2.text-xs.font-bold.text-white.bg-indigo-600.hover:bg-indigo-700.rounded-lg.shadow-sm.flex.items-center.gap-2', {
|
|
745
|
+
disabled: dataExchangeState.loading,
|
|
746
|
+
onclick: executeImport
|
|
747
|
+
}, dataExchangeState.loading ? 'Importing Rows...' : 'Start Import ➔')
|
|
748
|
+
] : null,
|
|
749
|
+
|
|
750
|
+
dataExchangeState.step === 3 ? [
|
|
751
|
+
m('div'),
|
|
752
|
+
m('button.px-5.py-2.text-xs.font-bold.text-white.bg-indigo-600.hover:bg-indigo-700.rounded-lg.shadow-sm', {
|
|
753
|
+
onclick: function() {
|
|
754
|
+
dataExchangeState.importOpen = false;
|
|
755
|
+
loadRecords(dataExchangeState.modelName, state.pagination.page, state.filters);
|
|
756
|
+
}
|
|
757
|
+
}, 'Done & Refresh Records')
|
|
758
|
+
] : null,
|
|
759
|
+
])
|
|
760
|
+
])
|
|
761
|
+
]);
|
|
762
|
+
}
|
|
763
|
+
};
|
|
764
|
+
|
|
765
|
+
// Visual Data Exchange Export Modal
|
|
766
|
+
var DataExchangeExportModal = {
|
|
767
|
+
view: function() {
|
|
768
|
+
if (!dataExchangeState.exportOpen) return null;
|
|
769
|
+
const modelLabel = dataExchangeState.modelMeta?.label || dataExchangeState.modelName;
|
|
770
|
+
const cols = Object.keys(dataExchangeState.exportColumns);
|
|
771
|
+
|
|
772
|
+
return m('div.fixed.inset-0.z-50.flex.items-center.justify-center.p-4', [
|
|
773
|
+
m('div.fixed.inset-0.bg-black/60.backdrop-blur-sm', {
|
|
774
|
+
onclick: function() {
|
|
775
|
+
if (!dataExchangeState.exportLoading) dataExchangeState.exportOpen = false;
|
|
776
|
+
}
|
|
777
|
+
}),
|
|
778
|
+
m('div.relative.bg-white.dark:bg-slate-900.rounded-2xl.shadow-2xl.border.border-gray-200.dark:border-slate-800.max-w-lg.w-full.flex.flex-col.overflow-hidden.z-10', [
|
|
779
|
+
// Modal Header
|
|
780
|
+
m('div.px-6.py-4.border-b.border-gray-100.dark:border-slate-800.flex.items-center.justify-between.bg-gray-50/50.dark:bg-slate-800/40', [
|
|
781
|
+
m('div.flex.items-center.gap-3', [
|
|
782
|
+
m('div.w-9.h-9.rounded-xl.bg-emerald-100.dark:bg-emerald-950/70.text-emerald-600.dark:text-emerald-400.flex.items-center.justify-center.font-bold', '📤'),
|
|
783
|
+
m('div', [
|
|
784
|
+
m('h3.text-base.font-bold.text-gray-900.dark:text-slate-100', 'Export Model Records'),
|
|
785
|
+
m('p.text-xs.text-gray-500.dark:text-slate-400', 'Source: ' + modelLabel)
|
|
786
|
+
])
|
|
787
|
+
]),
|
|
788
|
+
m('button.text-gray-400.hover:text-gray-600.dark:hover:text-slate-200.p-1.5.rounded-lg', {
|
|
789
|
+
onclick: function() { dataExchangeState.exportOpen = false; }
|
|
790
|
+
}, '✕')
|
|
791
|
+
]),
|
|
792
|
+
|
|
793
|
+
// Modal Body
|
|
794
|
+
m('div.p-6.space-y-5.overflow-y-auto.max-h-[70vh]', [
|
|
795
|
+
// Format Selection
|
|
796
|
+
m('div', [
|
|
797
|
+
m('label.text-xs.font-bold.text-gray-700.dark:text-slate-300.block.mb-2', 'Export Format'),
|
|
798
|
+
m('div.grid.grid-cols-2.gap-3', [
|
|
799
|
+
m('div.p-3.rounded-xl.border.cursor-pointer.flex.items-center.gap-3.transition-colors', {
|
|
800
|
+
class: dataExchangeState.exportFormat === 'xlsx'
|
|
801
|
+
? 'border-indigo-600 bg-indigo-50/60 dark:bg-indigo-950/40 text-indigo-900 dark:text-indigo-200 font-bold'
|
|
802
|
+
: 'border-gray-200 dark:border-slate-700 hover:border-gray-300 dark:hover:border-slate-600 text-gray-700 dark:text-slate-300',
|
|
803
|
+
onclick: function() { dataExchangeState.exportFormat = 'xlsx'; }
|
|
804
|
+
}, [
|
|
805
|
+
m('span.text-2xl', '📗'),
|
|
806
|
+
m('div', [
|
|
807
|
+
m('div.text-xs', 'Excel Spreadsheet'),
|
|
808
|
+
m('div.text-2xs.text-gray-500.dark:text-slate-400.font-normal', '.xlsx format with styles')
|
|
809
|
+
])
|
|
810
|
+
]),
|
|
811
|
+
|
|
812
|
+
m('div.p-3.rounded-xl.border.cursor-pointer.flex.items-center.gap-3.transition-colors', {
|
|
813
|
+
class: dataExchangeState.exportFormat === 'csv'
|
|
814
|
+
? 'border-indigo-600 bg-indigo-50/60 dark:bg-indigo-950/40 text-indigo-900 dark:text-indigo-200 font-bold'
|
|
815
|
+
: 'border-gray-200 dark:border-slate-700 hover:border-gray-300 dark:hover:border-slate-600 text-gray-700 dark:text-slate-300',
|
|
816
|
+
onclick: function() { dataExchangeState.exportFormat = 'csv'; }
|
|
817
|
+
}, [
|
|
818
|
+
m('span.text-2xl', '📄'),
|
|
819
|
+
m('div', [
|
|
820
|
+
m('div.text-xs', 'CSV File'),
|
|
821
|
+
m('div.text-2xs.text-gray-500.dark:text-slate-400.font-normal', 'RFC 4180 with UTF-8 BOM')
|
|
822
|
+
])
|
|
823
|
+
])
|
|
824
|
+
])
|
|
825
|
+
]),
|
|
826
|
+
|
|
827
|
+
// Scope Selection
|
|
828
|
+
dataExchangeState.selectedRecords.length > 0 ? m('div', [
|
|
829
|
+
m('label.text-xs.font-bold.text-gray-700.dark:text-slate-300.block.mb-1.5', 'Export Scope'),
|
|
830
|
+
m('div.flex.gap-4 text-xs', [
|
|
831
|
+
m('label.flex.items-center.gap-1.5.cursor-pointer', [
|
|
832
|
+
m('input[type=radio][name=export_scope]', {
|
|
833
|
+
checked: dataExchangeState.exportScope === 'selected',
|
|
834
|
+
onchange: function() { dataExchangeState.exportScope = 'selected'; }
|
|
835
|
+
}),
|
|
836
|
+
'Selected Only (' + dataExchangeState.selectedRecords.length + ' rows)'
|
|
837
|
+
]),
|
|
838
|
+
m('label.flex.items-center.gap-1.5.cursor-pointer', [
|
|
839
|
+
m('input[type=radio][name=export_scope]', {
|
|
840
|
+
checked: dataExchangeState.exportScope === 'all',
|
|
841
|
+
onchange: function() { dataExchangeState.exportScope = 'all'; }
|
|
842
|
+
}),
|
|
843
|
+
'All Matching Records'
|
|
844
|
+
])
|
|
845
|
+
])
|
|
846
|
+
]) : null,
|
|
847
|
+
|
|
848
|
+
// Column Selection
|
|
849
|
+
m('div', [
|
|
850
|
+
m('div.flex.items-center.justify-between.mb-2', [
|
|
851
|
+
m('label.text-xs.font-bold.text-gray-700.dark:text-slate-300', 'Select Columns to Include'),
|
|
852
|
+
m('div.flex.gap-2', [
|
|
853
|
+
m('button.text-2xs.text-indigo-600.dark:text-indigo-400.hover:underline.font-semibold', {
|
|
854
|
+
onclick: function() {
|
|
855
|
+
for (const c of cols) dataExchangeState.exportColumns[c] = true;
|
|
856
|
+
}
|
|
857
|
+
}, 'Select All'),
|
|
858
|
+
m('span.text-gray-400', '·'),
|
|
859
|
+
m('button.text-2xs.text-gray-500.dark:text-slate-400.hover:underline.font-semibold', {
|
|
860
|
+
onclick: function() {
|
|
861
|
+
for (const c of cols) dataExchangeState.exportColumns[c] = false;
|
|
862
|
+
}
|
|
863
|
+
}, 'Deselect All')
|
|
864
|
+
])
|
|
865
|
+
]),
|
|
866
|
+
m('div.grid.grid-cols-2.gap-2.border.border-gray-200.dark:border-slate-800.rounded-xl.p-3.max-h-48.overflow-y-auto', cols.map(function(c) {
|
|
867
|
+
return m('label.flex.items-center.gap-2.text-xs.text-gray-700.dark:text-slate-300.cursor-pointer', [
|
|
868
|
+
m('input[type=checkbox]', {
|
|
869
|
+
checked: !!dataExchangeState.exportColumns[c],
|
|
870
|
+
onchange: function(e) {
|
|
871
|
+
dataExchangeState.exportColumns[c] = e.target.checked;
|
|
872
|
+
}
|
|
873
|
+
}),
|
|
874
|
+
c
|
|
875
|
+
]);
|
|
876
|
+
}))
|
|
877
|
+
])
|
|
878
|
+
]),
|
|
879
|
+
|
|
880
|
+
// Modal Footer
|
|
881
|
+
m('div.px-6.py-4.border-t.border-gray-100.dark:border-slate-800.bg-gray-50/50.dark:bg-slate-800/40.flex.items-center.justify-end.gap-3', [
|
|
882
|
+
m('button.px-4.py-2.text-xs.font-semibold.text-gray-700.dark:text-slate-300.hover:bg-gray-100.dark:hover:bg-slate-800.rounded-lg', {
|
|
883
|
+
onclick: function() { dataExchangeState.exportOpen = false; }
|
|
884
|
+
}, 'Cancel'),
|
|
885
|
+
m('button.px-5.py-2.text-xs.font-bold.text-white.bg-emerald-600.hover:bg-emerald-700.rounded-lg.shadow-sm.flex.items-center.gap-2', {
|
|
886
|
+
disabled: dataExchangeState.exportLoading,
|
|
887
|
+
onclick: executeExport
|
|
888
|
+
}, dataExchangeState.exportLoading ? 'Generating Export...' : 'Download Export ➔')
|
|
889
|
+
])
|
|
890
|
+
])
|
|
891
|
+
]);
|
|
892
|
+
}
|
|
893
|
+
};
|
|
894
|
+
|
|
186
895
|
// Record List Component - displays records with dynamic columns
|
|
187
896
|
const RecordList = {
|
|
188
897
|
oninit(vnode) {
|
|
@@ -282,65 +991,18 @@ const RecordList = {
|
|
|
282
991
|
]),
|
|
283
992
|
m('.flex.flex-wrap.items-center.gap-2', [
|
|
284
993
|
m('button.inline-flex.items-center.gap-2.px-4.py-2.text-sm.font-medium.text-indigo-700.bg-white.dark:bg-slate-800.border.border-indigo-200.rounded-lg.hover:bg-indigo-50.transition-colors', {
|
|
285
|
-
onclick:
|
|
286
|
-
|
|
287
|
-
const payload = { selectAll: true, filters: state.filters };
|
|
288
|
-
if (state.trashedView) payload.trashed = 'only';
|
|
289
|
-
await downloadDataExchangeXlsx(modelName, payload);
|
|
290
|
-
} catch (err) {
|
|
291
|
-
notifyError('Error: ' + err.message);
|
|
292
|
-
}
|
|
994
|
+
onclick: () => {
|
|
995
|
+
openDataExchangeExport(modelName, modelMeta, state.selectedRecords);
|
|
293
996
|
},
|
|
294
997
|
}, [
|
|
295
998
|
m('svg.w-4.h-4', { fill: 'none', viewBox: '0 0 24 24', stroke: 'currentColor' },
|
|
296
999
|
m('path', { 'stroke-linecap': 'round', 'stroke-linejoin': 'round', 'stroke-width': '2', d: 'M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4' })
|
|
297
1000
|
),
|
|
298
|
-
'Export
|
|
1001
|
+
'Export',
|
|
299
1002
|
]),
|
|
300
|
-
!state.trashedView ? m('input[type=file]', {
|
|
301
|
-
id: 'data-exchange-import-' + modelName,
|
|
302
|
-
style: 'display:none',
|
|
303
|
-
accept: '.csv,.xlsx,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,text/csv',
|
|
304
|
-
onchange: async (e) => {
|
|
305
|
-
const file = e.target.files && e.target.files[0];
|
|
306
|
-
e.target.value = '';
|
|
307
|
-
if (!file) return;
|
|
308
|
-
const adminPath = window.__ADMIN_PATH__ || '/_admin';
|
|
309
|
-
const mode = (await confirmAction({
|
|
310
|
-
title: 'Import Mode',
|
|
311
|
-
message: 'Confirm to upsert records matching existing IDs, or cancel to insert as new records only.',
|
|
312
|
-
confirmText: 'Upsert by ID',
|
|
313
|
-
cancelText: 'Insert only'
|
|
314
|
-
})) ? 'upsert' : 'insert';
|
|
315
|
-
const upsertKey = 'id';
|
|
316
|
-
const fd = new FormData();
|
|
317
|
-
fd.append('file', file);
|
|
318
|
-
try {
|
|
319
|
-
const fetchFn = (typeof fetchWithTimeout === 'function' ? fetchWithTimeout : fetch);
|
|
320
|
-
const res = await fetchFn(
|
|
321
|
-
adminPath + '/api/data-exchange/import/' + modelName +
|
|
322
|
-
'?mode=' + encodeURIComponent(mode) + '&upsertKey=' + encodeURIComponent(upsertKey),
|
|
323
|
-
{ method: 'POST', body: fd, credentials: 'include', timeout: 60000 }
|
|
324
|
-
);
|
|
325
|
-
const body = await res.json().catch(function () { return ({}); });
|
|
326
|
-
if (!res.ok) {
|
|
327
|
-
throw new Error(body.error || 'Import failed');
|
|
328
|
-
}
|
|
329
|
-
var msg = 'Import finished: created ' + body.created + ', updated ' + (body.updated || 0) + ', failed ' + (body.failed || 0);
|
|
330
|
-
if (body.errors && body.errors.length) {
|
|
331
|
-
msg += 'First errors: ' + body.errors.slice(0, 3).map(function (x) { return 'row ' + x.row + ': ' + x.message; }).join('; ');
|
|
332
|
-
}
|
|
333
|
-
notifySuccess(msg);
|
|
334
|
-
loadRecords(modelName, state.pagination.page, state.filters);
|
|
335
|
-
} catch (err) {
|
|
336
|
-
notifyError('Error: ' + err.message);
|
|
337
|
-
}
|
|
338
|
-
},
|
|
339
|
-
}) : null,
|
|
340
1003
|
!state.trashedView ? m('button.inline-flex.items-center.gap-2.px-4.py-2.text-sm.font-medium.text-indigo-700.bg-white.dark:bg-slate-800.border.border-indigo-200.rounded-lg.hover:bg-indigo-50.transition-colors', {
|
|
341
1004
|
onclick: function () {
|
|
342
|
-
|
|
343
|
-
if (el) el.click();
|
|
1005
|
+
openDataExchangeImport(modelName, modelMeta);
|
|
344
1006
|
},
|
|
345
1007
|
}, [
|
|
346
1008
|
m('svg.w-4.h-4', { fill: 'none', viewBox: '0 0 24 24', stroke: 'currentColor' },
|
|
@@ -595,30 +1257,13 @@ const RecordList = {
|
|
|
595
1257
|
m('button.inline-flex.items-center.gap-1.px-3.py-1.5.text-sm.font-medium.text-violet-600.bg-white dark:bg-slate-800.border.border-violet-200.rounded.hover:bg-violet-50.transition-colors', {
|
|
596
1258
|
disabled: state.bulkActionInProgress,
|
|
597
1259
|
onclick: async () => {
|
|
598
|
-
state.
|
|
599
|
-
m.redraw();
|
|
600
|
-
try {
|
|
601
|
-
const payload = state.selectAllMode
|
|
602
|
-
? { selectAll: true, filters: state.filters }
|
|
603
|
-
: { ids: Array.from(state.selectedRecords) };
|
|
604
|
-
if (!state.selectAllMode && (!payload.ids || payload.ids.length === 0)) {
|
|
605
|
-
notifyWarning('Please select at least one record to export');
|
|
606
|
-
return;
|
|
607
|
-
}
|
|
608
|
-
if (state.trashedView) payload.trashed = 'only';
|
|
609
|
-
await downloadDataExchangeXlsx(modelName, payload);
|
|
610
|
-
} catch (err) {
|
|
611
|
-
notifyError('Error: ' + err.message);
|
|
612
|
-
} finally {
|
|
613
|
-
state.bulkActionInProgress = false;
|
|
614
|
-
m.redraw();
|
|
615
|
-
}
|
|
1260
|
+
openDataExchangeExport(modelName, modelMeta, state.selectedRecords);
|
|
616
1261
|
},
|
|
617
1262
|
}, [
|
|
618
1263
|
m('svg.w-4.h-4', { fill: 'none', viewBox: '0 0 24 24', stroke: 'currentColor' },
|
|
619
1264
|
m('path', { 'stroke-linecap': 'round', 'stroke-linejoin': 'round', 'stroke-width': '2', d: 'M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4' })
|
|
620
1265
|
),
|
|
621
|
-
'Export
|
|
1266
|
+
'Export Selected',
|
|
622
1267
|
]),
|
|
623
1268
|
!state.trashedView ? m(BulkFieldUpdateDropdown, {
|
|
624
1269
|
modelName: modelName,
|
|
@@ -810,6 +1455,8 @@ const RecordList = {
|
|
|
810
1455
|
onPageChange: (newPage) => loadRecords(modelName, newPage),
|
|
811
1456
|
}),
|
|
812
1457
|
]),
|
|
1458
|
+
dataExchangeState.importOpen ? m(DataExchangeImportModal) : null,
|
|
1459
|
+
dataExchangeState.exportOpen ? m(DataExchangeExportModal) : null,
|
|
813
1460
|
]);
|
|
814
1461
|
},
|
|
815
1462
|
};
|