tiny-rdm/frontend/src/components/content_value/ContentValueHash.vue

310 lines
9.3 KiB
Vue
Raw Normal View History

2023-06-27 15:53:29 +08:00
<script setup>
import { computed, h, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import ContentToolbar from './ContentToolbar.vue'
2023-08-02 17:57:39 +08:00
import AddLink from '@/components/icons/AddLink.vue'
2023-08-20 14:42:36 +08:00
import { NButton, NCode, NIcon, NInput } from 'naive-ui'
2023-08-02 17:57:39 +08:00
import { types, types as redisTypes } from '@/consts/support_redis_type.js'
import EditableTableColumn from '@/components/common/EditableTableColumn.vue'
import useDialogStore from 'stores/dialog.js'
import useConnectionStore from 'stores/connections.js'
import { isEmpty } from 'lodash'
2023-06-27 15:53:29 +08:00
const i18n = useI18n()
const props = defineProps({
name: String,
db: Number,
keyPath: String,
keyCode: {
type: Array,
default: null,
},
2023-06-27 15:53:29 +08:00
ttl: {
type: Number,
default: -1,
},
value: Object,
size: Number,
2023-06-27 15:53:29 +08:00
})
/**
*
* @type {ComputedRef<string|number[]>}
*/
const keyName = computed(() => {
return !isEmpty(props.keyCode) ? props.keyCode : props.keyPath
})
const filterOption = [
{
value: 1,
label: i18n.t('common.field'),
},
{
value: 2,
label: i18n.t('common.value'),
},
]
const filterType = ref(1)
2023-06-27 15:53:29 +08:00
const connectionStore = useConnectionStore()
const dialogStore = useDialogStore()
const keyType = redisTypes.HASH
const currentEditRow = ref({
no: 0,
key: '',
value: null,
})
const fieldColumn = reactive({
key: 'key',
title: i18n.t('common.field'),
2023-06-27 15:53:29 +08:00
align: 'center',
titleAlign: 'center',
resizable: true,
filterOptionValue: null,
filter(value, row) {
return !!~row.key.indexOf(value.toString())
},
// sorter: (row1, row2) => row1.key - row2.key,
render: (row) => {
const isEdit = currentEditRow.value.no === row.no
if (isEdit) {
return h(NInput, {
value: currentEditRow.value.key,
'onUpdate:value': (val) => {
currentEditRow.value.key = val
},
})
} else {
return row.key
}
},
})
const valueColumn = reactive({
key: 'value',
title: i18n.t('common.value'),
2023-06-27 15:53:29 +08:00
align: 'center',
titleAlign: 'center',
resizable: true,
filterOptionValue: null,
filter(value, row) {
return !!~row.value.indexOf(value.toString())
},
// sorter: (row1, row2) => row1.value - row2.value,
// ellipsis: {
// tooltip: true
// },
render: (row) => {
const isEdit = currentEditRow.value.no === row.no
if (isEdit) {
return h(NInput, {
value: currentEditRow.value.value,
type: 'textarea',
autosize: { minRow: 2, maxRows: 5 },
style: 'text-align: left;',
'onUpdate:value': (val) => {
currentEditRow.value.value = val
},
})
} else {
return h(NCode, { language: 'plaintext', wordWrap: true }, { default: () => row.value })
}
},
})
const actionColumn = {
key: 'action',
title: i18n.t('interface.action'),
2023-06-27 15:53:29 +08:00
width: 100,
align: 'center',
titleAlign: 'center',
fixed: 'right',
render: (row) => {
return h(EditableTableColumn, {
editing: currentEditRow.value.no === row.no,
bindKey: row.key,
onEdit: () => {
currentEditRow.value.no = row.no
currentEditRow.value.key = row.key
currentEditRow.value.value = row.value
},
onDelete: async () => {
try {
const { success, msg } = await connectionStore.removeHashField(
props.name,
props.db,
keyName.value,
2023-08-02 17:57:39 +08:00
row.key,
2023-06-27 15:53:29 +08:00
)
if (success) {
connectionStore.loadKeyValue(props.name, props.db, keyName.value).then((r) => {})
$message.success(i18n.t('dialogue.delete_key_succ', { key: row.key }))
2023-06-27 15:53:29 +08:00
// update display value
// if (!isEmpty(removed)) {
// for (const elem of removed) {
// delete props.value[elem]
// }
// }
} else {
$message.error(msg)
2023-06-27 15:53:29 +08:00
}
} catch (e) {
$message.error(e.message)
2023-06-27 15:53:29 +08:00
}
},
onSave: async () => {
try {
const { success, msg } = await connectionStore.setHash(
props.name,
props.db,
keyName.value,
2023-06-27 15:53:29 +08:00
row.key,
currentEditRow.value.key,
2023-08-02 17:57:39 +08:00
currentEditRow.value.value,
2023-06-27 15:53:29 +08:00
)
if (success) {
connectionStore.loadKeyValue(props.name, props.db, keyName.value).then((r) => {})
$message.success(i18n.t('dialogue.save_value_succ'))
2023-06-27 15:53:29 +08:00
// update display value
// if (!isEmpty(updated)) {
// for (const key in updated) {
// props.value[key] = updated[key]
// }
// }
} else {
$message.error(msg)
2023-06-27 15:53:29 +08:00
}
} catch (e) {
$message.error(e.message)
2023-06-27 15:53:29 +08:00
} finally {
currentEditRow.value.no = 0
}
},
onCancel: () => {
currentEditRow.value.no = 0
},
})
},
}
const columns = reactive([
{
key: 'no',
title: '#',
width: 80,
align: 'center',
titleAlign: 'center',
},
fieldColumn,
valueColumn,
actionColumn,
])
const tableData = computed(() => {
const data = []
let index = 0
for (const key in props.value) {
data.push({
no: ++index,
key,
value: props.value[key],
})
}
return data
})
const onAddRow = () => {
dialogStore.openAddFieldsDialog(props.name, props.db, props.keyPath, props.keyCode, types.HASH)
2023-06-27 15:53:29 +08:00
}
const filterValue = ref('')
const onFilterInput = (val) => {
switch (filterType.value) {
case filterOption[0].value:
// filter field
2023-06-27 15:53:29 +08:00
valueColumn.filterOptionValue = null
fieldColumn.filterOptionValue = val
break
case filterOption[1].value:
// filter value
2023-06-27 15:53:29 +08:00
fieldColumn.filterOptionValue = null
valueColumn.filterOptionValue = val
break
}
}
const onChangeFilterType = (type) => {
onFilterInput(filterValue.value)
}
const clearFilter = () => {
fieldColumn.filterOptionValue = null
valueColumn.filterOptionValue = null
}
const onUpdateFilter = (filters, sourceColumn) => {
switch (filterType.value) {
case filterOption[0].value:
fieldColumn.filterOptionValue = filters[sourceColumn.key]
break
case filterOption[1].value:
valueColumn.filterOptionValue = filters[sourceColumn.key]
break
}
}
</script>
<template>
<div class="content-wrapper flex-box-v">
<content-toolbar
:db="props.db"
:key-path="props.keyPath"
:key-code="props.keyCode"
:key-type="keyType"
:server="props.name"
:ttl="ttl" />
2023-06-27 15:53:29 +08:00
<div class="tb2 flex-box-h">
<div class="flex-box-h">
<n-input-group>
<n-select
v-model:value="filterType"
:consistent-menu-width="false"
:options="filterOption"
style="width: 120px"
2023-08-24 15:23:25 +08:00
@update:value="onChangeFilterType" />
2023-06-27 15:53:29 +08:00
<n-input
v-model:value="filterValue"
:placeholder="$t('interface.search')"
2023-06-27 15:53:29 +08:00
clearable
@clear="clearFilter"
2023-08-24 15:23:25 +08:00
@update:value="onFilterInput" />
2023-06-27 15:53:29 +08:00
</n-input-group>
</div>
<div class="tb2-extra-info flex-item-expand">
<n-tag size="large">{{ $t('interface.total', { size: props.size }) }}</n-tag>
</div>
<n-button plain :focusable="false" @click="onAddRow">
2023-06-27 15:53:29 +08:00
<template #icon>
<n-icon :component="AddLink" size="18" />
</template>
{{ $t('interface.add_row') }}
2023-06-27 15:53:29 +08:00
</n-button>
</div>
2023-09-02 18:23:40 +08:00
<div class="value-wrapper fill-height flex-box-h">
2023-06-27 15:53:29 +08:00
<n-data-table
:key="(row) => row.no"
:columns="columns"
:data="tableData"
:single-column="true"
:single-line="false"
2023-09-02 18:23:40 +08:00
:bordered="false"
:bottom-bordered="false"
2023-06-27 15:53:29 +08:00
flex-height
max-height="100%"
size="small"
striped
virtual-scroll
2023-08-24 15:23:25 +08:00
@update:filters="onUpdateFilter" />
2023-06-27 15:53:29 +08:00
</div>
</div>
</template>
<style lang="scss" scoped></style>