|
| 1 | +/** |
| 2 | + * Copyright (c) 2021 Hengyang Zhang |
| 3 | + * |
| 4 | + * This software is released under the MIT License. |
| 5 | + * https://opensource.org/licenses/MIT |
| 6 | + */ |
| 7 | +import { t } from "@app/locale" |
| 8 | +import { CircleCheck, Clock, Delete, EditPen, Lock } from "@element-plus/icons-vue" |
| 9 | +import { css } from '@emotion/css' |
| 10 | +import Flex from "@pages/components/Flex" |
| 11 | +import { ElButton, ElCard, ElCheckboxButton, ElCheckboxGroup, ElDivider, ElIcon, ElMessageBox, ElTag, useNamespace } from "element-plus" |
| 12 | +import { createStringUnionGuard } from 'typescript-guard' |
| 13 | +import { type Component, computed, defineComponent, FunctionalComponent, h, type StyleValue } from "vue" |
| 14 | +import { verifyCanModify } from "../common" |
| 15 | +import { useLimitAction, useLimitTable } from "../context" |
| 16 | +import Rule from "./Rule" |
| 17 | + |
| 18 | +type Props = { |
| 19 | + value: timer.limit.Item |
| 20 | +} |
| 21 | + |
| 22 | +type Flag = keyof timer.limit.Rule & ('enabled' | 'locked' | 'allowDelay') |
| 23 | +const isFlag = createStringUnionGuard<Flag>('allowDelay', 'enabled', 'locked') |
| 24 | + |
| 25 | +const FLAG_GROUP: [Flag, Component][] = [ |
| 26 | + ['enabled', CircleCheck], |
| 27 | + ['allowDelay', Clock], |
| 28 | + ['locked', Lock], |
| 29 | +] |
| 30 | + |
| 31 | +const FlagRadioGroup = defineComponent<ModelValue<Record<Flag, boolean>>>(props => { |
| 32 | + const selected = computed(() => Object.entries(props.modelValue).filter(([_, v]) => !!v).map(e => e[0] as Flag)) |
| 33 | + return () => ( |
| 34 | + <ElCheckboxGroup |
| 35 | + modelValue={selected.value} |
| 36 | + onChange={selected => { |
| 37 | + const newVal = { ...props.modelValue } |
| 38 | + selected.forEach(s => isFlag(s) && (newVal[s] = true)) |
| 39 | + props.onChange?.(newVal) |
| 40 | + }} |
| 41 | + > |
| 42 | + {FLAG_GROUP.map(([value, icon]) => ( |
| 43 | + <ElCheckboxButton value={value}> |
| 44 | + <span style={{ margin: '-6px' } satisfies StyleValue}> |
| 45 | + <ElIcon size='small'>{h(icon)}</ElIcon> |
| 46 | + </span> |
| 47 | + </ElCheckboxButton> |
| 48 | + ))} |
| 49 | + </ElCheckboxGroup> |
| 50 | + ) |
| 51 | +}, { |
| 52 | + props: ['modelValue', 'onChange'] |
| 53 | +}) |
| 54 | + |
| 55 | +const CARD_PADDING = 10 |
| 56 | + |
| 57 | +const useStyle = () => { |
| 58 | + const cardNs = useNamespace('card') |
| 59 | + const cardCls = css` |
| 60 | + .${cardNs.e('body')} { |
| 61 | + padding: ${CARD_PADDING}px; |
| 62 | + } |
| 63 | + ` |
| 64 | + return cardCls |
| 65 | +} |
| 66 | + |
| 67 | +const ALL_WEEKDAYS = t(msg => msg.calendar.weekDays).split('|') |
| 68 | + |
| 69 | +const Divider: FunctionalComponent<{}> = () => { |
| 70 | + const marginInline = `${-CARD_PADDING}px` |
| 71 | + const width = `calc(100% + ${CARD_PADDING * 2}px)` |
| 72 | + return <ElDivider style={{ marginBlock: '6px', marginInline, width } satisfies StyleValue} /> |
| 73 | +} |
| 74 | + |
| 75 | +const _default = defineComponent<Props>(props => { |
| 76 | + const { deleteRow } = useLimitTable() |
| 77 | + const { modify } = useLimitAction() |
| 78 | + |
| 79 | + const handleModify = () => verifyCanModify(props.value) |
| 80 | + .then(() => modify(props.value)) |
| 81 | + .catch(() => {/** Do nothing */ }) |
| 82 | + |
| 83 | + const handleDelete = () => verifyCanModify(props.value) |
| 84 | + .then(() => ElMessageBox.confirm(t(msg => msg.limit.message.deleteConfirm, { name: props.value.name }))) |
| 85 | + .then(() => deleteRow(props.value)) |
| 86 | + .catch(() => {/** Do nothing */ }) |
| 87 | + |
| 88 | + const renderEffectiveTag = (weekdays: number[] | undefined) => { |
| 89 | + if (!weekdays || weekdays.length === 0 || weekdays.length === 7) { |
| 90 | + return ( |
| 91 | + <ElTag size="small" type="success"> |
| 92 | + {t(msg => msg.calendar.range.everyday)} |
| 93 | + </ElTag> |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + if (weekdays.length === 1) { |
| 98 | + return ( |
| 99 | + <ElTag size="small"> |
| 100 | + {ALL_WEEKDAYS?.[weekdays[0]]} |
| 101 | + </ElTag> |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + const firstDay = ALL_WEEKDAYS?.[weekdays[0]] |
| 106 | + const allDays = weekdays.map(w => ALL_WEEKDAYS?.[w]).join('、') |
| 107 | + |
| 108 | + return <ElTag size="small">{firstDay}...${allDays.length}</ElTag> |
| 109 | + } |
| 110 | + |
| 111 | + const clz = useStyle() |
| 112 | + |
| 113 | + return () => ( |
| 114 | + <ElCard class={clz} shadow="hover"> |
| 115 | + <Flex column gap={8}> |
| 116 | + <Flex justify="space-between" align="center"> |
| 117 | + <Flex align="center" gap={8}> |
| 118 | + <span style={{ fontWeight: 'bold' }}> |
| 119 | + {props.value.name || '-'} |
| 120 | + </span> |
| 121 | + {renderEffectiveTag(props.value.weekdays)} |
| 122 | + </Flex> |
| 123 | + <ElButton |
| 124 | + size="small" |
| 125 | + type='danger' |
| 126 | + text |
| 127 | + icon={Delete} |
| 128 | + onClick={handleDelete} |
| 129 | + /> |
| 130 | + </Flex> |
| 131 | + <Divider /> |
| 132 | + {/* Sites */} |
| 133 | + <Flex gap={2}> |
| 134 | + {props.value.cond.map((c, idx) => <ElTag key={idx} type='info'>{c}</ElTag>)} |
| 135 | + </Flex> |
| 136 | + <Divider /> |
| 137 | + {/** Content */} |
| 138 | + <Rule value={props.value} /> |
| 139 | + <Divider /> |
| 140 | + {/* Footer Button */} |
| 141 | + <Flex justify='between'> |
| 142 | + <FlagRadioGroup modelValue={props.value} onChange={() => {/** todo */ }} /> |
| 143 | + <ElButton text icon={EditPen} onClick={handleModify} size='small' /> |
| 144 | + </Flex> |
| 145 | + </Flex> |
| 146 | + </ElCard > |
| 147 | + ) |
| 148 | +}, { props: ['value'] }) |
| 149 | + |
| 150 | +export default _default |
0 commit comments