Skip to content

Commit 3e781ac

Browse files
committed
refactor(desk): centralize view edition in module
1 parent d811924 commit 3e781ac

File tree

13 files changed

+450
-326
lines changed

13 files changed

+450
-326
lines changed

packages/core/entities/view-gallery.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import View, { ViewColumn } from './view'
2-
3-
export interface ViewGalleryColumn extends ViewColumn {
4-
hide: boolean
5-
}
1+
import View from './view'
62

73
export interface ViewGallerySize {
84
width: number | string
@@ -24,8 +20,6 @@ export interface ViewGalleryThumbnail {
2420
export default class ViewGallery extends View {
2521
public readonly component = 'gallery'
2622

27-
public columns: ViewGalleryColumn[] = []
28-
2923
public thumbnail: ViewGalleryThumbnail = {
3024
key: '',
3125
position: '',

packages/core/entities/view.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,17 @@ export interface ViewFilter {
1111

1212
export interface ViewColumn extends Partial<Column> {
1313
id: string // link with column
14+
hide?: boolean
1415
}
1516

1617
export default class View {
1718
public id = ''
1819
public filters: ViewFilter[] = []
1920
public columns: ViewColumn[] = []
2021

21-
constructor(props?: Omit<View, 'id'>, id?: string) {
22+
constructor(props?: Partial<View>, id?: string) {
2223
Object.assign(this, props)
2324

2425
this.id = id || uuid()
2526
}
26-
27-
public merge(payload: Partial<View>) {
28-
const { columns, ...data } = payload
29-
30-
if (!columns) {
31-
return Object.assign(this, data)
32-
}
33-
34-
columns.forEach((c) => {
35-
const cColumn = this.columns.find((cv) => cv.id === c.id)
36-
37-
Object.assign(cColumn || {}, c)
38-
})
39-
40-
this.columns.sort((a, b) => {
41-
const aIndex = columns.findIndex((s) => s.id === a.id)
42-
const bIndex = columns.findIndex((s) => s.id === b.id)
43-
44-
if (aIndex === -1 || bIndex === -1) return 0
45-
46-
return aIndex - bIndex
47-
})
48-
}
4927
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<script setup lang="ts">
2+
import { debounce } from 'lodash'
3+
import { ref, watch, computed, onMounted } from 'vue'
4+
import { useStore } from '../store'
5+
6+
import CDrawerFilter from './CDrawerFilter.vue'
7+
import CDrawerHideColumns from './CDrawerHideColumns.vue'
8+
9+
// Props & emit
10+
const props = defineProps({
11+
collectionId: {
12+
type: String,
13+
required: true,
14+
},
15+
viewId: {
16+
type: String,
17+
default: '',
18+
},
19+
})
20+
21+
// collection
22+
const store = useStore()
23+
24+
const collection = computed(() => store.collections.find((c) => c.id === props.collectionId))
25+
26+
onMounted(async () => {
27+
if (!collection.value) {
28+
await store.setCollections()
29+
}
30+
})
31+
32+
// view
33+
34+
const view = computed(() => store.view.getRegister(props.collectionId, props.viewId).view)
35+
36+
watch(props, () => store.view.setRegister(props.collectionId, props.viewId), {
37+
deep: true,
38+
immediate: true,
39+
})
40+
41+
const search = ref({
42+
input: '',
43+
show: false,
44+
onInput: debounce((v) => (search.value.input = v), 100),
45+
})
46+
47+
async function load() {
48+
await store.item.setRegister(props.collectionId, {
49+
filters: view.value.filters,
50+
search: search.value.input,
51+
})
52+
}
53+
54+
watch(() => search.value.input, debounce(load, 500))
55+
</script>
56+
<template>
57+
<v-card-head v-if="collection">
58+
<slot name="left" />
59+
60+
<div class="grow"></div>
61+
62+
<div class="flex items-center transition-all">
63+
<transition name="slide-left">
64+
<is-input
65+
v-if="search.show || !!search.input"
66+
:model-value="search.input"
67+
:placeholder="$t('search')"
68+
size="sm"
69+
class="w-[300px] mr-2"
70+
@update:model-value="search.onInput"
71+
>
72+
<template #append>
73+
<v-btn
74+
v-if="search.input"
75+
text
76+
size="none"
77+
class="w-5 h-5"
78+
@click="search.input = ''"
79+
>
80+
<is-icon name="times" />
81+
</v-btn>
82+
</template>
83+
</is-input>
84+
</transition>
85+
86+
<v-btn text size="sm" @click="search.show = !search.show">
87+
<is-icon name="search" />
88+
</v-btn>
89+
90+
<v-btn text size="sm" @click="load">
91+
<is-icon name="rotate" />
92+
</v-btn>
93+
94+
<c-drawer-hide-columns v-model="view.columns" :collection-id="collectionId" />
95+
96+
<c-drawer-filter v-model="view.filters" :columns="collection?.columns" />
97+
</div>
98+
</v-card-head>
99+
</template>

packages/desktop/modules/collection/components/CDrawerFilter.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<script setup lang="ts">
22
import { ref } from 'vue'
33
4-
import { CollectionColumn } from '@/../core/entities/collection'
4+
import Column from '@core/entities/column'
55
import { useVModel } from 'vue-wind/composables/v-model'
6-
import { Filter } from '../composables/filter'
6+
import { ViewFilter } from '@core/entities/view'
77
88
import CFilter from './CFilter.vue'
99
1010
const props = defineProps({
1111
modelValue: {
12-
type: Array as () => Filter[],
12+
type: Array as () => ViewFilter[],
1313
default: () => [],
1414
},
1515
columns: {
16-
type: Array as () => CollectionColumn[],
16+
type: Array as () => Column[],
1717
default: () => [],
1818
},
1919
})
@@ -28,7 +28,7 @@ function clear() {
2828
filters.value = []
2929
}
3030
31-
function add(column: CollectionColumn) {
31+
function add(column: Column) {
3232
filters.value.push({
3333
columnId: column.id,
3434
field: column.field,

packages/desktop/modules/collection/components/CDrawerHideColumns.vue

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
<script setup lang="ts">
2-
import { ref } from 'vue'
3-
4-
import { CollectionColumn } from '@/../core/entities/collection'
2+
import { computed, ref } from 'vue'
53
import { useVModel } from '@vueuse/core'
6-
import { ViewGalleryColumn } from '@/../core/entities/view'
4+
5+
import View, { ViewColumn } from '@/../core/entities/view'
76
87
import VDraggable from 'vuedraggable'
8+
import { useStore } from '../store'
99
1010
const props = defineProps({
1111
modelValue: {
12-
type: Array as () => (ViewGalleryColumn & CollectionColumn)[],
12+
type: Array as () => View['columns'],
1313
default: () => [],
1414
},
15+
collectionId: {
16+
type: String,
17+
required: true,
18+
},
1519
})
1620
1721
const emit = defineEmits(['update:modelValue'])
@@ -31,6 +35,17 @@ function hideAll() {
3135
c.hide = true
3236
})
3337
}
38+
39+
// get label
40+
const store = useStore()
41+
42+
const collection = computed(() => store.collections.find((c) => c.id === props.collectionId))
43+
44+
function getLabel(viewColumn: ViewColumn) {
45+
const column = collection.value?.columns.find((c) => c.id === viewColumn.id)
46+
47+
return column?.label || viewColumn.id
48+
}
3449
</script>
3550

3651
<template>
@@ -75,7 +90,7 @@ function hideAll() {
7590
</v-btn>
7691

7792
<div>
78-
{{ column.label }}
93+
{{ getLabel(column) }}
7994
</div>
8095
</is-list-item>
8196
</template>

packages/desktop/modules/collection/components/CFilter.vue

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script setup lang="ts">
2-
import { CollectionColumn } from '@/../core/entities/collection'
2+
import Column from '@core/entities/column'
33
import { computed } from 'vue'
44
import { useVModel } from 'vue-wind/composables/v-model'
5-
import { Filter } from '../composables/filter'
5+
import { ViewFilter } from '@core/entities/view'
66
77
import CFilterNumber from './CFilterNumber.vue'
88
import CFilterRelation from './CFilterRelation.vue'
@@ -14,11 +14,11 @@ import CFilterText from './CFilterText.vue'
1414
1515
const props = defineProps({
1616
modelValue: {
17-
type: Object as () => Filter,
17+
type: Object as () => ViewFilter,
1818
default: null,
1919
},
2020
columns: {
21-
type: Array as () => CollectionColumn[],
21+
type: Array as () => Column[],
2222
default: () => [],
2323
},
2424
})
@@ -29,12 +29,13 @@ const emit = defineEmits(['update:modelValue', 'destroy'])
2929
const model = useVModel(props, 'modelValue', emit)
3030
3131
// set component
32-
const components: Record<CollectionColumn['type'], any> = {
32+
const components: Record<Column['type'], any> = {
3333
text: CFilterText,
3434
number: CFilterNumber,
3535
relation: CFilterRelation,
3636
select: CFilterSelect,
3737
script: CFilterScript,
38+
entry: CFilterText,
3839
}
3940
4041
const column = computed(() => props.columns.find((c) => c.id === model.value.columnId))

0 commit comments

Comments
 (0)