@olenbetong/synergi-react 5.0.0 → 5.2.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 (168) hide show
  1. package/dist/styles/styles.css +1 -1
  2. package/dist/styles/styles.css.map +1 -1
  3. package/es/components/Checkbox/index.css +1 -1
  4. package/es/components/Checkbox/index.js +1 -9
  5. package/es/components/CodeEditor/CodeEditor.css +6 -6
  6. package/es/components/ColorCard/index.css +17 -28
  7. package/es/components/DateNavigator/index.css +5 -7
  8. package/es/components/FilePond.js +3 -12
  9. package/es/components/ImageEditor/ImageEditor.css +10 -10
  10. package/es/components/ImageEditor/ImageEditor.js +4 -4
  11. package/es/components/ImageEditor/ResponsiveButton.js +2 -2
  12. package/es/components/ImageEditor/plugins/cropPlugin/CropToolbar.js +1 -1
  13. package/es/components/ImageEditor/plugins/redactPlugin/RedactToolbar.js +1 -1
  14. package/es/components/ImageEditor/plugins/redactPlugin/useRedactTool.js +18 -14
  15. package/es/components/ImageEditor/useImageHistory.js +13 -1
  16. package/es/components/MergeProgressDialog/MergeProgressDialogDs.js +3 -3
  17. package/es/components/MergeProgressDialog/useMergeJob.js +5 -4
  18. package/es/components/PageBanner/index.css +1 -1
  19. package/es/components/PdfViewer/PdfViewer.js +1 -1
  20. package/es/components/Portal/index.js +20 -20
  21. package/es/components/Spinner/index.css +1 -1
  22. package/es/components/SplitContainer/index.css +1 -1
  23. package/es/components/SplitContainer/index.js +3 -1
  24. package/es/components/Toolbar/Toolbar.css +137 -137
  25. package/es/components/Toolbar/index.js +6 -1
  26. package/es/components/ValueToggleGroup/index.css +2 -2
  27. package/es/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +7 -7
  28. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.js +8 -0
  29. package/es/elements/ElementLookupDialog/HIDScanner/useHIDScanner.js +3 -1
  30. package/es/elements-ds/DescriptionInput.d.ts +16 -0
  31. package/es/elements-ds/DescriptionInput.js +41 -0
  32. package/es/elements-ds/DuplicateElementDamage.d.ts +40 -0
  33. package/es/elements-ds/DuplicateElementDamage.js +57 -0
  34. package/es/elements-ds/ElementLookup.d.ts +23 -0
  35. package/es/elements-ds/ElementLookup.js +35 -0
  36. package/es/elements-ds/ElementLookupDialog.d.ts +7 -0
  37. package/es/elements-ds/ElementLookupDialog.js +84 -0
  38. package/es/elements-ds/FormFields.d.ts +43 -0
  39. package/es/elements-ds/FormFields.js +67 -0
  40. package/es/elements-ds/HIDScanner.d.ts +7 -0
  41. package/es/elements-ds/HIDScanner.js +36 -0
  42. package/es/elements-ds/ManualForm.d.ts +7 -0
  43. package/es/elements-ds/ManualForm.js +70 -0
  44. package/es/elements-ds/ProjectSelect.d.ts +28 -0
  45. package/es/elements-ds/ProjectSelect.js +45 -0
  46. package/es/elements-ds/elements-ds.css +157 -0
  47. package/es/elements-ds/index.d.ts +12 -0
  48. package/es/elements-ds/index.js +14 -0
  49. package/es/index.css +1 -1
  50. package/es/useTranslation/index.js +5 -3
  51. package/package.json +16 -4
  52. package/src/chrome/AppShell/AppShell.test.tsx +48 -0
  53. package/src/chrome/AppShell/index.tsx +29 -29
  54. package/src/chrome/openReport.test.ts +50 -0
  55. package/src/chrome/openReport.ts +34 -34
  56. package/src/colorScheme.test.ts +188 -0
  57. package/src/colorScheme.ts +125 -125
  58. package/src/components/Checkbox/Checkbox.browser.test.tsx +116 -0
  59. package/src/components/Checkbox/index.scss +2 -2
  60. package/src/components/Checkbox/index.tsx +1 -10
  61. package/src/components/CodeEditor/CodeEditor.css +6 -6
  62. package/src/components/CodeEditor/CodeEditor.tsx +173 -173
  63. package/src/components/CodeEditor/index.ts +1 -1
  64. package/src/components/ColorCard/ColorCard.test.tsx +34 -0
  65. package/src/components/ColorCard/index.css +17 -28
  66. package/src/components/DateNavigator/DateNavigator.test.tsx +175 -0
  67. package/src/components/DateNavigator/index.css +5 -7
  68. package/src/components/DateNavigator/index.tsx +186 -186
  69. package/src/components/FilePond.browser.test.tsx +389 -0
  70. package/src/components/FilePond.tsx +303 -312
  71. package/src/components/ImageEditor/ImageEditor.browser.test.tsx +286 -0
  72. package/src/components/ImageEditor/ImageEditor.css +10 -10
  73. package/src/components/ImageEditor/ImageEditor.test.tsx +299 -0
  74. package/src/components/ImageEditor/ImageEditor.tsx +348 -348
  75. package/src/components/ImageEditor/ResponsiveButton.tsx +2 -2
  76. package/src/components/ImageEditor/index.ts +9 -9
  77. package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
  78. package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +42 -48
  79. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.test.ts +225 -0
  80. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +405 -405
  81. package/src/components/ImageEditor/plugins/plugins.browser.test.ts +252 -0
  82. package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
  83. package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.test.ts +89 -0
  84. package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.ts +16 -13
  85. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
  86. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
  87. package/src/components/ImageEditor/plugins/zoomPlugin/useZoomPan.test.ts +154 -0
  88. package/src/components/ImageEditor/useActiveTool.ts +21 -21
  89. package/src/components/ImageEditor/useEdgeDetection.test.ts +96 -0
  90. package/src/components/ImageEditor/useEdgeDetection.ts +319 -319
  91. package/src/components/ImageEditor/useImageHistory.test.ts +409 -0
  92. package/src/components/ImageEditor/useImageHistory.ts +206 -197
  93. package/src/components/ImageEditor/useOpenCV.test.ts +208 -0
  94. package/src/components/ImageEditor/useOpenCV.ts +184 -184
  95. package/src/components/LinkedCardList/index.scss +95 -95
  96. package/src/components/MergeProgressDialog/MergeProgressDialogDs.browser.test.tsx +156 -0
  97. package/src/components/MergeProgressDialog/MergeProgressDialogDs.tsx +4 -3
  98. package/src/components/MergeProgressDialog/useMergeJob.test.ts +326 -0
  99. package/src/components/MergeProgressDialog/useMergeJob.ts +4 -3
  100. package/src/components/PageBanner/index.scss +10 -10
  101. package/src/components/PageBanner/index.tsx +80 -80
  102. package/src/components/PdfViewer/PdfViewer.browser.test.tsx +78 -0
  103. package/src/components/PdfViewer/PdfViewer.tsx +158 -158
  104. package/src/components/Portal/Portal.test.tsx +106 -0
  105. package/src/components/Portal/index.tsx +22 -25
  106. package/src/components/Sidebar/Sidebar.browser.test.tsx +57 -0
  107. package/src/components/Sidebar/Sidebar.test.tsx +123 -0
  108. package/src/components/Spinner/index.css +1 -1
  109. package/src/components/SplitContainer/SplitContainer.browser.test.tsx +304 -0
  110. package/src/components/SplitContainer/index.scss +5 -1
  111. package/src/components/SplitContainer/index.tsx +335 -333
  112. package/src/components/Toolbar/Toolbar.css +137 -137
  113. package/src/components/Toolbar/Toolbar.test.tsx +216 -0
  114. package/src/components/Toolbar/ToolbarBreadcrumbs.browser.test.tsx +137 -0
  115. package/src/components/Toolbar/index.tsx +270 -265
  116. package/src/components/ValueToggleGroup/ValueToggleGroup.test.tsx +112 -0
  117. package/src/components/ValueToggleGroup/index.css +2 -2
  118. package/src/designsystemet-web-types.d.ts +24 -24
  119. package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
  120. package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
  121. package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
  122. package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
  123. package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
  124. package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
  125. package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
  126. package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
  127. package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
  128. package/src/elements/ElementLookup.tsx +123 -123
  129. package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.browser.test.tsx +175 -0
  130. package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +7 -7
  131. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +8 -0
  132. package/src/elements/ElementLookupDialog/BarcodeScanner/engines.browser.test.ts +225 -0
  133. package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
  134. package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.test.ts +124 -0
  135. package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.ts +2 -1
  136. package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
  137. package/src/elements/ElementLookupDialog/getElement.test.ts +133 -0
  138. package/src/elements/ElementLookupDialog/index.tsx +226 -226
  139. package/src/elements/ElementLookupDialog/utils/Giai.test.ts +53 -0
  140. package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
  141. package/src/elements/ProjectSelect.tsx +130 -130
  142. package/src/elements/index.ts +15 -15
  143. package/src/elements-ds/DamageForm.browser.test.tsx +530 -0
  144. package/src/elements-ds/DescriptionInput.tsx +114 -0
  145. package/src/elements-ds/DuplicateElementDamage.tsx +185 -0
  146. package/src/elements-ds/ElementLookup.tsx +114 -0
  147. package/src/elements-ds/ElementLookupDialog.browser.test.tsx +207 -0
  148. package/src/elements-ds/ElementLookupDialog.tsx +194 -0
  149. package/src/elements-ds/FormFields.tsx +176 -0
  150. package/src/elements-ds/HIDScanner.tsx +53 -0
  151. package/src/elements-ds/ManualForm.tsx +129 -0
  152. package/src/elements-ds/ProjectSelect.tsx +120 -0
  153. package/src/elements-ds/elements-ds.css +157 -0
  154. package/src/elements-ds/index.ts +16 -0
  155. package/src/filter/index.ts +34 -34
  156. package/src/i18n.test.ts +31 -0
  157. package/src/i18n.ts +13 -13
  158. package/src/mui/MuiThemeProvider.tsx +135 -135
  159. package/src/test/fakeCamera.ts +98 -0
  160. package/src/test/fakeDataApi.ts +90 -0
  161. package/src/test/setup.browser.ts +21 -0
  162. package/src/test/setup.ts +23 -23
  163. package/src/useTranslation/index.ts +5 -3
  164. package/src/useTranslation/useTranslation.test.ts +128 -0
  165. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.d.ts +0 -1
  166. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.js +0 -103
  167. package/es/test/setup.d.ts +0 -6
  168. package/es/test/setup.js +0 -6
