DemuMesDataV/components/scrollBoard/index.vue

405 lines
8.7 KiB
Vue
Raw Normal View History

2018-12-08 19:05:46 +08:00
<template>
2019-06-26 15:25:53 +08:00
<div class="dv-scroll-board" :ref="ref">
<div class="header" v-if="header.length && mergedConfig">
<div
class="header-item"
v-for="(headerItem, i) in header"
:key="headerItem + i"
:style="`
background-color: ${mergedConfig.headerBGC};
height: ${mergedConfig.headerHeight}px;
line-height: ${mergedConfig.headerHeight}px;
width: ${widths[i]}px;
`"
:align="aligns[i]"
v-html="headerItem"
/>
</div>
<div
v-if="mergedConfig"
class="rows"
:style="`height: calc(100% - ${header.length ? mergedConfig.headerHeight : 0}px);`"
>
<div
class="row-item"
v-for="(row, ri) in rows"
2019-06-27 19:28:07 +08:00
:key="row.toString() + row.scroll"
2019-06-26 15:25:53 +08:00
:style="`
height: ${heights[ri]}px;
line-height: ${heights[ri]}px;
background-color: ${mergedConfig[row.rowIndex % 2 === 0 ? 'evenRowBGC' : 'oddRowBGC']};
`"
>
<div
class="ceil"
v-for="(ceil, ci) in row.ceils"
:key="ceil + ri + ci"
:style="`width: ${widths[ci]}px;`"
:align="aligns[ci]"
v-html="ceil"
@click="emitEvent(ri, ci, row, ceil)"
/>
2018-12-19 10:33:59 +08:00
2018-12-08 19:05:46 +08:00
</div>
2019-06-26 15:25:53 +08:00
</div>
2018-12-08 19:05:46 +08:00
</div>
</template>
<script>
2019-06-26 15:25:53 +08:00
import autoResize from '../../mixins/autoResize.js'
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
import { deepMerge } from '@jiaminghi/charts/lib/util/index'
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
import { deepClone } from '@jiaminghi/c-render/lib/plugin/util'
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
export default {
name: 'ScrollBoard',
mixins: [autoResize],
props: {
config: {
type: Object,
default: () => ({})
2018-12-08 19:05:46 +08:00
}
},
2019-06-26 15:25:53 +08:00
data () {
return {
ref: 'scroll-board',
defaultConfig: {
/**
* @description Board header
* @type {Array<String>}
* @default header = []
* @example header = ['column1', 'column2', 'column3']
*/
header: [],
/**
* @description Board data
* @type {Array<Array>}
* @default data = []
*/
data: [],
/**
* @description Row num
* @type {Number}
* @default rowNum = 5
*/
rowNum: 5,
/**
* @description Header background color
* @type {String}
* @default headerBGC = '#00BAFF'
*/
headerBGC: '#00BAFF',
/**
* @description Odd row background color
* @type {String}
* @default oddRowBGC = '#003B51'
*/
oddRowBGC: '#003B51',
/**
* @description Even row background color
* @type {String}
* @default evenRowBGC = '#003B51'
*/
evenRowBGC: '#0A2732',
/**
* @description Scroll wait time
* @type {Number}
* @default waitTime = 2000
*/
waitTime: 2000,
/**
* @description Header height
* @type {Number}
* @default headerHeight = 35
*/
headerHeight: 35,
/**
* @description Column width
* @type {Array<Number>}
* @default columnWidth = []
*/
columnWidth: [],
/**
* @description Column align
* @type {Array<String>}
* @default align = []
* @example align = ['left', 'center', 'right']
*/
align: [],
/**
* @description Show index
* @type {Boolean}
* @default index = false
*/
index: false,
/**
* @description Carousel type
* @type {String}
* @default carousel = 'single'
* @example carousel = 'single' | 'page'
*/
carousel: 'single'
},
mergedConfig: null,
header: [],
rowsData: [],
rows: [],
widths: [],
heights: [],
avgHeight: 0,
aligns: [],
animationIndex: 0,
animationHandler: ''
2018-12-08 19:05:46 +08:00
}
},
2019-06-26 15:25:53 +08:00
watch: {
config () {
const { stopAnimation, calcData } = this
2018-12-08 19:05:46 +08:00
2019-01-09 18:42:51 +08:00
stopAnimation()
2019-06-26 15:25:53 +08:00
calcData()
}
},
methods: {
afterAutoResizeMixinInit () {
const { calcData } = this
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
calcData()
2018-12-08 19:05:46 +08:00
},
2019-06-26 15:25:53 +08:00
onResize () {
const { mergedConfig, calcWidths, calcHeights } = this
if (!mergedConfig) return
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
calcWidths()
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
calcHeights()
2018-12-19 10:33:59 +08:00
},
2019-06-26 15:25:53 +08:00
calcData () {
const { mergeConfig, calcHeaderData, calcRowsData } = this
2019-01-16 16:56:11 +08:00
2019-06-26 15:25:53 +08:00
mergeConfig()
2019-01-16 16:56:11 +08:00
2019-06-26 15:25:53 +08:00
calcHeaderData()
2019-01-16 16:56:11 +08:00
2019-06-26 15:25:53 +08:00
calcRowsData()
2019-01-16 16:56:11 +08:00
2019-06-26 15:25:53 +08:00
const { calcWidths, calcHeights, calcAligns } = this
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
calcWidths()
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
calcHeights()
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
calcAligns()
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
const { animation } = this
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
animation(true)
},
mergeConfig () {
let { config, defaultConfig } = this
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
this.mergedConfig = deepMerge(deepClone(defaultConfig, true), config || {})
},
calcHeaderData () {
let { header, index } = this.mergedConfig
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
if (!header.length) {
this.header = []
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
return
}
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
header = [...header]
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
if (index) header.unshift('#')
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
this.header = header
2018-12-19 10:33:59 +08:00
},
2019-06-26 15:25:53 +08:00
calcRowsData () {
2019-06-27 19:28:07 +08:00
let { data, index, headerBGC, rowNum } = this.mergedConfig
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
if (index) {
data = data.map((row, i) => {
row = [...row]
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
const indexTag = `<span class="index" style="background-color: ${headerBGC};">${i + 1}</spand>`
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
row.unshift(indexTag)
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
return row
})
}
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
data = data.map((ceils, i) => ({ ceils, rowIndex: i }))
2018-12-19 10:33:59 +08:00
2019-06-27 19:28:07 +08:00
const rowLength = data.length
if (rowLength > rowNum && rowLength < 2 * rowNum) {
data = [...data, ...data]
}
data = data.map((d, i) => ({ ...d, scroll: i }))
2019-06-26 15:25:53 +08:00
this.rowsData = data
this.rows = data
2018-12-19 10:33:59 +08:00
},
2019-06-26 15:25:53 +08:00
calcWidths () {
const { width, mergedConfig, rowsData } = this
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
const { columnWidth } = mergedConfig
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
const usedWidth = columnWidth.reduce((all, w) => all + w, 0)
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
const columnNum = rowsData[0] ? rowsData[0].ceils.length : 0
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
const avgWidth = (width - usedWidth) / (columnNum - columnWidth.length)
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
const widths = new Array(columnNum).fill(avgWidth)
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
this.widths = deepMerge(widths, columnWidth)
2018-12-19 10:33:59 +08:00
},
2019-06-26 15:25:53 +08:00
calcHeights (onresize = false) {
const { height, mergedConfig, header } = this
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
const { headerHeight, rowNum, data } = mergedConfig
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
let allHeight = height
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
if (header.length) allHeight -= headerHeight
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
const avgHeight = allHeight / rowNum
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
this.avgHeight = avgHeight
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
if (!onresize) this.heights = new Array(data.length).fill(avgHeight)
},
calcAligns () {
const { header, mergedConfig } = this
2018-12-12 18:48:00 +08:00
2019-06-26 15:25:53 +08:00
const columnNum = header.length
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
let aligns = new Array(columnNum).fill('left')
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
const { align } = mergedConfig
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
this.aligns = deepMerge(aligns, align)
},
async animation (start = false) {
let { avgHeight, animationIndex, mergedConfig, rowsData, animation } = this
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
const { waitTime, carousel, rowNum } = mergedConfig
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
const rowLength = rowsData.length
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
if (rowNum >= rowLength) return
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
if (start) await new Promise(resolve => setTimeout(resolve, waitTime))
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
const animationNum = carousel === 'single' ? 1 : rowNum
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
let rows = rowsData.slice(animationIndex)
rows.push(...rowsData.slice(0, animationIndex))
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
this.rows = rows
this.heights = new Array(rowLength).fill(avgHeight)
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
await new Promise(resolve => setTimeout(resolve, 300))
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
this.heights.splice(0, animationNum, ...new Array(animationNum).fill(0))
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
animationIndex += animationNum
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
const back = animationIndex - rowLength
if (back >= 0) animationIndex = back
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
this.animationIndex = animationIndex
this.animationHandler = setTimeout(animation, waitTime - 300)
2019-01-10 17:31:23 +08:00
},
2018-12-19 10:33:59 +08:00
stopAnimation () {
2019-06-26 15:25:53 +08:00
const { animationHandler } = this
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
if (!animationHandler) return
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
clearTimeout(animationHandler)
},
emitEvent (ri, ci, row, ceil) {
const { ceils, rowIndex } = row
this.$emit('click', {
row: ceils,
ceil,
rowIndex,
columnIndex: ci
})
}
2018-12-19 10:33:59 +08:00
},
destroyed () {
const { stopAnimation } = this
stopAnimation()
2018-12-08 19:05:46 +08:00
}
}
</script>
<style lang="less">
2019-06-26 15:25:53 +08:00
.text {
padding: 0 10px;
box-sizing: border-box;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.dv-scroll-board {
2018-12-19 10:33:59 +08:00
position: relative;
2018-12-08 19:05:46 +08:00
width: 100%;
height: 100%;
2019-01-16 16:56:11 +08:00
color: #fff;
2018-12-08 19:05:46 +08:00
2019-06-26 15:25:53 +08:00
.header {
2018-12-19 10:33:59 +08:00
display: flex;
flex-direction: row;
2019-06-26 15:25:53 +08:00
font-size: 15px;
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
.header-item {
.text;
transition: all 0.3s;
2018-12-19 10:33:59 +08:00
}
2018-12-08 19:05:46 +08:00
}
2019-06-26 15:25:53 +08:00
.rows {
2019-01-10 17:31:23 +08:00
overflow: hidden;
2018-12-19 10:33:59 +08:00
2019-06-26 15:25:53 +08:00
.row-item {
display: flex;
font-size: 14px;
transition: all 0.3s;
2018-12-08 19:05:46 +08:00
}
2019-06-26 15:25:53 +08:00
.ceil {
.text;
2018-12-08 19:05:46 +08:00
}
2019-06-26 15:25:53 +08:00
.index {
border-radius: 3px;
padding: 0px 3px;
2018-12-08 19:05:46 +08:00
}
}
}
</style>