|
|
@ -4,18 +4,11 @@ import { watch } from 'vue' |
|
|
|
import type { ComputedRef, Ref } from 'vue' |
|
|
|
import type { ComputedRef, Ref } from 'vue' |
|
|
|
import { useNuxtApp } from '#app' |
|
|
|
import { useNuxtApp } from '#app' |
|
|
|
import { IsPublicInj } from '#imports' |
|
|
|
import { IsPublicInj } from '#imports' |
|
|
|
|
|
|
|
import type { Field } from '~/lib' |
|
|
|
|
|
|
|
|
|
|
|
export function useViewColumns(view: Ref<ViewType> | undefined, meta: ComputedRef<TableType>, reloadData?: () => void) { |
|
|
|
export function useViewColumns(view: Ref<ViewType> | undefined, meta: ComputedRef<TableType>, reloadData?: () => void) { |
|
|
|
const isPublic = inject(IsPublicInj, ref(false)) |
|
|
|
const isPublic = inject(IsPublicInj, ref(false)) |
|
|
|
const fields = ref< |
|
|
|
const fields = ref<Field[]>() |
|
|
|
{ |
|
|
|
|
|
|
|
order: number |
|
|
|
|
|
|
|
show: number | boolean |
|
|
|
|
|
|
|
title: string |
|
|
|
|
|
|
|
fk_column_id?: string |
|
|
|
|
|
|
|
system?: boolean |
|
|
|
|
|
|
|
}[] |
|
|
|
|
|
|
|
>() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const filterQuery = ref('') |
|
|
|
const filterQuery = ref('') |
|
|
|
|
|
|
|
|
|
|
@ -39,7 +32,7 @@ export function useViewColumns(view: Ref<ViewType> | undefined, meta: ComputedRe |
|
|
|
} |
|
|
|
} |
|
|
|
}, {}) |
|
|
|
}, {}) |
|
|
|
fields.value = meta.value?.columns |
|
|
|
fields.value = meta.value?.columns |
|
|
|
?.map((column) => { |
|
|
|
?.map((column: ColumnType) => { |
|
|
|
const currentColumnField = fieldById[column.id!] || {} |
|
|
|
const currentColumnField = fieldById[column.id!] || {} |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
@ -50,13 +43,13 @@ export function useViewColumns(view: Ref<ViewType> | undefined, meta: ComputedRe |
|
|
|
system: isSystemColumn(currentColumnField.type || false), |
|
|
|
system: isSystemColumn(currentColumnField.type || false), |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
.sort((a, b) => a.order - b.order) |
|
|
|
.sort((a: Field, b: Field) => a.order - b.order) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const showAll = async (ignoreIds?: any) => { |
|
|
|
const showAll = async (ignoreIds?: any) => { |
|
|
|
if (isPublic.value) { |
|
|
|
if (isPublic.value) { |
|
|
|
fields.value = fields.value?.map((field) => ({ |
|
|
|
fields.value = fields.value?.map((field: Field) => ({ |
|
|
|
...field, |
|
|
|
...field, |
|
|
|
show: true, |
|
|
|
show: true, |
|
|
|
})) |
|
|
|
})) |
|
|
@ -79,7 +72,7 @@ export function useViewColumns(view: Ref<ViewType> | undefined, meta: ComputedRe |
|
|
|
} |
|
|
|
} |
|
|
|
const hideAll = async (ignoreIds?: any) => { |
|
|
|
const hideAll = async (ignoreIds?: any) => { |
|
|
|
if (isPublic.value) { |
|
|
|
if (isPublic.value) { |
|
|
|
fields.value = fields.value?.map((field) => ({ |
|
|
|
fields.value = fields.value?.map((field: Field) => ({ |
|
|
|
...field, |
|
|
|
...field, |
|
|
|
show: false, |
|
|
|
show: false, |
|
|
|
})) |
|
|
|
})) |
|
|
@ -101,9 +94,9 @@ export function useViewColumns(view: Ref<ViewType> | undefined, meta: ComputedRe |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const saveOrUpdate = async (field: any, index: number) => { |
|
|
|
const saveOrUpdate = async (field: any, index: number) => { |
|
|
|
if (isPublic && fields.value) { |
|
|
|
if (isPublic.value && fields.value) { |
|
|
|
fields.value[index] = field |
|
|
|
fields.value[index] = field |
|
|
|
meta.value.columns = meta.value?.columns?.map((column) => { |
|
|
|
meta.value.columns = meta.value?.columns?.map((column: ColumnType) => { |
|
|
|
if (column.id === field.fk_column_id) { |
|
|
|
if (column.id === field.fk_column_id) { |
|
|
|
return { |
|
|
|
return { |
|
|
|
...column, |
|
|
|
...column, |
|
|
@ -128,14 +121,15 @@ export function useViewColumns(view: Ref<ViewType> | undefined, meta: ComputedRe |
|
|
|
reloadData?.() |
|
|
|
reloadData?.() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const metaColumnById = computed(() => { |
|
|
|
const metaColumnById = computed<Record<string, ColumnType>>(() => { |
|
|
|
return ( |
|
|
|
if (!meta.value?.columns) return {} |
|
|
|
meta.value?.columns?.reduce<Record<string, ColumnType>>((acc, curr) => { |
|
|
|
|
|
|
|
return { |
|
|
|
return meta.value?.columns?.reduce( |
|
|
|
|
|
|
|
(acc: ColumnType, curr: ColumnType) => ({ |
|
|
|
...acc, |
|
|
|
...acc, |
|
|
|
[curr.id!]: curr, |
|
|
|
[curr.id!]: curr, |
|
|
|
} |
|
|
|
}), |
|
|
|
}, {}) || {} |
|
|
|
{} as any, |
|
|
|
) |
|
|
|
) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
@ -160,33 +154,33 @@ export function useViewColumns(view: Ref<ViewType> | undefined, meta: ComputedRe |
|
|
|
|
|
|
|
|
|
|
|
const filteredFieldList = computed(() => { |
|
|
|
const filteredFieldList = computed(() => { |
|
|
|
return ( |
|
|
|
return ( |
|
|
|
fields.value?.filter((field) => { |
|
|
|
fields.value?.filter((field: Field) => { |
|
|
|
// hide system columns if not enabled
|
|
|
|
// hide system columns if not enabled
|
|
|
|
if (!showSystemFields.value && isSystemColumn(metaColumnById?.value?.[field.fk_column_id!])) { |
|
|
|
if (!showSystemFields.value && isSystemColumn(metaColumnById?.value?.[field.fk_column_id!])) { |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return !filterQuery?.value || field.title.toLowerCase().includes(filterQuery.value.toLowerCase()) |
|
|
|
return !filterQuery?.value || field.title.toLowerCase().includes(filterQuery.value.toLowerCase()) |
|
|
|
}) || {} |
|
|
|
}) || [] |
|
|
|
) |
|
|
|
) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
const sortedAndFilteredFields = computed<ColumnType[]>(() => { |
|
|
|
const sortedAndFilteredFields = computed<ColumnType[]>(() => { |
|
|
|
return (fields?.value |
|
|
|
return (fields?.value |
|
|
|
?.filter((c) => { |
|
|
|
?.filter((field: Field) => { |
|
|
|
// hide system columns if not enabled
|
|
|
|
// hide system columns if not enabled
|
|
|
|
if ( |
|
|
|
if ( |
|
|
|
!showSystemFields.value && |
|
|
|
!showSystemFields.value && |
|
|
|
metaColumnById.value && |
|
|
|
metaColumnById.value && |
|
|
|
metaColumnById?.value?.[c.fk_column_id!] && |
|
|
|
metaColumnById?.value?.[field.fk_column_id!] && |
|
|
|
isSystemColumn(metaColumnById.value?.[c.fk_column_id!]) |
|
|
|
isSystemColumn(metaColumnById.value?.[field.fk_column_id!]) |
|
|
|
) { |
|
|
|
) { |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
return c.show && metaColumnById?.value?.[c.fk_column_id!] |
|
|
|
return field.show && metaColumnById?.value?.[field.fk_column_id!] |
|
|
|
}) |
|
|
|
}) |
|
|
|
?.sort((a, b) => a.order - b.order) |
|
|
|
?.sort((a: Field, b: Field) => a.order - b.order) |
|
|
|
?.map((c) => metaColumnById?.value?.[c.fk_column_id!]) || []) as ColumnType[] |
|
|
|
?.map((field: Field) => metaColumnById?.value?.[field.fk_column_id!]) || []) as ColumnType[] |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
// reload view columns when table meta changes
|
|
|
|
// reload view columns when table meta changes
|
|
|
|