|
|
@ -1,20 +1,22 @@ |
|
|
|
import type { FilterType, ViewType } from 'nocodb-sdk' |
|
|
|
import type { ViewType } from 'nocodb-sdk' |
|
|
|
import type { ComputedRef, Ref } from 'vue' |
|
|
|
import type { ComputedRef, Ref } from 'vue' |
|
|
|
import { useNuxtApp, useUIPermission } from '#imports' |
|
|
|
import { useNuxtApp, useUIPermission } from '#imports' |
|
|
|
import { useMetas } from '~/composables/useMetas' |
|
|
|
import { useMetas } from '~/composables/useMetas' |
|
|
|
import { IsPublicInj, ReloadViewDataHookInj } from '~/context' |
|
|
|
import { IsPublicInj, ReloadViewDataHookInj } from '~/context' |
|
|
|
|
|
|
|
import type { Filter } from '~/lib' |
|
|
|
|
|
|
|
|
|
|
|
export function useViewFilters( |
|
|
|
export function useViewFilters( |
|
|
|
view: Ref<ViewType> | undefined, |
|
|
|
view: Ref<ViewType> | undefined, |
|
|
|
parentId?: string, |
|
|
|
parentId?: string, |
|
|
|
autoApply?: ComputedRef<boolean>, |
|
|
|
autoApply?: ComputedRef<boolean>, |
|
|
|
reloadData?: () => void, |
|
|
|
reloadData?: () => void, |
|
|
|
|
|
|
|
siblingFilters?: Filter[], |
|
|
|
) { |
|
|
|
) { |
|
|
|
const { filters: sharedViewFilter } = useSharedView() |
|
|
|
const { nestedFilters } = useSharedView() |
|
|
|
|
|
|
|
|
|
|
|
const reloadHook = inject(ReloadViewDataHookInj) |
|
|
|
const reloadHook = inject(ReloadViewDataHookInj) |
|
|
|
|
|
|
|
|
|
|
|
const _filters = ref<(FilterType & { status?: 'update' | 'delete' | 'create'; parentId?: string })[]>([]) |
|
|
|
const _filters = ref<Filter[]>([]) |
|
|
|
|
|
|
|
|
|
|
|
const isPublic = inject(IsPublicInj, ref(false)) |
|
|
|
const isPublic = inject(IsPublicInj, ref(false)) |
|
|
|
const { $api } = useNuxtApp() |
|
|
|
const { $api } = useNuxtApp() |
|
|
@ -22,10 +24,15 @@ export function useViewFilters( |
|
|
|
const { metas } = useMetas() |
|
|
|
const { metas } = useMetas() |
|
|
|
|
|
|
|
|
|
|
|
const filters = computed({ |
|
|
|
const filters = computed({ |
|
|
|
get: () => (isPublic.value ? sharedViewFilter.value : _filters.value), |
|
|
|
get: () => (isPublic.value ? siblingFilters || nestedFilters.value : _filters.value), |
|
|
|
set: (value) => { |
|
|
|
set: (value) => { |
|
|
|
if (isPublic.value) { |
|
|
|
if (isPublic.value) { |
|
|
|
sharedViewFilter.value = value |
|
|
|
if (siblingFilters) { |
|
|
|
|
|
|
|
siblingFilters = value |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
nestedFilters.value = value |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
nestedFilters.value = [...nestedFilters.value] |
|
|
|
reloadHook?.trigger() |
|
|
|
reloadHook?.trigger() |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
_filters.value = value |
|
|
|
_filters.value = value |
|
|
@ -33,6 +40,13 @@ export function useViewFilters( |
|
|
|
}, |
|
|
|
}, |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const placeholderFilter: Filter = { |
|
|
|
|
|
|
|
comparison_op: 'eq', |
|
|
|
|
|
|
|
value: '', |
|
|
|
|
|
|
|
status: 'create', |
|
|
|
|
|
|
|
logical_op: 'and', |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const loadFilters = async (hookId?: string) => { |
|
|
|
const loadFilters = async (hookId?: string) => { |
|
|
|
if (isPublic.value) return |
|
|
|
if (isPublic.value) return |
|
|
|
|
|
|
|
|
|
|
@ -78,7 +92,7 @@ export function useViewFilters( |
|
|
|
reloadData?.() |
|
|
|
reloadData?.() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const deleteFilter = async (filter: FilterType & { status: string }, i: number) => { |
|
|
|
const deleteFilter = async (filter: Filter, i: number) => { |
|
|
|
// if shared or sync permission not allowed simply remove it from array
|
|
|
|
// if shared or sync permission not allowed simply remove it from array
|
|
|
|
if (isPublic.value || !isUIAllowed('filterSync')) { |
|
|
|
if (isPublic.value || !isUIAllowed('filterSync')) { |
|
|
|
filters.value.splice(i, 1) |
|
|
|
filters.value.splice(i, 1) |
|
|
@ -101,9 +115,14 @@ export function useViewFilters( |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const saveOrUpdate = async (filter: FilterType & { status?: string }, i: number, force = false) => { |
|
|
|
const saveOrUpdate = async (filter: Filter, i: number, force = false) => { |
|
|
|
|
|
|
|
if (isPublic.value) { |
|
|
|
|
|
|
|
filters.value[i] = { ...filter } as any |
|
|
|
|
|
|
|
filters.value = [...filters.value] |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
if (!view?.value) return |
|
|
|
if (!view?.value) return |
|
|
|
if (isPublic.value || !isUIAllowed('filterSync')) { |
|
|
|
if (!isUIAllowed('filterSync')) { |
|
|
|
// skip
|
|
|
|
// skip
|
|
|
|
} else if (!autoApply?.value && !force) { |
|
|
|
} else if (!autoApply?.value && !force) { |
|
|
|
filter.status = filter.id ? 'update' : 'create' |
|
|
|
filter.status = filter.id ? 'update' : 'create' |
|
|
@ -123,21 +142,19 @@ export function useViewFilters( |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const addFilter = () => { |
|
|
|
const addFilter = () => { |
|
|
|
filters.value.push({ |
|
|
|
filters.value.push(placeholderFilter) |
|
|
|
comparison_op: 'eq', |
|
|
|
|
|
|
|
value: '', |
|
|
|
|
|
|
|
status: 'create', |
|
|
|
|
|
|
|
logical_op: 'and', |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const addFilterGroup = async () => { |
|
|
|
const addFilterGroup = async () => { |
|
|
|
filters.value.push({ |
|
|
|
const child = placeholderFilter |
|
|
|
parentId, |
|
|
|
const placeHolderGroupFilter: Filter = { |
|
|
|
is_group: true, |
|
|
|
is_group: true, |
|
|
|
status: 'create', |
|
|
|
status: 'create', |
|
|
|
logical_op: 'and', |
|
|
|
logical_op: 'and', |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
if (isPublic.value) placeHolderGroupFilter.children = [child] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
filters.value.push(placeHolderGroupFilter) |
|
|
|
const index = filters.value.length - 1 |
|
|
|
const index = filters.value.length - 1 |
|
|
|
await saveOrUpdate(filters.value[index], index, true) |
|
|
|
await saveOrUpdate(filters.value[index], index, true) |
|
|
|
} |
|
|
|
} |
|
|
|