import xor from 'lodash-es/xor' import { h } from 'vue' import SDescAvatar from '../../../components/SDescAvatar.vue' import SDescPill from '../../../components/SDescPill.vue' import { type DropdownSection } from '../../../composables/Dropdown' import { type TableCell } from '../../../composables/Table' import { type RelatedManyFieldData } from '../FieldData' import { type FilterOperator } from '../FilterOperator' import { type ResourceFetcher } from '../ResourceFetcher' import { EmptyFilterInput } from '../filter-inputs/EmptyFilterInput' import { type FilterInput } from '../filter-inputs/FilterInput' import { SelectFilterInput } from '../filter-inputs/SelectFilterInput' import { EmptyFilterOption, Field } from './Field' export class RelatedManyField extends Field { fetcher: ResourceFetcher constructor(ctx: any, data: RelatedManyFieldData, fetcher: ResourceFetcher) { super(ctx, data) this.fetcher = fetcher } override async tableFilterMenu( filters: any[], onFilterUpdated: (filters: any[]) => void ): Promise { const method = this.data.resourceEndpointMethod const url = this.data.resourceEndpointPath const key = this.data.resourceEndpointDataKey if (!url) { return null } const emptyActive = this.emptyFilterActiveFor(this.data.key, filters) const selected = emptyActive ? [EmptyFilterOption] : this.inFilterValueFor(this.data.key, filters) const res = await this.fetcher(method, url, this.data.resourceEndpointBody) const data = key ? res[key] : res const isAvatar = this.data.displayAs === 'avatars' const options = data.map((item: any) => isAvatar ? { type: 'avatar' as const, label: item[this.data.resourceTitle], image: this.data.resourceImage ? item[this.data.resourceImage] : null, value: this.resolveValue(item[this.data.filterKey]) } : { label: item[this.data.resourceTitle], value: this.resolveValue(item[this.data.filterKey]) }) // The pinned "empty" option filters records with no related items at // all. It is mutually exclusive with the value options: selecting it // swaps the whole condition to a valueless `empty`, and selecting a // value swaps back to `in`. if (this.emptyOperatorsEnabled()) { options.unshift({ label: this.emptyOptionLabel(), value: EmptyFilterOption }) } return { type: 'filter', search: true, selected, options, onClick: (v) => { if (v === EmptyFilterOption) { onFilterUpdated?.(emptyActive ? [this.data.key, 'in', []] : [this.data.key, 'empty', null]) return } onFilterUpdated?.([this.data.key, 'in', xor(emptyActive ? [] : selected, [v])]) } } } override tableCell(v: any, _r: any): TableCell { const items = (v ?? []) as any[] if (this.data.displayAs === 'avatars') { return { type: 'avatars', avatars: items.map((item) => ({ image: this.data.image ? item[this.data.image] : null, name: item[this.data.title] })), avatarCount: 6, nameCount: 0, tooltip: true } } return { type: 'pills', pills: items.map((item) => ({ label: item[this.data.title], value: item[this.data.filterKey] })) } } override availableFilters(): Partial> { const method = this.data.resourceEndpointMethod const url = this.data.resourceEndpointPath const key = this.data.resourceEndpointDataKey if (!url) { return {} } const optionsResolver = async () => { const res = await this.fetcher(method, url, this.data.resourceEndpointBody) const data = key ? res[key] : res return data.map((item: any) => ({ value: this.resolveValue(item[this.data.filterKey]), label: item[this.data.resourceTitle] })) } const selectOne = new SelectFilterInput().options(optionsResolver) const selectMany = new SelectFilterInput().options(optionsResolver).multiple() const filters: Partial> = { '=': selectOne, '!=': selectOne, 'in': selectMany } // The valueless operators are strictly opt-in via the backend's field // definition — see `FieldDataBase.emptyOperators`. if (this.data.emptyOperators) { filters.empty = new EmptyFilterInput() filters.notEmpty = new EmptyFilterInput() } return filters } // Lens id fields serialize as `{ value, display, path? }`; filters need the // raw scalar. Unwrap that shape, passing plain scalars through untouched. private resolveValue(raw: any): any { return raw !== null && typeof raw === 'object' && 'value' in raw ? raw.value : raw } override dataListItemComponent(): any { return this.defineDataListItemComponent((value) => { // related_many serializes as an array of relation objects (or null). // Read the display name / image from the same keys `tableCell()` uses, // instead of letting the generic fallback render `[object Object]`. const items = (value ?? []) as any[] if (items.length === 0) { return null } if (this.data.displayAs === 'avatars') { return h(SDescAvatar, { avatar: items.map((item) => ({ avatar: this.data.image ? (item[this.data.image] ?? null) : null, name: item[this.data.title] ?? null })) }) } // 'pills' (and the null default): render one pill per related item. return h(SDescPill, { pill: items.map((item) => ({ label: item[this.data.title] ?? '' })) }) }) } override formInputComponent() { throw new Error('Not implemented.') } }