index.vue 3.8 KB
<template>
	<div>
    <draggable class="home-wrap" v-model="layoutList" :disabled="true" filter=".undraggable" @change="handleChange">
			<div
				class="cell-item draggable"
				ref="cellItem"
				v-for="(item, index) in calcLayoutList"
				:key="index"
				:style="{
					'width': `calc((100% - ${(item.rowCount- 1) * 8}px) * ${item.width / 100})`,
					'height': `calc(${item.height}% - 8px)`,
					'margin-right': item.isRowLastTag ? '0' : '8px'
				}"
			>
				<!-- @adjust="handleAdjust" -->
				<component
					:is="item.component" :componentName="item.component" style="width:100%; height:100%" 
				></component>
			</div>
    </draggable>
  </div>
</template>

<script>
import Api from '~/service/Api';
import draggable from "vuedraggable";
import CommonSystem from './components/CommonSystem'
import ForeignExchangeRates from "./components/ForeignExchangeRates";
import NoticeAnnouncement from "./components/NoticeAnnouncement";
import Double from "./components/Double";
import { cloneDeep } from 'lodash'

export default {
  name: "home",
  components: {
		draggable,
		CommonSystem,
		ForeignExchangeRates,
		NoticeAnnouncement,
		Double
  },
  data () {
    return {
      layoutList: [
				{
          component: 'Double',
					width: 58,
					height: 52
				},
				{
          component: 'ForeignExchangeRates',
					width: 42,
					height: 52
        },
				{
          component: 'NoticeAnnouncement',
					width: 58,
					height: 48
				},
				{
          component: 'CommonSystem',
					width: 42,
					height: 48
				},
			],
			calcLayoutList: []
    }
  },
  mounted () {
		this.handleCalcLayoutList()
  },
  methods: {
		handleCalcLayoutList () {
			let rowIndex = 0
			let rowTotalWidth = 0
			let list = cloneDeep(this.layoutList)
			let curList = []
			// 处理当前card处于哪一行,添加行索引
			list.map((item) => {
				if (rowTotalWidth + item.width < 100) {
					rowTotalWidth += item.width
				} else if (rowTotalWidth + item.width === 100) {
					if (!rowTotalWidth) {
						rowIndex++
					}
					rowTotalWidth = 100
				} else {
					rowTotalWidth = item.width
					rowIndex++
				}
				item.rowIndex = rowIndex
			})
			let curInd = 0
			// 处理每一行有几个card,为了方便计算显示的宽度和margin
			list.map((item, index) => {
				if (curInd !== item.rowIndex) {
					curInd = item.rowIndex
				}
				let len = list.filter((fil) => {
					return fil.rowIndex === curInd
				}).length
				item.rowCount = len
			})
			// 处理每一行最后一项的问题
			list.map((item, index) => {
				item.isRowLastTag = false
				if (index < (list.length - 1) && (item.rowIndex != list[index + 1].rowIndex)) {
					item.isRowLastTag = true
				}
				if (index === list.length - 1) {
					item.isRowLastTag = true
				}
			})
			this.calcLayoutList = list
		},
		async handleChange(e){
			// console.log(e);
			// console.log(this.layoutList)
			// let params = {
			// 	layoutList: this.layoutList
			// }
			// let res = await Api.post(`/business/home`, params);
			// if (res.respCode == SUCCESS) {
			// 	this.$message({type: 'success',message: '操作成功!'});
			// }
			this.handleCalcLayoutList()
		},
		// // 传回滑动值及当前组件名称
		// handleAdjust(val) {
		// 	// 改变对应组件的宽度
		// 	this.layoutList.forEach(item=>{
		// 		if(val.componentName === item.component) {
		// 			item.width = val.value;
		// 		}
		// 	})
		// 	this.handleCalcLayoutList()
		// }
  }
};
</script>

<style scoped>
.home-wrap {
  width: 100%;
  height: calc(100vh - 42px);
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  flex-wrap: wrap;
  overflow: auto;
  padding: 8px 0;
  box-sizing: border-box;
	user-select: none;
	min-width: 1000px;
  overflow-x: auto;
}
.cell-item {
  /* height: calc((100% - 50px) / 2); */
  margin-bottom: 8px;
  margin-right: 8px;
  position: relative;
}
</style>