@@ -1,312 +1,303 @@
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
+
165
+ useEffect(() => {
166
+ return () => {
167
+ pondRef.current?.destroy();
168
+ pondRef.current = null;
169
+ };
170
+ }, []);
171
+
172
+ useEffect(() => {
173
+ async function removeUploadedFile(source: string, load: () => void, error: (error: string) => void) {
174
+ let { dataHandler } = dataObject;
175
+ try {
176
+ let result = await dataHandler.destroy({
177
+ PrimKey: source,
178
+ } as Partial<T>);
179
+
180
+ if (result) {
181
+ onFileRemoved?.(source);
182
+ load();
183
+ } else {
184
+ error("Failed to delete file on server. Delete request returned false");
185
+ }
186
+ } catch (err: any) {
187
+ error(
188
+ err.responseText ??
189
+ err.statusText ??
190
+ err.message ??
191
+ "Failed to delete file on server but no error was returned.",
192
+ );
193
+ }
194
+ }
195
+
196
+ let options: FilePondOptions = {
197
+ allowMultiple: Boolean(props.multiple),
198
+ allowImageResize: true,
199
+ imageResizeMode: "contain",
200
+ imageResizeTargetWidth: 2400,
201
+ imageResizeTargetHeight: 2400,
202
+ imageResizeUpscale: false,
203
+ ...labels,
204
+ onaddfilestart: (file) => {
205
+ if (file.status !== 5 && file.status !== 6 && file.status !== 10) {
206
+ onUploadStart();
207
+ }
208
+ },
209
+ onprocessfile: (error, file) => {
210
+ onFileUploaded(error, file);
211
+
212
+ let refFiles = pondRef.current?.getFiles();
213
+ let found = refFiles?.find(
214
+ (file) => file.status !== 2 && file.status !== 5 && file.status !== 6 && file.status !== 10,
215
+ );
216
+
217
+ if (found === undefined) {
218
+ onFinishedUploads();
219
+ }
220
+ },
221
+ onprocessfiles: () => onFinishedUploads(),
222
+ onerror: (error) => onError(error.body),
223
+ beforeRemoveFile: () => {
224
+ if (confirm(getLocalizedString("Are you sure you want to remove this file?"))) {
225
+ return true;
226
+ }
227
+
228
+ return false;
229
+ },
230
+ server: {
231
+ load: getFileUrl(dataObject, fieldName, primKey, "view"),
232
+ restore: getFileUrl(dataObject, fieldName, primKey, "view"),
233
+
234
+ revert: removeUploadedFile,
235
+ remove: removeUploadedFile,
236
+
237
+ process: {
238
+ url: getFileUrl(dataObject, fieldName),
239
+ ondata: (formData) => {
240
+ let masterChildCriteria = dataObject.getParameter("masterChildCriteria");
241
+ let allFields = { ...masterChildCriteria, ...fields };
242
+
243
+ for (let [field, value] of Object.entries(allFields)) {
244
+ if (value !== undefined) {
245
+ formData.append(field, String(value));
246
+ }
247
+ }
248
+
249
+ return formData;
250
+ },
251
+ onload: (response) => JSON.parse(response),
252
+ },
253
+ },
254
+ ...filePondOptions,
255
+ };
256
+
257
+ if (pondRef.current) {
258
+ pondRef.current.setOptions(options);
259
+ } else {
260
+ pondRef.current = create(inputRefProp?.current ?? inputRef.current ?? undefined, options);
261
+ }
262
+ }, [
263
+ dataObject,
264
+ props.multiple,
265
+ fields,
266
+ fieldName,
267
+ primKey,
268
+ inputRefProp,
269
+ filePondOptions,
270
+ onError,
271
+ onFileUploaded,
272
+ onUploadStart,
273
+ onFinishedUploads,
274
+ onFileRemoved,
275
+ ]);
276
+
277
+ useEffect(() => {
278
+ if (pondRef.current) {
279
+ files.forEach((file, index) => {
280
+ let options: { index: number } & Partial<FilePondInitialFile["options"]> = {
281
+ index,
282
+ type: "local",
283
+ };
284
+
285
+ pondRef.current?.addFile(file.PrimKey, options);
286
+ });
287
+ }
288
+
289
+ return () => {
290
+ // The data has changed: clear the list without deleting the files on the server
291
+ // (`remove: false` keeps FilePond from calling `server.remove` for them).
292
+ pondRef.current?.removeFiles({ remove: false });
293
+ };
294
+ }, [files]);
295
+
296
+ return (
297
+ <div className="filepond--wrapper">
298
+ {attachmentPrimKeys && <input type="hidden" name="AttachmentPrimkeys" value={attachmentPrimKeys} />}
299
+
300
+ <input type="file" {...props} name="File" ref={inputRefProp ?? inputRef} />
301
+ </div>
302
+ );
303
+ }