|
1
|
+<template>
|
|
2
|
+ <div class="dv-scroll-board">
|
|
3
|
+ <div
|
|
4
|
+ class="header"
|
|
5
|
+ v-if="status.header.length && status.mergedConfig"
|
|
6
|
+ :style="`background-color: ${status.mergedConfig.headerBGC};`"
|
|
7
|
+ >
|
|
8
|
+ <div
|
|
9
|
+ class="header-item"
|
|
10
|
+ v-for="(headerItem, i) in status.header"
|
|
11
|
+ :key="`${headerItem}${i}`"
|
|
12
|
+ :style="`
|
|
13
|
+ height: ${status.mergedConfig.headerHeight}px;
|
|
14
|
+ line-height: ${status.mergedConfig.headerHeight}px;
|
|
15
|
+ width: ${status.widths[i]}px;
|
|
16
|
+ `"
|
|
17
|
+ :align="status.aligns[i]"
|
|
18
|
+ v-html="headerItem"
|
|
19
|
+ />
|
|
20
|
+ </div>
|
|
21
|
+
|
|
22
|
+ <div
|
|
23
|
+ v-if="status.mergedConfig"
|
|
24
|
+ class="rows"
|
|
25
|
+ :style="`height: ${h - (status.header.length ? status.mergedConfig.headerHeight : 0)}px;`"
|
|
26
|
+ >
|
|
27
|
+ <div
|
|
28
|
+ class="row-item"
|
|
29
|
+ v-for="(row, ri) in status.rows"
|
|
30
|
+ :key="`${row.toString()}${row.scroll}`"
|
|
31
|
+ :style="`
|
|
32
|
+ height: ${status.heights[ri]}px;
|
|
33
|
+ line-height: ${status.heights[ri]}px;
|
|
34
|
+ background-color: ${status.mergedConfig[row.rowIndex % 2 === 0 ? 'evenRowBGC' : 'oddRowBGC']};
|
|
35
|
+ `"
|
|
36
|
+ >
|
|
37
|
+ <div
|
|
38
|
+ class="ceil"
|
|
39
|
+ v-for="(ceil, ci) in row.ceils"
|
|
40
|
+ :key="`${ceil}${ri}${ci}`"
|
|
41
|
+ :style="`width: ${status.widths[ci]}px;`"
|
|
42
|
+ :align="status.aligns[ci]"
|
|
43
|
+ v-html="ceil"
|
|
44
|
+ />
|
|
45
|
+ </div>
|
|
46
|
+ </div>
|
|
47
|
+ </div>
|
|
48
|
+</template>
|
|
49
|
+
|
|
50
|
+<script setup lang="ts">
|
|
51
|
+import { PropType, onUnmounted, reactive, toRefs, watch, onMounted } from 'vue'
|
|
52
|
+import { CreateComponentType } from '@/packages/index.d'
|
|
53
|
+import { useChartDataFetch } from '@/hooks'
|
|
54
|
+import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
|
|
55
|
+import merge from 'lodash/merge'
|
|
56
|
+import cloneDeep from 'lodash/cloneDeep'
|
|
57
|
+import { getDeviceDetail, getAttribute } from '@/api/external/common/index'
|
|
58
|
+
|
|
59
|
+const props = defineProps({
|
|
60
|
+ chartConfig: {
|
|
61
|
+ type: Object as PropType<CreateComponentType>,
|
|
62
|
+ required: true
|
|
63
|
+ }
|
|
64
|
+})
|
|
65
|
+
|
|
66
|
+// 这里能拿到图表宽高等
|
|
67
|
+const { w, h } = toRefs(props.chartConfig.attr)
|
|
68
|
+// 这里能拿到上面 config.ts 里的 option 数据
|
|
69
|
+// const { rowNum, headerHeight, index, backgroundColor } = toRefs(props.chartConfig.option)
|
|
70
|
+
|
|
71
|
+const status = reactive({
|
|
72
|
+ defaultConfig: {
|
|
73
|
+ /**
|
|
74
|
+ * @description Board header
|
|
75
|
+ * @type {Array<String>}
|
|
76
|
+ * @default header = []
|
|
77
|
+ * @example header = ['column1', 'column2', 'column3']
|
|
78
|
+ */
|
|
79
|
+ header: [],
|
|
80
|
+ /**
|
|
81
|
+ * @description Board dataset
|
|
82
|
+ * @type {Array<Array>}
|
|
83
|
+ * @default dataset = []
|
|
84
|
+ */
|
|
85
|
+ dataset: [],
|
|
86
|
+ /**
|
|
87
|
+ * @description Row num
|
|
88
|
+ * @type {Number}
|
|
89
|
+ * @default rowNum = 5
|
|
90
|
+ */
|
|
91
|
+ rowNum: 5,
|
|
92
|
+ /**
|
|
93
|
+ * @description Header background color
|
|
94
|
+ * @type {String}
|
|
95
|
+ * @default headerBGC = '#00BAFF'
|
|
96
|
+ */
|
|
97
|
+ headerBGC: '#00BAFF',
|
|
98
|
+ /**
|
|
99
|
+ * @description Odd row background color
|
|
100
|
+ * @type {String}
|
|
101
|
+ * @default oddRowBGC = '#003B51'
|
|
102
|
+ */
|
|
103
|
+ oddRowBGC: '#003B51',
|
|
104
|
+ /**
|
|
105
|
+ * @description Even row background color
|
|
106
|
+ * @type {String}
|
|
107
|
+ * @default evenRowBGC = '#003B51'
|
|
108
|
+ */
|
|
109
|
+ evenRowBGC: '#0A2732',
|
|
110
|
+ /**
|
|
111
|
+ * @description Scroll wait time
|
|
112
|
+ * @type {Number}
|
|
113
|
+ * @default waitTime = 2
|
|
114
|
+ */
|
|
115
|
+ waitTime: 2,
|
|
116
|
+ /**
|
|
117
|
+ * @description Header height
|
|
118
|
+ * @type {Number}
|
|
119
|
+ * @default headerHeight = 35
|
|
120
|
+ */
|
|
121
|
+ headerHeight: 35,
|
|
122
|
+ /**
|
|
123
|
+ * @description Column width
|
|
124
|
+ * @type {Array<Number>}
|
|
125
|
+ * @default columnWidth = []
|
|
126
|
+ */
|
|
127
|
+ columnWidth: [],
|
|
128
|
+ /**
|
|
129
|
+ * @description Column align
|
|
130
|
+ * @type {Array<String>}
|
|
131
|
+ * @default align = []
|
|
132
|
+ * @example align = ['left', 'center', 'right']
|
|
133
|
+ */
|
|
134
|
+ align: [],
|
|
135
|
+ /**
|
|
136
|
+ * @description Show index
|
|
137
|
+ * @type {Boolean}
|
|
138
|
+ * @default index = false
|
|
139
|
+ */
|
|
140
|
+ index: false,
|
|
141
|
+ /**
|
|
142
|
+ * @description index Header
|
|
143
|
+ * @type {String}
|
|
144
|
+ * @default indexHeader = '#'
|
|
145
|
+ */
|
|
146
|
+ indexHeader: '#',
|
|
147
|
+ /**
|
|
148
|
+ * @description Carousel type
|
|
149
|
+ * @type {String}
|
|
150
|
+ * @default carousel = 'single'
|
|
151
|
+ * @example carousel = 'single' | 'page'
|
|
152
|
+ */
|
|
153
|
+ carousel: 'single',
|
|
154
|
+ /**
|
|
155
|
+ * @description Pause scroll when mouse hovered
|
|
156
|
+ * @type {Boolean}
|
|
157
|
+ * @default hoverPause = true
|
|
158
|
+ * @example hoverPause = true | false
|
|
159
|
+ */
|
|
160
|
+ hoverPause: true
|
|
161
|
+ },
|
|
162
|
+ mergedConfig: props.chartConfig.option,
|
|
163
|
+ header: [],
|
|
164
|
+ rowsData: [],
|
|
165
|
+ rows: [
|
|
166
|
+ {
|
|
167
|
+ ceils: [],
|
|
168
|
+ rowIndex: 0,
|
|
169
|
+ scroll: 0
|
|
170
|
+ }
|
|
171
|
+ ],
|
|
172
|
+ widths: [],
|
|
173
|
+ heights: [0],
|
|
174
|
+ avgHeight: 0,
|
|
175
|
+ aligns: [],
|
|
176
|
+ animationIndex: 0,
|
|
177
|
+ animationHandler: 0,
|
|
178
|
+ updater: 0,
|
|
179
|
+ needCalc: false
|
|
180
|
+})
|
|
181
|
+
|
|
182
|
+const calcData = () => {
|
|
183
|
+ mergeConfig()
|
|
184
|
+ calcHeaderData()
|
|
185
|
+ calcRowsData()
|
|
186
|
+ calcWidths()
|
|
187
|
+ calcHeights()
|
|
188
|
+ calcAligns()
|
|
189
|
+ animation(true)
|
|
190
|
+}
|
|
191
|
+
|
|
192
|
+onMounted(() => {
|
|
193
|
+ calcData()
|
|
194
|
+})
|
|
195
|
+
|
|
196
|
+const mergeConfig = () => {
|
|
197
|
+ status.mergedConfig = merge(cloneDeep(status.defaultConfig), props.chartConfig.option)
|
|
198
|
+}
|
|
199
|
+
|
|
200
|
+const calcHeaderData = () => {
|
|
201
|
+ let { header, index, indexHeader } = status.mergedConfig
|
|
202
|
+ if (!header.length) {
|
|
203
|
+ status.header = []
|
|
204
|
+ return
|
|
205
|
+ }
|
|
206
|
+ header = [...header]
|
|
207
|
+ if (index) header.unshift(indexHeader)
|
|
208
|
+ status.header = header
|
|
209
|
+}
|
|
210
|
+
|
|
211
|
+const calcRowsData = () => {
|
|
212
|
+ let { dataset, index, headerBGC, rowNum } = status.mergedConfig
|
|
213
|
+ if (index) {
|
|
214
|
+ dataset = dataset.map((row: any, i: number) => {
|
|
215
|
+ row = [...row]
|
|
216
|
+ const indexTag = `<span class="index" style="background-color: ${headerBGC};border-radius: 3px;padding: 0px 3px;">${
|
|
217
|
+ i + 1
|
|
218
|
+ }</span>`
|
|
219
|
+ row.unshift(indexTag)
|
|
220
|
+ return row
|
|
221
|
+ })
|
|
222
|
+ }
|
|
223
|
+ dataset = dataset.map((ceils: any, i: number) => ({ ceils, rowIndex: i }))
|
|
224
|
+ const rowLength = dataset.length
|
|
225
|
+ if (rowLength > rowNum && rowLength < 2 * rowNum) {
|
|
226
|
+ dataset = [...dataset, ...dataset]
|
|
227
|
+ }
|
|
228
|
+ dataset = dataset.map((d: any, i: number) => ({ ...d, scroll: i }))
|
|
229
|
+
|
|
230
|
+ status.rowsData = dataset
|
|
231
|
+ status.rows = dataset
|
|
232
|
+}
|
|
233
|
+
|
|
234
|
+const calcWidths = () => {
|
|
235
|
+ const { mergedConfig, rowsData } = status
|
|
236
|
+ const { columnWidth, header } = mergedConfig
|
|
237
|
+ const usedWidth = columnWidth.reduce((all: any, ws: number) => all + ws, 0)
|
|
238
|
+ let columnNum = 0
|
|
239
|
+ if (rowsData[0]) {
|
|
240
|
+ columnNum = (rowsData[0] as any).ceils.length
|
|
241
|
+ } else if (header.length) {
|
|
242
|
+ columnNum = header.length
|
|
243
|
+ }
|
|
244
|
+ const avgWidth = (w.value - usedWidth) / (columnNum - columnWidth.length)
|
|
245
|
+ const widths = new Array(columnNum).fill(avgWidth)
|
|
246
|
+ status.widths = merge(widths, columnWidth)
|
|
247
|
+}
|
|
248
|
+
|
|
249
|
+const calcHeights = (onresize = false) => {
|
|
250
|
+ const { mergedConfig, header } = status
|
|
251
|
+ const { headerHeight, rowNum, dataset } = mergedConfig
|
|
252
|
+ let allHeight = h.value
|
|
253
|
+ if (header.length) allHeight -= headerHeight
|
|
254
|
+ const avgHeight = allHeight / rowNum
|
|
255
|
+ status.avgHeight = avgHeight
|
|
256
|
+ if (!onresize) status.heights = new Array(dataset.length).fill(avgHeight)
|
|
257
|
+}
|
|
258
|
+
|
|
259
|
+const calcAligns = () => {
|
|
260
|
+ const { header, mergedConfig } = status
|
|
261
|
+
|
|
262
|
+ const columnNum = header.length
|
|
263
|
+
|
|
264
|
+ let aligns = new Array(columnNum).fill('left')
|
|
265
|
+
|
|
266
|
+ const { align } = mergedConfig
|
|
267
|
+
|
|
268
|
+ status.aligns = merge(aligns, align)
|
|
269
|
+}
|
|
270
|
+
|
|
271
|
+const animation = async (start = false) => {
|
|
272
|
+ const { needCalc } = status
|
|
273
|
+
|
|
274
|
+ if (needCalc) {
|
|
275
|
+ calcRowsData()
|
|
276
|
+ calcHeights()
|
|
277
|
+ status.needCalc = false
|
|
278
|
+ }
|
|
279
|
+ let { avgHeight, animationIndex, mergedConfig, rowsData, updater } = status
|
|
280
|
+ const { waitTime, carousel, rowNum } = mergedConfig
|
|
281
|
+
|
|
282
|
+ const rowLength = rowsData.length
|
|
283
|
+ if (rowNum >= rowLength) return
|
|
284
|
+ if (start) {
|
|
285
|
+ await new Promise(resolve => setTimeout(resolve, waitTime * 1000))
|
|
286
|
+ if (updater !== status.updater) return
|
|
287
|
+ }
|
|
288
|
+ const animationNum = carousel === 'single' ? 1 : rowNum
|
|
289
|
+ let rows = rowsData.slice(animationIndex)
|
|
290
|
+ rows.push(...rowsData.slice(0, animationIndex))
|
|
291
|
+ status.rows = rows.slice(0, carousel === 'page' ? rowNum * 2 : rowNum + 1)
|
|
292
|
+ status.heights = new Array(rowLength).fill(avgHeight)
|
|
293
|
+ await new Promise(resolve => setTimeout(resolve, 300))
|
|
294
|
+ if (updater !== status.updater) return
|
|
295
|
+ status.heights.splice(0, animationNum, ...new Array(animationNum).fill(0))
|
|
296
|
+ animationIndex += animationNum
|
|
297
|
+ const back = animationIndex - rowLength
|
|
298
|
+ if (back >= 0) animationIndex = back
|
|
299
|
+ status.animationIndex = animationIndex
|
|
300
|
+ status.animationHandler = setTimeout(animation, waitTime * 1000 - 300) as any
|
|
301
|
+}
|
|
302
|
+
|
|
303
|
+const stopAnimation = () => {
|
|
304
|
+ status.updater = (status.updater + 1) % 999999
|
|
305
|
+ if (!status.animationHandler) return
|
|
306
|
+ clearTimeout(status.animationHandler)
|
|
307
|
+}
|
|
308
|
+
|
|
309
|
+const onRestart = async () => {
|
|
310
|
+ try {
|
|
311
|
+ if (!status.mergedConfig) return
|
|
312
|
+ stopAnimation()
|
|
313
|
+ calcData()
|
|
314
|
+ } catch (error) {
|
|
315
|
+ console.log(error)
|
|
316
|
+ }
|
|
317
|
+}
|
|
318
|
+
|
|
319
|
+watch(
|
|
320
|
+ () => w.value,
|
|
321
|
+ () => {
|
|
322
|
+ onRestart()
|
|
323
|
+ }
|
|
324
|
+)
|
|
325
|
+
|
|
326
|
+watch(
|
|
327
|
+ () => h.value,
|
|
328
|
+ () => {
|
|
329
|
+ onRestart()
|
|
330
|
+ }
|
|
331
|
+)
|
|
332
|
+
|
|
333
|
+// 数据更新
|
|
334
|
+watch(
|
|
335
|
+ () => props.chartConfig.option,
|
|
336
|
+ () => {
|
|
337
|
+ onRestart()
|
|
338
|
+ },
|
|
339
|
+ { deep: true }
|
|
340
|
+)
|
|
341
|
+
|
|
342
|
+// 数据更新 (默认更新 dataset,若更新之后有其它操作,可添加回调函数)
|
|
343
|
+//特殊处理 获取属性,把标识符转为物模型的属性名称
|
|
344
|
+useChartDataFetch(props.chartConfig, useChartEditStore, async (resData: any[], res: any) => {
|
|
345
|
+ const { requestParams } = res
|
|
346
|
+ if (!requestParams) return
|
|
347
|
+ const { Params } = requestParams
|
|
348
|
+ if (!Params) return
|
|
349
|
+ const { entityId } = Params
|
|
350
|
+ const thingsModel = await handleDeviceProfileAttributes(entityId)
|
|
351
|
+ const { attribute } = thingsModel as any
|
|
352
|
+ const resDataFormat = resData.reduce((acc, curr) => {
|
|
353
|
+ attribute.forEach((item: any) => {
|
|
354
|
+ if (item.identifier === curr[0]) {
|
|
355
|
+ curr[0] = item.name
|
|
356
|
+ }
|
|
357
|
+ })
|
|
358
|
+ acc.push(curr)
|
|
359
|
+ return [...acc]
|
|
360
|
+ }, [])
|
|
361
|
+ props.chartConfig.option.dataset = resDataFormat
|
|
362
|
+ onRestart()
|
|
363
|
+})
|
|
364
|
+
|
|
365
|
+onUnmounted(() => {
|
|
366
|
+ stopAnimation()
|
|
367
|
+})
|
|
368
|
+
|
|
369
|
+const handleDeviceProfileAttributes = async (entityId: string) => {
|
|
370
|
+ const deviceDetailRes = await getDeviceDetail(entityId)
|
|
371
|
+ const { deviceProfileId } = deviceDetailRes
|
|
372
|
+ if (!deviceProfileId) return
|
|
373
|
+ const attributeRes = await getAttribute(deviceProfileId)
|
|
374
|
+ const dataFormat = handleDataFormat(deviceDetailRes, attributeRes)
|
|
375
|
+ return dataFormat
|
|
376
|
+}
|
|
377
|
+
|
|
378
|
+const handleDataFormat = (deviceDetail: any, attributes: any) => {
|
|
379
|
+ const { name, tbDeviceId } = deviceDetail
|
|
380
|
+ const attribute = attributes.map((item: any) => ({
|
|
381
|
+ identifier: item.identifier,
|
|
382
|
+ name: item.name,
|
|
383
|
+ detail: item.detail
|
|
384
|
+ }))
|
|
385
|
+ return {
|
|
386
|
+ name,
|
|
387
|
+ tbDeviceId,
|
|
388
|
+ attribute
|
|
389
|
+ }
|
|
390
|
+}
|
|
391
|
+</script>
|
|
392
|
+
|
|
393
|
+<style lang="scss" scoped>
|
|
394
|
+.dv-scroll-board {
|
|
395
|
+ position: relative;
|
|
396
|
+ width: 100%;
|
|
397
|
+ height: 100%;
|
|
398
|
+ color: #fff;
|
|
399
|
+
|
|
400
|
+ .text {
|
|
401
|
+ padding: 0 10px;
|
|
402
|
+ box-sizing: border-box;
|
|
403
|
+ white-space: nowrap;
|
|
404
|
+ overflow: hidden;
|
|
405
|
+ text-overflow: ellipsis;
|
|
406
|
+ }
|
|
407
|
+
|
|
408
|
+ .header {
|
|
409
|
+ display: flex;
|
|
410
|
+ flex-direction: row;
|
|
411
|
+ font-size: 15px;
|
|
412
|
+
|
|
413
|
+ .header-item {
|
|
414
|
+ transition: all 0.3s;
|
|
415
|
+ }
|
|
416
|
+ }
|
|
417
|
+
|
|
418
|
+ .rows {
|
|
419
|
+ overflow: hidden;
|
|
420
|
+
|
|
421
|
+ .row-item {
|
|
422
|
+ display: flex;
|
|
423
|
+ font-size: 14px;
|
|
424
|
+ transition: all 0.3s;
|
|
425
|
+ overflow: hidden;
|
|
426
|
+ }
|
|
427
|
+ }
|
|
428
|
+}
|
|
429
|
+</style> |
...
|
...
|
|