|
| 1 | +/** |
| 2 | + * This source file is available under the terms of the |
| 3 | + * Pimcore Open Core License (POCL) |
| 4 | + * Full copyright and license information is available in |
| 5 | + * LICENSE.md which is distributed with this source code. |
| 6 | + * |
| 7 | + * @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com) |
| 8 | + * @license Pimcore Open Core License (POCL) |
| 9 | + */ |
| 10 | + |
| 11 | +import { isArray, isEmpty, isNil, isPlainObject, isString } from 'lodash' |
| 12 | +import { type DataObjectGetSearchApiResponse } from '@Pimcore/modules/search/search-api-slice.gen' |
| 13 | +import { type ManyToManyRelationValueItem } from '@Pimcore/components/many-to-many-relation/hooks/use-value' |
| 14 | +import { type VisibleFieldDefinition } from '../../many-to-many-object-relation' |
| 15 | + |
| 16 | +export type SearchItem = NonNullable<DataObjectGetSearchApiResponse['items']>[number] |
| 17 | + |
| 18 | +const isBlankColumnValue = (v: unknown): boolean => { |
| 19 | + if (isNil(v)) return true |
| 20 | + if (isString(v)) return isEmpty(v) || v === 'null' |
| 21 | + if (isArray(v) || isPlainObject(v)) return isEmpty(v) |
| 22 | + return false |
| 23 | +} |
| 24 | + |
| 25 | +export const buildLabel = (item: SearchItem, resolvedVisibleDefs: VisibleFieldDefinition[]): string => { |
| 26 | + const fullpath = item.columns?.find(c => c.key === 'fullpath')?.value as string ?? String(item.id) |
| 27 | + if (resolvedVisibleDefs.length === 0) return fullpath |
| 28 | + |
| 29 | + return resolvedVisibleDefs |
| 30 | + .map(fd => { |
| 31 | + const v = item.columns?.find(c => c.key === fd.key)?.value |
| 32 | + if (isBlankColumnValue(v)) return '-' |
| 33 | + return isArray(v) || isPlainObject(v) ? JSON.stringify(v) : String(v) |
| 34 | + }) |
| 35 | + .join(', ') |
| 36 | +} |
| 37 | + |
| 38 | +export const processItems = ( |
| 39 | + items: SearchItem[], |
| 40 | + resolvedVisibleDefs: VisibleFieldDefinition[], |
| 41 | + itemMap: Map<number, ManyToManyRelationValueItem> |
| 42 | +): Map<number, string> => { |
| 43 | + const newLabels = new Map<number, string>() |
| 44 | + items.forEach(item => { |
| 45 | + if (item.id === undefined) return |
| 46 | + const fullpath = item.columns?.find(c => c.key === 'fullpath')?.value as string ?? String(item.id) |
| 47 | + const classname = item.columns?.find(c => c.key === 'classname')?.value as string ?? 'object' |
| 48 | + itemMap.set(item.id, { |
| 49 | + id: item.id, |
| 50 | + type: 'object', |
| 51 | + subtype: classname, |
| 52 | + fullPath: fullpath, |
| 53 | + isPublished: null |
| 54 | + }) |
| 55 | + newLabels.set(item.id, buildLabel(item, resolvedVisibleDefs)) |
| 56 | + }) |
| 57 | + return newLabels |
| 58 | +} |
0 commit comments