Files
shanxia2.0/web-admin/scripts/check-mp-pl-plan-detail-actions.mjs
T
2026-09-17 09:10:10 +08:00

182 lines
5.2 KiB
JavaScript

/**
* Mirrors web-admin/src/utils/mp-pl-plan-detail-actions.ts (no vitest in web-admin).
*/
function splitMpPlanDetailActions(input) {
const primary = []
const more = []
if (input.canEdit) primary.push({ kind: 'edit', label: '编辑' })
if (input.canArrange) {
primary.push({ kind: 'arrange', label: input.arrangeLabel })
}
if (input.canCopyDriver) more.push({ kind: 'copy-driver', label: '复制给司机' })
if (input.canCopySite) more.push({ kind: 'copy-site', label: '复制给现场' })
if (input.canCopyPoundQrcode) more.push({ kind: 'copy-pound-qrcode', label: '复制磅单二维码' })
more.push({ kind: 'copy', label: '复制' })
if (input.canWait) more.push({ kind: 'wait', label: '等客户通知' })
if (input.canCancelWait) more.push({ kind: 'cancel-wait', label: '取消等通知' })
if (input.canVoid) more.push({ kind: 'void', label: '作废', danger: true })
while (primary.length > 2) more.unshift(primary.pop())
if (primary.length === 0) {
const preferKinds = ['copy', 'wait', 'cancel-wait', 'void']
for (const kind of preferKinds) {
if (primary.length >= 2) break
const idx = more.findIndex((a) => a.kind === kind)
if (idx < 0) continue
primary.push(more.splice(idx, 1)[0])
}
while (primary.length < 2 && more.length) {
primary.push(more.shift())
}
}
return { primary, more }
}
function splitMpPlanFormFooterActions(input) {
const more = [{ key: 'cancel', label: '取消' }]
const primary = []
const hasStrongPrimary = input.showSaveAndArrange || input.showRearrange
if (input.showDraft) primary.push({ key: 'draft', label: '暂存' })
if (input.showSave) {
primary.push({
key: 'save',
label: input.saveLabel,
type: hasStrongPrimary ? 'default' : 'primary',
ghost: hasStrongPrimary
})
}
if (input.showRearrange) {
primary.push({ key: 'rearrange', label: '调整运输', type: 'primary' })
} else if (input.showSaveAndArrange) {
primary.push({
key: 'saveAndArrange',
label: input.saveAndArrangeLabel,
type: 'primary'
})
}
while (primary.length > 3) {
const overflow = primary.pop()
more.splice(1, 0, overflow)
}
if (primary.length === 0 && more.length > 1) {
const first = more.find((a) => a.key !== 'cancel') || more[0]
if (first) {
more.splice(more.indexOf(first), 1)
primary.push({ ...first, type: first.type || 'primary' })
}
}
return { primary, more }
}
function assert(cond, msg) {
if (!cond) {
console.error('FAIL:', msg)
process.exit(1)
}
}
const both = splitMpPlanDetailActions({
canEdit: true,
canArrange: true,
arrangeLabel: '安排运输',
canWait: true,
canCancelWait: false,
canVoid: true
})
assert(
both.primary.map((a) => a.kind).join(',') === 'edit,arrange',
'primary should be edit,arrange'
)
assert(
both.more.map((a) => a.kind).join(',') === 'copy,wait,void',
'more should be copy,wait,void'
)
const withArrangeCopy = splitMpPlanDetailActions({
canEdit: true,
canArrange: true,
arrangeLabel: '调整运输',
canCopyDriver: true,
canCopySite: true,
canCopyPoundQrcode: true,
canWait: false,
canCancelWait: false,
canVoid: true
})
assert(
withArrangeCopy.more.map((a) => a.kind).join(',') ===
'copy-driver,copy-site,copy-pound-qrcode,copy,void',
'arrange copy actions go first in more'
)
const onlyEdit = splitMpPlanDetailActions({
canEdit: true,
canArrange: false,
arrangeLabel: '安排运输',
canWait: true,
canCancelWait: false,
canVoid: true
})
assert(onlyEdit.primary.map((a) => a.kind).join(',') === 'edit', 'only edit stays primary')
assert(
onlyEdit.more.map((a) => a.kind).join(',') === 'copy,wait,void',
'rest stay in more when primary already has items'
)
const onlyVoid = splitMpPlanDetailActions({
canEdit: false,
canArrange: false,
arrangeLabel: '安排运输',
canWait: false,
canCancelWait: false,
canVoid: true
})
assert(onlyVoid.primary.length === 2, 'promote more into primary only when empty')
assert(onlyVoid.primary[0].kind === 'copy', 'first promoted is copy')
assert(onlyVoid.primary[1].kind === 'void', 'second promoted is void')
assert(onlyVoid.more.map((a) => a.kind).join(',') === '', 'more empty after promote')
const onlyArrangeCopy = splitMpPlanDetailActions({
canEdit: false,
canArrange: false,
arrangeLabel: '安排运输',
canCopyDriver: true,
canCopySite: false,
canCopyPoundQrcode: false,
canWait: false,
canCancelWait: false,
canVoid: false
})
assert(
onlyArrangeCopy.primary.map((a) => a.kind).join(',') === 'copy,copy-driver',
'prefer copy over arrange-copy when promoting'
)
assert(
onlyArrangeCopy.more.map((a) => a.kind).join(',') === '',
'more empty after promote with only copy actions'
)
const formNew = splitMpPlanFormFooterActions({
showDraft: true,
showSave: true,
showSaveAndArrange: true,
showRearrange: false,
saveLabel: '创建',
saveAndArrangeLabel: '创建并安排运输'
})
assert(
formNew.primary.map((a) => a.key).join(',') === 'draft,save,saveAndArrange',
'form primary shows draft,save,saveAndArrange'
)
assert(formNew.more.map((a) => a.key).join(',') === 'cancel', 'form more only cancel')
console.log('check-mp-pl-plan-detail-actions: ok')