Skip to content

Commit 7b59a6d

Browse files
authored
Merge pull request #151 from vue-pivottable/fix/hmr
fix: delay Draggable render to prevent _sortable.option error on HMR …
2 parents 63ff4b5 + 2ea40c8 commit 7b59a6d

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed

src/components/pivottable-ui/VDragAndDropCell.vue

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<template>
22
<Draggable
3+
v-if="showDraggable && modelItems.length > 0"
34
tag="td"
45
:list="modelItems"
5-
:group="{ name: 'sharted', pull: true, put: true }"
6+
:group="{ name: 'shared', pull: true, put: true }"
67
:ghost-class="'pvtPlaceholder'"
78
:prevent-on-filter="false"
89
:class="classes"
@@ -40,7 +41,7 @@
4041
</template>
4142

4243
<script setup>
43-
import { ref, onMounted, computed } from 'vue'
44+
import { ref, onMounted, computed, watch, nextTick, onBeforeUnmount } from 'vue'
4445
import { VueDraggableNext as Draggable } from 'vue-draggable-next'
4546
import VDraggableAttribute from './VDraggableAttribute.vue'
4647
@@ -95,6 +96,7 @@ const props = defineProps({
9596
})
9697
9798
const modelItems = ref([])
99+
const showDraggable = ref(false)
98100
99101
const onDragMove = (event) => {
100102
const draggedItem = event.draggedContext.element
@@ -118,8 +120,22 @@ const onDragEnd = () => {
118120
119121
onMounted(() => {
120122
modelItems.value = [...props.attributeNames]
123+
nextTick(() => {
124+
showDraggable.value = true
125+
})
121126
})
122127
128+
onBeforeUnmount(() => {
129+
showDraggable.value = false
130+
})
131+
132+
watch(
133+
() => props.attributeNames,
134+
(newVal) => {
135+
modelItems.value = [...newVal]
136+
}
137+
)
138+
123139
const hideDropDownForUnused = computed(() => {
124140
return props.cellType === 'unused' && props.hideFilterBoxOfUnusedAttributes
125141
})

src/composables/useMaterializeInput.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ export function useMaterializeInput(dataSource, options) {
2424
for (const attr of Object.keys(record)) {
2525
if (!(attr in newAllFilters)) {
2626
newAllFilters[attr] = {}
27-
if (recordsProcessed > 0) { newAllFilters[attr].null = recordsProcessed }
27+
if (recordsProcessed > 0) {
28+
newAllFilters[attr].null = recordsProcessed
29+
}
2830
}
2931
}
3032

3133
for (const attr in newAllFilters) {
3234
const value = attr in record ? record[attr] : 'null'
33-
if (!(value in newAllFilters[attr])) { newAllFilters[attr][value] = 0 }
35+
if (!(value in newAllFilters[attr])) {
36+
newAllFilters[attr][value] = 0
37+
}
3438
newAllFilters[attr][value]++
3539
}
3640

@@ -51,7 +55,9 @@ export function useMaterializeInput(dataSource, options) {
5155

5256
watch(
5357
() => options.derivedAttributes.value,
54-
() => { processData(dataSource.value) }
58+
() => {
59+
processData(dataSource.value)
60+
}
5561
)
5662

5763
return {

src/composables/useProvidePivotData.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export function providePivotData(props) {
77
const error = ref(null)
88

99
const pivotData = computed(() => {
10-
try { return new PivotData(props) } catch (err) {
10+
try {
11+
return new PivotData(props)
12+
} catch (err) {
1113
console.error(err.stack)
1214
error.value = 'An error occurred computing the PivotTable results.'
1315
return null
@@ -35,7 +37,9 @@ export function providePivotData(props) {
3537
})
3638

3739
const allColorScales = computed(() => {
38-
const values = rowKeys.value.reduce((acc, r) => { return acc.concat(colKeys.value.map((c) => getAggregator(r, c).value())) }, [])
40+
const values = rowKeys.value.reduce((acc, r) => {
41+
return acc.concat(colKeys.value.map((c) => getAggregator(r, c).value()))
42+
}, [])
3943
return colorScaleGenerator(values)
4044
})
4145
const rowColorScales = computed(() =>
@@ -56,7 +60,13 @@ export function providePivotData(props) {
5660
)
5761

5862
const valueCellColors = (rowKey, colKey, value) => {
59-
if (props.heatmapMode === 'full') { return allColorScales.value(value) } else if (props.heatmapMode === 'row') { return rowColorScales.value[rowKey](value) } else if (props.heatmapMode === 'col') { return colColorScales.value[colKey](value) }
63+
if (props.heatmapMode === 'full') {
64+
return allColorScales.value(value)
65+
} else if (props.heatmapMode === 'row') {
66+
return rowColorScales.value[rowKey](value)
67+
} else if (props.heatmapMode === 'col') {
68+
return colColorScales.value[colKey](value)
69+
}
6070
return {}
6171
}
6272
const rowTotalValues = colKeys.value.map((x) => getAggregator([], x).value())
@@ -76,15 +86,27 @@ export function providePivotData(props) {
7686
let x
7787
if (i !== 0) {
7888
let noDraw = true
79-
for (x = 0; x <= j; x++) { if (arr[i - 1][x] !== arr[i][x]) { noDraw = false } }
80-
if (noDraw) { return -1 }
89+
for (x = 0; x <= j; x++) {
90+
if (arr[i - 1][x] !== arr[i][x]) {
91+
noDraw = false
92+
}
93+
}
94+
if (noDraw) {
95+
return -1
96+
}
8197
}
8298

8399
let len = 0
84100
while (i + len < arr.length) {
85101
let stop = false
86-
for (x = 0; x <= j; x++) { if (arr[i][x] !== arr[i + len][x]) { stop = true } }
87-
if (stop) { break }
102+
for (x = 0; x <= j; x++) {
103+
if (arr[i][x] !== arr[i + len][x]) {
104+
stop = true
105+
}
106+
}
107+
if (stop) {
108+
break
109+
}
88110
len++
89111
}
90112
return len
@@ -109,4 +131,6 @@ export function providePivotData(props) {
109131
return pivotDataContext
110132
}
111133

112-
export function useProvidePivotData() { return inject(PIVOT_DATA_KEY) }
134+
export function useProvidePivotData() {
135+
return inject(PIVOT_DATA_KEY)
136+
}

0 commit comments

Comments
 (0)