|
| 1 | +import type { JSONSchema7 } from 'json-schema'; |
| 2 | + |
| 3 | +import { Logger } from '@jsfe/engine/logger'; |
| 4 | + |
| 5 | +import type { |
| 6 | + ArrayChildWidgetOptions, |
| 7 | + ArrayWidgetOptions, |
| 8 | + WidgetTypeBaseParameters, |
| 9 | +} from './types/form.js'; |
| 10 | + |
| 11 | +import { getChildUiSchema, makeIdFromPath } from './utils/object-paths.js'; |
| 12 | + |
| 13 | +const log = new Logger(); |
| 14 | + |
| 15 | +/** |
| 16 | + * Generates widget options for an array field based on the provided schema, |
| 17 | + * data, path, and UI state. |
| 18 | + * |
| 19 | + * @returns The options for rendering the array field widget. |
| 20 | + */ |
| 21 | +export function widgetArray({ |
| 22 | + data = [], |
| 23 | + form, |
| 24 | + level = 0, |
| 25 | + path, |
| 26 | + required = false, |
| 27 | + schema, |
| 28 | + schemaPath, |
| 29 | + uiSchema, |
| 30 | +}: WidgetTypeBaseParameters): ArrayWidgetOptions { |
| 31 | + if (!Array.isArray(data)) throw new Error('Incorrect data'); |
| 32 | + |
| 33 | + const addItemClick = (_event: Event) => { |
| 34 | + log.trace({ _event }); |
| 35 | + |
| 36 | + if (!Array.isArray(data)) return; |
| 37 | + |
| 38 | + if (typeof schema.items !== 'object' || !('type' in schema.items)) return; |
| 39 | + |
| 40 | + if (schema.items.type === 'string') { |
| 41 | + data.push(schema.items.default ?? ''); |
| 42 | + } else if (schema.items.properties) { |
| 43 | + data.push(schema.items.default ?? {}); |
| 44 | + } else if (schema.items.type === 'array') { |
| 45 | + data.push(schema.items.default ?? []); |
| 46 | + } |
| 47 | + |
| 48 | + const schemaPathAugmented = [...schemaPath]; |
| 49 | + schemaPathAugmented.push('items'); |
| 50 | + form.handleChange( |
| 51 | + [...path, data.length - 1], |
| 52 | + data.at(-1), |
| 53 | + schemaPathAugmented, |
| 54 | + ); |
| 55 | + }; |
| 56 | + |
| 57 | + const pathAsString = path.join('.'); |
| 58 | + const parentSelector = `[name="${pathAsString}"]`; |
| 59 | + |
| 60 | + const children: ArrayChildWidgetOptions[] = []; |
| 61 | + for (const [index] of data.entries()) { |
| 62 | + if ( |
| 63 | + typeof schema.items !== 'object' || |
| 64 | + Array.isArray(schema.items) || |
| 65 | + !Array.isArray(data) |
| 66 | + ) |
| 67 | + continue; |
| 68 | + |
| 69 | + const schemaPathAugmented = [...schemaPath]; |
| 70 | + schemaPathAugmented.push('items'); |
| 71 | + |
| 72 | + const widget = form.traverse({ |
| 73 | + data: data[index], |
| 74 | + form, |
| 75 | + level: level + 1, |
| 76 | + path: [...path, index], |
| 77 | + pathAsString: '_TODO_', |
| 78 | + required: required, |
| 79 | + schema: schema.items as JSONSchema7, |
| 80 | + schemaPath: schemaPathAugmented, |
| 81 | + uiSchema: getChildUiSchema(uiSchema, index), |
| 82 | + }); |
| 83 | + |
| 84 | + const move = (direction: number) => (_event: Event) => { |
| 85 | + if (!Array.isArray(data)) return; |
| 86 | + const hold = data[index] as unknown; |
| 87 | + |
| 88 | + data[index] = data[index + direction] as unknown; |
| 89 | + |
| 90 | + data[index + direction] = hold; |
| 91 | + form.handleChange([...path], data, schemaPathAugmented); |
| 92 | + }; |
| 93 | + |
| 94 | + const controls = { |
| 95 | + delete: { |
| 96 | + click: (_event: Event) => { |
| 97 | + const newValue = data.filter((_, index_) => index_ !== index); |
| 98 | + |
| 99 | + form.handleChange([...path], newValue, schemaPathAugmented); |
| 100 | + }, |
| 101 | + }, |
| 102 | + |
| 103 | + down: { |
| 104 | + click: move(1), |
| 105 | + disabled: data[index + 1] === undefined, |
| 106 | + }, |
| 107 | + |
| 108 | + handle: { |
| 109 | + dragstart: (event: DragEvent) => { |
| 110 | + log.trace(event); |
| 111 | + if (!event.dataTransfer) return; |
| 112 | + event.dataTransfer.setData('integer', String(index)); |
| 113 | + }, |
| 114 | + mousedown: (_event: MouseEvent) => { |
| 115 | + // FIXME: |
| 116 | + // event.target!.style.cursor = 'grab'; |
| 117 | + }, |
| 118 | + }, |
| 119 | + |
| 120 | + up: { |
| 121 | + click: move(-1), |
| 122 | + disabled: data[index - 1] === undefined, |
| 123 | + }, |
| 124 | + wrapper: { |
| 125 | + dragenter: (event: DragEvent) => { |
| 126 | + const arrayElement = (event.target as HTMLElement).closest( |
| 127 | + parentSelector, |
| 128 | + ); |
| 129 | + log.trace(arrayElement); |
| 130 | + event.stopPropagation(); |
| 131 | + arrayElement?.setAttribute('data-dropzone', ''); |
| 132 | + }, |
| 133 | + dragleave: (event: DragEvent) => { |
| 134 | + const arrayElement = (event.target as HTMLElement).closest( |
| 135 | + parentSelector, |
| 136 | + ); |
| 137 | + event.stopPropagation(); |
| 138 | + arrayElement |
| 139 | + ?.closest(parentSelector) |
| 140 | + ?.removeAttribute('data-dropzone'); |
| 141 | + }, |
| 142 | + dragover: (event: DragEvent) => { |
| 143 | + event.preventDefault(); |
| 144 | + event.stopPropagation(); |
| 145 | + const dataTransfer = event.dataTransfer; |
| 146 | + if (dataTransfer) dataTransfer.dropEffect = 'move'; |
| 147 | + }, |
| 148 | + drop: (event: DragEvent) => { |
| 149 | + event.stopPropagation(); |
| 150 | + const index_ = event.dataTransfer?.getData('integer'); |
| 151 | + if (!index_) return; |
| 152 | + const originIndex = Number.parseInt(index_, 10); |
| 153 | + |
| 154 | + if (!Array.isArray(data)) return; |
| 155 | + const hold = data[index] as unknown; |
| 156 | + |
| 157 | + data[index] = data[originIndex] as unknown; |
| 158 | + |
| 159 | + data[originIndex] = hold; |
| 160 | + form.handleChange([...path], data, schemaPathAugmented); |
| 161 | + |
| 162 | + (event.target as HTMLElement) |
| 163 | + .closest(parentSelector) |
| 164 | + ?.removeAttribute('data-dropzone'); |
| 165 | + }, |
| 166 | + }, |
| 167 | + }; |
| 168 | + |
| 169 | + children.push({ ...widget, controls }); |
| 170 | + } |
| 171 | + const arrayLabel = schema.title ?? uiSchema['ui:title']; |
| 172 | + |
| 173 | + // eslint-disable-next-line sonarjs/no-nested-template-literals |
| 174 | + let itemLabel = `List item ${arrayLabel ? `(${arrayLabel})` : ''}`; |
| 175 | + |
| 176 | + if ( |
| 177 | + typeof schema.items === 'object' && |
| 178 | + !Array.isArray(schema.items) && |
| 179 | + 'title' in schema.items && |
| 180 | + schema.items.title |
| 181 | + ) { |
| 182 | + itemLabel = schema.items.title; |
| 183 | + } |
| 184 | + |
| 185 | + const options: ArrayWidgetOptions = { |
| 186 | + children, |
| 187 | + classes: {}, |
| 188 | + controls: { add: { click: addItemClick } }, |
| 189 | + form, |
| 190 | + html: { |
| 191 | + element: 'fieldset', |
| 192 | + id: makeIdFromPath(pathAsString), |
| 193 | + name: pathAsString, |
| 194 | + }, |
| 195 | + itemLabel, |
| 196 | + label: arrayLabel, |
| 197 | + level, |
| 198 | + path, |
| 199 | + pathAsString, |
| 200 | + schema, |
| 201 | + value: data, |
| 202 | + widget: 'Array', |
| 203 | + }; |
| 204 | + |
| 205 | + return options; |
| 206 | +} |
0 commit comments