@nice2dev/ui 1.4.6 → 1.4.8

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.
@@ -0,0 +1,958 @@
1
+ import { jsxs as p, jsx as s } from "react/jsx-runtime";
2
+ import { useNiceTranslation as re, useControllableState as _e, useRovingFocus as De, NiceIcon as A } from "@nice2dev/ui-core";
3
+ import { useState as x, useRef as ne, useMemo as ie } from "react";
4
+ import { NiceSelect as ve } from "../editors/inputs/NiceSelect.mjs";
5
+ import { NiceTabs as we } from "../navigation/NiceTabs.mjs";
6
+ import { moduleDataSourceTable as ke } from "./moduleDataSource.mjs";
7
+ import { emptyQueryDesign as Ce, suggestJoins as Ie, relatedTables as Ne, designId as W, NICE_SQL_DIALECTS as $e, addRelatedTable as Te, pickColumns as me, filterRuleCount as Pe, rootTableId as be, DESIGN_CARD as T, addTableInstance as Se, tableHandleOf as V, removeTable as Le, emptyFilterGroup as Me } from "./queryDesign.mjs";
8
+ import { ColumnsPanel as je, FilterPanel as Ae, GroupsPanel as Ee, JoinsPanel as Re, ShapePanel as ze, ParametersPanel as Fe, SqlPanel as Be, PreviewPanel as Je } from "./queryDesignerPanels.mjs";
9
+ import { generateQuerySql as Qe } from "./querySql.mjs";
10
+ /* empty css */
11
+ const ge = [], xe = [], Ue = {
12
+ postgresql: "PostgreSQL",
13
+ sqlserver: "SQL Server",
14
+ mysql: "MySQL",
15
+ sqlite: "SQLite",
16
+ oracle: "Oracle"
17
+ };
18
+ function sn({
19
+ value: q,
20
+ defaultValue: P,
21
+ onChange: F,
22
+ schemas: M = ge,
23
+ extraSchemas: R = ge,
24
+ sources: E = xe,
25
+ dialects: w = $e,
26
+ onPreview: U,
27
+ readOnly: B = !1,
28
+ height: $ = 640,
29
+ className: _,
30
+ style: S,
31
+ id: X,
32
+ "data-testid": j
33
+ }) {
34
+ const { t: r } = re(), [h, k] = _e({
35
+ value: q,
36
+ defaultValue: P ?? {
37
+ key: "source1",
38
+ kind: "query",
39
+ query: Ce(),
40
+ sql: ""
41
+ },
42
+ onChange: F
43
+ }), [N, J] = x({ past: [], future: [] }), [D, d] = x({}), [L, a] = x("columns"), [z, m] = x(!1), e = ne(null), l = ne(null);
44
+ De(l, {
45
+ orientation: "horizontal",
46
+ itemSelector: ".nice-query-designer__tool"
47
+ });
48
+ const t = h.query, o = h.dialect ?? "postgresql", i = h.shape ?? "list", y = ie(
49
+ () => E.filter((n) => n.key !== h.key).map((n) => ke(n, { schemas: [...M, ...R], sources: E })),
50
+ [E, h.key, M, R]
51
+ ), f = ie(
52
+ () => [...M, ...R, ...y],
53
+ [M, R, y]
54
+ ), C = ie(
55
+ () => Object.fromEntries(
56
+ E.filter((n) => "sql" in n && typeof n.sql == "string").map((n) => [n.key, n.sql])
57
+ ),
58
+ [E]
59
+ ), I = (n) => {
60
+ B || (J((c) => ({ past: [...c.past.slice(-49), h], future: [] })), k({
61
+ ...n,
62
+ sql: Qe(n.query, {
63
+ dialect: n.dialect ?? "postgresql",
64
+ shape: n.shape ?? "list",
65
+ sources: C
66
+ })
67
+ }));
68
+ }, g = (n) => I({ ...h, query: n }), Y = () => {
69
+ const n = N.past[N.past.length - 1];
70
+ n && (J({ past: N.past.slice(0, -1), future: [h, ...N.future] }), k(n));
71
+ }, K = () => {
72
+ const n = N.future[0];
73
+ n && (J({ past: [...N.past, h], future: N.future.slice(1) }), k(n));
74
+ }, u = t.tables.find((n) => n.id === D.tableId), Z = (n) => g(me(t, n, u?.id)), pe = (n, c) => {
75
+ const v = Se(t, n, { select: c });
76
+ g(v.model), d({ tableId: v.tableId });
77
+ }, ae = (n, c, v) => g({
78
+ ...t,
79
+ tables: t.tables.map(
80
+ (G) => G.id !== n ? G : {
81
+ ...G,
82
+ columns: G.columns.map(
83
+ (Q) => Q.name === c ? { ...Q, selected: v ?? !Q.selected } : Q
84
+ )
85
+ }
86
+ )
87
+ }), oe = (n) => {
88
+ if (!u || !D.column)
89
+ return;
90
+ const c = t.sorts.filter(
91
+ (v) => !(v.tableId === u.id && v.column === D.column)
92
+ );
93
+ g({
94
+ ...t,
95
+ sorts: [
96
+ ...c,
97
+ { id: W("s"), tableId: u.id, column: D.column, direction: n }
98
+ ]
99
+ });
100
+ }, ce = (n) => {
101
+ !u || !D.column || g({
102
+ ...t,
103
+ tables: t.tables.map(
104
+ (c) => c.id !== u.id ? c : {
105
+ ...c,
106
+ columns: c.columns.map(
107
+ (v) => v.name === D.column ? {
108
+ ...v,
109
+ selected: !0,
110
+ aggregate: v.aggregate === n ? void 0 : n
111
+ } : v
112
+ )
113
+ }
114
+ )
115
+ });
116
+ }, he = () => {
117
+ if (!u || !D.column) {
118
+ a("groups");
119
+ return;
120
+ }
121
+ const n = t.groupBy.some((c) => c.tableId === u.id && c.column === D.column);
122
+ g({
123
+ ...t,
124
+ groupBy: n ? t.groupBy.filter((c) => !(c.tableId === u.id && c.column === D.column)) : [...t.groupBy, { tableId: u.id, column: D.column }]
125
+ }), a("groups");
126
+ }, ye = () => {
127
+ const n = t.filter ?? Me();
128
+ u && D.column && g({
129
+ ...t,
130
+ conditions: [],
131
+ filter: {
132
+ ...n,
133
+ items: [
134
+ ...n.items,
135
+ {
136
+ kind: "rule",
137
+ id: W("r"),
138
+ left: { kind: "column", tableId: u.id, column: D.column },
139
+ operator: "eq",
140
+ right: { kind: "value", value: "" }
141
+ }
142
+ ]
143
+ }
144
+ }), a("filter");
145
+ }, fe = () => {
146
+ const n = (t.computed?.length ?? 0) + 1;
147
+ g({
148
+ ...t,
149
+ computed: [
150
+ ...t.computed ?? [],
151
+ {
152
+ id: W("c"),
153
+ alias: `Computed${n}`,
154
+ fn: "concat",
155
+ args: u && D.column ? [
156
+ { kind: "column", tableId: u.id, column: D.column },
157
+ { kind: "value", value: "" }
158
+ ] : [
159
+ { kind: "value", value: "" },
160
+ { kind: "value", value: "" }
161
+ ],
162
+ selected: !0
163
+ }
164
+ ]
165
+ }), a("columns");
166
+ }, qe = () => {
167
+ const n = be(t), c = new Map(n ? [[n, 0]] : []);
168
+ let v = !0;
169
+ for (; v; ) {
170
+ v = !1;
171
+ for (const O of t.joins)
172
+ c.has(O.leftTableId) && !c.has(O.rightTableId) && (c.set(O.rightTableId, c.get(O.leftTableId) + 1), v = !0);
173
+ }
174
+ const G = /* @__PURE__ */ new Map(), Q = t.tables.map((O) => {
175
+ const se = c.get(O.id) ?? Math.max(0, ...c.values()) + 1, ue = G.get(se) ?? 0;
176
+ return G.set(se, ue + 1), {
177
+ ...O,
178
+ position: {
179
+ x: 24 + se * (T.width + T.gapX),
180
+ y: 24 + ue * (T.header + 9 * T.row + T.gapY)
181
+ }
182
+ };
183
+ });
184
+ g({ ...t, tables: Q });
185
+ }, de = Ie(t, f), te = u ? Ne(t, u.id, f) : [], b = (n, c, v, G, Q = {}) => /* @__PURE__ */ s(
186
+ "button",
187
+ {
188
+ type: "button",
189
+ className: `nice-query-designer__tool${Q.pressed ? " nice-query-designer__tool--on" : ""}`,
190
+ title: v,
191
+ "aria-label": v,
192
+ "aria-pressed": Q.pressed,
193
+ disabled: B || Q.disabled,
194
+ onClick: G,
195
+ "data-tool": n,
196
+ children: /* @__PURE__ */ s(A, { name: c, size: 16 })
197
+ },
198
+ n
199
+ ), ee = !u || !D.column, H = {
200
+ def: h,
201
+ commit: I,
202
+ setQuery: g,
203
+ allSchemas: f,
204
+ readOnly: B,
205
+ dialects: w,
206
+ onPreview: U,
207
+ sourceSql: C,
208
+ picked: D,
209
+ onSelectPart: d
210
+ };
211
+ return /* @__PURE__ */ p(
212
+ "div",
213
+ {
214
+ id: X,
215
+ className: `nice-query-designer${_ ? ` ${_}` : ""}`,
216
+ style: { height: $, ...S },
217
+ "data-testid": j,
218
+ children: [
219
+ /* @__PURE__ */ p(
220
+ "div",
221
+ {
222
+ ref: l,
223
+ className: "nice-query-designer__toolbar",
224
+ role: "toolbar",
225
+ "aria-label": r("queryDesigner.tools", "Designer tools"),
226
+ children: [
227
+ /* @__PURE__ */ p("div", { className: "nice-query-designer__tools", children: [
228
+ b(
229
+ "add",
230
+ "plus",
231
+ r("queryDesigner.tool.add", "Add a table"),
232
+ () => e.current?.focus()
233
+ ),
234
+ b(
235
+ "autoJoin",
236
+ "link",
237
+ r("queryDesigner.tool.autoJoin", "Join along foreign keys"),
238
+ () => g({
239
+ ...t,
240
+ joins: [...t.joins, ...de.map((n) => ({ ...n.join, id: W("j") }))]
241
+ }),
242
+ { disabled: de.length === 0 }
243
+ ),
244
+ b(
245
+ "related",
246
+ "git-branch",
247
+ r("queryDesigner.tool.related", "Add a related table"),
248
+ () => m((n) => !n),
249
+ { disabled: te.length === 0, pressed: z }
250
+ ),
251
+ b(
252
+ "remove",
253
+ "trash",
254
+ r("queryDesigner.tool.remove", "Remove the table"),
255
+ () => {
256
+ u && (g(Le(t, u.id)), d({}));
257
+ },
258
+ { disabled: !u }
259
+ )
260
+ ] }),
261
+ /* @__PURE__ */ p("div", { className: "nice-query-designer__tools", children: [
262
+ b(
263
+ "all",
264
+ "check-square",
265
+ r("queryDesigner.tool.all", "Pick every column"),
266
+ () => Z("all")
267
+ ),
268
+ b(
269
+ "none",
270
+ "square",
271
+ r("queryDesigner.tool.none", "Pick no column"),
272
+ () => Z("none")
273
+ ),
274
+ b(
275
+ "invert",
276
+ "repeat",
277
+ r("queryDesigner.tool.invert", "Invert the picked columns"),
278
+ () => Z("invert")
279
+ ),
280
+ b(
281
+ "keys",
282
+ "hash",
283
+ r("queryDesigner.tool.keys", "Pick the key columns"),
284
+ () => Z("keys")
285
+ )
286
+ ] }),
287
+ /* @__PURE__ */ p("div", { className: "nice-query-designer__tools", children: [
288
+ b(
289
+ "filter",
290
+ "filter",
291
+ r("queryDesigner.tool.filter", "Filter by the picked column"),
292
+ ye
293
+ ),
294
+ b(
295
+ "sortAsc",
296
+ "sort-asc",
297
+ r("queryDesigner.tool.sortAsc", "Sort ascending"),
298
+ () => oe("ASC"),
299
+ { disabled: ee }
300
+ ),
301
+ b(
302
+ "sortDesc",
303
+ "sort-desc",
304
+ r("queryDesigner.tool.sortDesc", "Sort descending"),
305
+ () => oe("DESC"),
306
+ { disabled: ee }
307
+ ),
308
+ b(
309
+ "group",
310
+ "group",
311
+ r("queryDesigner.tool.group", "Group by the picked column"),
312
+ he
313
+ ),
314
+ b(
315
+ "sum",
316
+ "bar-chart",
317
+ r("queryDesigner.tool.sum", "Sum the picked column"),
318
+ () => ce("SUM"),
319
+ { disabled: ee }
320
+ ),
321
+ b(
322
+ "count",
323
+ "list-ordered",
324
+ r("queryDesigner.tool.count", "Count the picked column"),
325
+ () => ce("COUNT"),
326
+ { disabled: ee }
327
+ ),
328
+ b(
329
+ "computed",
330
+ "code",
331
+ r("queryDesigner.tool.computed", "Add a computed column"),
332
+ fe
333
+ ),
334
+ b(
335
+ "distinct",
336
+ "layers",
337
+ r("queryDesigner.tool.distinct", "Drop repeated rows"),
338
+ () => g({ ...t, distinct: !t.distinct }),
339
+ { pressed: t.distinct }
340
+ )
341
+ ] }),
342
+ /* @__PURE__ */ p("div", { className: "nice-query-designer__tools", children: [
343
+ b(
344
+ "shapeFlat",
345
+ "table",
346
+ r("queryDesigner.shape.flat", "Plain rows"),
347
+ () => I({ ...h, shape: "flat" }),
348
+ { pressed: i === "flat" }
349
+ ),
350
+ b(
351
+ "shapeList",
352
+ "list",
353
+ r("queryDesigner.shape.list", "Records with nested parts"),
354
+ () => I({ ...h, shape: "list" }),
355
+ { pressed: i === "list" }
356
+ ),
357
+ b(
358
+ "shapeObject",
359
+ "box",
360
+ r("queryDesigner.shape.object", "One record"),
361
+ () => I({ ...h, shape: "object" }),
362
+ { pressed: i === "object" }
363
+ )
364
+ ] }),
365
+ /* @__PURE__ */ p("div", { className: "nice-query-designer__tools", children: [
366
+ b("arrange", "grid", r("queryDesigner.tool.arrange", "Arrange the tables"), qe, {
367
+ disabled: t.tables.length < 2
368
+ }),
369
+ b("undo", "undo", r("queryDesigner.tool.undo", "Undo"), Y, {
370
+ disabled: N.past.length === 0
371
+ }),
372
+ b("redo", "redo", r("queryDesigner.tool.redo", "Redo"), K, {
373
+ disabled: N.future.length === 0
374
+ }),
375
+ b(
376
+ "sql",
377
+ "database",
378
+ r("queryDesigner.tool.sql", "Show the SQL"),
379
+ () => a("sql")
380
+ ),
381
+ U && b(
382
+ "preview",
383
+ "play",
384
+ r("queryDesigner.tool.preview", "Preview the rows"),
385
+ () => a("preview")
386
+ )
387
+ ] }),
388
+ /* @__PURE__ */ s("div", { className: "nice-query-designer__dialect", children: /* @__PURE__ */ s(
389
+ ve,
390
+ {
391
+ "aria-label": r("queryDesigner.dialect", "Database"),
392
+ value: o,
393
+ options: w.map((n) => ({ value: n, label: Ue[n] })),
394
+ onChange: (n) => I({ ...h, dialect: String(n) }),
395
+ disabled: B,
396
+ size: "sm"
397
+ }
398
+ ) })
399
+ ]
400
+ }
401
+ ),
402
+ z && te.length > 0 && /* @__PURE__ */ s(
403
+ "div",
404
+ {
405
+ className: "nice-query-designer__related",
406
+ role: "group",
407
+ "aria-label": r("queryDesigner.tool.related", "Add a related table"),
408
+ children: te.map((n, c) => /* @__PURE__ */ p(
409
+ "button",
410
+ {
411
+ type: "button",
412
+ className: "nice-query-designer__chip",
413
+ onClick: () => {
414
+ g(Te(t, n)), m(!1);
415
+ },
416
+ children: [
417
+ /* @__PURE__ */ s(A, { name: n.direction === "out" ? "box" : "list", size: 14 }),
418
+ n.table.name,
419
+ /* @__PURE__ */ s("span", { className: "nice-query-designer__muted", children: n.direction === "out" ? r("queryDesigner.related.object", "as one object") : r("queryDesigner.related.list", "as a list") })
420
+ ]
421
+ },
422
+ `${n.table.schema ?? ""}.${n.table.name}.${n.toColumn}.${c}`
423
+ ))
424
+ }
425
+ ),
426
+ /* @__PURE__ */ p("div", { className: "nice-query-designer__main", children: [
427
+ /* @__PURE__ */ s(
428
+ Ye,
429
+ {
430
+ schemas: M,
431
+ extraSchemas: R,
432
+ sourceTables: y,
433
+ query: t,
434
+ searchRef: e,
435
+ readOnly: B,
436
+ onAdd: pe,
437
+ onToggleColumn: ae,
438
+ onPickTable: (n) => d({ tableId: n }),
439
+ onSetColumns: (n, c, v) => g(me(t, { names: c, selected: v }, n))
440
+ }
441
+ ),
442
+ /* @__PURE__ */ s(
443
+ Ge,
444
+ {
445
+ query: t,
446
+ picked: D,
447
+ readOnly: B,
448
+ onSelectPart: d,
449
+ onQuery: g,
450
+ onToggleColumn: ae
451
+ }
452
+ )
453
+ ] }),
454
+ /* @__PURE__ */ s("div", { className: "nice-query-designer__panels", children: /* @__PURE__ */ s(
455
+ we,
456
+ {
457
+ activeTab: L,
458
+ onChange: a,
459
+ tabs: [
460
+ {
461
+ id: "columns",
462
+ label: r("queryDesigner.tab.columns", "Columns"),
463
+ content: /* @__PURE__ */ s(je, { ...H })
464
+ },
465
+ {
466
+ id: "filter",
467
+ label: r("queryDesigner.tab.filter", "Filter"),
468
+ badge: Pe(t.filter) || void 0,
469
+ content: /* @__PURE__ */ s(Ae, { ...H })
470
+ },
471
+ {
472
+ id: "groups",
473
+ label: r("queryDesigner.tab.groups", "Groups and totals"),
474
+ badge: t.groupBy.length || void 0,
475
+ content: /* @__PURE__ */ s(Ee, { ...H })
476
+ },
477
+ {
478
+ id: "joins",
479
+ label: r("queryDesigner.tab.joins", "Joins"),
480
+ badge: t.joins.length || void 0,
481
+ content: /* @__PURE__ */ s(Re, { ...H })
482
+ },
483
+ {
484
+ id: "shape",
485
+ label: r("queryDesigner.tab.shape", "Shape"),
486
+ content: /* @__PURE__ */ s(ze, { ...H })
487
+ },
488
+ {
489
+ id: "parameters",
490
+ label: r("queryDesigner.tab.parameters", "Parameters"),
491
+ content: /* @__PURE__ */ s(Fe, { ...H })
492
+ },
493
+ {
494
+ id: "sql",
495
+ label: r("queryDesigner.tab.sql", "SQL"),
496
+ content: /* @__PURE__ */ s(Be, { ...H })
497
+ },
498
+ ...U ? [
499
+ {
500
+ id: "preview",
501
+ label: r("queryDesigner.tab.preview", "Preview"),
502
+ content: /* @__PURE__ */ s(Je, { ...H })
503
+ }
504
+ ] : []
505
+ ]
506
+ }
507
+ ) })
508
+ ]
509
+ }
510
+ );
511
+ }
512
+ function Ye({
513
+ schemas: q,
514
+ extraSchemas: P,
515
+ sourceTables: F,
516
+ query: M,
517
+ searchRef: R,
518
+ readOnly: E,
519
+ onAdd: w,
520
+ onToggleColumn: U,
521
+ onPickTable: B,
522
+ onSetColumns: $
523
+ }) {
524
+ const { t: _ } = re(), [S, X] = x(""), [j, r] = x({}), h = ne(null), k = S.trim().toLowerCase(), N = (d) => !k || d.name.toLowerCase().includes(k) || d.columns.some((L) => L.name.toLowerCase().includes(k)), J = [
525
+ { id: "own", label: _("queryDesigner.browser.module", "This module"), tables: q },
526
+ ...[...new Set(P.map((d) => d.schema ?? ""))].map((d) => ({
527
+ id: `m:${d}`,
528
+ label: d || _("queryDesigner.browser.other", "Other modules"),
529
+ tables: P.filter((L) => (L.schema ?? "") === d)
530
+ })),
531
+ {
532
+ id: "sources",
533
+ label: _("queryDesigner.browser.sources", "Data sources"),
534
+ tables: F
535
+ }
536
+ ].filter((d) => d.tables.length > 0), D = (d) => M.tables.find((L) => L.name === d.name && (L.schema ?? "") === (d.schema ?? ""));
537
+ return /* @__PURE__ */ p(
538
+ "aside",
539
+ {
540
+ className: "nice-query-designer__browser",
541
+ "aria-label": _("queryDesigner.browser", "Database"),
542
+ children: [
543
+ /* @__PURE__ */ p("div", { className: "nice-query-designer__search", children: [
544
+ /* @__PURE__ */ s(A, { name: "search", size: 14 }),
545
+ /* @__PURE__ */ s(
546
+ "input",
547
+ {
548
+ ref: R,
549
+ type: "search",
550
+ value: S,
551
+ onChange: (d) => X(d.target.value),
552
+ placeholder: _("queryDesigner.browser.search", "Find a table or column"),
553
+ "aria-label": _("queryDesigner.browser.search", "Find a table or column")
554
+ }
555
+ )
556
+ ] }),
557
+ /* @__PURE__ */ p("div", { className: "nice-query-designer__browser-list", children: [
558
+ J.map((d) => {
559
+ const L = d.tables.filter(N);
560
+ return L.length === 0 ? null : /* @__PURE__ */ p("section", { className: "nice-query-designer__browser-group", children: [
561
+ /* @__PURE__ */ s("h4", { children: d.label }),
562
+ /* @__PURE__ */ s("ul", { children: L.map((a) => {
563
+ const z = `${d.id}|${a.schema ?? ""}|${a.name}`, m = D(a), e = m?.columns.filter((o) => o.selected).length ?? 0, l = j[z] ?? (k !== "" && !a.name.toLowerCase().includes(k)), t = k ? a.columns.filter(
564
+ (o) => a.name.toLowerCase().includes(k) || o.name.toLowerCase().includes(k)
565
+ ) : a.columns;
566
+ return /* @__PURE__ */ p("li", { className: "nice-query-designer__browser-table", children: [
567
+ /* @__PURE__ */ p("div", { className: "nice-query-designer__browser-row", children: [
568
+ /* @__PURE__ */ s(
569
+ "button",
570
+ {
571
+ type: "button",
572
+ className: "nice-query-designer__icon-btn",
573
+ "aria-expanded": l,
574
+ "aria-label": l ? _("queryDesigner.collapse", "Collapse") : _("queryDesigner.expand", "Expand"),
575
+ onClick: () => r((o) => ({ ...o, [z]: !l })),
576
+ children: /* @__PURE__ */ s(A, { name: l ? "chevron-down" : "chevron-right", size: 12 })
577
+ }
578
+ ),
579
+ /* @__PURE__ */ s(
580
+ "input",
581
+ {
582
+ type: "checkbox",
583
+ checked: !!m && e === a.columns.length && a.columns.length > 0,
584
+ ref: (o) => {
585
+ o && (o.indeterminate = !!m && e > 0 && e < a.columns.length);
586
+ },
587
+ disabled: E,
588
+ "aria-label": _(
589
+ "queryDesigner.browser.allOf",
590
+ "Every column of {table}"
591
+ ).replace("{table}", a.name),
592
+ onChange: (o) => m ? $(
593
+ m.id,
594
+ a.columns.map((i) => i.name),
595
+ o.target.checked
596
+ ) : w(a, "all")
597
+ }
598
+ ),
599
+ /* @__PURE__ */ p(
600
+ "button",
601
+ {
602
+ type: "button",
603
+ className: "nice-query-designer__browser-name",
604
+ title: a.description ?? a.name,
605
+ disabled: E && !m,
606
+ onClick: () => m ? B(m.id) : w(a, "none"),
607
+ children: [
608
+ /* @__PURE__ */ s(A, { name: d.id === "sources" ? "server" : "table", size: 14 }),
609
+ /* @__PURE__ */ s("span", { children: a.name })
610
+ ]
611
+ }
612
+ ),
613
+ /* @__PURE__ */ s("span", { className: "nice-query-designer__muted", children: m ? `${e}/${a.columns.length}` : a.columns.length }),
614
+ !E && // the same table once more, under its own name (a second address)
615
+ /* @__PURE__ */ s(
616
+ "button",
617
+ {
618
+ type: "button",
619
+ className: "nice-query-designer__icon-btn",
620
+ title: m ? _("queryDesigner.browser.addAgain", "Add it once more") : _("queryDesigner.browser.add", "Add it"),
621
+ "aria-label": `${m ? _("queryDesigner.browser.addAgain", "Add it once more") : _("queryDesigner.browser.add", "Add it")}: ${a.name}`,
622
+ onClick: () => w(a, "none"),
623
+ children: /* @__PURE__ */ s(A, { name: "plus", size: 12 })
624
+ }
625
+ )
626
+ ] }),
627
+ l && /* @__PURE__ */ s("ul", { className: "nice-query-designer__browser-columns", children: t.map((o, i) => {
628
+ const y = !!m?.columns.find((f) => f.name === o.name)?.selected;
629
+ return /* @__PURE__ */ s("li", { children: /* @__PURE__ */ p("label", { children: [
630
+ /* @__PURE__ */ s(
631
+ "input",
632
+ {
633
+ type: "checkbox",
634
+ checked: y,
635
+ disabled: E,
636
+ onChange: (f) => {
637
+ const C = f.nativeEvent, I = h.current;
638
+ if (C.shiftKey && I && I.table === z && m) {
639
+ const [g, Y] = [
640
+ Math.min(I.index, i),
641
+ Math.max(I.index, i)
642
+ ];
643
+ $(
644
+ m.id,
645
+ t.slice(g, Y + 1).map((K) => K.name),
646
+ f.target.checked
647
+ );
648
+ } else m ? U(m.id, o.name, f.target.checked) : w(a, [o.name]);
649
+ h.current = { table: z, index: i };
650
+ }
651
+ }
652
+ ),
653
+ o.isPrimaryKey && /* @__PURE__ */ s(A, { name: "lock", size: 12 }),
654
+ o.references && /* @__PURE__ */ s(A, { name: "link", size: 12 }),
655
+ /* @__PURE__ */ s("span", { children: o.name }),
656
+ /* @__PURE__ */ s("span", { className: "nice-query-designer__muted", children: o.dataType })
657
+ ] }) }, o.name);
658
+ }) })
659
+ ] }, z);
660
+ }) })
661
+ ] }, d.id);
662
+ }),
663
+ J.length === 0 && /* @__PURE__ */ s("p", { className: "nice-query-designer__muted", children: _("queryDesigner.browser.empty", "No tables: the module has no entities yet.") })
664
+ ] })
665
+ ]
666
+ }
667
+ );
668
+ }
669
+ const le = (q, P) => P ? q.columns.filter((F) => F.selected) : q.columns;
670
+ function Ge({
671
+ query: q,
672
+ picked: P,
673
+ readOnly: F,
674
+ onSelectPart: M,
675
+ onQuery: R,
676
+ onToggleColumn: E
677
+ }) {
678
+ const { t: w } = re(), [U, B] = x({}), [$, _] = x(null), [S, X] = x(null), [j, r] = x(null), h = ne(null), k = (e) => $?.id === e.id ? { x: $.x, y: $.y } : e.position, N = new Map(q.tables.map((e) => [e.id, e])), J = (e, l) => {
679
+ const o = le(e, !!U[e.id]).findIndex((y) => y.name === l), i = k(e);
680
+ return o < 0 ? i.y + T.header / 2 : i.y + T.header + o * T.row + T.row / 2;
681
+ }, D = (e) => T.header + Math.max(1, le(e, !!U[e.id]).length) * T.row + 8, d = Math.max(640, ...q.tables.map((e) => k(e).x + T.width + 120)), L = Math.max(320, ...q.tables.map((e) => k(e).y + D(e) + 80)), a = (e) => {
682
+ const l = N.get(e.leftTableId), t = N.get(e.rightTableId);
683
+ if (!l || !t)
684
+ return null;
685
+ const o = k(l), i = k(t), y = J(l, e.leftColumn), f = J(t, e.rightColumn), C = o.x + T.width, I = i.x + T.width;
686
+ let g, Y, K, u;
687
+ return i.x >= C ? [g, Y, K, u] = [C, i.x, C + 40, i.x - 40] : I <= o.x ? [g, Y, K, u] = [o.x, I, o.x - 40, I + 40] : [g, Y, K, u] = [
688
+ C,
689
+ I,
690
+ Math.max(C, I) + 50,
691
+ Math.max(C, I) + 50
692
+ ], {
693
+ d: `M ${g} ${y} C ${K} ${y}, ${u} ${f}, ${Y} ${f}`,
694
+ mx: (g + Y) / 2,
695
+ my: (y + f) / 2
696
+ };
697
+ }, z = (e, l) => {
698
+ e.tableId !== l.tableId && R({
699
+ ...q,
700
+ joins: [
701
+ ...q.joins,
702
+ {
703
+ id: W("j"),
704
+ type: "LEFT",
705
+ leftTableId: e.tableId,
706
+ leftColumn: e.column,
707
+ rightTableId: l.tableId,
708
+ rightColumn: l.column,
709
+ nest: "flat"
710
+ }
711
+ ]
712
+ });
713
+ }, m = (e) => {
714
+ const l = h.current?.getBoundingClientRect(), t = h.current;
715
+ return {
716
+ x: e.clientX - (l?.left ?? 0) + (t?.scrollLeft ?? 0),
717
+ y: e.clientY - (l?.top ?? 0) + (t?.scrollTop ?? 0)
718
+ };
719
+ };
720
+ return /* @__PURE__ */ s(
721
+ "div",
722
+ {
723
+ ref: h,
724
+ className: "nice-query-designer__canvas",
725
+ role: "group",
726
+ "aria-label": w("queryDesigner.canvas", "Tables of the query"),
727
+ onPointerMove: (e) => {
728
+ if ($) {
729
+ const l = m(e);
730
+ _({ ...$, x: Math.max(0, l.x - $.dx), y: Math.max(0, l.y - $.dy) });
731
+ } else if (S) {
732
+ const l = m(e);
733
+ X({ ...S, x: l.x, y: l.y });
734
+ }
735
+ },
736
+ onPointerUp: (e) => {
737
+ if ($)
738
+ R({
739
+ ...q,
740
+ tables: q.tables.map(
741
+ (l) => l.id === $.id ? { ...l, position: { x: $.x, y: $.y } } : l
742
+ )
743
+ }), _(null);
744
+ else if (S) {
745
+ const l = document.elementFromPoint?.(e.clientX, e.clientY)?.closest("[data-column]");
746
+ l?.dataset.table && l.dataset.column && z(S, { tableId: l.dataset.table, column: l.dataset.column }), X(null);
747
+ } else (e.target === h.current || e.target.classList?.contains("nice-query-designer__surface")) && M({});
748
+ },
749
+ children: /* @__PURE__ */ p("div", { className: "nice-query-designer__surface", style: { width: d, height: L }, children: [
750
+ /* @__PURE__ */ p(
751
+ "svg",
752
+ {
753
+ className: "nice-query-designer__lines",
754
+ width: d,
755
+ height: L,
756
+ "aria-hidden": "true",
757
+ children: [
758
+ q.joins.map((e) => {
759
+ const l = a(e);
760
+ return l ? /* @__PURE__ */ s(
761
+ "path",
762
+ {
763
+ d: l.d,
764
+ className: `nice-query-designer__line nice-query-designer__line--${e.nest ?? "flat"}${P.joinId === e.id ? " nice-query-designer__line--picked" : ""}`
765
+ },
766
+ e.id
767
+ ) : null;
768
+ }),
769
+ S && (() => {
770
+ const e = N.get(S.tableId);
771
+ if (!e)
772
+ return null;
773
+ const l = k(e), t = J(e, S.column);
774
+ return /* @__PURE__ */ s(
775
+ "path",
776
+ {
777
+ d: `M ${l.x + T.width} ${t} L ${S.x} ${S.y}`,
778
+ className: "nice-query-designer__line nice-query-designer__line--draft"
779
+ }
780
+ );
781
+ })()
782
+ ]
783
+ }
784
+ ),
785
+ q.joins.map((e) => {
786
+ const l = a(e);
787
+ return l ? /* @__PURE__ */ s(
788
+ "button",
789
+ {
790
+ type: "button",
791
+ className: `nice-query-designer__join${P.joinId === e.id ? " nice-query-designer__join--picked" : ""}`,
792
+ style: { left: l.mx - 12, top: l.my - 12 },
793
+ title: w("queryDesigner.join.edit", "Edit the join"),
794
+ "aria-label": `${w("queryDesigner.join.edit", "Edit the join")}: ${V(N.get(e.leftTableId) ?? { name: "" })}.${e.leftColumn} = ${V(N.get(e.rightTableId) ?? { name: "" })}.${e.rightColumn}`,
795
+ onClick: () => M({ joinId: e.id }),
796
+ children: /* @__PURE__ */ s(
797
+ A,
798
+ {
799
+ name: e.nest === "list" ? "list" : e.nest === "object" ? "box" : "link",
800
+ size: 12
801
+ }
802
+ )
803
+ },
804
+ e.id
805
+ ) : null;
806
+ }),
807
+ q.tables.map((e) => {
808
+ const l = k(e), t = !!U[e.id], o = be(q) === e.id;
809
+ return /* @__PURE__ */ p(
810
+ "div",
811
+ {
812
+ className: `nice-query-designer__card${P.tableId === e.id ? " nice-query-designer__card--picked" : ""}`,
813
+ style: { left: l.x, top: l.y, width: T.width },
814
+ children: [
815
+ /* @__PURE__ */ p(
816
+ "div",
817
+ {
818
+ className: "nice-query-designer__card-head",
819
+ tabIndex: 0,
820
+ role: "button",
821
+ "aria-label": `${V(e)}${o ? ` (${w("queryDesigner.root", "from")})` : ""}`,
822
+ "aria-pressed": P.tableId === e.id && !P.column,
823
+ onClick: () => M({ tableId: e.id }),
824
+ onKeyDown: (i) => {
825
+ const y = i.shiftKey ? 40 : 10, f = {
826
+ ArrowLeft: [-y, 0],
827
+ ArrowRight: [y, 0],
828
+ ArrowUp: [0, -y],
829
+ ArrowDown: [0, y]
830
+ }[i.key];
831
+ f && !F ? (i.preventDefault(), R({
832
+ ...q,
833
+ tables: q.tables.map(
834
+ (C) => C.id === e.id ? {
835
+ ...C,
836
+ position: {
837
+ x: Math.max(0, C.position.x + f[0]),
838
+ y: Math.max(0, C.position.y + f[1])
839
+ }
840
+ } : C
841
+ )
842
+ })) : (i.key === "Enter" || i.key === " ") && (i.preventDefault(), M({ tableId: e.id }));
843
+ },
844
+ onPointerDown: (i) => {
845
+ if (F || i.target.closest("button"))
846
+ return;
847
+ const y = m(i);
848
+ _({
849
+ id: e.id,
850
+ dx: y.x - e.position.x,
851
+ dy: y.y - e.position.y,
852
+ x: e.position.x,
853
+ y: e.position.y
854
+ });
855
+ },
856
+ children: [
857
+ /* @__PURE__ */ s(A, { name: e.schema === "source" ? "server" : "table", size: 14 }),
858
+ /* @__PURE__ */ s(
859
+ "span",
860
+ {
861
+ className: "nice-query-designer__card-title",
862
+ title: e.alias ? `${e.name} (${e.alias})` : e.name,
863
+ children: V(e)
864
+ }
865
+ ),
866
+ o && /* @__PURE__ */ s("span", { className: "nice-query-designer__badge", children: w("queryDesigner.root", "from") }),
867
+ /* @__PURE__ */ s(
868
+ "button",
869
+ {
870
+ type: "button",
871
+ className: "nice-query-designer__icon-btn",
872
+ title: t ? w("queryDesigner.card.unfold", "Show every column") : w("queryDesigner.card.fold", "Show only the picked columns"),
873
+ "aria-label": t ? w("queryDesigner.card.unfold", "Show every column") : w("queryDesigner.card.fold", "Show only the picked columns"),
874
+ "aria-pressed": t,
875
+ onClick: () => B((i) => ({ ...i, [e.id]: !t })),
876
+ children: /* @__PURE__ */ s(A, { name: t ? "eye-off" : "eye", size: 12 })
877
+ }
878
+ )
879
+ ]
880
+ }
881
+ ),
882
+ /* @__PURE__ */ s("ul", { className: "nice-query-designer__card-columns", children: le(e, t).map((i) => {
883
+ const y = P.tableId === e.id && P.column === i.name;
884
+ return /* @__PURE__ */ p(
885
+ "li",
886
+ {
887
+ className: `nice-query-designer__col${y ? " nice-query-designer__col--picked" : ""}${i.selected ? " nice-query-designer__col--on" : ""}`,
888
+ "data-table": e.id,
889
+ "data-column": i.name,
890
+ children: [
891
+ /* @__PURE__ */ s(
892
+ "input",
893
+ {
894
+ type: "checkbox",
895
+ checked: i.selected,
896
+ disabled: F,
897
+ "aria-label": `${V(e)}.${i.name}`,
898
+ onChange: (f) => E(e.id, i.name, f.target.checked)
899
+ }
900
+ ),
901
+ /* @__PURE__ */ p(
902
+ "button",
903
+ {
904
+ type: "button",
905
+ className: "nice-query-designer__col-name",
906
+ "aria-pressed": y,
907
+ onClick: () => M({ tableId: e.id, column: i.name }),
908
+ children: [
909
+ i.isPrimaryKey && /* @__PURE__ */ s(A, { name: "lock", size: 10 }),
910
+ /* @__PURE__ */ s("span", { children: i.name }),
911
+ i.alias && /* @__PURE__ */ s("span", { className: "nice-query-designer__muted", children: i.alias })
912
+ ]
913
+ }
914
+ ),
915
+ i.aggregate && /* @__PURE__ */ s("span", { className: "nice-query-designer__badge", children: i.aggregate }),
916
+ !F && /* @__PURE__ */ s(
917
+ "button",
918
+ {
919
+ type: "button",
920
+ className: `nice-query-designer__dot${j?.tableId === e.id && j.column === i.name ? " nice-query-designer__dot--on" : ""}`,
921
+ title: w(
922
+ "queryDesigner.col.join",
923
+ "Join from this column (drag to a column, or pick one then another)"
924
+ ),
925
+ "aria-label": `${w("queryDesigner.col.joinShort", "Join from")} ${V(e)}.${i.name}`,
926
+ "aria-pressed": j?.tableId === e.id && j.column === i.name,
927
+ onPointerDown: (f) => {
928
+ f.stopPropagation();
929
+ const C = m(f);
930
+ X({ tableId: e.id, column: i.name, x: C.x, y: C.y });
931
+ },
932
+ onClick: () => {
933
+ j && (j.tableId !== e.id || j.column !== i.name) ? (z(j, { tableId: e.id, column: i.name }), r(null)) : r(j ? null : { tableId: e.id, column: i.name });
934
+ },
935
+ children: /* @__PURE__ */ s(A, { name: "link", size: 10 })
936
+ }
937
+ )
938
+ ]
939
+ },
940
+ i.name
941
+ );
942
+ }) })
943
+ ]
944
+ },
945
+ e.id
946
+ );
947
+ }),
948
+ q.tables.length === 0 && /* @__PURE__ */ s("p", { className: "nice-query-designer__hint", children: w(
949
+ "queryDesigner.canvas.empty",
950
+ "Tick a table on the left to take all its columns, or click its name to add it empty."
951
+ ) })
952
+ ] })
953
+ }
954
+ );
955
+ }
956
+ export {
957
+ sn as NiceQueryDesigner
958
+ };