hrms-edm/Services/client/src/components/ListView.vue

523 lines
13 KiB
Vue
Raw Normal View History

2023-11-28 09:22:44 +07:00
<script lang="ts" setup>
import { computed, ref } from 'vue'
import { storeToRefs } from 'pinia'
import type { QTableProps } from 'quasar'
2023-11-28 09:22:44 +07:00
import { useTreeDataStore, type TreeDataFolder } from '@/stores/tree-data'
import { useFileInfoStore } from '@/stores/file-info-data'
import FileIcon from '@/components/FileIcon.vue'
2023-11-29 17:11:21 +07:00
import DialogDelete from '@/components/DialogDelete.vue'
2023-12-06 10:34:13 +07:00
import UploadExistDialog from './UploadExistDialog.vue'
2023-11-28 17:44:56 +07:00
import FileForm from './FileForm.vue'
import FolderForm from './FolderForm.vue'
2023-11-28 09:22:44 +07:00
const { getFormatDate, getSize, getType, getFileInfo } = useFileInfoStore()
2023-12-04 02:45:11 +00:00
const { listDataFile, listDataFolder, currentDept, currentPath } =
storeToRefs(useTreeDataStore())
2023-11-28 17:44:56 +07:00
const {
createFolder,
editFolder,
getFolder,
deleteFolder,
uploadFile,
updateFile,
deleteFile,
checkFile,
2023-11-28 17:44:56 +07:00
} = useTreeDataStore()
const DEPT_NAME = ['ตู้เอกสาร', 'ลิ้นชัก', 'แฟ้ม', 'แฟ้มย่อย'] as const
const props = defineProps<{
mode: 'admin' | 'user'
}>()
2023-11-28 09:22:44 +07:00
const currentLevel = computed(() =>
currentDept.value === 0
? 'ตู้จัดเก็บเอกสาร'
: currentDept.value === 1
2023-12-04 02:45:11 +00:00
? 'ลิ้นชัก'
: currentDept.value === 2
? 'แฟ้ม'
: 'แฟ้มย่อย',
2023-11-28 09:22:44 +07:00
)
const currentIcon = computed(() =>
currentDept.value === 0
? 'mdi-file-cabinet'
: currentDept.value === 1
2023-12-04 02:45:11 +00:00
? 'inbox'
: 'o_folder_open',
2023-11-28 09:22:44 +07:00
)
2023-11-28 17:44:56 +07:00
const folderFormState = ref<boolean>(false)
const folderFormPath = ref<string>('')
const folderFormData = ref<{
name?: string
}>({})
const folderFormType = ref<'edit' | 'create'>('create')
const fileFormError = ref<{ fileExist?: boolean }>({})
2023-12-01 11:46:55 +07:00
const fileExistNotification = ref<boolean>(false)
2023-11-28 17:44:56 +07:00
const fileFormState = ref<boolean>(false)
const fileFormPath = ref<string>('')
const fileFormData = ref<{
file?: File
title?: string
description?: string
2023-12-01 11:46:55 +07:00
keyword?: string[]
category?: string[]
2023-11-28 17:44:56 +07:00
}>({})
const fileFormType = ref<'edit' | 'create'>('create')
const fileFormComponent = ref<InstanceType<typeof FileForm>>()
2023-11-28 17:44:56 +07:00
2023-11-29 17:11:21 +07:00
const dialogDeleteState = ref<boolean>(false)
const deleteFormPath = ref<string>('')
const deleteFormType = ref<'deleteFolder' | 'deleteFile'>()
function triggerFolderDelete(pathname: string) {
deleteFormType.value = 'deleteFolder'
deleteFormPath.value = pathname
dialogDeleteState.value = !dialogDeleteState.value
}
function triggerFileDelete(pathname: string) {
deleteFormType.value = 'deleteFile'
deleteFormPath.value = pathname
dialogDeleteState.value = !dialogDeleteState.value
}
2023-11-28 17:44:56 +07:00
function triggerFolderCreate() {
folderFormType.value = 'create'
folderFormData.value = {}
folderFormState.value = !folderFormState.value
}
function triggerFolderEdit(name: string, pathname: string) {
folderFormType.value = 'edit'
folderFormPath.value = pathname
folderFormData.value.name = name
folderFormState.value = true
}
async function submitFolderForm(value: {
mode: 'create' | 'edit'
name: string
}) {
if (value.mode === 'create') {
await createFolder(value.name)
} else {
await editFolder(value.name, folderFormPath.value)
}
}
function triggerFileCreate() {
fileFormType.value = 'create'
fileFormData.value = {}
fileFormState.value = !fileFormState.value
}
function triggerFileEdit(
value: {
title: string
description: string
2023-12-01 11:46:55 +07:00
keyword: string[]
category: string[]
2023-11-28 17:44:56 +07:00
},
2023-12-04 02:45:11 +00:00
pathname: string,
2023-11-28 17:44:56 +07:00
) {
fileFormState.value = true
fileFormType.value = 'edit'
fileFormPath.value = pathname
fileFormData.value = {
title: value.title,
description: value.description,
keyword: value.keyword,
2023-12-01 11:46:55 +07:00
category: value.category,
2023-11-28 17:44:56 +07:00
}
}
2023-12-01 11:46:55 +07:00
const currentParam = ref<Parameters<typeof submitFileForm>[0]>()
async function submitFileForm(
value: {
mode: 'create' | 'edit'
file?: File
title: string
description: string
keyword: string[]
category: string[]
},
2023-12-04 02:45:11 +00:00
force = false,
2023-12-01 11:46:55 +07:00
) {
currentParam.value = value
if (value.file && checkFile(value.file.name) && !force) {
fileExistNotification.value = true
return
}
if (value.mode === 'create' && value.file) {
2023-11-28 17:44:56 +07:00
await uploadFile(currentPath.value, value.file, {
title: value.title,
description: value.description,
keyword: value.keyword,
category: value.category,
})
} else {
await updateFile(
fileFormPath.value,
{
title: value.title,
description: value.description,
keyword: value.keyword,
category: value.category,
},
2023-12-04 02:45:11 +00:00
value.file,
2023-11-28 17:44:56 +07:00
)
}
fileFormData.value = {}
fileFormState.value = false
2023-12-01 11:46:55 +07:00
currentParam.value = undefined
fileFormComponent.value?.reset()
2023-11-28 17:44:56 +07:00
}
2023-11-28 09:22:44 +07:00
const columnsFolder: QTableProps['columns'] = [
{
name: 'name',
required: true,
label: 'ชื่อ',
align: 'left',
field: (row) => row.name,
format: (val) => `${val}`,
sortable: true,
style: 'width: 1000px',
},
{
name: 'createdBy',
align: 'center',
label: 'สร้างโดย',
field: 'createdBy',
style: 'width: 20px',
sortable: true,
},
{
name: 'createdAt',
2023-11-28 09:22:44 +07:00
align: 'center',
label: 'วันที่สร้าง',
field: 'createdAt',
sortable: true,
style: 'width: 20px',
},
{
name: 'actions',
align: 'center',
label: '',
field: '',
style: 'width: 5px',
},
]
const columnsFile: QTableProps['columns'] = [
{
name: 'name',
2023-11-28 09:22:44 +07:00
required: true,
label: 'ชื่อไฟล์',
align: 'left',
field: (row) => row.fileName,
format: (val) => `${val}`,
sortable: true,
style: 'width: 200px',
},
{
name: 'title',
align: 'center',
label: 'ชื่อเรื่อง',
field: 'title',
style: 'width: 200px',
sortable: true,
},
{
name: 'fileType',
align: 'center',
label: 'ประเภทของไฟล์',
field: 'fileType',
sortable: true,
style: 'width: 200px',
},
{
name: 'actions',
align: 'center',
label: '',
field: '',
style: 'width: 20px',
},
]
2023-11-28 09:22:44 +07:00
const onRowClick = (evt: Event, row: TreeDataFolder, index: number) => {
getFolder(row.pathname)
}
2023-11-28 09:22:44 +07:00
</script>
<template>
2023-11-30 17:13:00 +07:00
<div class="q-mt-md">
2023-11-28 09:22:44 +07:00
<div class="q-gutter-sm">
<div
class="flex flex-break d justify-between space-between"
2023-12-01 15:40:54 +07:00
v-if="currentDept >= 1 && props.mode == 'admin' && currentDept != 4"
2023-11-28 09:22:44 +07:00
>
<div>
<span class="text-h6">{{ currentLevel }}</span>
</div>
<div>
<q-btn
outline
push
class="q-px-md q-ml-md q-py-sm"
:label="'สร้าง' + currentLevel"
type="submit"
color="primary"
dense
icon="add"
@click.stop="() => triggerFolderCreate()"
2023-12-04 09:28:40 +07:00
id="listViewFolderCreate"
2023-11-28 09:22:44 +07:00
/>
</div>
</div>
2023-11-28 17:44:56 +07:00
2023-11-28 09:22:44 +07:00
<q-table
flat
bordered
:rows="listDataFolder"
:columns="columnsFolder"
row-key="name"
hide-bottom
:rows-per-page-options="[0]"
2023-11-28 09:22:44 +07:00
@row-click="onRowClick"
class="cursor"
2023-12-04 02:45:11 +00:00
v-if="currentDept != 4"
2023-11-28 09:22:44 +07:00
>
<template v-slot:body-cell-name="nameRow">
<q-td style="width: 50%">
2023-11-28 09:22:44 +07:00
<q-icon :name="currentIcon" size="2em" color="primary" />
{{ nameRow.row.name }}
</q-td>
</template>
2023-12-06 16:11:53 +07:00
<template v-slot:body-cell-createdBy="createdByRow">
<q-td>
<div class="justify-center center-content">
{{ createdByRow.row.createdBy }}
</div>
</q-td>
</template>
<template v-slot:body-cell-createdAt="createdAtRow">
<q-td>
<div class="justify-center">
{{ getFormatDate(createdAtRow.row.createdAt) }}
</div>
</q-td>
</template>
2023-11-28 09:22:44 +07:00
<template v-slot:body-cell-actions="actionsRow">
<q-td class="justify-center">
<div>
2023-12-06 16:11:53 +07:00
<q-icon
@click.stop
class="q-ma-sm"
name="o_info"
size="2em"
color="primary"
/>
2023-11-28 09:22:44 +07:00
<q-tooltip
anchor="center left"
self="center right"
:offset="[5, 1]"
>
{{ actionsRow.row.name }}
</q-tooltip>
</div>
<div v-if="props.mode === 'admin'">
<q-btn
flat
color="positive"
dense
2023-12-06 11:39:37 +07:00
icon="o_edit"
2023-11-28 09:22:44 +07:00
@click.stop="
2023-11-28 17:44:56 +07:00
triggerFolderEdit(
actionsRow.row.name,
2023-12-04 02:45:11 +00:00
actionsRow.row.pathname,
2023-11-28 17:44:56 +07:00
)
"
2023-12-04 09:28:40 +07:00
id="listViewFolderEdit"
/>
2023-12-06 16:11:53 +07:00
<q-btn
flat
color="negative"
dense
2023-12-06 16:11:53 +07:00
:data-testid="actionsRow.row.name"
2023-12-06 11:39:37 +07:00
icon="mdi-trash-can-outline"
2023-11-29 17:11:21 +07:00
@click.stop="triggerFolderDelete(actionsRow.row.pathname)"
2023-12-04 09:28:40 +07:00
id="listViewFolderDelete"
/>
2023-11-28 09:22:44 +07:00
</div>
</q-td>
</template>
</q-table>
</div>
</div>
2023-11-30 17:13:00 +07:00
<div class="q-mt-md" v-if="currentDept >= 3">
2023-11-28 09:22:44 +07:00
<div class="q-gutter-sm">
<div class="flex flex-break d justify-between space-between">
<div>
<span class="text-h6">เอกสาร</span>
</div>
<div>
<q-btn
2023-11-28 09:22:44 +07:00
v-if="props.mode == 'admin'"
2023-11-28 09:22:44 +07:00
outline
push
class="q-px-md q-ml-md q-py-sm"
label="สร้างเอกสาร"
type="submit"
color="primary"
dense
icon="add"
@click.stop="() => triggerFileCreate()"
2023-12-04 09:28:40 +07:00
id="listViewFileCreate"
2023-11-28 09:22:44 +07:00
/>
</div>
</div>
<q-table
flat
bordered
:rows="listDataFile"
:columns="columnsFile"
row-key="name"
:pagination="{
rowsPerPage: 0,
}"
class="cursor"
2023-11-28 09:22:44 +07:00
>
<template v-slot:body-cell-name="nameRow">
<q-td
style="width: 50%"
2023-11-28 09:22:44 +07:00
@click="
() => {
currentDept >= 3
? getFileInfo(nameRow.row)
: getFolder(nameRow.row.pathname)
2023-11-28 09:22:44 +07:00
}
"
2023-12-04 09:28:40 +07:00
id="listViewGetFileInfo"
2023-11-28 09:22:44 +07:00
>
2023-11-28 09:22:44 +07:00
<file-icon size="list" :fileMimeType="nameRow.row.fileType" />
{{ nameRow.row.fileName }}
2023-11-28 09:22:44 +07:00
</q-td>
</template>
2023-11-28 09:22:44 +07:00
<template v-slot:body-cell-fileType="fileTypeRow">
<q-td>
<div class="justify-center">
{{ getType(fileTypeRow.row.fileType) }}
</div>
2023-11-28 09:22:44 +07:00
</q-td>
</template>
2023-11-28 09:22:44 +07:00
<template v-slot:body-cell-actions="actionsRow">
<q-td class="justify-center">
<div>
2023-12-06 16:11:53 +07:00
<q-icon
class="q-ma-sm"
name="o_info"
size="2em"
color="primary"
/>
2023-11-28 09:22:44 +07:00
<q-tooltip
anchor="center left"
self="center right"
:offset="[5, 1]"
>
{{ getSize(actionsRow.row.fileSize) }}
2023-11-28 09:22:44 +07:00
</q-tooltip>
</div>
<div v-if="props.mode === 'admin'">
2023-11-28 17:44:56 +07:00
<q-btn
flat
color="positive"
dense
2023-12-06 11:39:37 +07:00
icon="o_edit"
2023-12-07 11:26:19 +07:00
@click.stop="
2023-11-28 17:44:56 +07:00
() => triggerFileEdit(actionsRow.row, actionsRow.row.pathname)
"
2023-12-04 09:28:40 +07:00
id="listViewFileEdit"
2023-11-28 17:44:56 +07:00
/>
<q-btn
flat
color="negative"
dense
2023-12-06 11:39:37 +07:00
icon="mdi-trash-can-outline"
2023-12-07 11:26:19 +07:00
@click.stop="() => triggerFileDelete(actionsRow.row.pathname)"
2023-12-04 09:28:40 +07:00
id="listViewFileDelete"
2023-11-28 17:44:56 +07:00
/>
2023-11-28 09:22:44 +07:00
</div>
</q-td>
</template>
</q-table>
</div>
</div>
2023-11-28 17:44:56 +07:00
<file-form
ref="fileFormComponent"
2023-11-28 17:44:56 +07:00
:mode="fileFormType"
:error="fileFormError"
2023-11-28 17:44:56 +07:00
v-model:open="fileFormState"
v-model:title="fileFormData.title"
v-model:description="fileFormData.description"
v-model:keyword="fileFormData.keyword"
v-model:category="fileFormData.category"
@reset="() => (fileFormError = {})"
@filechange="(name: string) => (fileFormError.fileExist = checkFile(name))"
2023-11-28 17:44:56 +07:00
@submit="submitFileForm"
2023-11-28 09:24:20 +07:00
/>
2023-11-28 17:44:56 +07:00
<folder-form
:mode="folderFormType"
:tree="DEPT_NAME[currentDept]"
v-if="currentDept < 4"
v-model:open="folderFormState"
v-model:name="folderFormData.name"
@submit="submitFolderForm"
2023-11-28 09:24:20 +07:00
/>
2023-11-29 17:11:21 +07:00
2023-12-06 10:34:13 +07:00
<upload-exist-dialog
v-model:notification="fileExistNotification"
@confirm="() => currentParam && submitFileForm(currentParam, true)"
@cancel="() => (currentParam = undefined)"
/>
2023-11-29 17:11:21 +07:00
<dialog-delete
v-model:open="dialogDeleteState"
@confirm="
() =>
deleteFormType &&
(deleteFolder(deleteFormPath), deleteFile(deleteFormPath))
"
/>
2023-11-28 09:22:44 +07:00
</template>
<style lang="scss" scoped>
.justify-center {
display: flex;
justify-content: center;
align-items: center;
}
2023-11-28 09:22:44 +07:00
2023-12-06 16:11:53 +07:00
.center-content {
margin-right: 18px;
}
2023-11-28 09:22:44 +07:00
.cursor {
cursor: pointer;
}
2023-11-28 09:22:44 +07:00
</style>