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

Commit

Permalink
perf: categories in the post settings supports showing whether there …
Browse files Browse the repository at this point in the history
…is a password or not (#540)

Signed-off-by: Ryan Wang <[email protected]>
  • Loading branch information
ruibaby authored Apr 7, 2022
1 parent 30f3349 commit 118b793
Showing 1 changed file with 34 additions and 41 deletions.
75 changes: 34 additions & 41 deletions src/components/Category/CategoryTree.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<template>
<a-tree
v-if="!categories.loading"
:checkedKeys="categoryIds"
:treeData="categoryTree"
:treeData.sync="categories.data"
checkStrictly
checkable
defaultExpandAll
Expand All @@ -14,37 +15,6 @@
<script>
import apiClient from '@/utils/api-client'
function concreteTree(parentCategory, categories) {
categories.forEach(category => {
if (parentCategory.key === category.parentId) {
if (!parentCategory.children) {
parentCategory.children = []
}
parentCategory.children.push({
key: category.id,
title: category.name,
isLeaf: false
})
}
})
if (parentCategory.children) {
parentCategory.children.forEach(category => concreteTree(category, categories))
} else {
parentCategory.isLeaf = true
}
}
function getConcreteTree(categories) {
const topCategoryNode = {
key: 0,
title: 'top',
children: []
}
concreteTree(topCategoryNode, categories)
return topCategoryNode.children
}
export default {
name: 'CategoryTree',
model: {
Expand All @@ -66,14 +36,6 @@ export default {
}
}
},
computed: {
categoryTree() {
if (!this.categories.data.length) {
return []
}
return getConcreteTree(this.categories.data)
}
},
created() {
this.handleListCategories()
},
Expand All @@ -84,14 +46,45 @@ export default {
const { data } = await apiClient.category.list({ sort: [], more: false })
this.categories.data = data
this.categories.data = this.convertDataToTree(data)
} catch (error) {
this.$log.error(error)
} finally {
this.categories.loading = false
}
},
convertDataToTree(categories) {
const hashMap = {}
const treeData = []
categories.forEach(
category => (hashMap[category.id] = { ...category, key: category.id, title: category.name, children: [] })
)
categories.forEach(category => {
const current = hashMap[category.id]
if (category.parentId) {
hashMap[category.parentId].children.push(current)
} else {
treeData.push(current)
}
})
// set hasPassword field for tree node
const setHasPasswordField = (categories, hasPassword = false) => {
categories.forEach(category => {
category.hasPassword = !!category.password || hasPassword
if (category.hasPassword) {
category.title = `${category.title}(加密)`
}
if (category.children && category.children.length) {
setHasPasswordField(category.children, category.hasPassword)
}
})
}
setHasPasswordField(treeData)
return treeData
},
onCheck(checkedKeys, e) {
this.$log.debug('Chekced keys', checkedKeys)
this.$log.debug('e', e)
Expand Down

0 comments on commit 118b793

Please sign in to comment.