@olenbetong/synergi-react 2.9.0 → 3.0.0
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/es/chrome/AppShell/index.d.ts +8 -0
- package/es/chrome/AppShell/index.js +10 -130
- package/es/components/ImageEditor/ImageEditor.css +10 -0
- package/es/components/ImageEditor/index.d.ts +1 -0
- package/es/components/ImageEditor/index.js +1 -0
- package/es/components/ImageEditor/plugins/cropPlugin/CropOverlay.js +27 -5
- package/es/components/ImageEditor/plugins/cropPlugin/CropToolbar.js +4 -3
- package/es/components/ImageEditor/plugins/cropPlugin/useCropTool.d.ts +20 -3
- package/es/components/ImageEditor/plugins/cropPlugin/useCropTool.js +216 -33
- package/es/components/ImageEditor/plugins/zoomPlugin/ZoomContext.d.ts +5 -0
- package/es/components/ImageEditor/plugins/zoomPlugin/ZoomContext.js +7 -0
- package/es/components/ImageEditor/useEdgeDetection.d.ts +0 -2
- package/es/components/ImageEditor/useEdgeDetection.js +11 -30
- package/es/components/ImageEditor/useOpenCV.d.ts +26 -1
- package/es/components/ImageEditor/useOpenCV.js +108 -42
- package/es/components/SplitContainer/index.js +2 -4
- package/es/filter/ChipListInput.d.ts +13 -0
- package/es/filter/ChipListInput.js +34 -0
- package/es/filter/FilterBuilder.css +152 -0
- package/es/filter/FilterBuilder.d.ts +115 -0
- package/es/filter/FilterBuilder.js +146 -0
- package/es/filter/FilterBuilder.test.d.ts +1 -0
- package/es/filter/FilterBuilder.test.js +120 -0
- package/es/filter/FilterEditor.css +231 -0
- package/es/filter/FilterEditor.d.ts +17 -0
- package/es/filter/FilterEditor.js +38 -0
- package/es/filter/FilterEditor.test.d.ts +1 -0
- package/es/filter/FilterEditor.test.js +183 -0
- package/es/filter/FilterGroupEditor.d.ts +22 -0
- package/es/filter/FilterGroupEditor.js +34 -0
- package/es/filter/FilterNameDialog.css +22 -0
- package/es/filter/FilterNameDialog.d.ts +18 -0
- package/es/filter/FilterNameDialog.js +32 -0
- package/es/filter/FilterRow.d.ts +14 -0
- package/es/filter/FilterRow.js +32 -0
- package/es/filter/FilterShareDialog.css +28 -0
- package/es/filter/FilterShareDialog.d.ts +23 -0
- package/es/filter/FilterShareDialog.js +63 -0
- package/es/filter/FilterStringField.d.ts +15 -0
- package/es/filter/FilterStringField.js +37 -0
- package/es/filter/FilterValueEditor.d.ts +16 -0
- package/es/filter/FilterValueEditor.js +85 -0
- package/es/filter/SavedFilterTree.css +107 -0
- package/es/filter/SavedFilterTree.d.ts +45 -0
- package/es/filter/SavedFilterTree.js +44 -0
- package/es/filter/SavedFilterTree.test.d.ts +1 -0
- package/es/filter/SavedFilterTree.test.js +75 -0
- package/es/filter/index.d.ts +11 -0
- package/es/filter/index.js +10 -0
- package/es/filter/types.d.ts +34 -0
- package/es/filter/types.js +1 -0
- package/es/index.d.ts +2 -1
- package/es/index.js +6 -1
- package/es/mui/MuiThemeProvider.d.ts +24 -0
- package/es/mui/MuiThemeProvider.js +152 -0
- package/es/mui/index.d.ts +1 -0
- package/es/mui/index.js +1 -0
- package/es/report/ReportLauncher.css +147 -0
- package/es/report/ReportLauncher.d.ts +55 -0
- package/es/report/ReportLauncher.js +142 -0
- package/es/report/ReportLauncher.test.d.ts +1 -0
- package/es/report/ReportLauncher.test.js +231 -0
- package/es/report/ReportParameters.css +29 -0
- package/es/report/ReportParameters.d.ts +46 -0
- package/es/report/ReportParameters.js +65 -0
- package/es/report/ReportParameters.test.d.ts +1 -0
- package/es/report/ReportParameters.test.js +84 -0
- package/es/report/index.d.ts +2 -0
- package/es/report/index.js +2 -0
- package/es/test/setup.d.ts +1 -0
- package/es/test/setup.js +6 -0
- package/package.json +22 -7
- package/src/chrome/AppShell/index.tsx +34 -171
- package/src/components/ImageEditor/ImageEditor.css +10 -0
- package/src/components/ImageEditor/index.ts +1 -0
- package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +66 -20
- package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +19 -3
- package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +266 -39
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +8 -0
- package/src/components/ImageEditor/useEdgeDetection.ts +11 -51
- package/src/components/ImageEditor/useOpenCV.ts +112 -62
- package/src/components/SplitContainer/index.tsx +3 -4
- package/src/filter/ChipListInput.tsx +79 -0
- package/src/filter/FilterBuilder.css +152 -0
- package/src/filter/FilterBuilder.test.tsx +153 -0
- package/src/filter/FilterBuilder.tsx +435 -0
- package/src/filter/FilterEditor.css +231 -0
- package/src/filter/FilterEditor.test.tsx +259 -0
- package/src/filter/FilterEditor.tsx +74 -0
- package/src/filter/FilterGroupEditor.tsx +160 -0
- package/src/filter/FilterNameDialog.css +22 -0
- package/src/filter/FilterNameDialog.tsx +81 -0
- package/src/filter/FilterRow.tsx +117 -0
- package/src/filter/FilterShareDialog.css +28 -0
- package/src/filter/FilterShareDialog.tsx +201 -0
- package/src/filter/FilterStringField.tsx +77 -0
- package/src/filter/FilterValueEditor.tsx +220 -0
- package/src/filter/SavedFilterTree.css +107 -0
- package/src/filter/SavedFilterTree.test.tsx +94 -0
- package/src/filter/SavedFilterTree.tsx +222 -0
- package/src/filter/index.ts +11 -0
- package/src/filter/types.ts +38 -0
- package/src/index.ts +39 -1
- package/src/mui/MuiThemeProvider.tsx +174 -0
- package/src/mui/index.ts +1 -0
- package/src/report/ReportLauncher.css +147 -0
- package/src/report/ReportLauncher.test.tsx +281 -0
- package/src/report/ReportLauncher.tsx +421 -0
- package/src/report/ReportParameters.css +29 -0
- package/src/report/ReportParameters.test.tsx +133 -0
- package/src/report/ReportParameters.tsx +198 -0
- package/src/report/index.ts +2 -0
- package/src/test/setup.ts +8 -0
|
@@ -1,23 +1,53 @@
|
|
|
1
1
|
import { useEffect, useState } from "react";
|
|
2
2
|
|
|
3
|
-
let loadPromise: Promise<unknown> | null = null;
|
|
4
|
-
|
|
5
3
|
declare global {
|
|
6
4
|
interface Window {
|
|
5
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
|
|
7
6
|
cv: any;
|
|
8
7
|
}
|
|
9
8
|
}
|
|
10
9
|
|
|
11
|
-
|
|
10
|
+
/**
|
|
11
|
+
* OpenCV.js builds to try, in order. Both are single-file builds with the WASM embedded,
|
|
12
|
+
* so no companion `.wasm` request is needed.
|
|
13
|
+
*
|
|
14
|
+
* Note: opencv.org only publishes rolling branch builds — pinned version paths such as
|
|
15
|
+
* `/4.10.0/opencv.js` were removed upstream and now return 404. `5.x` is the current
|
|
16
|
+
* development line and `4.x` the stable line; the 4.x entry is a fallback for the case
|
|
17
|
+
* where the 5.x build is temporarily broken or unavailable.
|
|
18
|
+
*/
|
|
19
|
+
export const DEFAULT_OPENCV_URLS = ["https://docs.opencv.org/5.x/opencv.js", "https://docs.opencv.org/4.x/opencv.js"];
|
|
20
|
+
|
|
21
|
+
/** Milliseconds to wait for the WASM runtime to initialise before giving up on a build. */
|
|
22
|
+
const INIT_TIMEOUT_MS = 60_000;
|
|
23
|
+
|
|
24
|
+
let scriptUrls = [...DEFAULT_OPENCV_URLS];
|
|
25
|
+
let loadPromise: Promise<void> | null = null;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Overrides which OpenCV.js build(s) to load — e.g. to self-host instead of using the CDN.
|
|
29
|
+
* Must be called before anything triggers a load; ignored once loading has started.
|
|
30
|
+
*/
|
|
31
|
+
export function configureOpenCV(urls: string | string[]) {
|
|
32
|
+
if (loadPromise) {
|
|
33
|
+
console.warn("[OpenCV] configureOpenCV() ignored — OpenCV is already loading or loaded");
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
scriptUrls = Array.isArray(urls) ? [...urls] : [urls];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function isReady() {
|
|
40
|
+
return typeof window.cv?.Mat !== "undefined";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Polls for cv.Mat, which signals that the WASM runtime has fully initialised. */
|
|
44
|
+
function waitForCvMat(timeoutMs: number): Promise<void> {
|
|
12
45
|
return new Promise((resolve, reject) => {
|
|
13
46
|
let elapsed = 0;
|
|
14
47
|
let interval = setInterval(() => {
|
|
15
|
-
|
|
16
|
-
// cv.Mat signals that the WASM runtime has fully initialised
|
|
17
|
-
if (window.cv?.Mat) {
|
|
48
|
+
if (isReady()) {
|
|
18
49
|
clearInterval(interval);
|
|
19
|
-
|
|
20
|
-
resolve(); // callers read window.cv directly
|
|
50
|
+
resolve();
|
|
21
51
|
return;
|
|
22
52
|
}
|
|
23
53
|
elapsed += 100;
|
|
@@ -29,76 +59,94 @@ function waitForCvMat(timeoutMs = 30_000): Promise<void> {
|
|
|
29
59
|
});
|
|
30
60
|
}
|
|
31
61
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
// Already initialised (e.g. hot-reload)
|
|
39
|
-
if (window.cv?.Mat) {
|
|
40
|
-
console.log("[OpenCV] already initialised on window.cv");
|
|
41
|
-
loadPromise = Promise.resolve(window.cv);
|
|
42
|
-
return loadPromise;
|
|
43
|
-
}
|
|
62
|
+
/** Loads a single OpenCV.js build, handling both the promise-based and polling Emscripten builds. */
|
|
63
|
+
function loadScript(url: string): Promise<void> {
|
|
64
|
+
return new Promise<void>((resolve, reject) => {
|
|
65
|
+
// A previous failed attempt may have left a half-initialised global behind.
|
|
66
|
+
if (!isReady()) window.cv = undefined;
|
|
44
67
|
|
|
45
|
-
console.log("[OpenCV] injecting script tag...");
|
|
46
|
-
loadPromise = new Promise<unknown>((resolve, reject) => {
|
|
47
68
|
let script = document.createElement("script");
|
|
48
|
-
script.src =
|
|
69
|
+
script.src = url;
|
|
49
70
|
script.async = true;
|
|
50
71
|
|
|
72
|
+
let settle = (err?: Error) => {
|
|
73
|
+
if (err) {
|
|
74
|
+
script.remove();
|
|
75
|
+
reject(err);
|
|
76
|
+
} else {
|
|
77
|
+
resolve();
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
51
81
|
script.onload = () => {
|
|
52
|
-
console.log(
|
|
53
|
-
"[OpenCV] script onload fired. typeof window.cv=",
|
|
54
|
-
typeof window.cv,
|
|
55
|
-
"cv?.then=",
|
|
56
|
-
typeof window.cv?.then,
|
|
57
|
-
"cv?.Mat=",
|
|
58
|
-
typeof window.cv?.Mat,
|
|
59
|
-
);
|
|
60
82
|
if (typeof window.cv?.then === "function") {
|
|
61
|
-
console.log("[OpenCV] promise-based build detected, awaiting cv...");
|
|
62
83
|
// Emscripten promise-based build: cv itself is the promise.
|
|
63
|
-
// IMPORTANT:
|
|
64
|
-
//
|
|
65
|
-
// Instead
|
|
84
|
+
// IMPORTANT: never resolve *with* the instance — if the resolved value is
|
|
85
|
+
// itself thenable, the promise chain follows it forever and never settles.
|
|
86
|
+
// Instead mutate window.cv and signal readiness with undefined.
|
|
66
87
|
(window.cv as Promise<unknown>).then(
|
|
67
88
|
(instance) => {
|
|
68
|
-
console.log(
|
|
69
|
-
"[OpenCV] promise resolved, instance=",
|
|
70
|
-
typeof instance,
|
|
71
|
-
"hasReadMethod=",
|
|
72
|
-
typeof (instance as any)?.imread,
|
|
73
|
-
);
|
|
74
89
|
window.cv = instance;
|
|
75
|
-
|
|
76
|
-
},
|
|
77
|
-
(err) => {
|
|
78
|
-
console.error("[OpenCV] promise rejected", err);
|
|
79
|
-
reject(err);
|
|
90
|
+
settle();
|
|
80
91
|
},
|
|
92
|
+
(err: Error) => settle(err instanceof Error ? err : new Error(String(err))),
|
|
81
93
|
);
|
|
94
|
+
} else if (isReady()) {
|
|
95
|
+
settle();
|
|
82
96
|
} else {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
waitForCvMat().then(() => resolve(undefined), reject);
|
|
97
|
+
// Polling build: the WASM initialises asynchronously after the script loads.
|
|
98
|
+
waitForCvMat(INIT_TIMEOUT_MS).then(() => settle(), settle);
|
|
86
99
|
}
|
|
87
100
|
};
|
|
88
101
|
|
|
89
|
-
script.onerror = (
|
|
90
|
-
console.error("[OpenCV] script load error", err);
|
|
91
|
-
loadPromise = null;
|
|
92
|
-
reject(new Error("Failed to load OpenCV.js"));
|
|
93
|
-
};
|
|
102
|
+
script.onerror = () => settle(new Error(`Failed to load OpenCV.js from ${url}`));
|
|
94
103
|
|
|
95
104
|
document.head.appendChild(script);
|
|
96
105
|
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Lazily loads OpenCV.js, trying each configured build in order.
|
|
110
|
+
* Rejects only when every candidate fails. Callers read `window.cv`.
|
|
111
|
+
*
|
|
112
|
+
* IMPORTANT: this promise must never resolve *with* the OpenCV module. Emscripten's
|
|
113
|
+
* module object has its own `.then` method, so it is thenable — resolving a promise
|
|
114
|
+
* with it makes the promise machinery try to adopt it and call `.then` forever, which
|
|
115
|
+
* wedges the whole tab in an infinite microtask loop. Always resolve with `undefined`
|
|
116
|
+
* and let callers read `window.cv`.
|
|
117
|
+
*/
|
|
118
|
+
export function loadOpenCV(): Promise<void> {
|
|
119
|
+
if (loadPromise) return loadPromise;
|
|
120
|
+
|
|
121
|
+
// Already initialised (e.g. hot-reload, or loaded by the host page)
|
|
122
|
+
if (isReady()) {
|
|
123
|
+
loadPromise = Promise.resolve();
|
|
124
|
+
return loadPromise;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
loadPromise = (async () => {
|
|
128
|
+
let errors: Error[] = [];
|
|
129
|
+
for (let url of scriptUrls) {
|
|
130
|
+
try {
|
|
131
|
+
await loadScript(url);
|
|
132
|
+
if (!isReady()) throw new Error(`OpenCV.js loaded from ${url} but exposes no cv.Mat`);
|
|
133
|
+
return; // never `return window.cv` — see the note above
|
|
134
|
+
} catch (err) {
|
|
135
|
+
let error = err instanceof Error ? err : new Error(String(err));
|
|
136
|
+
errors.push(error);
|
|
137
|
+
console.warn(`[OpenCV] Could not load ${url}:`, error.message);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// Allow a later retry (e.g. after the network comes back) rather than caching the failure.
|
|
141
|
+
loadPromise = null;
|
|
142
|
+
throw new Error(`Failed to load OpenCV.js from: ${scriptUrls.join(", ")}`, { cause: errors });
|
|
143
|
+
})();
|
|
97
144
|
|
|
98
145
|
return loadPromise;
|
|
99
146
|
}
|
|
100
147
|
|
|
101
148
|
interface UseOpenCVResult {
|
|
149
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
|
|
102
150
|
cv: any | null;
|
|
103
151
|
ready: boolean;
|
|
104
152
|
error: Error | null;
|
|
@@ -106,28 +154,30 @@ interface UseOpenCVResult {
|
|
|
106
154
|
|
|
107
155
|
/** Lazily loads OpenCV.js from CDN. Starts loading only when `enabled` is true. */
|
|
108
156
|
export function useOpenCV(enabled = true): UseOpenCVResult {
|
|
157
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
|
|
109
158
|
let [cv, setCv] = useState<any | null>(null);
|
|
110
159
|
let [ready, setReady] = useState(false);
|
|
111
160
|
let [error, setError] = useState<Error | null>(null);
|
|
112
161
|
|
|
113
162
|
useEffect(() => {
|
|
114
163
|
if (!enabled) return;
|
|
115
|
-
|
|
164
|
+
let cancelled = false;
|
|
116
165
|
loadOpenCV()
|
|
117
166
|
.then(() => {
|
|
118
|
-
|
|
119
|
-
"[useOpenCV] loadOpenCV resolved. window.cv=",
|
|
120
|
-
typeof window.cv,
|
|
121
|
-
"imread=",
|
|
122
|
-
typeof window.cv?.imread,
|
|
123
|
-
);
|
|
167
|
+
if (cancelled) return;
|
|
124
168
|
setCv(window.cv);
|
|
125
169
|
setReady(true);
|
|
126
170
|
})
|
|
127
171
|
.catch((err: Error) => {
|
|
128
|
-
|
|
172
|
+
if (cancelled) return;
|
|
173
|
+
// OpenCV only powers optional features (border suggestion, perspective warp),
|
|
174
|
+
// so a load failure must never break the tools that depend on it.
|
|
175
|
+
console.warn("[useOpenCV] OpenCV failed to load — continuing without it:", err.message);
|
|
129
176
|
setError(err);
|
|
130
177
|
});
|
|
178
|
+
return () => {
|
|
179
|
+
cancelled = true;
|
|
180
|
+
};
|
|
131
181
|
}, [enabled]);
|
|
132
182
|
|
|
133
183
|
return { cv, ready, error };
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import "./index.scss";
|
|
2
2
|
|
|
3
|
+
import { createSettingsStore } from "@olenbetong/appframe-react";
|
|
4
|
+
|
|
3
5
|
import clsx from "clsx";
|
|
4
|
-
import localforage from "localforage";
|
|
5
6
|
import type React from "react";
|
|
6
7
|
import { Children, forwardRef, useEffect, useRef, useState } from "react";
|
|
7
8
|
|
|
8
9
|
const article = globalThis.af?.article?.id ?? globalThis.window?.location.pathname.substring(1).split("/")[0];
|
|
9
|
-
const settingsStore =
|
|
10
|
-
name: `AfSplitContainers.${globalThis.af?.userSession.login ?? "anonymous"}`,
|
|
11
|
-
});
|
|
10
|
+
const settingsStore = createSettingsStore(`AfSplitContainers.${globalThis.af?.userSession?.login ?? "anonymous"}`);
|
|
12
11
|
|
|
13
12
|
type SplitContainerSettings = {
|
|
14
13
|
layoutVersion: string | number;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { ChipRemovable, Textfield } from "@digdir/designsystemet-react";
|
|
2
|
+
import { getLocalizedString } from "@olenbetong/appframe-core";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
|
|
5
|
+
export type ChipListInputProps = {
|
|
6
|
+
value: string[];
|
|
7
|
+
onChange: (value: string[]) => void;
|
|
8
|
+
readOnly?: boolean;
|
|
9
|
+
"aria-label": string;
|
|
10
|
+
"data-size"?: "sm" | "md" | "lg";
|
|
11
|
+
type?: "text" | "number" | "date" | "datetime-local";
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Free text editor for the list operators. Values are added with Enter and
|
|
16
|
+
* rendered as removable chips.
|
|
17
|
+
*/
|
|
18
|
+
export function ChipListInput({
|
|
19
|
+
value,
|
|
20
|
+
onChange,
|
|
21
|
+
readOnly,
|
|
22
|
+
type = "text",
|
|
23
|
+
"aria-label": ariaLabel,
|
|
24
|
+
"data-size": size,
|
|
25
|
+
}: ChipListInputProps) {
|
|
26
|
+
let [draft, setDraft] = useState("");
|
|
27
|
+
|
|
28
|
+
function commit() {
|
|
29
|
+
let trimmed = draft.trim();
|
|
30
|
+
if (!trimmed || value.includes(trimmed)) {
|
|
31
|
+
setDraft("");
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
onChange([...value, trimmed]);
|
|
36
|
+
setDraft("");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function handleKeyDown(event: React.KeyboardEvent<HTMLInputElement>) {
|
|
40
|
+
if (event.key === "Enter") {
|
|
41
|
+
// The editor is often rendered inside a dialog; committing a value
|
|
42
|
+
// must not submit or close it.
|
|
43
|
+
event.preventDefault();
|
|
44
|
+
commit();
|
|
45
|
+
} else if (event.key === "Backspace" && draft === "" && value.length > 0) {
|
|
46
|
+
onChange(value.slice(0, -1));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<div className="ObFilterEditor-chipList">
|
|
52
|
+
{value.map((item, index) => (
|
|
53
|
+
<ChipRemovable
|
|
54
|
+
// Values are deduplicated on add, so the value is a stable key.
|
|
55
|
+
key={item}
|
|
56
|
+
data-size={size}
|
|
57
|
+
disabled={readOnly}
|
|
58
|
+
aria-label={`${getLocalizedString("Remove")} ${item}`}
|
|
59
|
+
onClick={() => onChange(value.filter((_, i) => i !== index))}
|
|
60
|
+
>
|
|
61
|
+
{item}
|
|
62
|
+
</ChipRemovable>
|
|
63
|
+
))}
|
|
64
|
+
{!readOnly && (
|
|
65
|
+
<Textfield
|
|
66
|
+
className="ObFilterEditor-chipListInput"
|
|
67
|
+
aria-label={ariaLabel}
|
|
68
|
+
data-size={size}
|
|
69
|
+
type={type}
|
|
70
|
+
value={draft}
|
|
71
|
+
placeholder={getLocalizedString("Add value")}
|
|
72
|
+
onChange={(event) => setDraft(event.target.value)}
|
|
73
|
+
onKeyDown={handleKeyDown}
|
|
74
|
+
onBlur={commit}
|
|
75
|
+
/>
|
|
76
|
+
)}
|
|
77
|
+
</div>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Colours use the contextual `--ds-color-*` aliases so the builder follows the
|
|
3
|
+
* active colour scheme. Never add hex fallbacks.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
.ObFilterBuilder-root {
|
|
7
|
+
display: grid;
|
|
8
|
+
gap: var(--ds-size-4);
|
|
9
|
+
/*
|
|
10
|
+
* The saved-filter list sits beside the editor. The buttons acting on the
|
|
11
|
+
* selected filter share the bottom row with OK/Cancel rather than being
|
|
12
|
+
* confined to the list's column, so they have room to stay on one line.
|
|
13
|
+
* On narrow viewports everything stacks.
|
|
14
|
+
*/
|
|
15
|
+
grid-template:
|
|
16
|
+
"sidebar main" 1fr
|
|
17
|
+
"error error" auto
|
|
18
|
+
"footer footer" auto
|
|
19
|
+
/ minmax(14rem, 22rem) minmax(0, 1fr);
|
|
20
|
+
block-size: 100%;
|
|
21
|
+
min-block-size: 0;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.ObFilterBuilder-sidebar {
|
|
25
|
+
grid-area: sidebar;
|
|
26
|
+
overflow: auto;
|
|
27
|
+
padding: var(--ds-size-2);
|
|
28
|
+
border: var(--ds-border-width-default) solid var(--ds-color-border-subtle);
|
|
29
|
+
border-radius: var(--ds-border-radius-md);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* The editor manages its own scrolling — the conditions scroll, the filter
|
|
34
|
+
* string stays put — so this only has to hand it the available height. A caller
|
|
35
|
+
* that replaces the editor through `renderMain` gets the same treatment.
|
|
36
|
+
*/
|
|
37
|
+
.ObFilterBuilder-main {
|
|
38
|
+
grid-area: main;
|
|
39
|
+
display: flex;
|
|
40
|
+
flex-direction: column;
|
|
41
|
+
gap: var(--ds-size-3);
|
|
42
|
+
min-inline-size: 0;
|
|
43
|
+
min-block-size: 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.ObFilterBuilder-main > * {
|
|
47
|
+
flex: 1 1 auto;
|
|
48
|
+
min-inline-size: 0;
|
|
49
|
+
min-block-size: 0;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.ObFilterBuilder-error {
|
|
53
|
+
grid-area: error;
|
|
54
|
+
margin: 0;
|
|
55
|
+
color: var(--ds-color-danger-text-default);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.ObFilterBuilder-footer {
|
|
59
|
+
grid-area: footer;
|
|
60
|
+
display: flex;
|
|
61
|
+
flex-wrap: wrap;
|
|
62
|
+
align-items: center;
|
|
63
|
+
justify-content: space-between;
|
|
64
|
+
gap: var(--ds-size-4);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.ObFilterBuilder-toolbar,
|
|
68
|
+
.ObFilterBuilder-actions {
|
|
69
|
+
display: flex;
|
|
70
|
+
flex-wrap: wrap;
|
|
71
|
+
align-items: center;
|
|
72
|
+
gap: var(--ds-size-2);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.ObFilterBuilder-actions {
|
|
76
|
+
margin-inline-start: auto;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/*
|
|
80
|
+
* Without the saved-filter list there are no buttons acting on one either, so
|
|
81
|
+
* the editor takes the full width.
|
|
82
|
+
*/
|
|
83
|
+
.ObFilterBuilder-root--noSavedFilters {
|
|
84
|
+
grid-template:
|
|
85
|
+
"main" 1fr
|
|
86
|
+
"error" auto
|
|
87
|
+
"footer" auto
|
|
88
|
+
/ minmax(0, 1fr);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/*
|
|
92
|
+
* `Dialog` caps itself at `--dsc-dialog-max-width` (40rem), so setting a width
|
|
93
|
+
* here would have no effect — the variable has to be raised instead. It needs
|
|
94
|
+
* to be wide enough that a condition — field, operator, value and the group
|
|
95
|
+
* buttons — fits on one line next to the saved-filter column.
|
|
96
|
+
*/
|
|
97
|
+
.ObFilterBuilder-dialog {
|
|
98
|
+
--dsc-dialog-max-width: 84rem;
|
|
99
|
+
--dsc-dialog-max-height: 90dvh;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/*
|
|
103
|
+
* A fixed height keeps the dialog from resizing as conditions are added and
|
|
104
|
+
* removed, and gives the saved-filter column and the editor a height to fill.
|
|
105
|
+
* The dialog itself must not scroll — the body does — or the heading would
|
|
106
|
+
* scroll away and the OK/Cancel row would not stay at the bottom.
|
|
107
|
+
*/
|
|
108
|
+
.ObFilterBuilder-dialog[open] {
|
|
109
|
+
display: flex;
|
|
110
|
+
flex-direction: column;
|
|
111
|
+
block-size: 48rem;
|
|
112
|
+
overflow: hidden;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/*
|
|
116
|
+
* The heading and the close button share the header row.
|
|
117
|
+
*/
|
|
118
|
+
.ObFilterBuilder-dialogHeader {
|
|
119
|
+
display: flex;
|
|
120
|
+
align-items: center;
|
|
121
|
+
justify-content: space-between;
|
|
122
|
+
gap: var(--ds-size-2);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
.ObFilterBuilder-dialogBody {
|
|
126
|
+
flex: 1 1 auto;
|
|
127
|
+
min-block-size: 0;
|
|
128
|
+
overflow: hidden;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
@media (width < 48rem) {
|
|
132
|
+
.ObFilterBuilder-root:not(.ObFilterBuilder-root--noSavedFilters) {
|
|
133
|
+
grid-template:
|
|
134
|
+
"sidebar" auto
|
|
135
|
+
"main" 1fr
|
|
136
|
+
"error" auto
|
|
137
|
+
"footer" auto
|
|
138
|
+
/ minmax(0, 1fr);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/*
|
|
142
|
+
* Stacked, the dialog body is the scroll container: the editor cannot give
|
|
143
|
+
* both the list and the conditions a useful height in the space left over.
|
|
144
|
+
*/
|
|
145
|
+
.ObFilterBuilder-dialogBody {
|
|
146
|
+
overflow: auto;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
.ObFilterBuilder-sidebar {
|
|
150
|
+
max-block-size: 12rem;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type { FieldMetadata, SavedFilter } from "@olenbetong/appframe-core";
|
|
2
|
+
import { render, screen, waitFor } from "@testing-library/react";
|
|
3
|
+
import userEvent from "@testing-library/user-event";
|
|
4
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
5
|
+
|
|
6
|
+
const save = vi.fn(async () => undefined);
|
|
7
|
+
const remove = vi.fn(async () => undefined);
|
|
8
|
+
const rename = vi.fn(async () => undefined);
|
|
9
|
+
const setCrossDomain = vi.fn(async () => undefined);
|
|
10
|
+
const setNamedFilter = vi.fn();
|
|
11
|
+
const parseFilterString = vi.fn(async () => null);
|
|
12
|
+
let savedFilters: SavedFilter[] = [];
|
|
13
|
+
|
|
14
|
+
vi.mock("@olenbetong/appframe-react", async (importOriginal) => ({
|
|
15
|
+
...(await importOriginal<Record<string, unknown>>()),
|
|
16
|
+
setNamedFilter: (...args: unknown[]) => setNamedFilter(...args),
|
|
17
|
+
useSavedFilters: () => ({
|
|
18
|
+
filters: savedFilters,
|
|
19
|
+
mine: savedFilters.filter((filter) => filter.createdByUser),
|
|
20
|
+
shared: savedFilters.filter((filter) => !filter.createdByUser),
|
|
21
|
+
loading: false,
|
|
22
|
+
error: null,
|
|
23
|
+
refresh: vi.fn(),
|
|
24
|
+
save,
|
|
25
|
+
rename,
|
|
26
|
+
remove,
|
|
27
|
+
setCrossDomain,
|
|
28
|
+
}),
|
|
29
|
+
useFilterConversion: () => ({
|
|
30
|
+
filterString: "[PersonID] Like '%a%'",
|
|
31
|
+
isConverting: false,
|
|
32
|
+
error: null,
|
|
33
|
+
parseFilterString,
|
|
34
|
+
}),
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
const { FilterBuilder } = await import("./FilterBuilder.js");
|
|
38
|
+
|
|
39
|
+
const FIELDS: FieldMetadata[] = [
|
|
40
|
+
{ name: "PersonID", caption: "Person ID", type: "string", nullable: true },
|
|
41
|
+
{ name: "Age", caption: "Age", type: "number" },
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
function savedFilter(overrides: Partial<SavedFilter> & Pick<SavedFilter, "filterId" | "name">): SavedFilter {
|
|
45
|
+
return {
|
|
46
|
+
primKey: `pk-${overrides.filterId}`,
|
|
47
|
+
criteria: "[PersonID] Like '%a%'",
|
|
48
|
+
dbObjectId: "aviw_Test_View",
|
|
49
|
+
accessLevel: "Manager",
|
|
50
|
+
createdByUser: true,
|
|
51
|
+
createdBy: "bvh",
|
|
52
|
+
domain: "OB",
|
|
53
|
+
crossDomain: false,
|
|
54
|
+
isPersonallyShared: false,
|
|
55
|
+
hideCriteria: false,
|
|
56
|
+
headerText: null,
|
|
57
|
+
comment: null,
|
|
58
|
+
parameters: null,
|
|
59
|
+
reportTitle: null,
|
|
60
|
+
created: null,
|
|
61
|
+
updated: null,
|
|
62
|
+
...overrides,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function renderBuilder(props: Record<string, unknown> = {}) {
|
|
67
|
+
return render(<FilterBuilder fields={FIELDS} viewName="aviw_Test_View" variant="inline" {...props} />);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
describe("FilterBuilder", () => {
|
|
71
|
+
beforeEach(() => {
|
|
72
|
+
vi.clearAllMocks();
|
|
73
|
+
savedFilters = [savedFilter({ filterId: 1, name: "Agder" })];
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("renders the saved filters beside the editor", () => {
|
|
77
|
+
renderBuilder();
|
|
78
|
+
|
|
79
|
+
expect(screen.getByRole("button", { name: "New Filter" })).toBeTruthy();
|
|
80
|
+
expect(screen.getByRole("button", { name: "Agder" })).toBeTruthy();
|
|
81
|
+
expect(screen.getByLabelText("Add condition")).toBeTruthy();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("disables save and share until a filter is selected", () => {
|
|
85
|
+
renderBuilder();
|
|
86
|
+
|
|
87
|
+
expect(screen.getByRole("button", { name: "Sharing..." }).hasAttribute("disabled")).toBe(true);
|
|
88
|
+
expect(screen.getByRole("button", { name: "Save" }).hasAttribute("disabled")).toBe(true);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("disables save as while the filter is empty", () => {
|
|
92
|
+
renderBuilder();
|
|
93
|
+
|
|
94
|
+
expect(screen.getByRole("button", { name: "Save as" }).hasAttribute("disabled")).toBe(true);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("parses the criteria of a selected filter back into the tree", async () => {
|
|
98
|
+
renderBuilder();
|
|
99
|
+
await userEvent.click(screen.getByRole("button", { name: "Agder" }));
|
|
100
|
+
|
|
101
|
+
expect(parseFilterString).toHaveBeenCalledWith("[PersonID] Like '%a%'");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("applies the pruned filter to the data object", async () => {
|
|
105
|
+
let dataObject = {} as never;
|
|
106
|
+
renderBuilder({
|
|
107
|
+
dataObject,
|
|
108
|
+
defaultValue: {
|
|
109
|
+
type: "group",
|
|
110
|
+
mode: "and",
|
|
111
|
+
items: [{ type: "expression", column: "PersonID", operator: "like", value: "a", valueType: "string" }],
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
await userEvent.click(screen.getByRole("button", { name: "OK" }));
|
|
116
|
+
|
|
117
|
+
expect(setNamedFilter).toHaveBeenCalledWith(
|
|
118
|
+
dataObject,
|
|
119
|
+
"filterBuilder",
|
|
120
|
+
expect.objectContaining({ type: "group" }),
|
|
121
|
+
{ type: "filterObject" },
|
|
122
|
+
);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("reports an empty filter as null", async () => {
|
|
126
|
+
let onApply = vi.fn();
|
|
127
|
+
renderBuilder({ onApply });
|
|
128
|
+
|
|
129
|
+
await userEvent.click(screen.getByRole("button", { name: "OK" }));
|
|
130
|
+
|
|
131
|
+
expect(onApply).toHaveBeenCalledWith(null, "[PersonID] Like '%a%'");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("saves under a new name and replaces a filter that already uses it", async () => {
|
|
135
|
+
renderBuilder({
|
|
136
|
+
defaultValue: {
|
|
137
|
+
type: "group",
|
|
138
|
+
mode: "and",
|
|
139
|
+
items: [{ type: "expression", column: "PersonID", operator: "like", value: "a", valueType: "string" }],
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
await userEvent.click(screen.getByRole("button", { name: "Save as" }));
|
|
144
|
+
await userEvent.type(screen.getByRole("textbox", { name: "Filter name" }), "Agder");
|
|
145
|
+
await userEvent.click(screen.getByRole("button", { name: "Replace" }));
|
|
146
|
+
|
|
147
|
+
await waitFor(() =>
|
|
148
|
+
expect(save).toHaveBeenCalledWith(
|
|
149
|
+
expect.objectContaining({ filterId: 1, name: "Agder", criteria: "[PersonID] Like '%a%'" }),
|
|
150
|
+
),
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
});
|