<script>
import { scroll } from 'quasar'
const { getScrollTarget, setScrollPosition } = scroll
import ResultsCollateTable from '../components/ResultsCollateTable.vue'
import { mapState, mapGetters } from 'vuex'
import fuzzysort from 'fuzzysort'
export default {
name: 'CollateResultsPage',
components: {
ResultsCollateTable
},
data () {
return {
items: [],
idx: 0,
turnOff: false,
itemLoad: 3,
delay: 1000
}
},
computed: {
...mapState('appCompareDatas', ['searchText']),
...mapState('compareDatas', [
'reviewItems',
'selectedPendingItems',
'selectedApprovedItems'
]),
...mapState('persistCompareDatas', [
'autoColumns'
]),
...mapGetters('persistCompareDatas', [
'currentColumnPreset',
'currentRowPreset'
]),
...mapGetters('compareDatas', [
'fetchItemsLoading',
'haveReviewItems',
'rollItems'
]),
allItems () {
return [
...this.selectedPendingItems,
...this.selectedApprovedItems
]
},
fieldRecords () {
const items = this.allItems, fields = this.autoColumns
const fieldSet = fields.reduce((o, f) => {
items.forEach(i => {
f.name in o ? o[f.name].add(i[f.name]) : o[f.name] = new Set([i[f.name]])
})
return o
}, {})
return fields.map(f => ({
name: f.name,
label: f.label,
value: Array.from(fieldSet[f.name])
}))
},
commonTags () {
return this.fieldRecords.filter(r => r.value.length === 1)
.map(r => ({...r, value: r.value[0]}))
},
nonCommonTags () {
return this.fieldRecords.filter(r => r.value.length !== 1)
},
nonCommonColumnNames () {
return this.nonCommonTags.map(f => f.name)
},
rolledItems () {
return this.rollItems(this.allItems)
},
rolledCheckNames () {
return this.rolledItems.checks
},
filteredRolledCheckNames () {
let names = [...this.rolledCheckNames]
if (this.currentRowPreset.value === 'hideZero') {
names = names.filter(name => (this.groupVioCount(name) > 0))
} else if (this.currentRowPreset.value === 'hidePendingZero') {
names = names.filter(name => (this.groupPendingVioCount(name) > 0))
} else if (this.currentRowPreset.value === 'showOnlyZero') {
names = names.filter(name => (this.groupVioCount(name) === 0))
}
return names
},
rolledTargets () {
return this.filteredRolledCheckNames.map(t => fuzzysort.prepare(t))
},
filteredCheckNames () {
return !this.searchText
? this.filteredRolledCheckNames.map(name => ({target: name}))
: fuzzysort.go(this.searchText, this.rolledTargets, {
threshold: -10000,
allowTypo: true,
limit: 200
})
},
showing () {
return `${this.filteredCheckNames.length}/${this.rolledCheckNames.length}`
}
},
methods: {
loadMore (index, done) {
// If there is more than 1 Data loaded, then add a delay to the rendering
if (this.selectedPendingItems.length + this.selectedApprovedItems.length === 1) {
this.delay = 0
}
// Mechanism for loading more violations
if (this.turnOff === false) {
setTimeout(() => {
let items = []
// Load violations in steps of itemLoad
for (var i = this.idx; i < this.idx + this.itemLoad; i++) {
if (i < this.filteredCheckNames.length) {
items.push(this.filteredCheckNames[i])
} else {
this.turnOff = true
}
}
this.idx = i
this.items = this.items.concat(items)
done()
}, this.delay)
}
},
orderItems (resultsMap) {
return this.rolledItems.ids
.filter(id => id in resultsMap)
.map(id => resultsMap[id])
},
highlight (result) {
return result ? fuzzysort.highlight(result, '<span class="text-primary">', '</span>') : ''
},
groupVioCount (groupName) {
const group = this.rolledItems.map[groupName]
const oids = Object.keys(group)
return oids.map(id => group[id])
.map(item => item.count)
.reduce((a, c) => a + c, 0)
},
groupPendingVioCount (groupName) {
const group = this.rolledItems.map[groupName]
const oids = Object.keys(group)
return oids.map(id => group[id])
.filter(item => item.scope === 'pending')
.map(item => item.count)
.reduce((a, c) => a + c, 0)
},
detectColumnPreset () {
const flaggedChecks = !this.searchText
? this.filteredRolledCheckNames.map(name => ({target: name}))
: fuzzysort.go(this.searchText, this.rolledTargets, {
threshold: -10000,
allowTypo: true,
limit: 1
})
flaggedChecks[0].target = flaggedChecks[0].target.startsWith('/') ? flaggedChecks[0].target.substring(1) : flaggedChecks[0].target
const numberElements = flaggedChecks[0].target.split('/')
return numberElements.length > 1
? 'test1' : 'test2'
},
scrollToElement (el) {
let target = getScrollTarget(el)
let offset = el.offsetTop - el.scrollHeight
let duration = 1000
setScrollPosition(target, offset, duration)
}
},
filters: {
sanitizedTagValues: v => v.toUpperCase()
},
mounted () {
this.currentColumnPreset.value = this.detectColumnPreset()
}
}
</script>