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

     1  <script setup lang="ts">
     2  import {useI18n} from '@/hooks/web/useI18n'
     3  import {Table} from '@/components/Table'
     4  import {h, onMounted, onUnmounted, reactive, ref, watch} from 'vue'
     5  import {Pagination, TableColumn} from '@/types/table'
     6  import api from "@/api/api";
     7  import {ElButton, ElTag} from 'element-plus'
     8  import {ApiEntityShort, ApiVariable} from "@/api/stub";
     9  import {useRouter} from "vue-router";
    10  import {ContentWrap} from "@/components/ContentWrap";
    11  import {useCache} from "@/hooks/web/useCache";
    12  import {UUID} from "uuid-generator-ts";
    13  import stream from "@/api/stream";
    14  import {EventStateChange} from "@/api/types";
    15  import {parseTime} from "@/utils";
    16  
    17  const {push} = useRouter()
    18  const {t} = useI18n()
    19  const {wsCache} = useCache()
    20  
    21  interface TableObject {
    22    tableList: ApiVariable[]
    23    params?: any
    24    loading: boolean
    25    sort?: string
    26  }
    27  
    28  interface Params {
    29    page?: number;
    30    limit?: number;
    31    sort?: string;
    32  }
    33  
    34  const cachePref = 'variables'
    35  const tableObject = reactive<TableObject>(
    36      {
    37        tableList: [],
    38        loading: false,
    39        sort: wsCache.get(cachePref + 'Sort') || '-createdAt'
    40      }
    41  );
    42  
    43  const columns: TableColumn[] = [
    44    {
    45      field: 'name',
    46      label: t('variables.name'),
    47      sortable: true,
    48      width: "180px"
    49    },
    50    {
    51      field: 'value',
    52      label: t('variables.value')
    53    },
    54    {
    55      field: 'createdAt',
    56      label: t('main.createdAt'),
    57      type: 'time',
    58      sortable: true,
    59      width: "170px",
    60      formatter: (row: ApiEntityShort) => {
    61        return h(
    62            'span',
    63            parseTime(row.createdAt)
    64        )
    65      }
    66    },
    67    {
    68      field: 'updatedAt',
    69      label: t('main.updatedAt'),
    70      type: 'time',
    71      sortable: true,
    72      width: "170px",
    73      formatter: (row: ApiEntityShort) => {
    74        return h(
    75            'span',
    76            parseTime(row.updatedAt)
    77        )
    78      }
    79    }
    80  ]
    81  const paginationObj = ref<Pagination>({
    82    currentPage: wsCache.get(cachePref + 'CurrentPage') || 1,
    83    pageSize: wsCache.get(cachePref + 'PageSize') || 50,
    84    total: 0,
    85    pageSizes: [50, 100, 150, 250],
    86  })
    87  
    88  const getList = async () => {
    89    tableObject.loading = true
    90  
    91    wsCache.set(cachePref + 'CurrentPage', paginationObj.value.currentPage)
    92    wsCache.set(cachePref + 'PageSize', paginationObj.value.pageSize)
    93    wsCache.set(cachePref + 'Sort', tableObject.sort)
    94  
    95    let params: Params = {
    96      page: paginationObj.value.currentPage,
    97      limit: paginationObj.value.pageSize,
    98      sort: tableObject.sort,
    99    }
   100  
   101    const res = await api.v1.variableServiceGetVariableList(params)
   102        .catch(() => {
   103        })
   104        .finally(() => {
   105          tableObject.loading = false
   106        })
   107    if (res) {
   108      const {items, meta} = res.data;
   109      tableObject.tableList = items;
   110      paginationObj.value.currentPage = meta.pagination.page;
   111      paginationObj.value.total = meta.pagination.total;
   112    } else {
   113      tableObject.tableList = [];
   114    }
   115  }
   116  
   117  watch(
   118      () => paginationObj.value.currentPage,
   119      () => {
   120        getList()
   121      }
   122  )
   123  
   124  watch(
   125      () => paginationObj.value.pageSize,
   126      () => {
   127        getList()
   128      }
   129  )
   130  
   131  const sortChange = (data) => {
   132    const {column, prop, order} = data;
   133    const pref: string = order === 'ascending' ? '+' : '-'
   134    tableObject.sort = pref + prop
   135    getList()
   136  }
   137  
   138  getList()
   139  
   140  const addNew = () => {
   141    push('/etc/variables/new')
   142  }
   143  
   144  const selectRow = (row) => {
   145    if (!row) {
   146      return
   147    }
   148    const {name} = row
   149    push(`/etc/variables/edit/${name}`)
   150  }
   151  
   152  const currentID = ref('')
   153  
   154  const onStateChanged = (event: EventStateChange) => {
   155    getList()
   156  }
   157  
   158  onMounted(() => {
   159    const uuid = new UUID()
   160    currentID.value = uuid.getDashFreeUUID()
   161  
   162    setTimeout(() => {
   163      stream.subscribe('event_removed_variable_model', currentID.value, onStateChanged);
   164      stream.subscribe('event_updated_variable_model', currentID.value, onStateChanged);
   165    }, 200)
   166  })
   167  
   168  onUnmounted(() => {
   169    stream.unsubscribe('event_removed_variable_model', currentID.value);
   170    stream.unsubscribe('event_updated_variable_model', currentID.value);
   171  })
   172  
   173  </script>
   174  
   175  <template>
   176    <ContentWrap>
   177      <ElButton class="flex mb-20px items-left" type="primary" @click="addNew()" plain>
   178        <Icon icon="ep:plus" class="mr-5px"/>
   179        {{ t('variables.addNew') }}
   180      </ElButton>
   181      <Table
   182          class="variables-table"
   183          :expand="true"
   184          :selection="false"
   185          v-model:pageSize="paginationObj.pageSize"
   186          v-model:currentPage="paginationObj.currentPage"
   187          :columns="columns"
   188          :data="tableObject.tableList"
   189          :loading="tableObject.loading"
   190          :pagination="paginationObj"
   191          @sort-change="sortChange"
   192          style="width: 100%"
   193          @current-change="selectRow"
   194          :showUpPagination="20"
   195      >
   196        <template #expand="{row}">
   197          <div class="tag-list" v-if="row.tags">
   198            <ElTag v-for="tag in row.tags" type="info" :key="tag" round effect="light" size="small">
   199              {{ tag }}
   200            </ElTag>
   201          </div>
   202        </template>
   203      </Table>
   204    </ContentWrap>
   205  
   206  </template>
   207  
   208  <style lang="less">
   209  
   210  .el-table__row {
   211    cursor: pointer;
   212  }
   213  
   214  .variables-table {
   215    .tag-list {
   216      .el-tag {
   217        margin: 0 5px;
   218      }
   219    }
   220  
   221    :deep(.el-table__row) {
   222      cursor: pointer;
   223    }
   224  
   225    tr.el-table__row [class*="el-table__cell"] {
   226    //background-color: green; border-top: var(--el-table-border); border-bottom: none !important;
   227      border-top: var(--el-table-border);
   228    }
   229  
   230    .el-table__expanded-cell {
   231      &.el-table__cell [class*="tag-list"] {
   232      //background-color: red!important; border-bottom: none !important;
   233      }
   234  
   235      &.el-table__cell:not(:has(.tag-list)) {
   236        display: none !important;
   237      //background-color: blue!important;
   238      }
   239    }
   240  
   241    .el-table td.el-table__cell,
   242    .el-table th.el-table__cell.is-leaf {
   243      border-bottom: none !important;
   244    }
   245  
   246    .el-table--enable-row-hover .el-table__body tr.el-table__row:hover,
   247    .el-table--enable-row-hover .el-table__body tr.el-table__row:hover + tr {
   248      & > td.el-table__cell {
   249        background-color: var(--el-table-row-hover-bg-color);
   250      }
   251    }
   252  }
   253  
   254  </style>