github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/views/Automation/components/TriggerForm.vue (about) 1 <script setup lang="ts"> 2 import {h, PropType, reactive, unref, watch} from 'vue' 3 import {Form} from '@/components/Form' 4 import {useI18n} from '@/hooks/web/useI18n' 5 import {useForm} from '@/hooks/web/useForm' 6 import {useValidator} from '@/hooks/web/useValidator' 7 import {FormSchema} from '@/types/form' 8 import {Trigger} from "@/views/Automation/components/types"; 9 import {ApiAttribute} from "@/api/stub"; 10 11 const {register, elFormRef, methods} = useForm() 12 const {required} = useValidator() 13 const {t} = useI18n() 14 15 const props = defineProps({ 16 trigger: { 17 type: Object as PropType<Nullable<Trigger>>, 18 default: () => null 19 } 20 }) 21 22 const rules = { 23 name: [required()], 24 pluginName: [required()], 25 } 26 27 const {setValues, setSchema} = methods 28 const pluginChanged = async (value?: string) => { 29 if (!value) { 30 value = 'state_change' 31 } 32 const schema = [ 33 {field: 'entityIds', path: 'hidden', value: value !== 'state_change'}, 34 {field: 'timePluginOptions', path: 'hidden', value: value !== 'time'}, 35 {field: 'alexaPluginOptions', path: 'hidden', value: value !== 'alexa'} 36 ] 37 setSchema(schema) 38 } 39 40 const schema = reactive<FormSchema[]>([ 41 { 42 field: 'name', 43 label: t('automation.triggers.name'), 44 component: 'Input', 45 colProps: { 46 span: 12 47 }, 48 componentProps: { 49 placeholder: t('automation.triggers.name') 50 } 51 }, 52 { 53 field: 'description', 54 label: t('automation.description'), 55 component: 'Input', 56 colProps: { 57 span: 12 58 }, 59 componentProps: { 60 placeholder: t('automation.description') 61 } 62 }, 63 { 64 field: 'area', 65 label: t('automation.area'), 66 value: null, 67 component: 'Area', 68 colProps: { 69 span: 12 70 }, 71 componentProps: { 72 placeholder: t('automation.area'), 73 } 74 }, 75 { 76 field: 'enabled', 77 label: t('automation.enabled'), 78 component: 'Switch', 79 value: false, 80 colProps: { 81 span: 24 82 }, 83 }, 84 { 85 field: 'script', 86 label: t('automation.triggers.script'), 87 component: 'Script', 88 colProps: { 89 span: 24 90 }, 91 value: null, 92 componentProps: { 93 placeholder: t('automation.triggers.script') 94 } 95 }, 96 { 97 field: 'script', 98 component: 'ScriptHelper', 99 colProps: { 100 span: 24 101 }, 102 value: null, 103 componentProps: { 104 placeholder: t('automation.triggers.script'), 105 } 106 }, 107 { 108 field: 'pluginName', 109 label: t('automation.triggers.pluginName'), 110 component: 'Select', 111 colProps: { 112 span: 24, 113 }, 114 value: 'state_change', 115 componentProps: { 116 options: [ 117 { 118 label: 'STATE_CHANGE', 119 value: 'state_change' 120 }, 121 { 122 label: 'TIME', 123 value: 'time' 124 }, 125 { 126 label: 'SYSTEM', 127 value: 'system' 128 }, 129 { 130 label: 'ALEXA', 131 value: 'alexa' 132 } 133 ], 134 clearable: false, 135 onChange: pluginChanged 136 } 137 }, 138 { 139 field: 'entityIds', 140 label: t('automation.triggers.entity'), 141 component: 'Entities', 142 colProps: { 143 span: 24 144 }, 145 hidden: false, 146 componentProps: { 147 placeholder: t('automation.triggers.entity') 148 } 149 }, 150 { 151 hidden: false, 152 field: 'timePluginOptions', 153 label: t('automation.triggers.timePluginOptions'), 154 component: 'Input', 155 colProps: { 156 span: 24 157 }, componentProps: { 158 placeholder: t('automation.triggers.timePluginOptions') 159 } 160 }, 161 { 162 hidden: false, 163 field: 'alexaPluginOptions', 164 label: t('automation.triggers.alexaSkillId'), 165 component: 'InputNumber', 166 colProps: { 167 span: 24 168 }, 169 componentProps: { 170 placeholder: t('automation.triggers.alexaSkillId') 171 } 172 }, 173 ]) 174 175 const prepareAttrs = (attrs?: Record<string, ApiAttribute>) => { 176 if (!attrs) { 177 return 178 } 179 if (attrs.hasOwnProperty('cron')) { 180 setValues({timePluginOptions: attrs.cron.string}) 181 } 182 if (attrs.hasOwnProperty('skillId')) { 183 setValues({alexaPluginOptions: attrs.skillId.int}) 184 } 185 } 186 187 watch( 188 () => props.trigger, 189 (val) => { 190 if (!val) return 191 setValues(val) 192 pluginChanged(val.pluginName) 193 prepareAttrs(val.attributes) 194 }, 195 { 196 deep: true, 197 immediate: true 198 } 199 ) 200 201 pluginChanged(props.trigger?.pluginName) 202 prepareAttrs(props.trigger?.attributes) 203 204 defineExpose({ 205 elFormRef, 206 getFormData: methods.getFormData 207 }) 208 </script> 209 210 <template> 211 <Form 212 :schema="schema" 213 :rules="rules" 214 label-position="top" 215 @register="register" 216 /> 217 </template>