-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseMaterializeInput.js
66 lines (53 loc) · 1.56 KB
/
useMaterializeInput.js
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
import { ref, watch } from 'vue'
import { PivotData } from '@/helper/utilities.js'
export function useMaterializeInput (dataSource, options) {
const rawData = ref(null)
const allFilters = ref({})
const materializedInput = ref([])
function processData (data) {
if (!data || rawData.value === data) return
rawData.value = data
const newAllFilters = {}
const newMaterializedInput = []
let recordsProcessed = 0
PivotData.forEachRecord(
data,
options.derivedAttributes.value,
function (record) {
newMaterializedInput.push(record)
for (const attr of Object.keys(record)) {
if (!(attr in newAllFilters)) {
newAllFilters[attr] = {}
if (recordsProcessed > 0) {
newAllFilters[attr].null = recordsProcessed
}
}
}
for (const attr in newAllFilters) {
const value = attr in record ? record[attr] : 'null'
if (!(value in newAllFilters[attr])) {
newAllFilters[attr][value] = 0
}
newAllFilters[attr][value]++
}
recordsProcessed++
}
)
allFilters.value = newAllFilters
materializedInput.value = newMaterializedInput
return {
AllFilters: newAllFilters,
materializedInput: newMaterializedInput
}
}
watch(() => dataSource.value, processData, { immediate: true })
watch(() => options.derivedAttributes.value, () => {
processData(dataSource.value)
})
return {
rawData,
allFilters,
materializedInput,
processData
}
}