LibraryTree.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <b-overlay :show="nodeTree.nodes.length === 0" class="h-100" z-index="0">
  3. <map-zoom
  4. id="library-tree"
  5. :min-zoom="0.3" :max-zoom="1" :center="center"
  6. :key="nodeDepartId"
  7. >
  8. <path
  9. v-for="link in nodeTree.links"
  10. :key="`${link.source.data.id}_${link.target.data.id}`"
  11. :d="lineGenerator([link.source, link.target])"
  12. :class="['svg-link', (link.linkType || link.target.data.linkType)]"
  13. />
  14. <circle
  15. v-for="node in nodeTree.nodes"
  16. :key="'circle' + node.data.id"
  17. :cx="node.x"
  18. :cy="node.y"
  19. class="svg-dot"
  20. :id="'preview-node-' + node.data.id"
  21. :class="['svg-dot-' + node.data.variant, { 'origin': node.parent === null }]"
  22. tabindex="0"
  23. @focus="activeNode = node"
  24. @mouseenter="activeNode = node"
  25. @mouseleave="activeNode = null"
  26. @blur="activeNode = null"
  27. @click.stop="onNodeClick(node.data)"
  28. @keyup.enter="onNodeClick(node.data)"
  29. />
  30. <g>
  31. <rect class="svg-overlay" />
  32. <foreignObject
  33. v-if="activeNode"
  34. :x="activeNode.x"
  35. :y="activeNode.y"
  36. >
  37. <dot-button
  38. :variant="activeNode.data.variant"
  39. active hovered
  40. @click="onNodeClick(activeNode.data)"
  41. >
  42. <template v-if="activeNode.data.type === 'prod' && !(activeNode.data.preTitle || activeNode.data.italTitle)">
  43. {{ $t('variants.' + activeNode.data.variant) }}<br>
  44. </template>
  45. <node-view-title v-else :node="activeNode.data" block />
  46. </dot-button>
  47. </foreignObject>
  48. </g>
  49. </map-zoom>
  50. <legend-toggle>
  51. <h6>Mode Arborescent</h6>
  52. <p>Ce mode vous permet d'observer l'arbre des relations d'un texte de départ.</p>
  53. Types de relation :
  54. <ul>
  55. <li><span class="children" /> Textes produits</li>
  56. <li><span class="siblings" /> Textes rebonds</li>
  57. </ul>
  58. </legend-toggle>
  59. <node-preview-zone v-model="previewNode" :nodes="dataNodes" @open-node="onPreviewNodeClick" />
  60. </b-overlay>
  61. </template>
  62. <script>
  63. import { mapGetters } from 'vuex'
  64. import { line } from 'd3-shape'
  65. import { forceSimulation, forceLink, forceManyBody, forceX, forceY } from 'd3-force'
  66. import { MapZoom, NodePreviewZone } from '@/components/layouts'
  67. import { NodeViewTitle } from '@/components/nodes'
  68. export default {
  69. name: 'LibraryTree',
  70. components: {
  71. MapZoom,
  72. NodePreviewZone,
  73. NodeViewTitle
  74. },
  75. data () {
  76. return {
  77. activeNode: null,
  78. previewNode: null
  79. }
  80. },
  81. computed: {
  82. ...mapGetters(['nodeDepartId', 'nodeTree']),
  83. dataNodes () {
  84. return this.nodeTree.nodes.map(node => node.data)
  85. },
  86. // ONE TIME GETTER
  87. simulation () {
  88. return forceSimulation()
  89. .force('charge', forceManyBody().strength((d) => {
  90. if (d.data.linkType === 'parents') return -2000
  91. if (d.data.linkType === 'siblings') return -500
  92. return -1000
  93. }))
  94. .force('link', forceLink().id(d => d.id).distance((d) => {
  95. if (d.linkType === 'parents') return 200
  96. if (d.linkType === 'siblings') return 100
  97. return 0
  98. }).strength(0.5))
  99. .force('x', forceX())
  100. .force('y', forceY())
  101. },
  102. center () {
  103. const { x, y } = this.nodeTree.nodes.length ? this.nodeTree.nodes[0] : { x: 0, y: 0 }
  104. return { x, y }
  105. },
  106. // ONE TIME GETTER
  107. lineGenerator () {
  108. return line().x(node => node.x).y(node => node.y)
  109. }
  110. },
  111. watch: {
  112. nodeTree (tree) {
  113. this.activeNode = null
  114. this.previewNode = null
  115. this.simulation.nodes(tree.nodes)
  116. this.simulation.force('link').links(tree.links)
  117. this.simulation.alpha(0.5).restart()
  118. }
  119. },
  120. methods: {
  121. onNodeClick (node) {
  122. this.$store.dispatch('GET_NODE', { id: node.id, dataLevel: 'partial' })
  123. this.previewNode = node
  124. this.$root.$emit('bv::show::popover', 'preview-node-' + node.id)
  125. },
  126. onPreviewNodeClick (ids) {
  127. this.$root.$emit('bv::hide::popover', 'preview-node-' + this.previewNode.id)
  128. this.$emit('open-node', ids)
  129. this.previewNode = null
  130. }
  131. },
  132. created () {
  133. this.$store.dispatch('INIT_LIBRARY_TREE')
  134. }
  135. }
  136. </script>
  137. <style lang="scss" scoped>
  138. .svg {
  139. &-link {
  140. stroke: grey;
  141. vector-effect: non-scaling-stroke;
  142. &.siblings {
  143. stroke-dasharray: 4;
  144. }
  145. &.parents {
  146. stroke: red;
  147. opacity: .3;
  148. }
  149. }
  150. &-dot {
  151. cursor: pointer;
  152. r: 9.5px;
  153. @each $color, $value in $theme-colors {
  154. &-#{$color} {
  155. fill: $value;
  156. @if $color == 'depart' {
  157. stroke: $black;
  158. stroke-width: 3px;
  159. vector-effect: non-scaling-stroke;
  160. }
  161. }
  162. }
  163. &.origin {
  164. r: 15px;
  165. }
  166. }
  167. &-overlay {
  168. width: 100%;
  169. height: 100%;
  170. x: -50%;
  171. y: -50%;
  172. fill: transparent;
  173. pointer-events: none;
  174. }
  175. }
  176. foreignObject {
  177. height: 1px;
  178. width: 1px;
  179. overflow: visible;
  180. }
  181. .dot-btn {
  182. @media (hover: none) {
  183. display: none;
  184. }
  185. transform: translate(-50%, -50%);
  186. pointer-events: none;
  187. min-width: 200px;
  188. word-break: unset;
  189. white-space: unset;
  190. border-radius: 19px !important;
  191. .node-view-title {
  192. min-width: 200px;
  193. max-width: 400px;
  194. padding-bottom: .25rem;
  195. }
  196. &-depart {
  197. box-shadow: none;
  198. }
  199. h6 {
  200. margin: 0;
  201. }
  202. }
  203. .legend-toggle {
  204. ul {
  205. list-style: none;
  206. padding: 0;
  207. margin-top: .5rem;
  208. li {
  209. font-style: italic;
  210. margin-left: 1rem;
  211. }
  212. }
  213. span {
  214. display: inline-block;
  215. height: 0px;
  216. width: 15px;
  217. margin-right: .5rem;
  218. }
  219. .children {
  220. border-top: 3px dashed grey;
  221. }
  222. .siblings {
  223. border-top: 3px solid grey;
  224. }
  225. }
  226. </style>