|
| 1 | +<template> |
| 2 | + <div class="container"> |
| 3 | + <tree |
| 4 | + :data="tree" |
| 5 | + node-text="name" |
| 6 | + layoutType="euclidean" |
| 7 | + type="tree" |
| 8 | + :zoomable="true" |
| 9 | + :radius="4" |
| 10 | + ></tree> |
| 11 | + </div> |
| 12 | +</template> |
| 13 | + |
| 14 | +<script> |
| 15 | +import { tree } from 'vued3tree' |
| 16 | +import { mapState } from 'vuex' |
| 17 | +export default { |
| 18 | + name: 'Tree', |
| 19 | + components: { |
| 20 | + tree |
| 21 | + }, |
| 22 | + computed: { |
| 23 | + ...mapState(['componentMap']), |
| 24 | + componentMap: { |
| 25 | + get () { |
| 26 | + return this.$store.state.componentMap |
| 27 | + } |
| 28 | + } |
| 29 | + }, |
| 30 | + data () { |
| 31 | + return { |
| 32 | + tree: null |
| 33 | + } |
| 34 | + }, |
| 35 | + methods: { |
| 36 | + formatComponentMap (compMap) { |
| 37 | + console.log('\n Map : ', compMap, '\n') |
| 38 | + let result = [] |
| 39 | + Object.values(compMap).forEach(compData => { |
| 40 | + result.push({ |
| 41 | + name: compData.componentName, |
| 42 | + children: compData.children |
| 43 | + }) |
| 44 | + }) |
| 45 | + return result |
| 46 | + }, |
| 47 | + transformToTree (data) { |
| 48 | + let result = {} |
| 49 | + const nodes = {} |
| 50 | + const formattedData = this.formatComponentMap(data) |
| 51 | +
|
| 52 | + // console.log('\n >>>> Formatted data <<<<'); |
| 53 | + // console.log('FormattedData: ', formattedData, '\n'); |
| 54 | +
|
| 55 | + // console.log('\n >>>> TRANSFORM TO TREE <<<< \n'); |
| 56 | +
|
| 57 | + formattedData.forEach(component => { |
| 58 | + if (!nodes[component.name]) { |
| 59 | + nodes[component.name] = { name: component.name, children: [] } |
| 60 | + result = nodes |
| 61 | + } |
| 62 | + // console.log('CURRENT COMPONENT: ', component.name); |
| 63 | + component.children.forEach(child => { |
| 64 | + // if(typeof child === 'object') child = child.componentName; |
| 65 | + nodes[child] = { name: child, children: [] } |
| 66 | + nodes[component.name].children.push(nodes[child]) |
| 67 | + // console.log('Adding child: ', typeof child, child); |
| 68 | + // console.log('\n'); |
| 69 | + }) |
| 70 | + }) |
| 71 | +
|
| 72 | + console.log('\n >>>> RESULTS <<<< ') |
| 73 | + console.log(result) |
| 74 | + console.log('\n >>>> ______ <<<<') |
| 75 | + return result |
| 76 | + }, |
| 77 | +
|
| 78 | + buildTree () { |
| 79 | + let build = this.transformToTree(this.componentMap) |
| 80 | + this.tree = build['App'] |
| 81 | + } |
| 82 | + }, |
| 83 | + created () { |
| 84 | + this.buildTree() |
| 85 | + } |
| 86 | +} |
| 87 | +</script> |
| 88 | +<style> |
| 89 | +.container { |
| 90 | + height: 100%; |
| 91 | + width: 100%; |
| 92 | +} |
| 93 | +
|
| 94 | +.treeclass .nodetree text { |
| 95 | + font-size: 19px !important; |
| 96 | + cursor: pointer; |
| 97 | + text-shadow: none !important; |
| 98 | + font-weight: bold; |
| 99 | +
|
| 100 | + /* none of these classes work |
| 101 | + color: white !important; |
| 102 | + background: white; |
| 103 | + margin: 1rem; */ |
| 104 | +} |
| 105 | + /* changes the circle node color */ |
| 106 | +.treeclass .nodetree circle { |
| 107 | + fill: rgb(232, 225, 16); |
| 108 | +} |
| 109 | +/* changes the stroke color */ |
| 110 | +.treeclass .linktree { |
| 111 | + stroke: rgb(232, 225, 16) !important; |
| 112 | + stroke-opacity: .4; |
| 113 | + stroke-width: 2px; |
| 114 | +} |
| 115 | +</style> |
0 commit comments