index.vue
6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<script setup lang="ts">
import { ComponentPublicInstance, ref, unref, watch } from 'vue';
import { Tooltip, Button } from 'ant-design-vue';
import { CollapseContainer } from '/@/components/Container';
import { Icon } from '/@/components/Icon';
import { FlipFlopItemType, ScheduleOptionItemType } from './types';
import { BasicForm, FormActionType } from '/@/components/Form';
import { getFormSchemas, FormFieldEnum } from './config';
import { useSceneLinkageDrawerContext } from '../SceneLinkageDrawer/sceneLinkageDrawerContext';
import { ScheduleTypeEnum, ScheduleTypeNameEnum } from '/@/enums/linkedgeEnum';
import { ConditionFilter } from '../ConditionFilter';
import { EnablingRule } from '../EnablingRule';
import { useModal } from '/@/components/Modal';
import { DataActionModeEnum } from '/@/enums/toolEnum';
import { EnableRuleFormModalParamsType, ScheduleType } from '../EnablingRule/type';
import { useFlipFlopData } from './useFlipFlopData';
import { createNewFlipFlopItem } from '.';
const { disabledDrawer } = useSceneLinkageDrawerContext();
const emit = defineEmits(['delete']);
const props = withDefaults(
defineProps<{
addButtonName?: string;
defaultNull?: boolean;
panelTitle?: (index: number) => string;
showAddButton?: boolean;
disabled?: boolean;
}>(),
{
addButtonName: '触发器 (OR)',
defaultNull: false,
panelTitle: (index: number) => `触发器${index}`,
showAddButton: true,
disabled: false,
}
);
const formSchemas = getFormSchemas();
const [registerModal, { openModal }] = useModal();
const handleOpenModal = (type: ScheduleTypeEnum, flipFlopItem: FlipFlopItemType) => {
if (unref(disabledDrawer) && type !== flipFlopItem.schedule.type) return;
if (type === ScheduleTypeEnum.ANY_TIME) {
flipFlopItem.schedule = { type };
return;
}
openModal(true, {
mode: DataActionModeEnum.CREATE,
record: {
type,
schedule: flipFlopItem.schedule,
key: flipFlopItem.key,
},
} as EnableRuleFormModalParamsType);
};
const handleRuleSetting = (params: ScheduleType, key: string) => {
const index = unref(flipFlopListElRef).findIndex((flipFlopItem) => flipFlopItem.key === key);
~index && (unref(flipFlopListElRef)[index].schedule = params);
};
const scheduleOptions: ScheduleOptionItemType[] = Object.keys(ScheduleTypeEnum).map((value) => ({
label: ScheduleTypeNameEnum[value],
value: value as ScheduleTypeEnum,
}));
const { organizationId } = useSceneLinkageDrawerContext();
const flipFlopListElRef = ref<FlipFlopItemType[]>(
props.defaultNull ? [] : [createNewFlipFlopItem()]
);
/**
* @description on organization change
*/
watch(organizationId, () => {
unref(flipFlopListElRef).forEach((flipFlopItem) =>
flipFlopItem.ref?.setFieldsValue({ [FormFieldEnum.ENTITY_ID]: [] })
);
});
const setFlopFlipFormRef = (
flipFlopItem: FlipFlopItemType,
el: Nullable<Element | ComponentPublicInstance>
) => {
flipFlopItem.ref = el as unknown as FormActionType;
};
const setFlopFlipConditionRef = (
flipFlopItem: FlipFlopItemType,
el: Nullable<Element | ComponentPublicInstance>
) => {
flipFlopItem.conditionRef = el as unknown as InstanceType<typeof ConditionFilter>;
};
const handleDelete = (flipFlopItem: FlipFlopItemType) => {
const index = unref(flipFlopListElRef).findIndex((item) => item.key === flipFlopItem.key);
~index && unref(flipFlopListElRef).splice(index, 1);
emit('delete');
};
const handleAdd = () => {
flipFlopListElRef.value.push(createNewFlipFlopItem());
};
const { getFieldsValue, setFieldsValue, validate, resetFieldsValue } =
useFlipFlopData(flipFlopListElRef);
defineExpose({ getFieldsValue, setFieldsValue, validate, resetFieldsValue });
</script>
<template>
<section>
<CollapseContainer
v-for="(flipFlopItem, index) in flipFlopListElRef"
:key="flipFlopItem.key"
:title="panelTitle(index + 1)"
class="mb-4"
>
<template #action>
<header class="flex items-center justify-between">
<div class="flex items-center">
<div class="flex">
<span class="mr-2">启用规则:</span>
<div
class="px-1 cursor-pointer"
v-for="schedule in scheduleOptions"
:key="schedule.value"
type="text"
:class="flipFlopItem.schedule.type === schedule.value ? 'text-blue-400' : ''"
@click="handleOpenModal(schedule.value, flipFlopItem)"
>
{{ schedule.label }}
</div>
</div>
<Tooltip title="删除">
<Icon
v-if="!disabledDrawer"
class="ml-2 cursor-pointer"
icon="fluent:delete-off-20-regular"
size="20"
@click="handleDelete(flipFlopItem)"
/>
</Tooltip>
</div>
</header>
</template>
<BasicForm
:ref="(el) => setFlopFlipFormRef(flipFlopItem, el)"
:key="flipFlopItem.key"
class="trigger-form"
layout="horizontal"
:disabled="disabledDrawer"
:schemas="formSchemas"
:showActionButtonGroup="false"
:baseColProps="{ span: 6, xxl: 6, xl: 8, lg: 12, sm: 24 }"
>
<template #conditionFilter="{ model }">
<ConditionFilter
:ref="(el) => setFlopFlipConditionRef(flipFlopItem, el)"
:triggerType="model[FormFieldEnum.CONDITION_VALUE_TYPE]"
:object-model="model[FormFieldEnum.CONDITION_KEY_DETAIL]"
:condition-type="model[FormFieldEnum.CONDITION_TYPE]"
/>
</template>
</BasicForm>
</CollapseContainer>
<Button
v-if="showAddButton && !disabledDrawer"
type="primary"
class="w-full"
@click="handleAdd"
>
<Icon icon="ant-design:plus-outlined" />
{{ addButtonName }}
</Button>
<EnablingRule @register="registerModal" @setting-complete="handleRuleSetting" />
</section>
</template>
<style lang="less" scoped>
.trigger-form {
:deep(.ant-form-item-control-input-content) {
width: 100%;
> div > div {
width: 100%;
}
.ant-input-number {
width: 100% !important;
}
// .ant-col {
// .ant-row{
// .ant-col {
// .
// }
// }
// }
}
}
</style>