mirror of
https://github.com/directus/directus.git
synced 2026-02-11 04:45:34 -05:00
* updated uses of useAliasFields * use fields instead * updated ternary check * remove dealiasing * Linty lint * temporary changes * finish alias cleanup * Fix linter warnings * fixed imports from merge * removed unsed variables * Add changeset --------- Co-authored-by: Nitwel <mail@nitwel.de> Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch> Co-authored-by: rijkvanzanten <rijkvanzanten@me.com> Co-authored-by: Jan Arends <jan.arends@mailbox.org>
101 lines
2.7 KiB
TypeScript
101 lines
2.7 KiB
TypeScript
import { useAliasFields } from '@/composables/use-alias-fields';
|
|
import { useExtension } from '@/composables/use-extension';
|
|
import { useFieldsStore } from '@/stores/fields';
|
|
import { Field } from '@directus/types';
|
|
import { get, getFieldsFromTemplate } from '@directus/utils';
|
|
import { set } from 'lodash';
|
|
import { render, renderFn } from 'micromustache';
|
|
import { ComputedRef, Ref, computed, unref } from 'vue';
|
|
|
|
type StringTemplate = {
|
|
fieldsInTemplate: ComputedRef<string[]>;
|
|
displayValue: ComputedRef<string | false>;
|
|
};
|
|
|
|
function resolve(path: string, scope: any) {
|
|
const value = get(scope, path);
|
|
return typeof value === 'object' ? JSON.stringify(value) : value;
|
|
}
|
|
|
|
export function renderStringTemplate(
|
|
template: Ref<string | null> | string,
|
|
item: Record<string, any> | undefined | null | Ref<Record<string, any> | undefined | null>
|
|
): StringTemplate {
|
|
const values = unref(item);
|
|
|
|
const templateString = unref(template);
|
|
|
|
const fieldsInTemplate = computed(() => getFieldsFromTemplate(templateString));
|
|
|
|
const displayValue = computed(() => {
|
|
if (!values || !templateString || !fieldsInTemplate.value) return false;
|
|
|
|
try {
|
|
return renderFn(templateString, resolve, values, { propsExist: true });
|
|
} catch {
|
|
return false;
|
|
}
|
|
});
|
|
|
|
return { fieldsInTemplate, displayValue };
|
|
}
|
|
|
|
export function renderPlainStringTemplate(template: string, item?: Record<string, any> | null): string | null {
|
|
const fieldsInTemplate = getFieldsFromTemplate(template);
|
|
|
|
if (!item || !template || !fieldsInTemplate) return null;
|
|
|
|
try {
|
|
return render(template, item, { propsExist: true });
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function renderDisplayStringTemplate(
|
|
collection: string,
|
|
template: string,
|
|
item: Record<string, any>
|
|
): string | null {
|
|
const fieldsStore = useFieldsStore();
|
|
|
|
const fields = getFieldsFromTemplate(template);
|
|
|
|
const fieldsUsed: Record<string, Field | null> = {};
|
|
|
|
for (const key of fields) {
|
|
set(fieldsUsed, key, fieldsStore.getField(collection, key));
|
|
}
|
|
|
|
const parsedItem: Record<string, any> = {};
|
|
|
|
const { getFromAliasedItem } = useAliasFields(fields, collection);
|
|
|
|
for (const key of fields) {
|
|
const value = getFromAliasedItem(item, key);
|
|
|
|
const display = useExtension(
|
|
'display',
|
|
computed(() => fieldsUsed[key]?.meta?.display ?? null)
|
|
);
|
|
|
|
if (value !== undefined && value !== null) {
|
|
set(
|
|
parsedItem,
|
|
key,
|
|
display.value?.handler
|
|
? display.value.handler(value, fieldsUsed[key]?.meta?.display_options ?? {}, {
|
|
interfaceOptions: fieldsUsed[key]?.meta?.options ?? {},
|
|
field: fieldsUsed[key] ?? undefined,
|
|
collection: collection,
|
|
})
|
|
: value
|
|
);
|
|
} else {
|
|
set(parsedItem, key, value);
|
|
}
|
|
}
|
|
|
|
return renderPlainStringTemplate(template, parsedItem);
|
|
}
|