github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/views/Automation/components/Conditions.vue (about)

     1  <script setup lang="ts">
     2  import {useI18n} from '@/hooks/web/useI18n'
     3  import {Table} from '@/components/Table'
     4  import {PropType, reactive, ref, unref, watch} from 'vue'
     5  import {TableColumn} from '@/types/table'
     6  import {ElButton, ElTag, ElPopconfirm} from 'element-plus'
     7  import {ApiCondition} from "@/api/stub";
     8  import {useRouter} from "vue-router";
     9  import ConditionForm from "./ConditionForm.vue";
    10  import {useEmitt} from "@/hooks/web/useEmitt";
    11  
    12  const {currentRoute, addRoute, push} = useRouter()
    13  const props = defineProps({
    14    conditions: {
    15      type: Array as PropType<ApiCondition[]>,
    16      default: () => []
    17    }
    18  })
    19  
    20  const writeRef = ref<ComponentRef<typeof ConditionForm>>()
    21  let currentCondition = reactive<Nullable<ApiCondition>>(null)
    22  let currentConditionIndex = ref(0)
    23  
    24  enum Mode {
    25    VIEW = 'VIEW',
    26    EDIT = 'EDIT',
    27    NEW = 'NEW'
    28  }
    29  
    30  const mode = ref<Mode>(Mode.VIEW)
    31  const {t} = useI18n()
    32  
    33  interface TableObject {
    34    tableList: ApiCondition[]
    35  }
    36  
    37  const tableObject = reactive<TableObject>(
    38      {
    39        tableList: [],
    40      }
    41  );
    42  
    43  watch(
    44      () => props.conditions,
    45      (val: ApiCondition[]) => {
    46        if (val == unref(tableObject.tableList)) return;
    47        tableObject.tableList = val
    48      },
    49      {
    50        immediate: true
    51      }
    52  )
    53  
    54  const columns: TableColumn[] =  [
    55    {
    56      field: 'name',
    57      label: t('conditions.name'),
    58    },
    59    {
    60      field: 'script',
    61      label: t('conditions.script'),
    62    },
    63    {
    64      field: 'operations',
    65      label: t('conditions.operations'),
    66      width: "200px",
    67      type: 'time',
    68  
    69    },
    70  ]
    71  
    72  const addNew = () => {
    73    currentCondition = null
    74    mode.value = Mode.NEW
    75  }
    76  
    77  const {emitter} = useEmitt()
    78  const updateConditions = () => {
    79    const triggers = unref(tableObject.tableList)
    80    emitter.emit('updateConditions', triggers)
    81  }
    82  
    83  const edit = (condition: ApiCondition, $index) => {
    84    currentConditionIndex.value = $index
    85    currentCondition = unref(condition)
    86    mode.value = Mode.EDIT
    87  }
    88  
    89  const save = async () => {
    90    const write = unref(writeRef)
    91    const validate = await write?.elFormRef?.validate()?.catch(() => {
    92    })
    93    const data = (await write?.getFormData())
    94    if (validate) {
    95      const condition = {
    96        id: data.id,
    97        name: data.name,
    98        script: data.script,
    99        scriptId: data.script?.id || null,
   100      } as ApiCondition
   101      if (mode.value === Mode.NEW) {
   102        tableObject.tableList.push(condition)
   103      } else {
   104        tableObject.tableList[currentConditionIndex.value] = condition
   105      }
   106      mode.value = Mode.VIEW
   107      updateConditions()
   108    }
   109  }
   110  
   111  const resetForm = () => {
   112    mode.value = Mode.VIEW
   113    currentCondition = null
   114  }
   115  
   116  const removeItem = () => {
   117    tableObject.tableList.splice(currentConditionIndex.value, 1);
   118    mode.value = Mode.VIEW
   119  }
   120  
   121  const goToScript = (trigger: ApiCondition) => {
   122    if (!trigger.script?.id) {
   123      return
   124    }
   125    push(`/scripts/edit/${trigger.script?.id}`)
   126  }
   127  
   128  </script>
   129  
   130  <template>
   131    <ElButton class="flex mb-20px items-left" @click="addNew()" plain v-if="mode==='VIEW'">
   132      <Icon icon="ep:plus" class="mr-5px"/>
   133      {{ t('conditions.addNew') }}
   134    </ElButton>
   135  
   136    <Table
   137        v-if="mode==='VIEW'"
   138        :selection="false"
   139        :columns="columns"
   140        :data="tableObject.tableList"
   141        style="width: 100%"
   142    >
   143  
   144      <template #script="{row}">
   145        <ElButton v-if="row.script" :link="true" @click.prevent.stop="goToScript(row)">
   146          {{ row.script.name }}
   147        </ElButton>
   148        <span v-else>-</span>
   149      </template>
   150  
   151      <template #operations="{ row }">
   152        <ElButton :link="true" @click.prevent.stop="edit(row)">
   153          {{ $t('main.edit') }}
   154        </ElButton>
   155  
   156      </template>
   157  
   158    </Table>
   159  
   160    <ConditionForm ref="writeRef" :condition="currentCondition" v-if="mode!=='VIEW'"/>
   161  
   162    <div style="text-align: right" v-if="mode!=='VIEW'">
   163  
   164      <ElButton v-if="mode === 'NEW'" type="primary" @click="save()">
   165        {{ t('conditions.addCondition') }}
   166      </ElButton>
   167  
   168      <ElButton v-if="mode === 'EDIT'" type="primary" @click="save()">
   169        {{ t('main.update') }}
   170      </ElButton>
   171  
   172      <ElButton type="default" @click="resetForm()">
   173        {{ t('main.cancel') }}
   174      </ElButton>
   175  
   176      <ElPopconfirm
   177          v-if="mode === 'EDIT'"
   178          :confirm-button-text="$t('main.ok')"
   179          :cancel-button-text="$t('main.no')"
   180          width="250"
   181          style="margin-left: 10px;"
   182          :title="$t('main.are_you_sure_to_do_want_this?')"
   183          @confirm="removeItem"
   184      >
   185        <template #reference>
   186          <ElButton class="mr-10px" type="danger" plain>
   187            <Icon icon="ep:delete" class="mr-5px"/>
   188            {{ t('main.remove') }}
   189          </ElButton>
   190        </template>
   191      </ElPopconfirm>
   192  
   193    </div>
   194  </template>
   195  
   196  <style lang="less">
   197  
   198  </style>