@olenbetong/synergi-react 4.3.0 → 5.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.
Files changed (81) hide show
  1. package/dist/styles/styles.css +1 -1
  2. package/dist/styles/styles.css.map +1 -1
  3. package/es/components/CodeEditor/CodeEditor.css +6 -6
  4. package/es/components/MergeProgressDialog/MergeProgressDialogDs.d.ts +7 -0
  5. package/es/components/MergeProgressDialog/MergeProgressDialogDs.js +20 -0
  6. package/es/components/MergeProgressDialog/index.d.ts +3 -8
  7. package/es/components/MergeProgressDialog/index.js +4 -113
  8. package/es/components/MergeProgressDialog/useMergeJob.d.ts +36 -0
  9. package/es/components/MergeProgressDialog/useMergeJob.js +141 -0
  10. package/es/components/ProgressBar/index.css +1 -1
  11. package/es/components/Toolbar/Toolbar.css +137 -137
  12. package/es/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +81 -0
  13. package/es/elements/ElementLookupDialog/BarcodeScanner/DeadIcon.d.ts +2 -2
  14. package/es/elements/ElementLookupDialog/BarcodeScanner/DeadIcon.js +1 -2
  15. package/es/elements/ElementLookupDialog/BarcodeScanner/Error.js +3 -29
  16. package/es/elements/ElementLookupDialog/BarcodeScanner/index.d.ts +1 -0
  17. package/es/elements/ElementLookupDialog/BarcodeScanner/index.js +3 -28
  18. package/es/index.css +1 -1
  19. package/es/index.d.ts +2 -1
  20. package/es/index.js +2 -1
  21. package/es/mui/index.d.ts +1 -0
  22. package/es/mui/index.js +1 -0
  23. package/es/test/setup.d.ts +5 -0
  24. package/package.json +21 -14
  25. package/src/chrome/AppShell/index.tsx +29 -29
  26. package/src/chrome/openReport.ts +34 -34
  27. package/src/colorScheme.ts +125 -125
  28. package/src/components/CodeEditor/CodeEditor.css +6 -6
  29. package/src/components/CodeEditor/CodeEditor.tsx +173 -173
  30. package/src/components/CodeEditor/index.ts +1 -1
  31. package/src/components/DateNavigator/index.tsx +186 -186
  32. package/src/components/FilePond.tsx +312 -312
  33. package/src/components/ImageEditor/ImageEditor.tsx +348 -348
  34. package/src/components/ImageEditor/index.ts +9 -9
  35. package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
  36. package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +48 -48
  37. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +405 -405
  38. package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
  39. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
  40. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
  41. package/src/components/ImageEditor/useActiveTool.ts +21 -21
  42. package/src/components/ImageEditor/useEdgeDetection.ts +319 -319
  43. package/src/components/ImageEditor/useImageHistory.ts +197 -197
  44. package/src/components/ImageEditor/useOpenCV.ts +184 -184
  45. package/src/components/LinkedCardList/index.scss +95 -95
  46. package/src/components/MergeProgressDialog/MergeProgressDialogDs.tsx +91 -0
  47. package/src/components/MergeProgressDialog/index.tsx +5 -136
  48. package/src/components/MergeProgressDialog/useMergeJob.ts +169 -0
  49. package/src/components/PageBanner/index.tsx +80 -80
  50. package/src/components/PdfViewer/PdfViewer.tsx +158 -158
  51. package/src/components/ProgressBar/index.scss +23 -54
  52. package/src/components/SplitContainer/index.tsx +333 -333
  53. package/src/components/Toolbar/Toolbar.css +137 -137
  54. package/src/components/Toolbar/index.tsx +265 -265
  55. package/src/designsystemet-web-types.d.ts +24 -24
  56. package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
  57. package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
  58. package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
  59. package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
  60. package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
  61. package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
  62. package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
  63. package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
  64. package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
  65. package/src/elements/ElementLookup.tsx +123 -123
  66. package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +81 -0
  67. package/src/elements/ElementLookupDialog/BarcodeScanner/DeadIcon.tsx +4 -4
  68. package/src/elements/ElementLookupDialog/BarcodeScanner/Error.tsx +7 -36
  69. package/src/elements/ElementLookupDialog/BarcodeScanner/index.tsx +20 -49
  70. package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
  71. package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
  72. package/src/elements/ElementLookupDialog/index.tsx +226 -226
  73. package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
  74. package/src/elements/ProjectSelect.tsx +130 -130
  75. package/src/elements/index.ts +15 -15
  76. package/src/filter/index.ts +34 -34
  77. package/src/i18n.ts +13 -13
  78. package/src/index.ts +5 -1
  79. package/src/mui/MuiThemeProvider.tsx +135 -135
  80. package/src/mui/index.ts +1 -0
  81. package/src/test/setup.ts +23 -8
