2023-06-27 15:53:29 +08:00
|
|
|
<script setup>
|
|
|
|
import { computed, h, reactive, ref } from 'vue'
|
|
|
|
import { useI18n } from 'vue-i18n'
|
2023-07-03 23:48:41 +08:00
|
|
|
import ContentToolbar from './ContentToolbar.vue'
|
2023-08-02 17:57:39 +08:00
|
|
|
import AddLink from '@/components/icons/AddLink.vue'
|
2023-11-16 01:06:59 +08:00
|
|
|
import { NButton, NCode, NIcon, NInput, useThemeVars } from 'naive-ui'
|
2023-10-11 01:15:23 +08:00
|
|
|
import { isEmpty, size } from 'lodash'
|
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'
|
2023-10-30 00:54:45 +08:00
|
|
|
import bytes from 'bytes'
|
2023-10-31 17:29:56 +08:00
|
|
|
import { decodeTypes, formatTypes } from '@/consts/value_view_type.js'
|
2023-11-05 11:57:52 +08:00
|
|
|
import useBrowserStore from 'stores/browser.js'
|
2023-11-08 23:45:33 +08:00
|
|
|
import LoadList from '@/components/icons/LoadList.vue'
|
|
|
|
import LoadAll from '@/components/icons/LoadAll.vue'
|
|
|
|
import IconButton from '@/components/common/IconButton.vue'
|
2023-11-14 14:49:16 +08:00
|
|
|
import ContentEntryEditor from '@/components/content_value/ContentEntryEditor.vue'
|
|
|
|
import FormatSelector from '@/components/content_value/FormatSelector.vue'
|
|
|
|
import Edit from '@/components/icons/Edit.vue'
|
2023-06-27 15:53:29 +08:00
|
|
|
|
|
|
|
const i18n = useI18n()
|
2023-10-30 00:54:45 +08:00
|
|
|
const themeVars = useThemeVars()
|
|
|
|
|
2023-06-27 15:53:29 +08:00
|
|
|
const props = defineProps({
|
|
|
|
name: String,
|
|
|
|
db: Number,
|
|
|
|
keyPath: String,
|
2023-10-11 01:15:23 +08:00
|
|
|
keyCode: {
|
|
|
|
type: Array,
|
|
|
|
default: null,
|
|
|
|
},
|
2023-06-27 15:53:29 +08:00
|
|
|
ttl: {
|
|
|
|
type: Number,
|
|
|
|
default: -1,
|
|
|
|
},
|
2023-11-15 23:41:53 +08:00
|
|
|
value: {
|
|
|
|
// [{v: string|number[], dv: string}]
|
|
|
|
type: Array,
|
|
|
|
default: () => {},
|
|
|
|
},
|
2023-09-28 16:01:58 +08:00
|
|
|
size: Number,
|
2023-10-30 00:54:45 +08:00
|
|
|
length: Number,
|
2023-11-14 14:49:16 +08:00
|
|
|
format: {
|
2023-10-31 17:29:56 +08:00
|
|
|
type: String,
|
2023-11-13 22:41:33 +08:00
|
|
|
default: formatTypes.RAW,
|
2023-10-31 17:29:56 +08:00
|
|
|
},
|
|
|
|
decode: {
|
|
|
|
type: String,
|
|
|
|
default: decodeTypes.NONE,
|
|
|
|
},
|
2023-11-08 23:45:33 +08:00
|
|
|
end: Boolean,
|
|
|
|
loading: Boolean,
|
2023-06-27 15:53:29 +08:00
|
|
|
})
|
|
|
|
|
2023-11-08 23:45:33 +08:00
|
|
|
const emit = defineEmits(['loadmore', 'loadall', 'reload', 'rename', 'delete'])
|
|
|
|
|
2023-10-11 01:15:23 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @type {ComputedRef<string|number[]>}
|
|
|
|
*/
|
|
|
|
const keyName = computed(() => {
|
|
|
|
return !isEmpty(props.keyCode) ? props.keyCode : props.keyPath
|
|
|
|
})
|
|
|
|
|
2023-11-05 11:57:52 +08:00
|
|
|
const browserStore = useBrowserStore()
|
2023-06-27 15:53:29 +08:00
|
|
|
const dialogStore = useDialogStore()
|
|
|
|
const keyType = redisTypes.LIST
|
2023-11-14 14:49:16 +08:00
|
|
|
const currentEditRow = reactive({
|
2023-06-27 15:53:29 +08:00
|
|
|
no: 0,
|
|
|
|
value: null,
|
2023-11-14 14:49:16 +08:00
|
|
|
format: formatTypes.RAW,
|
|
|
|
decode: decodeTypes.NONE,
|
|
|
|
})
|
|
|
|
const inEdit = computed(() => {
|
|
|
|
return currentEditRow.no > 0
|
2023-06-27 15:53:29 +08:00
|
|
|
})
|
2023-11-14 22:58:44 +08:00
|
|
|
const fullEdit = ref(false)
|
|
|
|
const inFullEdit = computed(() => {
|
|
|
|
return inEdit.value && fullEdit.value
|
|
|
|
})
|
|
|
|
|
2023-11-16 01:06:59 +08:00
|
|
|
const displayCode = computed(() => {
|
|
|
|
return props.format === formatTypes.JSON
|
|
|
|
})
|
|
|
|
const valueFilterOption = ref(null)
|
|
|
|
const valueColumn = computed(() => ({
|
2023-06-27 15:53:29 +08:00
|
|
|
key: 'value',
|
2023-09-07 18:38:34 +08:00
|
|
|
title: i18n.t('common.value'),
|
2023-11-16 01:06:59 +08:00
|
|
|
align: displayCode.value ? 'left' : 'center',
|
2023-06-27 15:53:29 +08:00
|
|
|
titleAlign: 'center',
|
2023-11-16 01:06:59 +08:00
|
|
|
ellipsis: displayCode.value
|
|
|
|
? false
|
|
|
|
: {
|
|
|
|
tooltip: true,
|
|
|
|
},
|
|
|
|
filterOptionValue: valueFilterOption.value,
|
|
|
|
className: inEdit.value ? 'clickable' : '',
|
2023-11-14 14:49:16 +08:00
|
|
|
filter: (value, row) => {
|
2023-11-16 01:06:59 +08:00
|
|
|
if (row.dv) {
|
|
|
|
return !!~row.dv.indexOf(value.toString())
|
|
|
|
}
|
2023-11-14 14:49:16 +08:00
|
|
|
return !!~row.v.indexOf(value.toString())
|
2023-06-27 15:53:29 +08:00
|
|
|
},
|
|
|
|
render: (row) => {
|
2023-11-16 01:06:59 +08:00
|
|
|
if (displayCode.value) {
|
|
|
|
return h(NCode, { language: 'json', wordWrap: true, code: row.dv || row.v })
|
|
|
|
}
|
2023-11-14 14:49:16 +08:00
|
|
|
return row.dv || row.v
|
|
|
|
},
|
2023-11-16 01:06:59 +08:00
|
|
|
}))
|
2023-11-14 14:49:16 +08:00
|
|
|
|
|
|
|
const startEdit = async (no, value) => {
|
|
|
|
currentEditRow.no = no
|
2023-11-15 23:41:53 +08:00
|
|
|
currentEditRow.value = value
|
2023-11-14 14:49:16 +08:00
|
|
|
}
|
|
|
|
|
2023-11-14 17:15:02 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string|number} pos
|
|
|
|
* @param {string} value
|
2023-11-15 23:41:53 +08:00
|
|
|
* @param {decodeTypes} decode
|
|
|
|
* @param {formatTypes} format
|
2023-11-14 17:15:02 +08:00
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
2023-11-14 14:49:16 +08:00
|
|
|
const saveEdit = async (pos, value, decode, format) => {
|
|
|
|
try {
|
|
|
|
const index = parseInt(pos) - 1
|
|
|
|
const row = props.value[index]
|
|
|
|
if (row == null) {
|
|
|
|
throw new Error('row not exists')
|
|
|
|
}
|
|
|
|
|
2023-11-17 23:50:10 +08:00
|
|
|
if (isEmpty(value)) {
|
|
|
|
value = currentEditRow.value
|
|
|
|
}
|
|
|
|
|
2023-11-17 17:20:32 +08:00
|
|
|
const { success, msg } = await browserStore.updateListItem({
|
2023-11-14 14:49:16 +08:00
|
|
|
server: props.name,
|
|
|
|
db: props.db,
|
|
|
|
key: keyName.value,
|
|
|
|
index,
|
|
|
|
value,
|
|
|
|
decode,
|
|
|
|
format,
|
|
|
|
})
|
|
|
|
if (success) {
|
|
|
|
$message.success(i18n.t('dialogue.save_value_succ'))
|
2023-06-27 15:53:29 +08:00
|
|
|
} else {
|
2023-11-14 14:49:16 +08:00
|
|
|
$message.error(msg)
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
2023-11-14 14:49:16 +08:00
|
|
|
} catch (e) {
|
|
|
|
$message.error(e.message)
|
|
|
|
}
|
|
|
|
}
|
2023-10-11 01:15:23 +08:00
|
|
|
|
2023-11-14 14:49:16 +08:00
|
|
|
const resetEdit = () => {
|
|
|
|
currentEditRow.no = 0
|
|
|
|
currentEditRow.value = null
|
2023-11-10 23:24:01 +08:00
|
|
|
}
|
|
|
|
|
2023-06-27 15:53:29 +08:00
|
|
|
const actionColumn = {
|
|
|
|
key: 'action',
|
2023-09-07 18:38:34 +08:00
|
|
|
title: i18n.t('interface.action'),
|
2023-06-27 15:53:29 +08:00
|
|
|
width: 100,
|
|
|
|
align: 'center',
|
|
|
|
titleAlign: 'center',
|
|
|
|
fixed: 'right',
|
2023-11-14 14:49:16 +08:00
|
|
|
render: (row, index) => {
|
2023-06-27 15:53:29 +08:00
|
|
|
return h(EditableTableColumn, {
|
2023-11-14 14:49:16 +08:00
|
|
|
editing: false,
|
|
|
|
bindKey: `#${index + 1}`,
|
2023-06-27 15:53:29 +08:00
|
|
|
onEdit: () => {
|
2023-11-14 14:49:16 +08:00
|
|
|
startEdit(index + 1, row.v)
|
2023-06-27 15:53:29 +08:00
|
|
|
},
|
|
|
|
onDelete: async () => {
|
|
|
|
try {
|
2023-11-05 11:57:52 +08:00
|
|
|
const { success, msg } = await browserStore.removeListItem(
|
2023-06-27 15:53:29 +08:00
|
|
|
props.name,
|
|
|
|
props.db,
|
2023-10-11 01:15:23 +08:00
|
|
|
keyName.value,
|
2023-11-14 14:49:16 +08:00
|
|
|
index,
|
2023-06-27 15:53:29 +08:00
|
|
|
)
|
|
|
|
if (success) {
|
2023-11-15 23:41:53 +08:00
|
|
|
props.value.splice(index, 1)
|
2023-11-14 17:15:02 +08:00
|
|
|
$message.success(i18n.t('dialogue.delete_key_succ', { key: `#${index + 1}` }))
|
2023-06-27 15:53:29 +08:00
|
|
|
} else {
|
2023-08-24 00:44:19 +08:00
|
|
|
$message.error(msg)
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2023-08-24 00:44:19 +08:00
|
|
|
$message.error(e.message)
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
2023-11-14 14:49:16 +08:00
|
|
|
|
2023-06-27 15:53:29 +08:00
|
|
|
const columns = computed(() => {
|
2023-11-14 14:49:16 +08:00
|
|
|
if (!inEdit.value) {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
key: 'no',
|
|
|
|
title: '#',
|
|
|
|
width: 80,
|
|
|
|
align: 'center',
|
|
|
|
titleAlign: 'center',
|
|
|
|
render: (row, index) => {
|
|
|
|
return index + 1
|
|
|
|
},
|
|
|
|
},
|
2023-11-16 01:06:59 +08:00
|
|
|
valueColumn.value,
|
2023-11-14 14:49:16 +08:00
|
|
|
actionColumn,
|
|
|
|
]
|
|
|
|
} else {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
key: 'no',
|
|
|
|
title: '#',
|
|
|
|
width: 80,
|
|
|
|
align: 'center',
|
|
|
|
titleAlign: 'center',
|
|
|
|
render: (row, index) => {
|
|
|
|
if (index + 1 === currentEditRow.no) {
|
|
|
|
// editing row, show edit state
|
|
|
|
return h(NIcon, { size: 16, color: 'red' }, () => h(Edit, { strokeWidth: 5 }))
|
|
|
|
} else {
|
|
|
|
return index + 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2023-11-16 01:06:59 +08:00
|
|
|
valueColumn.value,
|
2023-11-14 14:49:16 +08:00
|
|
|
]
|
|
|
|
}
|
2023-06-27 15:53:29 +08:00
|
|
|
})
|
|
|
|
|
2023-11-14 14:49:16 +08:00
|
|
|
const rowProps = (row, index) => {
|
|
|
|
return {
|
|
|
|
onClick: () => {
|
|
|
|
// in edit mode, switch edit row by click
|
|
|
|
if (inEdit.value) {
|
|
|
|
startEdit(index + 1, row.v)
|
|
|
|
}
|
|
|
|
},
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
2023-11-14 14:49:16 +08:00
|
|
|
}
|
2023-08-20 14:42:36 +08:00
|
|
|
|
2023-11-08 23:45:33 +08:00
|
|
|
const entries = computed(() => {
|
2023-11-14 14:49:16 +08:00
|
|
|
const len = size(props.value)
|
2023-11-08 23:45:33 +08:00
|
|
|
return `${len} / ${Math.max(len, props.length)}`
|
|
|
|
})
|
|
|
|
|
2023-06-27 15:53:29 +08:00
|
|
|
const onAddValue = (value) => {
|
2023-10-11 01:15:23 +08:00
|
|
|
dialogStore.openAddFieldsDialog(props.name, props.db, props.keyPath, props.keyCode, types.LIST)
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const filterValue = ref('')
|
|
|
|
const onFilterInput = (val) => {
|
2023-11-16 01:06:59 +08:00
|
|
|
valueFilterOption.value = val
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const clearFilter = () => {
|
2023-11-16 01:06:59 +08:00
|
|
|
valueFilterOption.value = null
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const onUpdateFilter = (filters, sourceColumn) => {
|
2023-11-16 01:06:59 +08:00
|
|
|
valueFilterOption.value = filters[sourceColumn.key]
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
2023-11-09 20:31:07 +08:00
|
|
|
|
2023-11-14 14:49:16 +08:00
|
|
|
const onFormatChanged = (selDecode, selFormat) => {
|
|
|
|
emit('reload', selDecode, selFormat)
|
|
|
|
}
|
|
|
|
|
2023-11-09 20:31:07 +08:00
|
|
|
defineExpose({
|
|
|
|
reset: () => {
|
|
|
|
clearFilter()
|
2023-11-14 14:49:16 +08:00
|
|
|
resetEdit()
|
2023-11-09 20:31:07 +08:00
|
|
|
},
|
|
|
|
})
|
2023-06-27 15:53:29 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div class="content-wrapper flex-box-v">
|
2023-10-11 01:15:23 +08:00
|
|
|
<content-toolbar
|
2023-11-14 22:58:44 +08:00
|
|
|
v-show="!inFullEdit"
|
2023-10-11 01:15:23 +08:00
|
|
|
:db="props.db"
|
|
|
|
:key-code="props.keyCode"
|
2023-10-20 18:34:19 +08:00
|
|
|
:key-path="props.keyPath"
|
2023-10-11 01:15:23 +08:00
|
|
|
:key-type="keyType"
|
2023-11-08 23:45:33 +08:00
|
|
|
:loading="props.loading"
|
2023-10-11 01:15:23 +08:00
|
|
|
:server="props.name"
|
2023-10-30 00:54:45 +08:00
|
|
|
:ttl="ttl"
|
2023-11-08 23:45:33 +08:00
|
|
|
class="value-item-part"
|
|
|
|
@delete="emit('delete')"
|
|
|
|
@reload="emit('reload')"
|
|
|
|
@rename="emit('rename')" />
|
2023-11-14 22:58:44 +08:00
|
|
|
<div v-show="!inFullEdit" class="tb2 value-item-part flex-box-h">
|
2023-06-27 15:53:29 +08:00
|
|
|
<div class="flex-box-h">
|
|
|
|
<n-input
|
|
|
|
v-model:value="filterValue"
|
2023-09-07 18:38:34 +08:00
|
|
|
: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
|
|
|
</div>
|
2023-10-30 00:54:45 +08:00
|
|
|
<div class="flex-item-expand"></div>
|
2023-11-08 23:45:33 +08:00
|
|
|
<n-button-group>
|
|
|
|
<icon-button
|
|
|
|
:disabled="props.end || props.loading"
|
|
|
|
:icon="LoadList"
|
|
|
|
border
|
|
|
|
size="18"
|
|
|
|
t-tooltip="interface.load_more_entries"
|
|
|
|
@click="emit('loadmore')" />
|
|
|
|
<icon-button
|
|
|
|
:disabled="props.end || props.loading"
|
|
|
|
:icon="LoadAll"
|
|
|
|
border
|
|
|
|
size="18"
|
|
|
|
t-tooltip="interface.load_all_entries"
|
|
|
|
@click="emit('loadall')" />
|
|
|
|
</n-button-group>
|
2023-10-20 18:34:19 +08:00
|
|
|
<n-button :focusable="false" plain @click="onAddValue">
|
2023-06-27 15:53:29 +08:00
|
|
|
<template #icon>
|
|
|
|
<n-icon :component="AddLink" size="18" />
|
|
|
|
</template>
|
2023-09-07 18:38:34 +08:00
|
|
|
{{ $t('interface.add_row') }}
|
2023-06-27 15:53:29 +08:00
|
|
|
</n-button>
|
|
|
|
</div>
|
2023-11-14 14:49:16 +08:00
|
|
|
<div class="value-wrapper value-item-part flex-box-h flex-item-expand">
|
|
|
|
<!-- table -->
|
2023-06-27 15:53:29 +08:00
|
|
|
<n-data-table
|
2023-11-14 22:58:44 +08:00
|
|
|
v-show="!inFullEdit"
|
2023-10-20 18:34:19 +08:00
|
|
|
:bordered="false"
|
|
|
|
:bottom-bordered="false"
|
2023-06-27 15:53:29 +08:00
|
|
|
:columns="columns"
|
2023-11-14 14:49:16 +08:00
|
|
|
:data="props.value"
|
2023-11-08 23:45:33 +08:00
|
|
|
:loading="props.loading"
|
2023-11-17 17:20:32 +08:00
|
|
|
:row-key="(row) => row.no"
|
2023-11-14 14:49:16 +08:00
|
|
|
:row-props="rowProps"
|
2023-06-27 15:53:29 +08:00
|
|
|
:single-column="true"
|
|
|
|
:single-line="false"
|
2023-11-08 23:45:33 +08:00
|
|
|
class="flex-item-expand"
|
2023-06-27 15:53:29 +08:00
|
|
|
flex-height
|
|
|
|
size="small"
|
|
|
|
striped
|
|
|
|
virtual-scroll
|
2023-08-24 15:23:25 +08:00
|
|
|
@update:filters="onUpdateFilter" />
|
2023-11-14 14:49:16 +08:00
|
|
|
|
|
|
|
<!-- edit pane -->
|
|
|
|
<content-entry-editor
|
|
|
|
v-show="inEdit"
|
2023-11-14 22:58:44 +08:00
|
|
|
v-model:fullscreen="fullEdit"
|
2023-11-14 14:49:16 +08:00
|
|
|
:decode="currentEditRow.decode"
|
|
|
|
:field="currentEditRow.no"
|
|
|
|
:field-label="$t('common.index')"
|
|
|
|
:field-readonly="true"
|
|
|
|
:format="currentEditRow.format"
|
|
|
|
:value="currentEditRow.value"
|
|
|
|
:value-label="$t('common.value')"
|
|
|
|
class="flex-item-expand"
|
|
|
|
style="width: 100%"
|
2023-11-14 22:58:44 +08:00
|
|
|
@close="resetEdit"
|
2023-11-14 14:49:16 +08:00
|
|
|
@save="saveEdit" />
|
2023-06-27 15:53:29 +08:00
|
|
|
</div>
|
2023-10-30 00:54:45 +08:00
|
|
|
<div class="value-footer flex-box-h">
|
2023-11-08 23:45:33 +08:00
|
|
|
<n-text v-if="!isNaN(props.length)">{{ $t('interface.entries') }}: {{ entries }}</n-text>
|
2023-10-30 00:54:45 +08:00
|
|
|
<n-divider v-if="!isNaN(props.length)" vertical />
|
|
|
|
<n-text v-if="!isNaN(props.size)">{{ $t('interface.memory_usage') }}: {{ bytes(props.size) }}</n-text>
|
|
|
|
<div class="flex-item-expand"></div>
|
2023-11-14 14:49:16 +08:00
|
|
|
<format-selector
|
|
|
|
v-show="!inEdit"
|
|
|
|
:decode="props.decode"
|
|
|
|
:disabled="inEdit"
|
|
|
|
:format="props.format"
|
|
|
|
@format-changed="onFormatChanged" />
|
2023-10-30 00:54:45 +08:00
|
|
|
</div>
|
2023-06-27 15:53:29 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2023-10-30 00:54:45 +08:00
|
|
|
<style lang="scss" scoped>
|
|
|
|
.value-footer {
|
|
|
|
border-top: v-bind('themeVars.borderColor') 1px solid;
|
|
|
|
background-color: v-bind('themeVars.tableHeaderColor');
|
|
|
|
}
|
|
|
|
</style>
|