index.vue 4.24 KB
Newer Older
1 2
<template>
  <div class="eContainer-home">
潘际乾 committed
3 4 5 6
    <c-row
      v-for="cRow in cellRows"
      :key="cRow"
    >
潘际乾 committed
7 8
      <div class="cell-item"
        :style="{'width': cellWidth + 'px'}"
潘际乾 committed
9 10 11 12 13 14 15 16
        v-for="cCol in cellCols"
        :key="cCol"
      >
        <component
          v-if="getComponentName([cRow - 1], [cCol - 1])"
          v-bind:is="getComponentName([cRow - 1], [cCol - 1])"
          :cellContentHeight="cellContentHeight"
        ></component>
潘际乾 committed
17
      </div>
潘际乾 committed
18
    </c-row>
19 20 21 22
  </div>
</template>

<script>
潘际乾 committed
23 24 25 26 27 28 29 30 31 32 33
import { createNamespacedHelpers } from "vuex";
const { mapState } = createNamespacedHelpers("UserContext");

import QuickVisit from "./cells/QuickVisit.vue";
import TaskStatistics from "./cells/TaskStatistics.vue";
import NoticeAnnouncement from "./cells/NoticeAnnouncement.vue";
import Hall from "./cells/Hall.vue";
import CustomerAnalyse from "./cells/CustomerAnalyse.vue";
import QuickSearch from "./cells/QuickSearch.vue";

import { cellDataDefinition } from "./config";
34 35 36 37

export default {
  name: "Home",
  components: {
潘际乾 committed
38 39 40 41 42 43
    QuickVisit,
    TaskStatistics,
    NoticeAnnouncement,
    Hall,
    CustomerAnalyse,
    QuickSearch,
潘际乾 committed
44
  },
45
  data() {
潘际乾 committed
46
    return {
潘际乾 committed
47
      cellContentHeight: 0,
潘际乾 committed
48
      cellWidth: 0
潘际乾 committed
49
    };
50
  },
潘际乾 committed
51
  created() {
52
    // this.loadCellData();
潘际乾 committed
53 54
  },
  mounted() {
潘际乾 committed
55 56 57
    this.calcAgain();
    this.calcAgainBind = this.calcAgain.bind(this);
    window.addEventListener("resize", this.calcAgainBind);
潘际乾 committed
58 59 60 61 62 63 64 65 66 67 68 69
  },
  computed: {
    ...mapState({
      cellRows: (state) => state.homeCellsSetting.cellRows,
      cellCols: (state) => state.homeCellsSetting.cellCols,
      cellNames: (state) => state.homeCellsSetting.cellNames,
    }),
  },
  watch: {
    cellRows(newVal, oldVal) {
      this.calcCellContentHeight()
    },
潘际乾 committed
70 71 72
    cellCols(newVal, oldVal) {
      this.calcCellContentWidth()
    }
潘际乾 committed
73
  },
潘际乾 committed
74
  methods: {
潘际乾 committed
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
    loadCellData() {
      this.$store.commit(
        "UserContext/setHomeDefaultCells",
        cellDataDefinition
      );
      let cellsSettingDefault = localStorage.getItem("cells-setting-default");
      if (!cellsSettingDefault) {
        const rows = 2;
        const cols = 3;
        localStorage.setItem(
          "cells-setting-default",
          JSON.stringify({
            cellRows: rows,
            cellCols: cols,
            cellNames: this.generateRowColNames(
              rows,
              cols,
              cellDataDefinition.map((definition) => definition.name)
            ),
          })
        );
      }
      let userCellsSetting = localStorage.getItem(
        `cells-setting-${this.$store.state.UserContext.userId}`
      );
      if (!userCellsSetting) {
        userCellsSetting = localStorage.getItem("cells-setting-default");
      }
      this.$store.commit(
        "UserContext/setHomeCellsSetting",
        JSON.parse(userCellsSetting)
      );
    },
    generateRowColNames(rows, cols, cells) {
      const rArr = [];
      for (let i = 0; i < rows; i++) {
        const cArr = [];
        for (let j = 0; j < cols; j++) {
          cArr.push(cells[i * cols + j]);
        }
        rArr.push(cArr);
      }
      return rArr;
    },
    getComponentName(rowIdx, colIdx) {
      return this.cellNames[rowIdx] ? this.cellNames[rowIdx][colIdx] : null;
    },
    calcCellContentHeight() {
潘际乾 committed
123
      // this.cellContentHeight = this.$el.clientHeight * this.getRowHeightPercent() - 52 - 10
潘际乾 committed
124
      this.cellContentHeight = Math.floor(((this.$el.clientHeight - 1) - (this.cellRows + 1) * 25 - (52 + 10) * this.cellRows) / this.cellRows)
潘际乾 committed
125 126 127 128
    },
    getRowHeightPercent() {
      // 每行预留 0.4% 的间距
      return 1 / this.cellRows - 0.004 * this.cellRows;
潘际乾 committed
129
    },
潘际乾 committed
130
    calcCellContentWidth() {
潘际乾 committed
131
      this.cellWidth = Math.floor(((this.$el.clientWidth - 1) - (this.cellCols + 1) * 25) / this.cellCols)
潘际乾 committed
132 133 134 135
    },
    calcAgain() {
      this.calcCellContentHeight()
      this.calcCellContentWidth()
潘际乾 committed
136
    }
潘际乾 committed
137 138
  },
  destroyed() {
潘际乾 committed
139
    window.removeEventListener("resize", this.calcAgainBind);
潘际乾 committed
140
  },
141 142 143 144 145
};
</script>

<style scoped>
.eContainer-home {
潘际乾 committed
146
  box-sizing: border-box;
147
  height: 100%;
潘际乾 committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161
  padding: 25px;
}
.eContainer-home .el-row {
  margin-bottom: 25px;
}
.eContainer-home .el-row:last-child {
  margin-bottom: 0;
}
.eContainer-home .cell-item {
  float: left;
  margin-right: 25px;
}
.eContainer-home .el-row .cell-item:last-child {
  margin-right: 0;
潘际乾 committed
162
}
163
</style>