github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/views/Variables/edit.vue (about) 1 <script setup lang="ts"> 2 import {computed, ref} from 'vue' 3 import {useI18n} from '@/hooks/web/useI18n' 4 import {ElButton, ElPopconfirm} from 'element-plus' 5 import {useRoute, useRouter} from 'vue-router' 6 import api from "@/api/api"; 7 import {ApiVariable} from "@/api/stub"; 8 import {ContentWrap} from "@/components/ContentWrap"; 9 import VariableForm from "@/views/Variables/components/VariableForm.vue"; 10 11 const {push} = useRouter() 12 const route = useRoute(); 13 const {t} = useI18n() 14 15 const variableName = computed(() => route.params.name); 16 const currentRow = ref<Nullable<ApiVariable>>(null) 17 18 const fetch = async () => { 19 const res = await api.v1.variableServiceGetVariableByName(variableName.value as string) 20 .catch(() => { 21 }) 22 .finally(() => { 23 }) 24 if (res) { 25 currentRow.value = res.data 26 } else { 27 currentRow.value = null 28 } 29 } 30 31 const download = async () => { 32 // window.location.href = 'data:application/octet-stream;base64,' + currentRow.value.value; 33 const a = document.createElement("a"); //Create <a> 34 a.href = 'data:application/octet-stream;base64,' + currentRow.value.value; 35 a.download = currentRow.value.name; //File name Here 36 a.click(); 37 } 38 39 const save = async () => { 40 const data = { 41 value: currentRow.value.value, 42 tags: currentRow.value.tags, 43 } 44 const res = await api.v1.variableServiceUpdateVariable(variableName.value as string, data) 45 .catch(() => { 46 }) 47 .finally(() => { 48 }) 49 50 } 51 52 const cancel = () => { 53 push('/etc/variables') 54 } 55 56 const remove = async () => { 57 const res = await api.v1.variableServiceDeleteVariable(variableName.value as string) 58 .catch(() => { 59 }) 60 .finally(() => { 61 }) 62 if (res) { 63 cancel() 64 } 65 } 66 fetch() 67 68 </script> 69 70 <template> 71 <ContentWrap> 72 <VariableForm v-if="currentRow" v-model="currentRow" :edit="true"/> 73 74 <div style="text-align: right"> 75 76 <ElButton type="primary" @click="download()"> 77 {{ t('main.download') }} 78 </ElButton> 79 80 <ElButton type="primary" @click="save()"> 81 {{ t('main.save') }} 82 </ElButton> 83 84 <ElButton type="default" @click="cancel()"> 85 {{ t('main.return') }} 86 </ElButton> 87 88 <ElPopconfirm 89 :confirm-button-text="$t('main.ok')" 90 :cancel-button-text="$t('main.no')" 91 width="250" 92 style="margin-left: 10px;" 93 :title="$t('main.are_you_sure_to_do_want_this?')" 94 @confirm="remove" 95 > 96 <template #reference> 97 <ElButton class="mr-10px" type="danger" plain> 98 <Icon icon="ep:delete" class="mr-5px"/> 99 {{ t('main.remove') }} 100 </ElButton> 101 </template> 102 </ElPopconfirm> 103 104 </div> 105 </ContentWrap> 106 107 </template> 108 109 <style lang="less" scoped> 110 111 </style>