Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
fix: classes count is displayed on structure component
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Oct 28, 2019
1 parent 01d9315 commit 23bdbed
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 46 deletions.
6 changes: 4 additions & 2 deletions components/fallback/Structure.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
:to="{ path: child.path, params: {} }"
:iri="child.iri"
:properties-count="_get(child, 'properties.length', 0)"
:classes-count="childClassesCount(child)"
:modified="child.modified"
:type="child.type" />
</div>
Expand All @@ -42,7 +43,7 @@
</template>

<script>
import { arrayToGroups } from '@/libs/utils'
import { arrayToGroups, childClassesCount } from '@/libs/utils'
import PouchBox from './PouchBox'
import _get from 'lodash/get'
import Dataset from 'indexed-dataset/dataset'
Expand Down Expand Up @@ -81,7 +82,8 @@ export default {
},
methods: {
_get,
arrayToGroups
arrayToGroups,
childClassesCount
}
}
</script>
47 changes: 3 additions & 44 deletions components/home/StructureHome.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
</template>

<script>
import { cloneDeep, get } from 'lodash'
import { hasCreativeWorkChild, arrayToGroups, iriToId } from '@/libs/utils'
import { cloneDeep } from 'lodash'
import { hasCreativeWorkChild, arrayToGroups, iriToId, childClassesCount } from '@/libs/utils'
import PouchBox from '@/components/fallback/PouchBox'
export default {
Expand Down Expand Up @@ -84,48 +84,7 @@ export default {
iriToId,
hasCreativeWorkChild,
arrayToGroups,
// childPropertiesCount (obj) {
// // returned cached version if we have it
// if (obj.hasOwnProperty('childPropertiesCount')) {
// return obj.childPropertiesCount
// }
// // compute it
// const properties = childPropertiesCount(obj)
// .reduce((tmp, p) => {
// tmp[p.subject.value] = true
// return tmp
// }, {})
// const count = Object.keys(properties).length
// // cache it
// obj.childPropertiesCount = count
// return count
// },
childClassesCount (obj, sum = 0, recursing = false) {
if (obj.hasOwnProperty('childClassesCount')) {
return obj.childClassesCount
}
let count
if (get(obj, 'children.length', false)) {
const childCount = obj.children.reduce((acc, child) => this.childClassesCount(child, acc, true), sum)
count = (obj.type === 'class' ? 1 : 0) + childCount
}
else {
count = sum + (obj.type === 'class' ? 1 : 0)
}
if (!recursing) {
obj.childClassesCount = count
}
return count
}
childClassesCount
}
}
// function childPropertiesCount (obj, properties = []) {
// if (obj.children) {
// const prop = Array.isArray(obj.properties) ? obj.properties : obj.properties.toArray()
// const childProps = obj.children.reduce((acc, child) => childPropertiesCount(child, acc), properties)
// return prop.concat(childProps)
// }
// return obj.properties.toArray()
// }
</script>
44 changes: 44 additions & 0 deletions libs/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _get from 'lodash/get'
import rdf from 'rdf-ext'
import { quadToNTriples } from '@rdfjs/to-ntriples'
import { termIRI } from '@/libs/rdf'
Expand Down Expand Up @@ -176,3 +177,46 @@ export function iriToId (iri) {
export function headTitle (title) {
return `${title} - ${process.env.EDITOR_TITLE}`
}

export function childClassesCount (obj, sum = 0, recursing = false) {
if (obj.hasOwnProperty('childClassesCount')) {
return obj.childClassesCount
}
let count
if (_get(obj, 'children.length', false)) {
const childCount = obj.children.reduce((acc, child) => childClassesCount(child, acc, true), sum)
count = (obj.type === 'class' ? 1 : 0) + childCount
}
else {
count = sum + (obj.type === 'class' ? 1 : 0)
}
if (!recursing) {
obj.childClassesCount = count
}
return count
}
export function childPropertiesCount (obj) {
// returned cached version if we have it
if (obj.hasOwnProperty('childPropertiesCount')) {
return obj.childPropertiesCount
}
// compute it
const properties = _childPropertiesCount(obj)
.reduce((tmp, p) => {
tmp[p.subject.value] = true
return tmp
}, {})
const count = Object.keys(properties).length
// cache it
obj.childPropertiesCount = count
return count
}

function _childPropertiesCount (obj, properties = []) {
if (obj.children) {
const prop = Array.isArray(obj.properties) ? obj.properties : obj.properties.toArray()
const childProps = obj.children.reduce((acc, child) => _childPropertiesCount(child, acc), properties)
return prop.concat(childProps)
}
return obj.properties.toArray()
}

0 comments on commit 23bdbed

Please sign in to comment.