@seip/bluebird-js 0.2.0 → 0.2.1
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.md +2 -1
- package/README.md +55 -0
- package/app/resources/html/layouts/main.html +2 -1
- package/app/resources/html/table.html +15 -0
- package/app/resources/layouts/AppLayout.jsx +4 -1
- package/app/resources/pages/Dashboard.jsx +27 -32
- package/app/resources/pages/TableDemo.jsx +85 -0
- package/app/resources/routes.jsx +5 -0
- package/app/resources/spa.js +6 -0
- package/app/routes/api.js +44 -0
- package/app/routes/web.js +31 -1
- package/core/react/client/api.js +2 -0
- package/core/react/client/datatable.js +595 -0
- package/core/react/client/i18n.js +102 -0
- package/core/react/client/index.d.ts +65 -0
- package/core/react/client/index.js +1 -0
- package/package.json +1 -1
- package/public/js/bluebird.js +732 -6
- package/templates/_base/app/resources/html/layouts/main.html +1 -1
package/public/js/bluebird.js
CHANGED
|
@@ -44,7 +44,24 @@
|
|
|
44
44
|
"value.invalid": "Invalid value",
|
|
45
45
|
"copy.done": "Copied to clipboard",
|
|
46
46
|
"auth.deniedTitle": "Access denied",
|
|
47
|
-
"auth.deniedText": "You do not have permission to view this section."
|
|
47
|
+
"auth.deniedText": "You do not have permission to view this section.",
|
|
48
|
+
"table.search": "Search...",
|
|
49
|
+
"table.empty": "No results",
|
|
50
|
+
"table.rowsPerPage": "Rows per page",
|
|
51
|
+
"table.range": "{from}-{to} of {total}",
|
|
52
|
+
"table.prev": "Previous page",
|
|
53
|
+
"table.next": "Next page",
|
|
54
|
+
"table.page": "Page {n} of {total}",
|
|
55
|
+
"table.actions": "Actions",
|
|
56
|
+
"table.view": "View",
|
|
57
|
+
"table.edit": "Edit",
|
|
58
|
+
"table.delete": "Delete",
|
|
59
|
+
"table.confirmTitle": "Delete this record?",
|
|
60
|
+
"table.confirmText": "This action cannot be undone.",
|
|
61
|
+
"table.cancel": "Cancel",
|
|
62
|
+
"table.more": "Show details",
|
|
63
|
+
"table.less": "Hide details",
|
|
64
|
+
"table.sortBy": "Sort by {column}"
|
|
48
65
|
},
|
|
49
66
|
"es": {
|
|
50
67
|
"toast.info": "Info",
|
|
@@ -81,7 +98,24 @@
|
|
|
81
98
|
"value.invalid": "Valor inválido",
|
|
82
99
|
"copy.done": "Copiado al portapapeles",
|
|
83
100
|
"auth.deniedTitle": "Acceso denegado",
|
|
84
|
-
"auth.deniedText": "No tienes permiso para ver esta sección."
|
|
101
|
+
"auth.deniedText": "No tienes permiso para ver esta sección.",
|
|
102
|
+
"table.search": "Buscar...",
|
|
103
|
+
"table.empty": "Sin resultados",
|
|
104
|
+
"table.rowsPerPage": "Filas por página",
|
|
105
|
+
"table.range": "{from}-{to} de {total}",
|
|
106
|
+
"table.prev": "Página anterior",
|
|
107
|
+
"table.next": "Página siguiente",
|
|
108
|
+
"table.page": "Página {n} de {total}",
|
|
109
|
+
"table.actions": "Acciones",
|
|
110
|
+
"table.view": "Ver",
|
|
111
|
+
"table.edit": "Editar",
|
|
112
|
+
"table.delete": "Eliminar",
|
|
113
|
+
"table.confirmTitle": "¿Eliminar este registro?",
|
|
114
|
+
"table.confirmText": "Esta acción no se puede deshacer.",
|
|
115
|
+
"table.cancel": "Cancelar",
|
|
116
|
+
"table.more": "Ver detalles",
|
|
117
|
+
"table.less": "Ocultar detalles",
|
|
118
|
+
"table.sortBy": "Ordenar por {column}"
|
|
85
119
|
},
|
|
86
120
|
"pt": {
|
|
87
121
|
"toast.info": "Info",
|
|
@@ -118,7 +152,24 @@
|
|
|
118
152
|
"value.invalid": "Valor inválido",
|
|
119
153
|
"copy.done": "Copiado para a área de transferência",
|
|
120
154
|
"auth.deniedTitle": "Acesso negado",
|
|
121
|
-
"auth.deniedText": "Você não tem permissão para ver esta seção."
|
|
155
|
+
"auth.deniedText": "Você não tem permissão para ver esta seção.",
|
|
156
|
+
"table.search": "Pesquisar...",
|
|
157
|
+
"table.empty": "Nenhum resultado",
|
|
158
|
+
"table.rowsPerPage": "Linhas por página",
|
|
159
|
+
"table.range": "{from}-{to} de {total}",
|
|
160
|
+
"table.prev": "Página anterior",
|
|
161
|
+
"table.next": "Próxima página",
|
|
162
|
+
"table.page": "Página {n} de {total}",
|
|
163
|
+
"table.actions": "Ações",
|
|
164
|
+
"table.view": "Ver",
|
|
165
|
+
"table.edit": "Editar",
|
|
166
|
+
"table.delete": "Excluir",
|
|
167
|
+
"table.confirmTitle": "Excluir este registro?",
|
|
168
|
+
"table.confirmText": "Esta ação não pode ser desfeita.",
|
|
169
|
+
"table.cancel": "Cancelar",
|
|
170
|
+
"table.more": "Ver detalhes",
|
|
171
|
+
"table.less": "Ocultar detalhes",
|
|
172
|
+
"table.sortBy": "Ordenar por {column}"
|
|
122
173
|
},
|
|
123
174
|
"fr": {
|
|
124
175
|
"toast.info": "Info",
|
|
@@ -155,7 +206,24 @@
|
|
|
155
206
|
"value.invalid": "Valeur invalide",
|
|
156
207
|
"copy.done": "Copié dans le presse-papiers",
|
|
157
208
|
"auth.deniedTitle": "Accès refusé",
|
|
158
|
-
"auth.deniedText": "Vous n'avez pas l'autorisation de voir cette section."
|
|
209
|
+
"auth.deniedText": "Vous n'avez pas l'autorisation de voir cette section.",
|
|
210
|
+
"table.search": "Rechercher...",
|
|
211
|
+
"table.empty": "Aucun résultat",
|
|
212
|
+
"table.rowsPerPage": "Lignes par page",
|
|
213
|
+
"table.range": "{from}-{to} sur {total}",
|
|
214
|
+
"table.prev": "Page précédente",
|
|
215
|
+
"table.next": "Page suivante",
|
|
216
|
+
"table.page": "Page {n} sur {total}",
|
|
217
|
+
"table.actions": "Actions",
|
|
218
|
+
"table.view": "Voir",
|
|
219
|
+
"table.edit": "Modifier",
|
|
220
|
+
"table.delete": "Supprimer",
|
|
221
|
+
"table.confirmTitle": "Supprimer cet enregistrement ?",
|
|
222
|
+
"table.confirmText": "Cette action est irréversible.",
|
|
223
|
+
"table.cancel": "Annuler",
|
|
224
|
+
"table.more": "Voir les détails",
|
|
225
|
+
"table.less": "Masquer les détails",
|
|
226
|
+
"table.sortBy": "Trier par {column}"
|
|
159
227
|
},
|
|
160
228
|
"de": {
|
|
161
229
|
"toast.info": "Info",
|
|
@@ -192,7 +260,24 @@
|
|
|
192
260
|
"value.invalid": "Ungültiger Wert",
|
|
193
261
|
"copy.done": "In die Zwischenablage kopiert",
|
|
194
262
|
"auth.deniedTitle": "Zugriff verweigert",
|
|
195
|
-
"auth.deniedText": "Du hast keine Berechtigung, diesen Bereich zu sehen."
|
|
263
|
+
"auth.deniedText": "Du hast keine Berechtigung, diesen Bereich zu sehen.",
|
|
264
|
+
"table.search": "Suchen...",
|
|
265
|
+
"table.empty": "Keine Ergebnisse",
|
|
266
|
+
"table.rowsPerPage": "Zeilen pro Seite",
|
|
267
|
+
"table.range": "{from}-{to} von {total}",
|
|
268
|
+
"table.prev": "Vorherige Seite",
|
|
269
|
+
"table.next": "Nächste Seite",
|
|
270
|
+
"table.page": "Seite {n} von {total}",
|
|
271
|
+
"table.actions": "Aktionen",
|
|
272
|
+
"table.view": "Ansehen",
|
|
273
|
+
"table.edit": "Bearbeiten",
|
|
274
|
+
"table.delete": "Löschen",
|
|
275
|
+
"table.confirmTitle": "Diesen Eintrag löschen?",
|
|
276
|
+
"table.confirmText": "Diese Aktion kann nicht rückgängig gemacht werden.",
|
|
277
|
+
"table.cancel": "Abbrechen",
|
|
278
|
+
"table.more": "Details anzeigen",
|
|
279
|
+
"table.less": "Details ausblenden",
|
|
280
|
+
"table.sortBy": "Nach {column} sortieren"
|
|
196
281
|
},
|
|
197
282
|
"it": {
|
|
198
283
|
"toast.info": "Info",
|
|
@@ -229,7 +314,24 @@
|
|
|
229
314
|
"value.invalid": "Valore non valido",
|
|
230
315
|
"copy.done": "Copiato negli appunti",
|
|
231
316
|
"auth.deniedTitle": "Accesso negato",
|
|
232
|
-
"auth.deniedText": "Non hai il permesso di vedere questa sezione."
|
|
317
|
+
"auth.deniedText": "Non hai il permesso di vedere questa sezione.",
|
|
318
|
+
"table.search": "Cerca...",
|
|
319
|
+
"table.empty": "Nessun risultato",
|
|
320
|
+
"table.rowsPerPage": "Righe per pagina",
|
|
321
|
+
"table.range": "{from}-{to} di {total}",
|
|
322
|
+
"table.prev": "Pagina precedente",
|
|
323
|
+
"table.next": "Pagina successiva",
|
|
324
|
+
"table.page": "Pagina {n} di {total}",
|
|
325
|
+
"table.actions": "Azioni",
|
|
326
|
+
"table.view": "Vedi",
|
|
327
|
+
"table.edit": "Modifica",
|
|
328
|
+
"table.delete": "Elimina",
|
|
329
|
+
"table.confirmTitle": "Eliminare questo record?",
|
|
330
|
+
"table.confirmText": "Questa azione non può essere annullata.",
|
|
331
|
+
"table.cancel": "Annulla",
|
|
332
|
+
"table.more": "Mostra dettagli",
|
|
333
|
+
"table.less": "Nascondi dettagli",
|
|
334
|
+
"table.sortBy": "Ordina per {column}"
|
|
233
335
|
}
|
|
234
336
|
};
|
|
235
337
|
const customMessages = {};
|
|
@@ -954,6 +1056,627 @@
|
|
|
954
1056
|
}
|
|
955
1057
|
}
|
|
956
1058
|
|
|
1059
|
+
const DT_ICONS = {
|
|
1060
|
+
search: '<path d="m21 21-4.34-4.34"/><path d="M11 19a8 8 0 1 0 0-16 8 8 0 0 0 0 16Z"/>',
|
|
1061
|
+
view: '<path d="M2.06 12.35a1 1 0 0 1 0-.7 10.75 10.75 0 0 1 19.88 0 1 1 0 0 1 0 .7 10.75 10.75 0 0 1-19.88 0"/><path d="M12 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6Z"/>',
|
|
1062
|
+
edit: '<path d="M21.17 6.81a1 1 0 0 0-3.99-3.99L3.84 16.17a2 2 0 0 0-.5.83l-1.32 4.35a.5.5 0 0 0 .62.62l4.35-1.32a2 2 0 0 0 .83-.5Z"/><path d="m15 5 4 4"/>',
|
|
1063
|
+
delete: '<path d="M3 6h18"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/><path d="M10 11v6"/><path d="M14 11v6"/>',
|
|
1064
|
+
prev: '<path d="m15 18-6-6 6-6"/>',
|
|
1065
|
+
next: '<path d="m9 18 6-6-6-6"/>',
|
|
1066
|
+
sortNone: '<path d="m7 15 5 5 5-5"/><path d="m7 9 5-5 5 5"/>',
|
|
1067
|
+
sortAsc: '<path d="m5 12 7-7 7 7"/><path d="M12 19V5"/>',
|
|
1068
|
+
sortDesc: '<path d="M12 5v14"/><path d="m19 12-7 7-7-7"/>',
|
|
1069
|
+
chevron: '<path d="m6 9 6 6 6-6"/>',
|
|
1070
|
+
};
|
|
1071
|
+
const DT_TONES = { info: "var(--info,#0284c7)", success: "var(--success,#16a34a)", warning: "var(--warning,#d97706)", danger: "var(--destructive,#dc2626)", error: "var(--destructive,#dc2626)" };
|
|
1072
|
+
let dtStylesInjected = false;
|
|
1073
|
+
|
|
1074
|
+
/**
|
|
1075
|
+
* Injects the self-contained DataTable styles (theme tokens with fallbacks, container-query responsive layout).
|
|
1076
|
+
*/
|
|
1077
|
+
function ensureDataTableStyles() {
|
|
1078
|
+
if (dtStylesInjected || !document.head) return;
|
|
1079
|
+
dtStylesInjected = true;
|
|
1080
|
+
const style = document.createElement("style");
|
|
1081
|
+
style.setAttribute("data-bb-datatable", "true");
|
|
1082
|
+
style.textContent = [
|
|
1083
|
+
".bb-dt{container-type:inline-size;overflow:hidden;border:1px solid var(--border,#e4e4e7);border-radius:1rem;background:var(--card,#fff);color:var(--card-foreground,#18181b);box-shadow:0 1px 2px rgba(0,0,0,.04);font-size:.875rem}",
|
|
1084
|
+
".bb-dt svg{width:1rem;height:1rem;flex-shrink:0;fill:none;stroke:currentColor;stroke-width:2;stroke-linecap:round;stroke-linejoin:round}",
|
|
1085
|
+
".bb-dt-bar{display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:.75rem;padding:.75rem;border-bottom:1px solid var(--border,#e4e4e7)}",
|
|
1086
|
+
".bb-dt-search{position:relative;width:100%;max-width:18rem}.bb-dt-search svg{position:absolute;left:.75rem;top:50%;transform:translateY(-50%);color:var(--muted-foreground,#71717a);pointer-events:none}.bb-dt-search input{height:2.25rem;padding-left:2.25rem}",
|
|
1087
|
+
".bb-dt-table{display:none;overflow-x:auto}.bb-dt table{width:100%;border-collapse:collapse}",
|
|
1088
|
+
"@container (min-width:42rem){.bb-dt-table{display:block}.bb-dt-cards{display:none}}",
|
|
1089
|
+
".bb-dt th{height:2.5rem;padding:0 1rem;text-align:left;white-space:nowrap;font-size:.75rem;font-weight:500;color:var(--muted-foreground,#71717a);background:color-mix(in oklab,var(--muted,#f4f4f5) 40%,transparent);border-bottom:1px solid var(--border,#e4e4e7)}",
|
|
1090
|
+
".bb-dt td{padding:.75rem 1rem;vertical-align:middle;border-top:1px solid color-mix(in oklab,var(--border,#e4e4e7) 70%,transparent)}.bb-dt tbody tr:first-child td{border-top:0}",
|
|
1091
|
+
".bb-dt tbody tr{transition:background-color .15s}.bb-dt tbody tr:hover{background:color-mix(in oklab,var(--accent,#f4f4f5) 40%,transparent)}",
|
|
1092
|
+
".bb-dt .bb-dt-right{text-align:right}.bb-dt .bb-dt-center{text-align:center}.bb-dt .bb-dt-num{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-variant-numeric:tabular-nums}.bb-dt .bb-dt-primary{font-weight:500}",
|
|
1093
|
+
".bb-dt-sort{display:inline-flex;align-items:center;gap:.25rem;margin:0 -.5rem;padding:.25rem .5rem;border:0;border-radius:.375rem;background:transparent;color:inherit;font:inherit;cursor:pointer}.bb-dt-sort:hover,.bb-dt-sort[data-active]{color:var(--foreground,#18181b)}.bb-dt-sort:hover{background:var(--accent,#f4f4f5)}.bb-dt-sort svg{width:.875rem;height:.875rem}.bb-dt-sort:not([data-active]) svg{opacity:.4}",
|
|
1094
|
+
".bb-dt-actions{display:flex;align-items:center;justify-content:flex-end;gap:.125rem}.bb-dt-actions .btn{color:var(--muted-foreground,#71717a)}.bb-dt-actions .btn:hover{color:var(--foreground,#18181b)}.bb-dt-actions .bb-dt-danger,.bb-dt-actions .bb-dt-danger:hover{color:var(--destructive,#dc2626)}",
|
|
1095
|
+
".bb-dt-cards{list-style:none;margin:0;padding:0}.bb-dt-cards>li{padding:1rem;border-top:1px solid color-mix(in oklab,var(--border,#e4e4e7) 70%,transparent)}.bb-dt-cards>li:first-child{border-top:0}",
|
|
1096
|
+
".bb-dt-card-head{display:flex;align-items:flex-start;justify-content:space-between;gap:.75rem}.bb-dt-card-title{min-width:0;flex:1;font-weight:500}.bb-dt-card-head .bb-dt-actions{margin:-.25rem -.5rem 0 0}",
|
|
1097
|
+
".bb-dt dl{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:.75rem 1rem;margin:.75rem 0 0}.bb-dt dt{font-size:.75rem;color:var(--muted-foreground,#71717a)}.bb-dt dd{margin:.125rem 0 0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}",
|
|
1098
|
+
".bb-dt-details{border-top:1px solid color-mix(in oklab,var(--border,#e4e4e7) 70%,transparent);padding-top:.75rem}",
|
|
1099
|
+
".bb-dt-toggle{display:inline-flex;align-items:center;gap:.25rem;margin-top:.75rem;padding:0;border:0;background:transparent;color:var(--muted-foreground,#71717a);font:inherit;font-size:.75rem;font-weight:500;cursor:pointer}.bb-dt-toggle:hover{color:var(--foreground,#18181b)}.bb-dt-toggle svg{width:.875rem;height:.875rem;transition:transform .2s}.bb-dt-toggle[aria-expanded=true] svg{transform:rotate(180deg)}",
|
|
1100
|
+
".bb-dt-foot{display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:.75rem;padding:.625rem 1rem;border-top:1px solid var(--border,#e4e4e7);font-size:.75rem;color:var(--muted-foreground,#71717a);font-variant-numeric:tabular-nums}.bb-dt-foot>div{display:flex;align-items:center;gap:.75rem}.bb-dt-foot label{display:inline-flex;align-items:center;gap:.5rem}.bb-dt-foot select{height:2rem;width:auto;padding-top:0;padding-bottom:0;font-size:.75rem}.bb-dt-foot .btn{width:2rem;height:2rem}.bb-dt-foot[hidden]{display:none}",
|
|
1101
|
+
".bb-dt-empty{padding:3rem 1.5rem;text-align:center;color:var(--muted-foreground,#71717a)}",
|
|
1102
|
+
".bb-dt-badge{display:inline-flex;align-items:center;gap:.375rem;border:1px solid color-mix(in oklab,var(--border,#e4e4e7) 80%,transparent);border-radius:9999px;padding:.125rem .625rem;background:color-mix(in oklab,var(--secondary,#f4f4f5) 75%,transparent);font-size:.75rem;font-weight:500}.bb-dt-badge i{width:.375rem;height:.375rem;border-radius:9999px}",
|
|
1103
|
+
".bb-dt[aria-busy=true] tbody,.bb-dt[aria-busy=true] .bb-dt-cards{opacity:.6;transition:opacity .15s}",
|
|
1104
|
+
".bb-dt-sk{display:block;height:.875rem;width:75%;border-radius:.375rem;background:var(--muted,#f4f4f5);animation:bb-dt-pulse 1.5s ease-in-out infinite}@keyframes bb-dt-pulse{50%{opacity:.45}}",
|
|
1105
|
+
"dialog.bb-dt-dialog{width:calc(100% - 2rem);max-width:24rem;margin:auto;padding:1.5rem;border:1px solid var(--border,#e4e4e7);border-radius:1rem;background:var(--popover,#fff);color:var(--popover-foreground,#18181b);box-shadow:0 24px 48px -12px rgba(0,0,0,.35)}",
|
|
1106
|
+
"dialog.bb-dt-dialog h2{margin:0;font-size:1.0625rem;font-weight:600}dialog.bb-dt-dialog p{margin:.25rem 0 0;font-size:.875rem;color:var(--muted-foreground,#71717a)}dialog.bb-dt-dialog .bb-dt-dialog-actions{display:flex;justify-content:flex-end;gap:.5rem;margin-top:1.5rem}",
|
|
1107
|
+
].join("");
|
|
1108
|
+
document.head.appendChild(style);
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
/**
|
|
1112
|
+
* Creates an inline SVG icon element.
|
|
1113
|
+
* @param {keyof typeof DT_ICONS} name
|
|
1114
|
+
* @returns {SVGSVGElement}
|
|
1115
|
+
*/
|
|
1116
|
+
function dtIcon(name) {
|
|
1117
|
+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
1118
|
+
svg.setAttribute("viewBox", "0 0 24 24");
|
|
1119
|
+
svg.setAttribute("aria-hidden", "true");
|
|
1120
|
+
svg.innerHTML = DT_ICONS[name];
|
|
1121
|
+
return svg;
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
/**
|
|
1125
|
+
* Creates an element with a class name and optional text content.
|
|
1126
|
+
* @param {string} tag
|
|
1127
|
+
* @param {string} [className]
|
|
1128
|
+
* @param {string} [text]
|
|
1129
|
+
* @returns {HTMLElement}
|
|
1130
|
+
*/
|
|
1131
|
+
function dtEl(tag, className, text) {
|
|
1132
|
+
const el = document.createElement(tag);
|
|
1133
|
+
if (className) el.className = className;
|
|
1134
|
+
if (text !== undefined && text !== null) el.textContent = String(text);
|
|
1135
|
+
return el;
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
/**
|
|
1139
|
+
* Builds a link from a pattern such as `/users/:id` or `/users/{id}/edit`.
|
|
1140
|
+
* @param {string|Function} pattern
|
|
1141
|
+
* @param {Record<string, any>} row
|
|
1142
|
+
* @returns {string}
|
|
1143
|
+
*/
|
|
1144
|
+
function dtHref(pattern, row) {
|
|
1145
|
+
if (typeof pattern === "function") return pattern(row);
|
|
1146
|
+
return String(pattern).replace(/:(\w+)|\{(\w+)\}/g, (_, a, b) => encodeURIComponent(String(row?.[a || b] ?? "")));
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
/**
|
|
1150
|
+
* Reads a (dotted) column value from a row.
|
|
1151
|
+
* @param {Record<string, any>} row
|
|
1152
|
+
* @param {{ key: string, value?: Function }} column
|
|
1153
|
+
*/
|
|
1154
|
+
function dtValue(row, column) {
|
|
1155
|
+
if (typeof column.value === "function") return column.value(row);
|
|
1156
|
+
return String(column.key).split(".").reduce((acc, part) => (acc == null ? acc : acc[part]), row);
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
/**
|
|
1160
|
+
* Renders a cell into a node: `render(row)` (string or Node), or a declarative `format`
|
|
1161
|
+
* (`badge`, `number`, `currency`, `date`, `datetime`, `boolean`). Values are always inserted as text.
|
|
1162
|
+
* @param {Record<string, any>} row
|
|
1163
|
+
* @param {Object} column
|
|
1164
|
+
* @returns {Node}
|
|
1165
|
+
*/
|
|
1166
|
+
function dtCell(row, column) {
|
|
1167
|
+
if (typeof column.render === "function") {
|
|
1168
|
+
const out = column.render(row);
|
|
1169
|
+
return out instanceof Node ? out : document.createTextNode(out == null ? "" : String(out));
|
|
1170
|
+
}
|
|
1171
|
+
const value = dtValue(row, column);
|
|
1172
|
+
if (value === null || value === undefined || value === "") {
|
|
1173
|
+
const dash = dtEl("span", "", "—");
|
|
1174
|
+
dash.style.color = "var(--muted-foreground,#71717a)";
|
|
1175
|
+
return dash;
|
|
1176
|
+
}
|
|
1177
|
+
const lang = getLocale();
|
|
1178
|
+
switch (column.format) {
|
|
1179
|
+
case "badge": {
|
|
1180
|
+
const badge = dtEl("span", "bb-dt-badge");
|
|
1181
|
+
const tone = column.tones?.[value];
|
|
1182
|
+
if (tone && DT_TONES[tone]) {
|
|
1183
|
+
const dot = dtEl("i");
|
|
1184
|
+
dot.style.background = DT_TONES[tone];
|
|
1185
|
+
badge.appendChild(dot);
|
|
1186
|
+
}
|
|
1187
|
+
badge.appendChild(document.createTextNode(String(value)));
|
|
1188
|
+
return badge;
|
|
1189
|
+
}
|
|
1190
|
+
case "number":
|
|
1191
|
+
return document.createTextNode(Number(value).toLocaleString(lang));
|
|
1192
|
+
case "currency":
|
|
1193
|
+
return document.createTextNode(Number(value).toLocaleString(lang, { style: "currency", currency: column.currency || "USD" }));
|
|
1194
|
+
case "date":
|
|
1195
|
+
case "datetime": {
|
|
1196
|
+
const dateOnly = typeof value === "string" && /^\d{4}-\d{2}-\d{2}$/.test(value);
|
|
1197
|
+
const date = dateOnly ? new Date(Number(value.slice(0, 4)), Number(value.slice(5, 7)) - 1, Number(value.slice(8, 10))) : new Date(value);
|
|
1198
|
+
if (Number.isNaN(date.getTime())) return document.createTextNode(String(value));
|
|
1199
|
+
return document.createTextNode(column.format === "date" ? date.toLocaleDateString(lang) : date.toLocaleString(lang));
|
|
1200
|
+
}
|
|
1201
|
+
case "boolean":
|
|
1202
|
+
return document.createTextNode(value ? "✓" : "—");
|
|
1203
|
+
default:
|
|
1204
|
+
return document.createTextNode(String(value));
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
/**
|
|
1209
|
+
* Normalizes a server response (`res.paginate()`, `res.ok(Model.paginate())`, or an array) into rows and total.
|
|
1210
|
+
* @param {any} body
|
|
1211
|
+
* @returns {{ rows: any[], total: number }}
|
|
1212
|
+
*/
|
|
1213
|
+
function dtNormalize(body) {
|
|
1214
|
+
if (Array.isArray(body)) return { rows: body, total: body.length };
|
|
1215
|
+
const data = body?.data;
|
|
1216
|
+
if (Array.isArray(data)) return { rows: data, total: Number(body.pagination?.total ?? body.total ?? data.length) };
|
|
1217
|
+
if (data && Array.isArray(data.data)) return { rows: data.data, total: Number(data.total ?? data.pagination?.total ?? data.data.length) };
|
|
1218
|
+
return { rows: [], total: 0 };
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
/**
|
|
1222
|
+
* Opens a native confirmation `<dialog>` and resolves `true` when confirmed.
|
|
1223
|
+
* @param {{ title: string, text: string, confirmLabel: string, danger?: boolean }} options
|
|
1224
|
+
* @returns {Promise<boolean>}
|
|
1225
|
+
*/
|
|
1226
|
+
function dtConfirm({ title, text, confirmLabel, danger }) {
|
|
1227
|
+
if (typeof HTMLDialogElement === "undefined") return Promise.resolve(window.confirm(title));
|
|
1228
|
+
return new Promise((resolve) => {
|
|
1229
|
+
const dialog = dtEl("dialog", "bb-dt-dialog");
|
|
1230
|
+
dialog.setAttribute("data-modal", "");
|
|
1231
|
+
dialog.setAttribute("aria-labelledby", "bb-dt-dialog-title");
|
|
1232
|
+
const heading = dtEl("h2", "", title);
|
|
1233
|
+
heading.id = "bb-dt-dialog-title";
|
|
1234
|
+
const actions = dtEl("div", "bb-dt-dialog-actions");
|
|
1235
|
+
const cancel = dtEl("button", "btn btn-outline", t("table.cancel"));
|
|
1236
|
+
cancel.type = "button";
|
|
1237
|
+
const ok = dtEl("button", danger ? "btn btn-danger" : "btn btn-primary", confirmLabel);
|
|
1238
|
+
ok.type = "button";
|
|
1239
|
+
actions.append(cancel, ok);
|
|
1240
|
+
dialog.append(heading, dtEl("p", "", text), actions);
|
|
1241
|
+
document.body.appendChild(dialog);
|
|
1242
|
+
let result = false;
|
|
1243
|
+
dialog.addEventListener("close", () => {
|
|
1244
|
+
dialog.remove();
|
|
1245
|
+
resolve(result);
|
|
1246
|
+
});
|
|
1247
|
+
cancel.addEventListener("click", () => closeTopLayerDialog(dialog));
|
|
1248
|
+
ok.addEventListener("click", () => {
|
|
1249
|
+
result = true;
|
|
1250
|
+
closeTopLayerDialog(dialog);
|
|
1251
|
+
});
|
|
1252
|
+
initModalDialog(dialog);
|
|
1253
|
+
dialog.showModal();
|
|
1254
|
+
cancel.focus();
|
|
1255
|
+
});
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
/**
|
|
1259
|
+
* Creates a responsive DataTable inside `el`: search, sortable columns, pagination with page sizes, view/edit/delete
|
|
1260
|
+
* and custom actions (delete asks in a native dialog), and cards with expandable details in narrow containers.
|
|
1261
|
+
* Data comes from `rows` or a server `url` (`?page&limit&search&sort&order`, answered with `res.paginate()`).
|
|
1262
|
+
* Every action also dispatches a bubbling `bluebird:datatable` event with `{ action, row }`.
|
|
1263
|
+
* @param {HTMLElement|string} target
|
|
1264
|
+
* @param {Object} options
|
|
1265
|
+
* @returns {{ reload: () => Promise<void>, setRows: (rows: any[]) => void, destroy: () => void }}
|
|
1266
|
+
*/
|
|
1267
|
+
function dataTable(target, options = {}) {
|
|
1268
|
+
const el = typeof target === "string" ? document.querySelector(target) : target;
|
|
1269
|
+
if (!el) throw new Error("Bluebird.dataTable: element not found");
|
|
1270
|
+
if (el.__bbDataTable) return el.__bbDataTable;
|
|
1271
|
+
ensureStyles();
|
|
1272
|
+
ensureDataTableStyles();
|
|
1273
|
+
|
|
1274
|
+
const columns = Array.isArray(options.columns) ? options.columns : [];
|
|
1275
|
+
const url = options.url || null;
|
|
1276
|
+
const pageSizes = Array.isArray(options.pageSizes) ? options.pageSizes : [10, 25, 50];
|
|
1277
|
+
const state = {
|
|
1278
|
+
rows: Array.isArray(options.rows) ? options.rows : [],
|
|
1279
|
+
total: 0,
|
|
1280
|
+
page: 1,
|
|
1281
|
+
limit: Number(options.pageSize) || 10,
|
|
1282
|
+
query: "",
|
|
1283
|
+
sort: options.defaultSort?.key ? { key: options.defaultSort.key, dir: options.defaultSort.dir === "desc" ? -1 : 1 } : { key: null, dir: 1 },
|
|
1284
|
+
loading: Boolean(url),
|
|
1285
|
+
expanded: new Set(),
|
|
1286
|
+
};
|
|
1287
|
+
let requestSeq = 0;
|
|
1288
|
+
let searchTimer = null;
|
|
1289
|
+
|
|
1290
|
+
const actionList = [];
|
|
1291
|
+
const pushBuiltIn = (name, label, value, extra = {}) => {
|
|
1292
|
+
if (!value) return;
|
|
1293
|
+
actionList.push({ action: name, label, icon: name, ...(typeof value === "string" ? { href: value } : typeof value === "function" ? { onClick: value } : value), ...extra });
|
|
1294
|
+
};
|
|
1295
|
+
pushBuiltIn("view", t("table.view"), options.onView);
|
|
1296
|
+
pushBuiltIn("edit", t("table.edit"), options.onEdit);
|
|
1297
|
+
(options.actions || []).forEach((a) => {
|
|
1298
|
+
if (a.action === "view" || a.action === "edit" || a.action === "delete") {
|
|
1299
|
+
actionList.push({ icon: a.action, label: t("table." + a.action), ...(a.action === "delete" ? { variant: "danger", confirm: true } : {}), ...a });
|
|
1300
|
+
} else {
|
|
1301
|
+
actionList.push(a);
|
|
1302
|
+
}
|
|
1303
|
+
});
|
|
1304
|
+
pushBuiltIn("delete", t("table.delete"), options.onDelete, { variant: "danger", confirm: options.deleteConfirm === false ? false : options.deleteConfirm || true });
|
|
1305
|
+
|
|
1306
|
+
const primary = columns.find((c) => c.primary) || columns[0];
|
|
1307
|
+
const mobileCols = columns.filter((c) => c !== primary && c.mobile !== false);
|
|
1308
|
+
const summaryCols = mobileCols.filter((c) => c.mobile !== "details").slice(0, 4);
|
|
1309
|
+
const detailCols = mobileCols.filter((c) => !summaryCols.includes(c));
|
|
1310
|
+
const alignClass = (c) => (c.align === "right" || c.numeric || c.format === "number" || c.format === "currency" ? "bb-dt-right" : c.align === "center" ? "bb-dt-center" : "");
|
|
1311
|
+
|
|
1312
|
+
el.innerHTML = "";
|
|
1313
|
+
const root = dtEl("div", "bb-dt");
|
|
1314
|
+
const bar = dtEl("div", "bb-dt-bar");
|
|
1315
|
+
const searchWrap = dtEl("div", "bb-dt-search");
|
|
1316
|
+
const search = dtEl("input", "field");
|
|
1317
|
+
search.type = "search";
|
|
1318
|
+
search.placeholder = options.searchPlaceholder || t("table.search");
|
|
1319
|
+
search.setAttribute("aria-label", search.placeholder);
|
|
1320
|
+
searchWrap.append(dtIcon("search"), search);
|
|
1321
|
+
if (options.searchable !== false) bar.appendChild(searchWrap);
|
|
1322
|
+
const tableWrap = dtEl("div", "bb-dt-table");
|
|
1323
|
+
const table = dtEl("table");
|
|
1324
|
+
if (options.caption) table.appendChild(Object.assign(dtEl("caption", "bb-vh"), { textContent: options.caption }));
|
|
1325
|
+
const thead = dtEl("thead");
|
|
1326
|
+
const tbody = dtEl("tbody");
|
|
1327
|
+
table.append(thead, tbody);
|
|
1328
|
+
tableWrap.appendChild(table);
|
|
1329
|
+
const cards = dtEl("ul", "bb-dt-cards");
|
|
1330
|
+
const foot = dtEl("div", "bb-dt-foot");
|
|
1331
|
+
if (bar.children.length) root.appendChild(bar);
|
|
1332
|
+
root.append(tableWrap, cards, foot);
|
|
1333
|
+
el.appendChild(root);
|
|
1334
|
+
|
|
1335
|
+
/**
|
|
1336
|
+
* Rows visible on the current page plus the total after search and sort.
|
|
1337
|
+
* @returns {{ rows: any[], total: number }}
|
|
1338
|
+
*/
|
|
1339
|
+
const view = () => {
|
|
1340
|
+
if (url) return { rows: state.rows, total: state.total };
|
|
1341
|
+
const q = state.query.trim().toLowerCase();
|
|
1342
|
+
let list = q
|
|
1343
|
+
? state.rows.filter((row) => columns.some((c) => c.searchable !== false && String(dtValue(row, c) ?? "").toLowerCase().includes(q)))
|
|
1344
|
+
: state.rows;
|
|
1345
|
+
if (state.sort.key) {
|
|
1346
|
+
const col = columns.find((c) => c.key === state.sort.key) || { key: state.sort.key };
|
|
1347
|
+
list = [...list].sort((a, b) => {
|
|
1348
|
+
const x = dtValue(a, col);
|
|
1349
|
+
const y = dtValue(b, col);
|
|
1350
|
+
if (x === y) return 0;
|
|
1351
|
+
if (x == null || x === "") return 1;
|
|
1352
|
+
if (y == null || y === "") return -1;
|
|
1353
|
+
const cmp = typeof x === "number" && typeof y === "number" ? x - y : String(x).localeCompare(String(y), undefined, { numeric: true, sensitivity: "base" });
|
|
1354
|
+
return cmp * state.sort.dir;
|
|
1355
|
+
});
|
|
1356
|
+
}
|
|
1357
|
+
const pages = Math.max(1, Math.ceil(list.length / state.limit));
|
|
1358
|
+
if (state.page > pages) state.page = pages;
|
|
1359
|
+
return { rows: list.slice((state.page - 1) * state.limit, state.page * state.limit), total: list.length };
|
|
1360
|
+
};
|
|
1361
|
+
|
|
1362
|
+
/**
|
|
1363
|
+
* Runs an action: navigation, confirmation, handler, declarative request, event, and reload.
|
|
1364
|
+
* @param {Object} action
|
|
1365
|
+
* @param {Record<string, any>} row
|
|
1366
|
+
*/
|
|
1367
|
+
const runAction = async (action, row) => {
|
|
1368
|
+
if (action.confirm) {
|
|
1369
|
+
const ok = await dtConfirm({
|
|
1370
|
+
title: action.action === "delete" ? t("table.confirmTitle") : action.label,
|
|
1371
|
+
text: typeof action.confirm === "string" ? action.confirm : t("table.confirmText"),
|
|
1372
|
+
confirmLabel: action.label,
|
|
1373
|
+
danger: action.variant === "danger",
|
|
1374
|
+
});
|
|
1375
|
+
if (!ok) return;
|
|
1376
|
+
}
|
|
1377
|
+
el.dispatchEvent(new CustomEvent("bluebird:datatable", { bubbles: true, detail: { action: action.action || action.label, row } }));
|
|
1378
|
+
try {
|
|
1379
|
+
if (typeof action.onClick === "function") await action.onClick(row);
|
|
1380
|
+
if (action.url) {
|
|
1381
|
+
const headers = { Accept: "application/json" };
|
|
1382
|
+
const csrf = getCsrfToken();
|
|
1383
|
+
if (csrf) headers["X-CSRF-Token"] = csrf;
|
|
1384
|
+
const res = await fetch(dtHref(action.url, row), { method: (action.method || (action.action === "delete" ? "DELETE" : "POST")).toUpperCase(), credentials: "same-origin", headers });
|
|
1385
|
+
const payload = (res.headers.get("Content-Type") || "").includes("application/json") ? await res.json() : {};
|
|
1386
|
+
if (!res.ok || payload?.status === "error") throw new Error(payload?.message || t("request.failed"));
|
|
1387
|
+
if (payload?.message) toast.success(payload.message);
|
|
1388
|
+
}
|
|
1389
|
+
if (!url && action.action === "delete") {
|
|
1390
|
+
const key = options.rowKey || "id";
|
|
1391
|
+
state.rows = state.rows.filter((r) => r[key] !== row[key]);
|
|
1392
|
+
}
|
|
1393
|
+
if (url && (action.url || action.action === "delete" || action.reload)) await load();
|
|
1394
|
+
else render();
|
|
1395
|
+
} catch (err) {
|
|
1396
|
+
toast.error(err?.message || t("request.failed"));
|
|
1397
|
+
}
|
|
1398
|
+
};
|
|
1399
|
+
|
|
1400
|
+
/**
|
|
1401
|
+
* Renders the action buttons for a row.
|
|
1402
|
+
* @param {Record<string, any>} row
|
|
1403
|
+
* @returns {HTMLElement|null}
|
|
1404
|
+
*/
|
|
1405
|
+
const actionsFor = (row) => {
|
|
1406
|
+
const list = actionList.filter((a) => !(typeof a.hidden === "function" && a.hidden(row)) && !(a.when && String(row[a.when.key]) !== String(a.when.value)));
|
|
1407
|
+
if (!list.length) return null;
|
|
1408
|
+
const wrap = dtEl("div", "bb-dt-actions");
|
|
1409
|
+
list.forEach((a) => {
|
|
1410
|
+
const withIcon = Boolean(a.icon && DT_ICONS[a.icon]);
|
|
1411
|
+
const cls = "btn btn-ghost btn-sm" + (withIcon ? " btn-icon" : "") + (a.variant === "danger" ? " bb-dt-danger" : "");
|
|
1412
|
+
const node = a.href ? dtEl("a", cls) : dtEl("button", cls);
|
|
1413
|
+
if (a.href) node.href = dtHref(a.href, row);
|
|
1414
|
+
else node.type = "button";
|
|
1415
|
+
node.setAttribute("aria-label", a.label);
|
|
1416
|
+
node.title = a.label;
|
|
1417
|
+
if (withIcon) node.appendChild(dtIcon(a.icon));
|
|
1418
|
+
else node.textContent = a.label;
|
|
1419
|
+
node.addEventListener("click", (e) => {
|
|
1420
|
+
e.stopPropagation();
|
|
1421
|
+
if (a.href) return;
|
|
1422
|
+
runAction(a, row);
|
|
1423
|
+
});
|
|
1424
|
+
wrap.appendChild(node);
|
|
1425
|
+
});
|
|
1426
|
+
return wrap;
|
|
1427
|
+
};
|
|
1428
|
+
|
|
1429
|
+
/**
|
|
1430
|
+
* Renders the header, rows, cards, and footer.
|
|
1431
|
+
*/
|
|
1432
|
+
const render = () => {
|
|
1433
|
+
const { rows, total } = view();
|
|
1434
|
+
const hasActions = actionList.length > 0;
|
|
1435
|
+
root.setAttribute("aria-busy", state.loading && state.rows.length ? "true" : "false");
|
|
1436
|
+
|
|
1437
|
+
const headRow = dtEl("tr");
|
|
1438
|
+
columns.forEach((c) => {
|
|
1439
|
+
const th = dtEl("th", alignClass(c));
|
|
1440
|
+
th.scope = "col";
|
|
1441
|
+
if (state.sort.key === c.key) th.setAttribute("aria-sort", state.sort.dir === 1 ? "ascending" : "descending");
|
|
1442
|
+
if (c.sortable) {
|
|
1443
|
+
const btn = dtEl("button", "bb-dt-sort", c.label);
|
|
1444
|
+
btn.type = "button";
|
|
1445
|
+
btn.title = t("table.sortBy", { column: c.label });
|
|
1446
|
+
if (state.sort.key === c.key) btn.setAttribute("data-active", "");
|
|
1447
|
+
btn.appendChild(dtIcon(state.sort.key !== c.key ? "sortNone" : state.sort.dir === 1 ? "sortAsc" : "sortDesc"));
|
|
1448
|
+
btn.addEventListener("click", () => {
|
|
1449
|
+
state.sort = state.sort.key !== c.key ? { key: c.key, dir: 1 } : state.sort.dir === 1 ? { key: c.key, dir: -1 } : { key: null, dir: 1 };
|
|
1450
|
+
state.page = 1;
|
|
1451
|
+
url ? load() : render();
|
|
1452
|
+
});
|
|
1453
|
+
th.appendChild(btn);
|
|
1454
|
+
} else {
|
|
1455
|
+
th.textContent = c.label;
|
|
1456
|
+
}
|
|
1457
|
+
headRow.appendChild(th);
|
|
1458
|
+
});
|
|
1459
|
+
if (hasActions) {
|
|
1460
|
+
const th = dtEl("th", "bb-dt-right");
|
|
1461
|
+
th.appendChild(dtEl("span", "bb-vh", t("table.actions")));
|
|
1462
|
+
headRow.appendChild(th);
|
|
1463
|
+
}
|
|
1464
|
+
thead.replaceChildren(headRow);
|
|
1465
|
+
|
|
1466
|
+
const showSkeleton = state.loading && !state.rows.length;
|
|
1467
|
+
tbody.replaceChildren();
|
|
1468
|
+
cards.replaceChildren();
|
|
1469
|
+
if (showSkeleton) {
|
|
1470
|
+
for (let i = 0; i < Math.min(5, state.limit); i++) {
|
|
1471
|
+
const tr = dtEl("tr");
|
|
1472
|
+
columns.concat(hasActions ? [{}] : []).forEach(() => {
|
|
1473
|
+
const td = dtEl("td");
|
|
1474
|
+
td.appendChild(dtEl("span", "bb-dt-sk"));
|
|
1475
|
+
tr.appendChild(td);
|
|
1476
|
+
});
|
|
1477
|
+
tbody.appendChild(tr);
|
|
1478
|
+
const li = dtEl("li");
|
|
1479
|
+
li.appendChild(dtEl("span", "bb-dt-sk"));
|
|
1480
|
+
cards.appendChild(li);
|
|
1481
|
+
}
|
|
1482
|
+
} else if (!rows.length) {
|
|
1483
|
+
const td = dtEl("td", "bb-dt-empty", options.empty || t("table.empty"));
|
|
1484
|
+
td.colSpan = columns.length + (hasActions ? 1 : 0);
|
|
1485
|
+
const tr = dtEl("tr");
|
|
1486
|
+
tr.appendChild(td);
|
|
1487
|
+
tbody.appendChild(tr);
|
|
1488
|
+
cards.appendChild(dtEl("li", "bb-dt-empty", options.empty || t("table.empty")));
|
|
1489
|
+
} else {
|
|
1490
|
+
rows.forEach((row, index) => {
|
|
1491
|
+
const tr = dtEl("tr");
|
|
1492
|
+
if (typeof options.onRowClick === "function") {
|
|
1493
|
+
tr.style.cursor = "pointer";
|
|
1494
|
+
tr.addEventListener("click", () => options.onRowClick(row));
|
|
1495
|
+
}
|
|
1496
|
+
columns.forEach((c) => {
|
|
1497
|
+
const td = dtEl("td", [alignClass(c), c.numeric ? "bb-dt-num" : "", c === primary ? "bb-dt-primary" : ""].filter(Boolean).join(" "));
|
|
1498
|
+
td.appendChild(dtCell(row, c));
|
|
1499
|
+
tr.appendChild(td);
|
|
1500
|
+
});
|
|
1501
|
+
if (hasActions) {
|
|
1502
|
+
const td = dtEl("td", "bb-dt-right");
|
|
1503
|
+
td.style.padding = ".375rem .5rem";
|
|
1504
|
+
const acts = actionsFor(row);
|
|
1505
|
+
if (acts) td.appendChild(acts);
|
|
1506
|
+
tr.appendChild(td);
|
|
1507
|
+
}
|
|
1508
|
+
tbody.appendChild(tr);
|
|
1509
|
+
|
|
1510
|
+
const key = row?.[options.rowKey || "id"] ?? `${state.page}-${index}`;
|
|
1511
|
+
const li = dtEl("li");
|
|
1512
|
+
const head = dtEl("div", "bb-dt-card-head");
|
|
1513
|
+
const title = dtEl("div", "bb-dt-card-title");
|
|
1514
|
+
if (primary) title.appendChild(dtCell(row, primary));
|
|
1515
|
+
head.appendChild(title);
|
|
1516
|
+
const acts = actionsFor(row);
|
|
1517
|
+
if (acts) head.appendChild(acts);
|
|
1518
|
+
li.appendChild(head);
|
|
1519
|
+
const field = (c) => {
|
|
1520
|
+
const box = dtEl("div");
|
|
1521
|
+
const dd = dtEl("dd", c.numeric ? "bb-dt-num" : "");
|
|
1522
|
+
dd.appendChild(dtCell(row, c));
|
|
1523
|
+
box.append(dtEl("dt", "", c.label), dd);
|
|
1524
|
+
return box;
|
|
1525
|
+
};
|
|
1526
|
+
if (summaryCols.length) {
|
|
1527
|
+
const dl = dtEl("dl");
|
|
1528
|
+
summaryCols.forEach((c) => dl.appendChild(field(c)));
|
|
1529
|
+
li.appendChild(dl);
|
|
1530
|
+
}
|
|
1531
|
+
if (detailCols.length) {
|
|
1532
|
+
const open = state.expanded.has(key);
|
|
1533
|
+
if (open) {
|
|
1534
|
+
const dl = dtEl("dl", "bb-dt-details");
|
|
1535
|
+
detailCols.forEach((c) => dl.appendChild(field(c)));
|
|
1536
|
+
li.appendChild(dl);
|
|
1537
|
+
}
|
|
1538
|
+
const toggle = dtEl("button", "bb-dt-toggle", t(open ? "table.less" : "table.more"));
|
|
1539
|
+
toggle.type = "button";
|
|
1540
|
+
toggle.setAttribute("aria-expanded", open ? "true" : "false");
|
|
1541
|
+
toggle.appendChild(dtIcon("chevron"));
|
|
1542
|
+
toggle.addEventListener("click", () => {
|
|
1543
|
+
if (state.expanded.has(key)) state.expanded.delete(key);
|
|
1544
|
+
else state.expanded.add(key);
|
|
1545
|
+
render();
|
|
1546
|
+
});
|
|
1547
|
+
li.appendChild(toggle);
|
|
1548
|
+
}
|
|
1549
|
+
cards.appendChild(li);
|
|
1550
|
+
});
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
foot.replaceChildren();
|
|
1554
|
+
foot.hidden = showSkeleton || total === 0;
|
|
1555
|
+
if (!foot.hidden) {
|
|
1556
|
+
const pages = Math.max(1, Math.ceil(total / state.limit));
|
|
1557
|
+
const from = (state.page - 1) * state.limit + 1;
|
|
1558
|
+
const to = Math.min(total, state.page * state.limit);
|
|
1559
|
+
foot.appendChild(dtEl("span", "", t("table.range", { from, to, total })));
|
|
1560
|
+
const right = dtEl("div");
|
|
1561
|
+
if (pageSizes.length > 1) {
|
|
1562
|
+
const label = dtEl("label", "", t("table.rowsPerPage"));
|
|
1563
|
+
const select = dtEl("select", "field");
|
|
1564
|
+
[...new Set([...pageSizes, state.limit])].sort((a, b) => a - b).forEach((n) => {
|
|
1565
|
+
const opt = dtEl("option", "", n);
|
|
1566
|
+
opt.value = String(n);
|
|
1567
|
+
if (n === state.limit) opt.selected = true;
|
|
1568
|
+
select.appendChild(opt);
|
|
1569
|
+
});
|
|
1570
|
+
select.addEventListener("change", () => {
|
|
1571
|
+
state.limit = Number(select.value);
|
|
1572
|
+
state.page = 1;
|
|
1573
|
+
url ? load() : render();
|
|
1574
|
+
});
|
|
1575
|
+
label.appendChild(select);
|
|
1576
|
+
right.appendChild(label);
|
|
1577
|
+
}
|
|
1578
|
+
right.appendChild(dtEl("span", "", t("table.page", { n: state.page, total: pages })));
|
|
1579
|
+
const nav = dtEl("div");
|
|
1580
|
+
nav.style.display = "flex";
|
|
1581
|
+
nav.style.gap = ".25rem";
|
|
1582
|
+
[["prev", state.page <= 1, -1], ["next", state.page >= pages, 1]].forEach(([name, disabled, step]) => {
|
|
1583
|
+
const btn = dtEl("button", "btn btn-outline btn-icon btn-sm");
|
|
1584
|
+
btn.type = "button";
|
|
1585
|
+
btn.disabled = disabled;
|
|
1586
|
+
btn.setAttribute("aria-label", t("table." + name));
|
|
1587
|
+
btn.appendChild(dtIcon(name));
|
|
1588
|
+
btn.addEventListener("click", () => {
|
|
1589
|
+
state.page += step;
|
|
1590
|
+
url ? load() : render();
|
|
1591
|
+
});
|
|
1592
|
+
nav.appendChild(btn);
|
|
1593
|
+
});
|
|
1594
|
+
right.appendChild(nav);
|
|
1595
|
+
foot.appendChild(right);
|
|
1596
|
+
}
|
|
1597
|
+
};
|
|
1598
|
+
|
|
1599
|
+
/**
|
|
1600
|
+
* Fetches the current page from the server (`url` mode).
|
|
1601
|
+
* @returns {Promise<void>}
|
|
1602
|
+
*/
|
|
1603
|
+
const load = async () => {
|
|
1604
|
+
if (!url) return render();
|
|
1605
|
+
const seq = ++requestSeq;
|
|
1606
|
+
state.loading = true;
|
|
1607
|
+
render();
|
|
1608
|
+
const qs = new URLSearchParams({ ...(options.params || {}), page: String(state.page), limit: String(state.limit) });
|
|
1609
|
+
if (state.query.trim()) qs.set("search", state.query.trim());
|
|
1610
|
+
if (state.sort.key) {
|
|
1611
|
+
qs.set("sort", state.sort.key);
|
|
1612
|
+
qs.set("order", state.sort.dir === -1 ? "desc" : "asc");
|
|
1613
|
+
}
|
|
1614
|
+
try {
|
|
1615
|
+
const res = await fetch(url + (url.includes("?") ? "&" : "?") + qs.toString(), { credentials: "same-origin", headers: { Accept: "application/json" } });
|
|
1616
|
+
const body = await res.json().catch(() => ({}));
|
|
1617
|
+
if (!res.ok || body?.status === "error") throw new Error(body?.message || t("request.failed"));
|
|
1618
|
+
if (seq !== requestSeq) return;
|
|
1619
|
+
const result = dtNormalize(body);
|
|
1620
|
+
state.rows = result.rows;
|
|
1621
|
+
state.total = result.total;
|
|
1622
|
+
} catch (err) {
|
|
1623
|
+
if (seq === requestSeq) toast.error(err?.message || t("request.failed"));
|
|
1624
|
+
} finally {
|
|
1625
|
+
if (seq === requestSeq) {
|
|
1626
|
+
state.loading = false;
|
|
1627
|
+
render();
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
};
|
|
1631
|
+
|
|
1632
|
+
search.addEventListener("input", () => {
|
|
1633
|
+
state.query = search.value;
|
|
1634
|
+
state.page = 1;
|
|
1635
|
+
if (!url) return render();
|
|
1636
|
+
clearTimeout(searchTimer);
|
|
1637
|
+
searchTimer = setTimeout(load, 300);
|
|
1638
|
+
});
|
|
1639
|
+
|
|
1640
|
+
const api = {
|
|
1641
|
+
reload: load,
|
|
1642
|
+
setRows: (rows) => {
|
|
1643
|
+
state.rows = Array.isArray(rows) ? rows : [];
|
|
1644
|
+
render();
|
|
1645
|
+
},
|
|
1646
|
+
destroy: () => {
|
|
1647
|
+
clearTimeout(searchTimer);
|
|
1648
|
+
el.innerHTML = "";
|
|
1649
|
+
delete el.__bbDataTable;
|
|
1650
|
+
},
|
|
1651
|
+
};
|
|
1652
|
+
el.__bbDataTable = api;
|
|
1653
|
+
el.dataset.bbDatatableInit = "1";
|
|
1654
|
+
url ? load() : render();
|
|
1655
|
+
return api;
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
/**
|
|
1659
|
+
* Initializes a declarative `[data-datatable]` element. Options come from a child
|
|
1660
|
+
* `<script type="application/json">` (use `safeJson()` on the server) plus `data-url`, `data-page-size`, and `data-search="false"`.
|
|
1661
|
+
* @param {HTMLElement} el
|
|
1662
|
+
*/
|
|
1663
|
+
function initDataTable(el) {
|
|
1664
|
+
if (!el || el.dataset.bbDatatableInit === "1") return;
|
|
1665
|
+
let config = {};
|
|
1666
|
+
const script = el.querySelector('script[type="application/json"]');
|
|
1667
|
+
if (script) {
|
|
1668
|
+
try {
|
|
1669
|
+
config = JSON.parse(script.textContent || "{}");
|
|
1670
|
+
} catch (err) {
|
|
1671
|
+
console.error("[Bluebird] Invalid data-datatable JSON config", err);
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
if (el.dataset.url) config.url = el.dataset.url;
|
|
1675
|
+
if (el.dataset.pageSize) config.pageSize = Number(el.dataset.pageSize);
|
|
1676
|
+
if (el.dataset.search === "false") config.searchable = false;
|
|
1677
|
+
dataTable(el, config);
|
|
1678
|
+
}
|
|
1679
|
+
|
|
957
1680
|
/**
|
|
958
1681
|
* Marks navigation links pointing at the active pathname with `aria-current="page"`.
|
|
959
1682
|
*/
|
|
@@ -984,6 +1707,7 @@
|
|
|
984
1707
|
syncActiveNavLinks();
|
|
985
1708
|
document.querySelectorAll("[data-carousel]").forEach(initCarousel);
|
|
986
1709
|
document.querySelectorAll("dialog[data-modal]").forEach(initModalDialog);
|
|
1710
|
+
document.querySelectorAll("[data-datatable]").forEach(initDataTable);
|
|
987
1711
|
}
|
|
988
1712
|
|
|
989
1713
|
window.addEventListener("storage", (e) => {
|
|
@@ -1143,6 +1867,7 @@
|
|
|
1143
1867
|
scanQueued = false;
|
|
1144
1868
|
document.querySelectorAll("[data-carousel]:not([data-bb-carousel-init='1'])").forEach(initCarousel);
|
|
1145
1869
|
document.querySelectorAll("dialog[data-modal]:not([data-bb-dialog-init='1'])").forEach(initModalDialog);
|
|
1870
|
+
document.querySelectorAll("[data-datatable]:not([data-bb-datatable-init='1'])").forEach(initDataTable);
|
|
1146
1871
|
});
|
|
1147
1872
|
});
|
|
1148
1873
|
observer.observe(document.documentElement, { childList: true, subtree: true });
|
|
@@ -1151,6 +1876,7 @@
|
|
|
1151
1876
|
global.Bluebird = {
|
|
1152
1877
|
__loaded: true,
|
|
1153
1878
|
toast,
|
|
1879
|
+
dataTable,
|
|
1154
1880
|
i18n: { t, getLocale, setLocale, addMessages, messages: MESSAGES },
|
|
1155
1881
|
csrf: getCsrfToken,
|
|
1156
1882
|
theme: { get: getTheme, set: setTheme, toggle: toggleTheme },
|