mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.5 KiB
80 lines
2.5 KiB
2 years ago
|
import { ViewTypes } from 'nocodb-sdk'
|
||
2 years ago
|
import type { FilterType, SortType, TableType, ViewType } from 'nocodb-sdk'
|
||
2 years ago
|
import type { Ref } from 'vue'
|
||
2 years ago
|
import { computed, reactive, useInjectionState, useNuxtApp, useProject, useTemplateRefsList } from '#imports'
|
||
2 years ago
|
|
||
2 years ago
|
const [useProvideSmartsheetStore, useSmartsheetStore] = useInjectionState(
|
||
2 years ago
|
(
|
||
|
view: Ref<ViewType>,
|
||
|
meta: Ref<TableType>,
|
||
|
shared = false,
|
||
|
initalSorts?: Ref<SortType[]>,
|
||
|
initialFilters?: Ref<FilterType[]>,
|
||
|
) => {
|
||
2 years ago
|
const { $api } = useNuxtApp()
|
||
|
const { sqlUi } = useProject()
|
||
|
|
||
|
const cellRefs = useTemplateRefsList<HTMLTableDataCellElement>()
|
||
|
|
||
|
// state
|
||
|
// todo: move to grid view store
|
||
|
const search = reactive({
|
||
|
field: '',
|
||
|
query: '',
|
||
|
})
|
||
|
|
||
|
// getters
|
||
|
const isLocked = computed(() => (view?.value as any)?.lock_type === 'locked')
|
||
|
const isPkAvail = computed(() => meta?.value?.columns?.some((c) => c.pk))
|
||
|
const isGrid = computed(() => (view?.value as any)?.type === ViewTypes.GRID)
|
||
|
const isForm = computed(() => (view?.value as any)?.type === ViewTypes.FORM)
|
||
|
const isSharedForm = computed(() => isForm?.value && shared)
|
||
|
const isGallery = computed(() => (view?.value as any)?.type === ViewTypes.GALLERY)
|
||
|
const xWhere = computed(() => {
|
||
|
let where
|
||
|
const col = meta?.value?.columns?.find(({ id }) => id === search.field) || meta?.value?.columns?.find((v) => v.pv)
|
||
|
if (!col) return
|
||
|
|
||
|
if (!search.query.trim()) return
|
||
|
if (['text', 'string'].includes(sqlUi.value.getAbstractType(col)) && col.dt !== 'bigint') {
|
||
|
where = `(${col.title},like,%${search.query.trim()}%)`
|
||
|
} else {
|
||
|
where = `(${col.title},eq,${search.query.trim()})`
|
||
|
}
|
||
|
return where
|
||
|
})
|
||
2 years ago
|
|
||
|
const isSqlView = computed(() => meta?.value?.type === 'view')
|
||
|
|
||
2 years ago
|
const sorts = initalSorts ?? ref<SortType[]>([])
|
||
|
const nestedFilters: Ref<FilterType[]> = initialFilters ?? ref<FilterType[]>([])
|
||
2 years ago
|
|
||
|
return {
|
||
|
view,
|
||
|
meta,
|
||
|
isLocked,
|
||
|
$api,
|
||
|
search,
|
||
|
xWhere,
|
||
|
isPkAvail,
|
||
|
isForm,
|
||
|
isGrid,
|
||
|
isGallery,
|
||
|
cellRefs,
|
||
|
isSharedForm,
|
||
2 years ago
|
sorts,
|
||
|
nestedFilters,
|
||
2 years ago
|
isSqlView,
|
||
2 years ago
|
}
|
||
2 years ago
|
},
|
||
|
'smartsheet-store',
|
||
|
)
|
||
2 years ago
|
|
||
|
export { useProvideSmartsheetStore }
|
||
|
|
||
|
export function useSmartsheetStoreOrThrow() {
|
||
|
const smartsheetStore = useSmartsheetStore()
|
||
|
if (smartsheetStore == null) throw new Error('Please call `useSmartsheetStore` on the appropriate parent component')
|
||
|
return smartsheetStore
|
||
|
}
|