WEB/src/views/order/orderManagement/component/OrderToolbarButtonGroup.vue
2026-08-12 18:10:00 +08:00

415 lines
13 KiB
Vue

<template>
<div ref="containerRef" class="order-toolbar-button-group" :class="{ 'order-toolbar-button-group--tile': isTileMode }">
<div ref="mainRef" class="order-toolbar-button-group__main">
<template v-for="button in displayButtons" :key="button.key">
<el-dropdown
v-if="button.buttonType === 'dropdown' && button.dropdownOptions?.length"
trigger="click"
split-button
:size="button.size || 'small'"
:type="resolveButtonStyle(button).type"
:plain="resolveButtonStyle(button).plain"
:disabled="resolveDisabled(button.disabledKey) || resolveLoading(button.loadingKey)"
@click="emitAction(button.action)"
@command="emitAction"
>
{{ resolveLabel(button.label, button.dynamicLabelKey) }}
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item
v-for="option in button.dropdownOptions"
:key="option.action"
:command="option.action"
:divided="option.divided"
>
{{ option.label }}
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
<el-button
v-else
:size="button.size || 'small'"
:type="resolveButtonStyle(button).type"
:plain="resolveButtonStyle(button).plain"
:disabled="resolveDisabled(button.disabledKey)"
:loading="resolveLoading(button.loadingKey)"
@click="emitAction(button.action)"
>
{{ resolveLabel(button.label, button.dynamicLabelKey) }}
</el-button>
</template>
<el-popover
v-if="!isTileMode && overflowButtons.length"
v-model:visible="expandVisible"
placement="bottom-start"
:width="172"
trigger="click"
popper-class="order-toolbar-button-group__popover"
>
<template #reference>
<el-button size="small" :type="expandVisible ? 'primary' : 'default'" plain>
展开{{ overflowButtons.length ? `(${overflowButtons.length})` : '' }}
</el-button>
</template>
<div class="order-toolbar-button-group__overflow-list">
<template v-for="button in overflowButtons" :key="`overflow-${button.key}`">
<template v-if="button.buttonType === 'dropdown' && button.dropdownOptions?.length">
<el-button
v-for="option in button.dropdownOptions"
:key="`overflow-${button.key}-${option.action}`"
:size="button.size || 'small'"
:type="resolveButtonStyle(button).type"
:plain="resolveButtonStyle(button).plain"
:disabled="resolveDisabled(button.disabledKey)"
:loading="resolveLoading(button.loadingKey)"
class="order-toolbar-button-group__overflow-item"
@click="handleOverflowClick(option.action)"
>
{{ resolveLabel(button.label, button.dynamicLabelKey) }} · {{ option.label }}
</el-button>
</template>
<el-button
v-else
:size="button.size || 'small'"
:type="resolveButtonStyle(button).type"
:plain="resolveButtonStyle(button).plain"
:disabled="resolveDisabled(button.disabledKey)"
:loading="resolveLoading(button.loadingKey)"
class="order-toolbar-button-group__overflow-item"
@click="handleOverflowClick(button.action)"
>
{{ resolveLabel(button.label, button.dynamicLabelKey) }}
</el-button>
</template>
</div>
</el-popover>
</div>
<div class="order-toolbar-button-group__mode-toggle" @click="toggleToolbarMode">
<el-tooltip :content="isTileMode ? '切换到折叠模式' : '切换到平铺模式'" placement="bottom">
<el-icon :size="14"><Grid v-if="isTileMode" /><Fold v-else /></el-icon>
</el-tooltip>
</div>
</div>
</template>
<script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import type { ToolbarButtonConfig } from '../config/order-common-config'
import { useSettingsStore } from '@/stores/modules/settings'
import { Grid, Fold } from '@element-plus/icons-vue'
const props = defineProps<{
buttons: ToolbarButtonConfig[]
resolveDisabled: (disabledKey?: string) => boolean
resolveLoading: (loadingKey?: string) => boolean
resolveLabel: (defaultLabel: string, dynamicLabelKey?: string) => string
layoutKey?: string | number
}>()
const emit = defineEmits<{
(e: 'action', action: ToolbarButtonConfig['action']): void
}>()
const settingsStore = useSettingsStore()
const isTileMode = computed(() => settingsStore.toolbarMode !== 'collapse')
const displayButtons = computed(() => isTileMode.value ? props.buttons : visibleButtons.value)
const toggleToolbarMode = () => {
settingsStore.changeSetting({
key: 'toolbarMode',
value: isTileMode.value ? 'collapse' : 'tile'
})
}
const containerRef = ref<HTMLElement | null>(null)
const mainRef = ref<HTMLElement | null>(null)
const visibleButtons = ref<ToolbarButtonConfig[]>([])
const overflowButtons = ref<ToolbarButtonConfig[]>([])
const expandVisible = ref(false)
let resizeObserver: ResizeObserver | null = null
let layoutFrame = 0
function emitAction(action: ToolbarButtonConfig['action']) {
emit('action', action)
}
/** 按钮 key → 强制样式覆盖,仅用于跨视图统一特定按钮样式 */
const BUTTON_STYLE_OVERRIDE: Record<string, { type: string; plain: boolean }> = {
// 各视图通过 category + type 自行控制样式
}
/** 根据 category 或 type/plain 解析按钮样式 */
function resolveButtonStyle(button: ToolbarButtonConfig): { type: string; plain: boolean } {
// 优先检查强制覆盖
const override = BUTTON_STYLE_OVERRIDE[button.key]
if (override) return override
switch (button.category) {
case 'refresh': return { type: 'primary', plain: false }
case 'setting': return { type: 'default', plain: false }
case 'action':
if (button.type) return { type: button.type, plain: false }
return { type: 'primary', plain: true }
case 'cancel': return { type: 'danger', plain: true }
default: return { type: button.type || 'default', plain: !!button.plain }
}
}
function handleOverflowClick(action: ToolbarButtonConfig['action']) {
expandVisible.value = false
emitAction(action)
}
function isMainOverflowing() {
const mainEl = mainRef.value
if (!mainEl) return false
return mainEl.scrollWidth > mainEl.clientWidth + 1
}
async function trimVisibleUntilFits() {
const buttons = props.buttons
if (!buttons.length) {
visibleButtons.value = []
overflowButtons.value = []
return
}
visibleButtons.value = [...buttons]
overflowButtons.value = []
await nextTick()
let guard = 0
while (isMainOverflowing() && visibleButtons.value.length > 0 && guard < buttons.length + 1) {
guard += 1
const removed = visibleButtons.value.pop()
if (!removed) break
overflowButtons.value.unshift(removed)
await nextTick()
}
if (!visibleButtons.value.length && overflowButtons.value.length) {
const first = overflowButtons.value[0]
if (!first) return
visibleButtons.value = [first]
overflowButtons.value = overflowButtons.value.slice(1)
await nextTick()
while (isMainOverflowing() && visibleButtons.value.length > 0 && overflowButtons.value.length > 0 && guard < buttons.length + 2) {
guard += 1
const removed = visibleButtons.value.pop()
if (!removed) break
overflowButtons.value.unshift(removed)
await nextTick()
}
}
}
async function scheduleLayout() {
// 平铺模式不需要溢出检测
if (isTileMode.value) return
const frame = ++layoutFrame
await nextTick()
if (frame !== layoutFrame) return
await trimVisibleUntilFits()
}
function setupResizeObserver() {
resizeObserver?.disconnect()
resizeObserver = new ResizeObserver(() => {
void scheduleLayout()
})
const parent = containerRef.value?.parentElement
if (parent) resizeObserver.observe(parent)
if (containerRef.value) resizeObserver.observe(containerRef.value)
if (mainRef.value) resizeObserver.observe(mainRef.value)
}
onMounted(() => {
visibleButtons.value = [...props.buttons]
setupResizeObserver()
void scheduleLayout()
})
onBeforeUnmount(() => {
resizeObserver?.disconnect()
resizeObserver = null
})
watch(
() => [props.buttons, props.layoutKey],
() => {
void scheduleLayout()
},
{ deep: true }
)
// 模式切换时重新布局
watch(isTileMode, () => {
if (!isTileMode.value) {
visibleButtons.value = [...props.buttons]
void scheduleLayout()
}
})
</script>
<style scoped lang="scss">
.order-toolbar-button-group {
position: relative;
display: flex;
align-items: flex-start;
flex: 1;
width: 100%;
max-width: 100%;
min-width: 0;
overflow: hidden;
gap: 6px;
}
.order-toolbar-button-group--tile {
overflow: visible;
}
.order-toolbar-button-group__main {
display: flex;
align-items: center;
gap: 6px;
width: 100%;
max-width: 100%;
min-width: 0;
overflow: hidden;
flex-wrap: nowrap;
}
.order-toolbar-button-group--tile .order-toolbar-button-group__main {
overflow: visible;
flex-wrap: wrap;
align-items: flex-start;
align-content: flex-start;
padding-right: 30px;
}
/* 平铺模式下切换按钮绝对定位到右上角,不参与按钮流 */
.order-toolbar-button-group--tile .order-toolbar-button-group__mode-toggle {
position: absolute;
top: 0;
right: 0;
margin-left: 0;
}
.order-toolbar-button-group__main :deep(.el-dropdown) {
flex-shrink: 0;
}
/* 清除 Element Plus 默认 .el-button + .el-button 左边距,
换行时每行首个按钮不会多出间隙,统一由 gap 控制等间距 */
.order-toolbar-button-group__main :deep(.el-button),
.order-toolbar-button-group__main :deep(.el-dropdown) {
margin-left: 0;
margin-right: 0;
}
.order-toolbar-button-group__overflow-list {
display: flex;
flex-direction: column;
gap: 8px;
}
.order-toolbar-button-group__overflow-item {
width: 100%;
margin: 0;
justify-content: flex-start;
}
.order-toolbar-button-group__mode-toggle {
display: flex;
align-items: center;
justify-content: center;
width: 24px;
min-width: 24px;
height: 24px;
border-radius: 4px;
background: #e8e8e8;
cursor: pointer;
color: #909399;
transition: all 0.3s;
flex-shrink: 0;
margin-left: auto;
&:hover {
background: #d0d0d0;
color: #409EFF;
}
}
/* 禁用按钮样式优化:保持字体颜色可读,背景极浅区分状态 */
.order-toolbar-button-group__main :deep(.el-button.is-disabled),
.order-toolbar-button-group__main :deep(.el-button.is-disabled:hover),
.order-toolbar-button-group__main :deep(.el-button.is-disabled:focus),
.order-toolbar-button-group__main :deep(.el-button.is-disabled:active) {
color: #606266;
background-color: #fafbfc;
border-color: #ebeef5;
}
/* primary 禁用:保持蓝字可见,极浅蓝背景 */
.order-toolbar-button-group__main :deep(.el-button--primary.is-disabled),
.order-toolbar-button-group__main :deep(.el-button--primary.is-disabled:hover),
.order-toolbar-button-group__main :deep(.el-button--primary.is-disabled:focus),
.order-toolbar-button-group__main :deep(.el-button--primary.is-disabled:active) {
color: #409eff;
background-color: #f5faff;
border-color: #e4efff;
}
/* primary + plain 禁用:白底蓝字 */
.order-toolbar-button-group__main :deep(.el-button--primary.is-plain.is-disabled),
.order-toolbar-button-group__main :deep(.el-button--primary.is-plain.is-disabled:hover),
.order-toolbar-button-group__main :deep(.el-button--primary.is-plain.is-disabled:focus),
.order-toolbar-button-group__main :deep(.el-button--primary.is-plain.is-disabled:active) {
color: #409eff;
background-color: #fff;
border-color: #409eff;
}
/* danger 禁用:白底红字 */
.order-toolbar-button-group__main :deep(.el-button--danger.is-disabled),
.order-toolbar-button-group__main :deep(.el-button--danger.is-disabled:hover),
.order-toolbar-button-group__main :deep(.el-button--danger.is-disabled:focus),
.order-toolbar-button-group__main :deep(.el-button--danger.is-disabled:active) {
color: #f56c6c;
background-color: #fff;
border-color: #f56c6c;
}
/* 溢出列表中的 disabled 按钮同样适用 */
.order-toolbar-button-group__overflow-list :deep(.el-button--primary.is-plain.is-disabled),
.order-toolbar-button-group__overflow-list :deep(.el-button--primary.is-plain.is-disabled:hover),
.order-toolbar-button-group__overflow-list :deep(.el-button--primary.is-plain.is-disabled:focus),
.order-toolbar-button-group__overflow-list :deep(.el-button--primary.is-plain.is-disabled:active) {
color: #409eff;
background-color: #fff;
border-color: #409eff;
}
.order-toolbar-button-group__overflow-list :deep(.el-button--danger.is-disabled),
.order-toolbar-button-group__overflow-list :deep(.el-button--danger.is-disabled:hover),
.order-toolbar-button-group__overflow-list :deep(.el-button--danger.is-disabled:focus),
.order-toolbar-button-group__overflow-list :deep(.el-button--danger.is-disabled:active) {
color: #f56c6c;
background-color: #fff;
border-color: #f56c6c;
}
</style>
<style lang="scss">
.order-toolbar-button-group__popover {
padding: 10px !important;
}
</style>