antvG6.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // import { Rect } from '@antv/g'
  2. import {
  3. Badge,
  4. // idOf,
  5. CommonEvent,
  6. BaseBehavior,
  7. // BaseNode
  8. Circle
  9. // treeToGraphData
  10. } from '@antv/g6'
  11. /**
  12. * 自定义树结构展开/收起
  13. */
  14. export class CollapseExpandTree extends BaseBehavior {
  15. constructor (context, options) {
  16. super(context, options)
  17. this.bindEvents()
  18. }
  19. update (options) {
  20. this.unbindEvents()
  21. super.update(options)
  22. this.bindEvents()
  23. }
  24. bindEvents () {
  25. const { graph } = this.context
  26. graph.on('collapse-expand', this.onCollapseExpand)
  27. }
  28. unbindEvents () {
  29. const { graph } = this.context
  30. graph.off('collapse-expand', this.onCollapseExpand)
  31. }
  32. status = 'idle';
  33. onCollapseExpand = async (event) => {
  34. this.status = 'busy'
  35. const { id, collapsed } = event
  36. const { graph } = this.context
  37. await graph.frontElement(id)
  38. if (collapsed) await graph.collapseElement(id)
  39. else await graph.expandElement(id)
  40. this.status = 'idle'
  41. }
  42. }
  43. export class MindMapNode extends Circle {
  44. static defaultStyleProps = {
  45. showBtn: true
  46. };
  47. constructor (options) {
  48. Object.assign(options.style, MindMapNode.defaultStyleProps)
  49. super(options)
  50. }
  51. // 获取子数据
  52. get childrenData () {
  53. // 调用上下文中的model对象的getChildrenData方法,传入当前对象的id,返回子数据
  54. return this.context.model.getChildrenData(this.id)
  55. }
  56. status = true
  57. drawCollapseShape (attributes, container) {
  58. // const iconStyle = this.getCollapseStyle(attributes)
  59. const nodes = this.context.graph.getNodeData(this.id)
  60. if (!nodes.children && !nodes.hasChildren) {
  61. return
  62. }
  63. const btn = this.upsert('collapse-expand', Badge, {
  64. backgroundFill: '#409EFF',
  65. backgroundHeight: 26,
  66. backgroundWidth: 26,
  67. lineHeight: 50,
  68. cursor: 'pointer',
  69. fill: '#fff',
  70. fontSize: 24,
  71. text: attributes.collapsed || nodes.hasChildren ? '+' : '-',
  72. textAlign: 'center',
  73. visibility: 'visible',
  74. x: 40,
  75. y: 0
  76. }, container)
  77. this.forwardEvent(btn, CommonEvent.CLICK, async (event) => {
  78. event.stopPropagation()
  79. if (!this.status) {
  80. return
  81. }
  82. const parent = this.context.graph.getNodeData(this.id)
  83. if (parent.hasChildren) {
  84. await this.addChildrenData(parent)
  85. return
  86. }
  87. this.status = false
  88. this.context.graph.emit('collapse-expand', {
  89. id: this.id,
  90. collapsed: !attributes.collapsed
  91. })
  92. setTimeout(() => {
  93. this.status = true
  94. }, 500)
  95. })
  96. }
  97. // 绘制标签形状
  98. drawTapShape (attributes, container) {
  99. const nodes = this.context.graph.getNodeData(this.id)
  100. const btn = this.upsert('status', Badge, {
  101. text: `${nodes.tag} [标注]`,
  102. fontSize: 14,
  103. textAlign: 'left',
  104. transform: [['translate', 90, 30]],
  105. padding: [10],
  106. fill: '#409EFF',
  107. zIndex: 5
  108. }, container)
  109. this.forwardEvent(btn, CommonEvent.CLICK, async (event) => {
  110. event.stopPropagation()
  111. nodes.editTag && nodes.editTag(nodes)
  112. })
  113. }
  114. async addChildrenData (parent) {
  115. this.status = false
  116. const { graph } = this.context
  117. // 展开关闭
  118. const data = await parent.getChildren(this.id)
  119. if (!data) {
  120. return
  121. }
  122. graph.addNodeData(data.nodes)
  123. graph.addEdgeData(data.edges)
  124. graph.updateNodeData([{
  125. id: this.id,
  126. hasChildren: false,
  127. children: [...data.nodes.map(e => e.id)]
  128. }])
  129. await graph.render()
  130. this.status = true
  131. }
  132. forwardEvent (target, type, listener) {
  133. if (target && !Reflect.has(target, '__bind__')) {
  134. Reflect.set(target, '__bind__', true)
  135. target.addEventListener(type, listener)
  136. }
  137. }
  138. render (attributes = this.parsedAttributes, container = this) {
  139. super.render(attributes, container)
  140. this.drawCollapseShape(attributes, container)
  141. this.drawTapShape(attributes, container)
  142. }
  143. }