index1.vue 4.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
 
 
 <template>
  <div>
    <draggable class="home-wrap" v-model="layoutList" filter=".undraggable">
        <div
          class="cell-item"
          :class="[canDrag === item.component ? 'unDraggable' : 'draggable']"
          ref="cellItem"
          v-for="(item, index) in layoutList"
          :style="{'width': `calc((100% - 75px) * ${item.width / 100})`}"
        >
          <component
            :is="item.component"
          ></component>
          <div class="move-tar-right" @mousedown.stop="(e) => handleMousedown(e, item, index)"></div>
        </div>
    </draggable>
  </div>
</template>
<script>
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 draggable from "vuedraggable";
export default {
  name: 'Home',
  components: {
    QuickVisit,
    TaskStatistics,
    NoticeAnnouncement,
    Hall,
    CustomerAnalyse,
    QuickSearch,
    draggable,
  },
  computed: {
    calcLayoutList () {
      let list = []
      let widthNum = 0
      let col = 0
      this.layoutList.map((item, index) => {{
        if (widthNum <= 100 && (widthNum + item.width > 100)) {
          widthNum = 0
          col = 0
          list.push({
            ...item,
            col: col
          })
        } else {
          col++
          list.push({
            ...item,
            col: col
          })
        }
      }})
      console.log('list', list)
      return list
    }
  },
  data () {
    return {
      disabledDrag: false,
      layoutList: [
        {
          component: 'QuickVisit',
          width: 33
        },
        {
          component: 'TaskStatistics',
          width: 33
        },
        {
          component: 'NoticeAnnouncement',
          width: 33
        },
        {
          component: 'Hall',
          width: 33
        },
        {
          component: 'CustomerAnalyse',
          width: 33
        },
        {
          component: 'QuickSearch',
          width: 33
        },
      ],
      // 控制是否可以拖拽
      canDrag: null,
      index: 0,
      clientWidth: 0,
      startPos: 0
    }
  },
  mounted () {
    // 监听鼠标离开'home-wrap'的时候移除move的监听事件
    document.querySelector('.home-wrap').addEventListener('mouseleave', () => {
      console.log('leave')
      document.removeEventListener('mousemove', this.handleMousemove)
    })
    // 监听鼠标从'home-wrap'上松开的时候移除move的监听事件
    document.querySelector('.home-wrap').addEventListener('mouseup', () => {
      console.log('up-')
      this.disabledDrag = false
      this.canDrag = null
      document.removeEventListener('mousemove', this.handleMousemove)
    })
  },
  methods: {
    // 阻止默认事件
    pauseEvent(e) {
      if(e.stopPropagation) e.stopPropagation();
      if(e.preventDefault) e.preventDefault();
      e.cancelBubble=true;
      e.returnValue=false;
      return false;
    },
    // 鼠标按下时
    handleMousedown (e, paramsData, index) {
      console.log('mousedown', paramsData)
      this.pauseEvent(e)
      this.canDrag = paramsData.component
      this.disabledDrag = true
      this.clientWidth = this.$refs.cellItem[index].clientWidth
      this.startPos = e.clientX
      this.index = index
      document.addEventListener('mousemove', this.handleMousemove)
    },
    // 鼠标move事件
    handleMousemove (e) {
      this.pauseEvent(e)
      let nodeList = this.$refs.cellItem
      let distance = e.clientX - this.startPos
      nodeList[this.index].style.width = this.clientWidth + distance + 'px'
    },
  }
}
</script>
<style scoped>
.home-wrap {
  width: 100%;
  height: calc(100vh - 100px);
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  flex-wrap: wrap;
  overflow: auto;
  padding: 16px;
  box-sizing: border-box;
  user-select: none;
}
.cell-item {
  height: calc((100% - 50px) / 2);
  margin-bottom: 25px;
  margin-right: 25px;
  position: relative;
}
.move-tar-right {
  width: 10px;
  height: 100%;
  position: absolute;
  right: 0;
  top: 0;
}
.move-tar-right:hover {
  cursor: col-resize;
}
</style>