@@ -1,312 +1,312 @@
1
- import "filepond-plugin-file-poster/dist/filepond-plugin-file-poster.css";
2
- import "filepond-plugin-image-preview/dist/filepond-plugin-image-preview.css";
3
- import "filepond/dist/filepond.css";
4
-
5
- import type { DataObject } from "@olenbetong/appframe-data";
6
- import { useData, useFetchData } from "@olenbetong/appframe-react";
7
-
8
- import {
9
- create,
10
- type FilePondErrorDescription,
11
- type FilePondFile,
12
- type FilePondInitialFile,
13
- type FilePond as FilePondInstance,
14
- type FilePondOptions,
15
- FileStatus,
16
- registerPlugin,
17
- } from "filepond";
18
- import FilePondPluginImageExifOrientation from "filepond-plugin-image-exif-orientation";
19
- import FilePondPluginImagePreview from "filepond-plugin-image-preview";
20
- import FilePondPluginImageResize from "filepond-plugin-image-resize";
21
- import FilePondPluginImageTransform from "filepond-plugin-image-transform";
22
- import { type RefObject, useEffect, useMemo, useRef } from "react";
23
- import { getLocalizedString } from "../i18n.js";
24
-
25
- export { FileStatus, registerPlugin };
26
-
27
- registerPlugin(
28
- FilePondPluginImageExifOrientation,
29
- FilePondPluginImageResize,
30
- FilePondPluginImagePreview,
31
- FilePondPluginImageTransform,
32
- );
33
-
34
- let isMostLikelyMobile =
35
- navigator.userAgent.match(/Android/i) ||
36
- navigator.userAgent.match(/iPhone/i) ||
37
- navigator.userAgent.match(/iPad/i) ||
38
- navigator.userAgent.match(/iPod/i);
39
-
40
- const labels = {
41
- labelIdle: isMostLikelyMobile
42
- ? getLocalizedString("Take a photo, or upload an existing image/file")
43
- : getLocalizedString('Drag & Drop your files or <span class="filepond--label-action"> Browse </span>'),
44
- labelInvalidField: getLocalizedString("Field contains invalid files"),
45
- labelFileWaitingForSize: getLocalizedString("Waiting for size"),
46
- labelFileSizeNotAvailable: getLocalizedString("Size not available"),
47
- labelFileLoading: getLocalizedString("Loading"),
48
- labelFileLoadError: getLocalizedString("Error during load"),
49
- labelFileProcessing: getLocalizedString("Uploading"),
50
- labelFileProcessingComplete: getLocalizedString("Upload complete"),
51
- labelFileProcessingAborted: getLocalizedString("Upload cancelled"),
52
- labelFileProcessingError: getLocalizedString("Error during upload"),
53
- labelFileProcessingRevertError: getLocalizedString("Error during revert"),
54
- labelFileRemoveError: getLocalizedString("Error during remove"),
55
- labelTapToCancel: getLocalizedString("tap to cancel"),
56
- labelTapToRetry: getLocalizedString("tap to retry"),
57
- labelTapToUndo: getLocalizedString("tap to undo"),
58
- labelButtonRemoveItem: getLocalizedString("Remove"),
59
- labelButtonAbortItemLoad: getLocalizedString("Abort"),
60
- labelButtonRetryItemLoad: getLocalizedString("Retry"),
61
- labelButtonAbortItemProcessing: getLocalizedString("Cancel"),
62
- labelButtonUndoItemProcessing: getLocalizedString("Undo"),
63
- labelButtonRetryItemProcessing: getLocalizedString("Retry"),
64
- labelButtonProcessItem: getLocalizedString("Upload"),
65
- labelMaxFileSizeExceeded: getLocalizedString("File is too large"),
66
- labelMaxFileSize: getLocalizedString("Maximum file size is {filesize}"),
67
- labelMaxTotalFileSizeExceeded: getLocalizedString("Maximum total size exceeded"),
68
- labelMaxTotalFileSize: getLocalizedString("Maximum total file size is {filesize}"),
69
- labelFileTypeNotAllowed: getLocalizedString("File of invalid type"),
70
- fileValidateTypeLabelExpectedTypes: getLocalizedString("Expects {allButLastType} or {lastType}"),
71
- imageValidateSizeLabelFormatError: getLocalizedString("Image type not supported"),
72
- imageValidateSizeLabelImageSizeTooSmall: getLocalizedString("Image is too small"),
73
- imageValidateSizeLabelImageSizeTooBig: getLocalizedString("Image is too big"),
74
- imageValidateSizeLabelExpectedMinSize: getLocalizedString("Minimum size is {minWidth} × {minHeight}"),
75
- imageValidateSizeLabelExpectedMaxSize: getLocalizedString("Maximum size is {maxWidth} × {maxHeight}"),
76
- imageValidateSizeLabelImageResolutionTooLow: getLocalizedString("Resolution is too low"),
77
- imageValidateSizeLabelImageResolutionTooHigh: getLocalizedString("Resolution is too high"),
78
- imageValidateSizeLabelExpectedMinResolution: getLocalizedString("Minimum resolution is {minResolution}"),
79
- imageValidateSizeLabelExpectedMaxResolution: getLocalizedString("Maximum resolution is {maxResolution}"),
80
- };
81
-
82
- type FilePondProps<T extends Record<string, unknown>> = React.HTMLProps<HTMLInputElement> & {
83
- dataObject: DataObject<T>;
84
- fields: Partial<T>;
85
- onUploadStart: () => void;
86
- onFileUploaded: (error: FilePondErrorDescription | null, file: FilePondFile) => void;
87
- onFinishedUploads: () => void;
88
- onError: (error: string) => void;
89
- onFileRemoved?: (primKey: string) => void;
90
- /**
91
- * If the file should be uploaded to a blob field, set the field name here
92
- */
93
- fieldName?: string;
94
- /**
95
- * If the file should be added to an existing record, set the prim key here
96
- */
97
- primKey?: string;
98
- /**
99
- * If a reference to the file input element is needed, pass a ref here.
100
- */
101
- inputRef?: RefObject<HTMLInputElement>;
102
- /**
103
- * Options to pass to FilePond
104
- */
105
- filePondOptions?: Partial<FilePondOptions>;
106
- attachmentPrimKeys?: string[];
107
- whereClause?: string;
108
- };
109
-
110
- function getFileUrl<T extends Record<string, unknown>>(
111
- dataObject: DataObject<T>,
112
- fieldName?: string,
113
- primKey?: string,
114
- action = "upload",
115
- ) {
116
- let url =
117
- action === "upload"
118
- ? dataObject.dataHandler.getUploadUrl()
119
- : dataObject.dataHandler.getFileStoreURL(primKey ?? "", fieldName ?? "");
120
-
121
- if (action === "upload") {
122
- url += "/";
123
- }
124
-
125
- return url?.replaceAll("//", "/") ?? "";
126
- }
127
-
128
- type FileStoreDataObjectRecord = {
129
- PrimKey: string;
130
- FileName: string | null;
131
- FileSize: number | null;
132
- FileRef: string | null;
133
- ElementRef: string | null;
134
- Damage: boolean;
135
- DamageID: number | null;
136
- };
137
-
138
- export function FilePond<T extends FileStoreDataObjectRecord>({
139
- dataObject,
140
- fields,
141
- onUploadStart,
142
- onFileUploaded,
143
- onFinishedUploads,
144
- onFileRemoved,
145
- onError,
146
- fieldName,
147
- filePondOptions,
148
- primKey,
149
- attachmentPrimKeys,
150
- whereClause,
151
- inputRef: inputRefProp,
152
- ...props
153
- }: FilePondProps<T>) {
154
- let inputRef = useRef<HTMLInputElement>(null);
155
- let pondRef = useRef<FilePondInstance | null>(null);
156
- let filesData = useData(dataObject);
157
- let { data: filesFetched } = useFetchData(dataObject, whereClause ?? "1=2");
158
- let files = useMemo(() => {
159
- if (whereClause) {
160
- return filesFetched;
161
- }
162
- return filesData;
163
- }, [whereClause, filesFetched, filesData]);
164
- let clearFilesRef = useRef(false);
165
-
166
- useEffect(() => {
167
- return () => {
168
- pondRef.current?.destroy();
169
- pondRef.current = null;
170
- };
171
- }, []);
172
-
173
- useEffect(() => {
174
- async function removeUploadedFile(source: string, load: () => void, error: (error: string) => void) {
175
- // this method is also called when we clear the files on data object
176
- // refresh. Haven't found any elegant way to work around that, so instead
177
- // we set this ref to true when we remove the files to have a way
178
- // to discover why the files are being removed
179
- if (clearFilesRef.current) return;
180
-
181
- let { dataHandler } = dataObject;
182
- try {
183
- let result = await dataHandler.destroy({
184
- PrimKey: source,
185
- } as Partial<T>);
186
-
187
- if (result) {
188
- onFileRemoved?.(source);
189
- load();
190
- } else {
191
- error("Failed to delete file on server. Delete request returned false");
192
- }
193
- } catch (err: any) {
194
- error(
195
- err.responseText ??
196
- err.statusText ??
197
- err.message ??
198
- "Failed to delete file on server but no error was returned.",
199
- );
200
- }
201
- }
202
-
203
- let options: FilePondOptions = {
204
- allowMultiple: Boolean(props.multiple),
205
- allowImageResize: true,
206
- imageResizeMode: "contain",
207
- imageResizeTargetWidth: 2400,
208
- imageResizeTargetHeight: 2400,
209
- imageResizeUpscale: false,
210
- ...labels,
211
- onaddfilestart: (file) => {
212
- if (file.status !== 5 && file.status !== 6 && file.status !== 10) {
213
- onUploadStart();
214
- }
215
- },
216
- onprocessfile: (error, file) => {
217
- onFileUploaded(error, file);
218
-
219
- let refFiles = pondRef.current?.getFiles();
220
- let found = refFiles?.find(
221
- (file) => file.status !== 2 && file.status !== 5 && file.status !== 6 && file.status !== 10,
222
- );
223
-
224
- if (found === undefined) {
225
- onFinishedUploads();
226
- }
227
- },
228
- onprocessfiles: () => onFinishedUploads(),
229
- onerror: (error) => onError(error.body),
230
- beforeRemoveFile: () => {
231
- if (confirm(getLocalizedString("Are you sure you want to remove this file?"))) {
232
- return true;
233
- }
234
-
235
- return false;
236
- },
237
- server: {
238
- load: getFileUrl(dataObject, fieldName, primKey, "view"),
239
- restore: getFileUrl(dataObject, fieldName, primKey, "view"),
240
-
241
- revert: removeUploadedFile,
242
- remove: removeUploadedFile,
243
-
244
- process: {
245
- url: getFileUrl(dataObject, fieldName),
246
- ondata: (formData) => {
247
- let masterChildCriteria = dataObject.getParameter("masterChildCriteria");
248
- let allFields = { ...masterChildCriteria, ...fields };
249
-
250
- for (let [field, value] of Object.entries(allFields)) {
251
- if (value !== undefined) {
252
- formData.append(field, String(value));
253
- }
254
- }
255
-
256
- return formData;
257
- },
258
- onload: (response) => JSON.parse(response),
259
- },
260
- },
261
- ...filePondOptions,
262
- };
263
-
264
- if (pondRef.current) {
265
- pondRef.current.setOptions(options);
266
- } else {
267
- pondRef.current = create(inputRefProp?.current ?? inputRef.current ?? undefined, options);
268
- }
269
- }, [
270
- dataObject,
271
- props.multiple,
272
- fields,
273
- fieldName,
274
- primKey,
275
- inputRefProp,
276
- filePondOptions,
277
- onError,
278
- onFileUploaded,
279
- onUploadStart,
280
- onFinishedUploads,
281
- onFileRemoved,
282
- ]);
283
-
284
- useEffect(() => {
285
- if (pondRef.current) {
286
- files.forEach((file, index) => {
287
- let options: { index: number } & Partial<FilePondInitialFile["options"]> = {
288
- index,
289
- type: "local",
290
- };
291
-
292
- pondRef.current?.addFile(file.PrimKey, options);
293
- });
294
- }
295
-
296
- return () => {
297
- // Indicate that we are clearing the files because data has changed,
298
- // to prevent the remove callback from deleting the files
299
- clearFilesRef.current = true;
300
- pondRef.current?.removeFiles();
301
- clearFilesRef.current = false;
302
- };
303
- }, [files]);
304
-
305
- return (
306
- <div className="filepond--wrapper">
307
- {attachmentPrimKeys && <input type="hidden" name="AttachmentPrimkeys" value={attachmentPrimKeys} />}
308
-
309
- <input type="file" {...props} name="File" ref={inputRefProp ?? inputRef} />
310
- </div>
311
- );
312
- }
1
+ import "filepond-plugin-file-poster/dist/filepond-plugin-file-poster.css";
2
+ import "filepond-plugin-image-preview/dist/filepond-plugin-image-preview.css";
3
+ import "filepond/dist/filepond.css";
4
+
5
+ import type { DataObject } from "@olenbetong/appframe-data";
6
+ import { useData, useFetchData } from "@olenbetong/appframe-react";
7
+
8
+ import {
9
+ create,
10
+ type FilePondErrorDescription,
11
+ type FilePondFile,
12
+ type FilePondInitialFile,
13
+ type FilePond as FilePondInstance,
14
+ type FilePondOptions,
15
+ FileStatus,
16
+ registerPlugin,
17
+ } from "filepond";
18
+ import FilePondPluginImageExifOrientation from "filepond-plugin-image-exif-orientation";
19
+ import FilePondPluginImagePreview from "filepond-plugin-image-preview";
20
+ import FilePondPluginImageResize from "filepond-plugin-image-resize";
21
+ import FilePondPluginImageTransform from "filepond-plugin-image-transform";
22
+ import { type RefObject, useEffect, useMemo, useRef } from "react";
23
+ import { getLocalizedString } from "../i18n.js";
24
+
25
+ export { FileStatus, registerPlugin };
26
+
27
+ registerPlugin(
28
+ FilePondPluginImageExifOrientation,
29
+ FilePondPluginImageResize,
30
+ FilePondPluginImagePreview,
31
+ FilePondPluginImageTransform,
32
+ );
33
+
34
+ let isMostLikelyMobile =
35
+ navigator.userAgent.match(/Android/i) ||
36
+ navigator.userAgent.match(/iPhone/i) ||
37
+ navigator.userAgent.match(/iPad/i) ||
38
+ navigator.userAgent.match(/iPod/i);
39
+
40
+ const labels = {
41
+ labelIdle: isMostLikelyMobile
42
+ ? getLocalizedString("Take a photo, or upload an existing image/file")
43
+ : getLocalizedString('Drag & Drop your files or <span class="filepond--label-action"> Browse </span>'),
44
+ labelInvalidField: getLocalizedString("Field contains invalid files"),
45
+ labelFileWaitingForSize: getLocalizedString("Waiting for size"),
46
+ labelFileSizeNotAvailable: getLocalizedString("Size not available"),
47
+ labelFileLoading: getLocalizedString("Loading"),
48
+ labelFileLoadError: getLocalizedString("Error during load"),
49
+ labelFileProcessing: getLocalizedString("Uploading"),
50
+ labelFileProcessingComplete: getLocalizedString("Upload complete"),
51
+ labelFileProcessingAborted: getLocalizedString("Upload cancelled"),
52
+ labelFileProcessingError: getLocalizedString("Error during upload"),
53
+ labelFileProcessingRevertError: getLocalizedString("Error during revert"),
54
+ labelFileRemoveError: getLocalizedString("Error during remove"),
55
+ labelTapToCancel: getLocalizedString("tap to cancel"),
56
+ labelTapToRetry: getLocalizedString("tap to retry"),
57
+ labelTapToUndo: getLocalizedString("tap to undo"),
58
+ labelButtonRemoveItem: getLocalizedString("Remove"),
59
+ labelButtonAbortItemLoad: getLocalizedString("Abort"),
60
+ labelButtonRetryItemLoad: getLocalizedString("Retry"),
61
+ labelButtonAbortItemProcessing: getLocalizedString("Cancel"),
62
+ labelButtonUndoItemProcessing: getLocalizedString("Undo"),
63
+ labelButtonRetryItemProcessing: getLocalizedString("Retry"),
64
+ labelButtonProcessItem: getLocalizedString("Upload"),
65
+ labelMaxFileSizeExceeded: getLocalizedString("File is too large"),
66
+ labelMaxFileSize: getLocalizedString("Maximum file size is {filesize}"),
67
+ labelMaxTotalFileSizeExceeded: getLocalizedString("Maximum total size exceeded"),
68
+ labelMaxTotalFileSize: getLocalizedString("Maximum total file size is {filesize}"),
69
+ labelFileTypeNotAllowed: getLocalizedString("File of invalid type"),
70
+ fileValidateTypeLabelExpectedTypes: getLocalizedString("Expects {allButLastType} or {lastType}"),
71
+ imageValidateSizeLabelFormatError: getLocalizedString("Image type not supported"),
72
+ imageValidateSizeLabelImageSizeTooSmall: getLocalizedString("Image is too small"),
73
+ imageValidateSizeLabelImageSizeTooBig: getLocalizedString("Image is too big"),
74
+ imageValidateSizeLabelExpectedMinSize: getLocalizedString("Minimum size is {minWidth} × {minHeight}"),
75
+ imageValidateSizeLabelExpectedMaxSize: getLocalizedString("Maximum size is {maxWidth} × {maxHeight}"),
76
+ imageValidateSizeLabelImageResolutionTooLow: getLocalizedString("Resolution is too low"),
77
+ imageValidateSizeLabelImageResolutionTooHigh: getLocalizedString("Resolution is too high"),
78
+ imageValidateSizeLabelExpectedMinResolution: getLocalizedString("Minimum resolution is {minResolution}"),
79
+ imageValidateSizeLabelExpectedMaxResolution: getLocalizedString("Maximum resolution is {maxResolution}"),
80
+ };
81
+
82
+ type FilePondProps<T extends Record<string, unknown>> = React.HTMLProps<HTMLInputElement> & {
83
+ dataObject: DataObject<T>;
84
+ fields: Partial<T>;
85
+ onUploadStart: () => void;
86
+ onFileUploaded: (error: FilePondErrorDescription | null, file: FilePondFile) => void;
87
+ onFinishedUploads: () => void;
88
+ onError: (error: string) => void;
89
+ onFileRemoved?: (primKey: string) => void;
90
+ /**
91
+ * If the file should be uploaded to a blob field, set the field name here
92
+ */
93
+ fieldName?: string;
94
+ /**
95
+ * If the file should be added to an existing record, set the prim key here
96
+ */
97
+ primKey?: string;
98
+ /**
99
+ * If a reference to the file input element is needed, pass a ref here.
100
+ */
101
+ inputRef?: RefObject<HTMLInputElement>;
102
+ /**
103
+ * Options to pass to FilePond
104
+ */
105
+ filePondOptions?: Partial<FilePondOptions>;
106
+ attachmentPrimKeys?: string[];
107
+ whereClause?: string;
108
+ };
109
+
110
+ function getFileUrl<T extends Record<string, unknown>>(
111
+ dataObject: DataObject<T>,
112
+ fieldName?: string,
113
+ primKey?: string,
114
+ action = "upload",
115
+ ) {
116
+ let url =
117
+ action === "upload"
118
+ ? dataObject.dataHandler.getUploadUrl()
119
+ : dataObject.dataHandler.getFileStoreURL(primKey ?? "", fieldName ?? "");
120
+
121
+ if (action === "upload") {
122
+ url += "/";
123
+ }
124
+
125
+ return url?.replaceAll("//", "/") ?? "";
126
+ }
127
+
128
+ type FileStoreDataObjectRecord = {
129
+ PrimKey: string;
130
+ FileName: string | null;
131
+ FileSize: number | null;
132
+ FileRef: string | null;
133
+ ElementRef: string | null;
134
+ Damage: boolean;
135
+ DamageID: number | null;
136
+ };
137
+
138
+ export function FilePond<T extends FileStoreDataObjectRecord>({
139
+ dataObject,
140
+ fields,
141
+ onUploadStart,
142
+ onFileUploaded,
143
+ onFinishedUploads,
144
+ onFileRemoved,
145
+ onError,
146
+ fieldName,
147
+ filePondOptions,
148
+ primKey,
149
+ attachmentPrimKeys,
150
+ whereClause,
151
+ inputRef: inputRefProp,
152
+ ...props
153
+ }: FilePondProps<T>) {
154
+ let inputRef = useRef<HTMLInputElement>(null);
155
+ let pondRef = useRef<FilePondInstance | null>(null);
156
+ let filesData = useData(dataObject);
157
+ let { data: filesFetched } = useFetchData(dataObject, whereClause ?? "1=2");
158
+ let files = useMemo(() => {
159
+ if (whereClause) {
160
+ return filesFetched;
161
+ }
162
+ return filesData;
163
+ }, [whereClause, filesFetched, filesData]);
164
+ let clearFilesRef = useRef(false);
165
+
166
+ useEffect(() => {
167
+ return () => {
168
+ pondRef.current?.destroy();
169
+ pondRef.current = null;
170
+ };
171
+ }, []);
172
+
173
+ useEffect(() => {
174
+ async function removeUploadedFile(source: string, load: () => void, error: (error: string) => void) {
175
+ // this method is also called when we clear the files on data object
176
+ // refresh. Haven't found any elegant way to work around that, so instead
177
+ // we set this ref to true when we remove the files to have a way
178
+ // to discover why the files are being removed
179
+ if (clearFilesRef.current) return;
180
+
181
+ let { dataHandler } = dataObject;
182
+ try {
183
+ let result = await dataHandler.destroy({
184
+ PrimKey: source,
185
+ } as Partial<T>);
186
+
187
+ if (result) {
188
+ onFileRemoved?.(source);
189
+ load();
190
+ } else {
191
+ error("Failed to delete file on server. Delete request returned false");
192
+ }
193
+ } catch (err: any) {
194
+ error(
195
+ err.responseText ??
196
+ err.statusText ??
197
+ err.message ??
198
+ "Failed to delete file on server but no error was returned.",
199
+ );
200
+ }
201
+ }
202
+
203
+ let options: FilePondOptions = {
204
+ allowMultiple: Boolean(props.multiple),
205
+ allowImageResize: true,
206
+ imageResizeMode: "contain",
207
+ imageResizeTargetWidth: 2400,
208
+ imageResizeTargetHeight: 2400,
209
+ imageResizeUpscale: false,
210
+ ...labels,
211
+ onaddfilestart: (file) => {
212
+ if (file.status !== 5 && file.status !== 6 && file.status !== 10) {
213
+ onUploadStart();
214
+ }
215
+ },
216
+ onprocessfile: (error, file) => {
217
+ onFileUploaded(error, file);
218
+
219
+ let refFiles = pondRef.current?.getFiles();
220
+ let found = refFiles?.find(
221
+ (file) => file.status !== 2 && file.status !== 5 && file.status !== 6 && file.status !== 10,
222
+ );
223
+
224
+ if (found === undefined) {
225
+ onFinishedUploads();
226
+ }
227
+ },
228
+ onprocessfiles: () => onFinishedUploads(),
229
+ onerror: (error) => onError(error.body),
230
+ beforeRemoveFile: () => {
231
+ if (confirm(getLocalizedString("Are you sure you want to remove this file?"))) {
232
+ return true;
233
+ }
234
+
235
+ return false;
236
+ },
237
+ server: {
238
+ load: getFileUrl(dataObject, fieldName, primKey, "view"),
239
+ restore: getFileUrl(dataObject, fieldName, primKey, "view"),
240
+
241
+ revert: removeUploadedFile,
242
+ remove: removeUploadedFile,
243
+
244
+ process: {
245
+ url: getFileUrl(dataObject, fieldName),
246
+ ondata: (formData) => {
247
+ let masterChildCriteria = dataObject.getParameter("masterChildCriteria");
248
+ let allFields = { ...masterChildCriteria, ...fields };
249
+
250
+ for (let [field, value] of Object.entries(allFields)) {
251
+ if (value !== undefined) {
252
+ formData.append(field, String(value));
253
+ }
254
+ }
255
+
256
+ return formData;
257
+ },
258
+ onload: (response) => JSON.parse(response),
259
+ },
260
+ },
261
+ ...filePondOptions,
262
+ };
263
+
264
+ if (pondRef.current) {
265
+ pondRef.current.setOptions(options);
266
+ } else {
267
+ pondRef.current = create(inputRefProp?.current ?? inputRef.current ?? undefined, options);
268
+ }
269
+ }, [
270
+ dataObject,
271
+ props.multiple,
272
+ fields,
273
+ fieldName,
274
+ primKey,
275
+ inputRefProp,
276
+ filePondOptions,
277
+ onError,
278
+ onFileUploaded,
279
+ onUploadStart,
280
+ onFinishedUploads,
281
+ onFileRemoved,
282
+ ]);
283
+
284
+ useEffect(() => {
285
+ if (pondRef.current) {
286
+ files.forEach((file, index) => {
287
+ let options: { index: number } & Partial<FilePondInitialFile["options"]> = {
288
+ index,
289
+ type: "local",
290
+ };
291
+
292
+ pondRef.current?.addFile(file.PrimKey, options);
293
+ });
294
+ }
295
+
296
+ return () => {
297
+ // Indicate that we are clearing the files because data has changed,
298
+ // to prevent the remove callback from deleting the files
299
+ clearFilesRef.current = true;
300
+ pondRef.current?.removeFiles();
301
+ clearFilesRef.current = false;
302
+ };
303
+ }, [files]);
304
+
305
+ return (
306
+ <div className="filepond--wrapper">
307
+ {attachmentPrimKeys && <input type="hidden" name="AttachmentPrimkeys" value={attachmentPrimKeys} />}
308
+
309
+ <input type="file" {...props} name="File" ref={inputRefProp ?? inputRef} />
310
+ </div>
311
+ );
312
+ }