mindmap.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. import { Graph, Cell, Node, Path } from '@antv/x6'
  2. import { getTextSize, getTextWidth } from "./measure.js"
  3. import Hierarchy from '@antv/hierarchy'
  4. export default {
  5. data() {
  6. return {
  7. mindMapDataCurrent:{},
  8. positionCurrent:{},
  9. addTypeCurrent:'',
  10. styleConfig:{},
  11. mindmapAssistData:{
  12. mindmapDataUse:[],
  13. deletedMindmapData:[],// 用于撤销删除,恢复删除掉的数据
  14. mindmapDataRecoverUse:[],//用于 重做时 恢复思维导图数据
  15. },
  16. childNodeSizes:[],//数据节点内部内容宽度数组
  17. linkLists:[], //link列表
  18. childNodeCells:[],//存放所有child子数据节点
  19. }
  20. },
  21. mounted() {
  22. },
  23. created() {
  24. this.styleConfig=this.$store.state.sand.styleConfig
  25. // 中心主题
  26. Graph.registerNode(
  27. 'mindmap-topic',
  28. {
  29. inherit: 'rect',
  30. markup: [
  31. {
  32. tagName: 'rect',
  33. selector: 'body',
  34. },
  35. {
  36. tagName: 'image',
  37. selector: 'leftImg',
  38. },
  39. {
  40. tagName: 'image',
  41. selector: 'rightImg',
  42. },
  43. {
  44. tagName: 'text',
  45. selector: 'text',
  46. },
  47. ],
  48. attrs: {
  49. body: {
  50. rx: 6,
  51. ry: 6,
  52. strokeWidth: 1,
  53. width:100,
  54. height:50
  55. },
  56. leftImg: {
  57. ref: 'body',
  58. refX: -16,
  59. refY: '50%',
  60. refY2: -8,
  61. width: 16,
  62. height: 16,
  63. 'xlink:href':require('@/assets/img/icons/add_blue_new.png'),
  64. event: 'add:topic:left',
  65. class: 'left-topic-image',
  66. },
  67. rightImg: {
  68. ref: 'body',
  69. refX: '100%',
  70. refY: '50%',
  71. refY2: -8,
  72. width: 16,
  73. height: 16,
  74. 'xlink:href':require('@/assets/img/icons/add_blue_new.png'),
  75. event: 'add:topic:right',
  76. class: 'right-topic-image',
  77. },
  78. text: {
  79. fontSize: 14,
  80. fill: this.$store.state.sand.styleConfig.color,
  81. textWrap:{
  82. width:-10
  83. }
  84. }
  85. },
  86. },
  87. true,
  88. )
  89. // 左分支主题
  90. Graph.registerNode(
  91. 'mindmap-topic-left',
  92. {
  93. inherit: 'rect',
  94. markup: [
  95. {
  96. tagName: 'rect',
  97. selector: 'body',
  98. },
  99. {
  100. tagName: 'image',
  101. selector: 'leftImg',
  102. },
  103. {
  104. tagName: 'image',
  105. selector: 'rightImg',
  106. },
  107. {
  108. tagName: 'text',
  109. selector: 'text',
  110. },
  111. ],
  112. attrs: {
  113. body: {
  114. rx: 6,
  115. ry: 6,
  116. strokeWidth: 1,
  117. width:100,
  118. height:50
  119. },
  120. leftImg: {
  121. ref: 'body',
  122. refX: -16,
  123. refY: '50%',
  124. refY2: -8,
  125. width: 16,
  126. height: 16,
  127. 'xlink:href':require('@/assets/img/icons/add_blue_new.png'),
  128. event: 'add:topic:left',
  129. class: 'left-topic-image',
  130. },
  131. text: {
  132. fontSize: 14,
  133. fill: this.$store.state.sand.styleConfig.color,
  134. textWrap:{
  135. width:-10
  136. }
  137. }
  138. },
  139. },
  140. true,
  141. )
  142. // 右分支主题
  143. Graph.registerNode(
  144. 'mindmap-topic-right',
  145. {
  146. inherit: 'rect',
  147. markup: [
  148. {
  149. tagName: 'rect',
  150. selector: 'body',
  151. },
  152. {
  153. tagName: 'image',
  154. selector: 'leftImg',
  155. },
  156. {
  157. tagName: 'image',
  158. selector: 'rightImg',
  159. },
  160. {
  161. tagName: 'text',
  162. selector: 'text',
  163. },
  164. ],
  165. attrs: {
  166. body: {
  167. rx: 6,
  168. ry: 6,
  169. strokeWidth: 1,
  170. width:100,
  171. height:50
  172. },
  173. rightImg: {
  174. ref: 'body',
  175. refX: '100%',
  176. refY: '50%',
  177. refY2: -8,
  178. width: 16,
  179. height: 16,
  180. 'xlink:href':require('@/assets/img/icons/add_blue_new.png'),
  181. event: 'add:topic:right',
  182. class: 'right-topic-image',
  183. },
  184. text: {
  185. fontSize: 14,
  186. fill:this.$store.state.sand.styleConfig.color,
  187. textWrap:{
  188. width:-10
  189. }
  190. }
  191. },
  192. },
  193. true,
  194. )
  195. //数据节点标题
  196. Graph.registerNode(
  197. 'mindmap-child-datanode-title',
  198. {
  199. inherit: 'rect',
  200. markup: [
  201. {
  202. tagName: 'rect',
  203. selector: 'body',
  204. },
  205. {
  206. tagName: 'text',
  207. selector: 'text',
  208. },
  209. ],
  210. attrs: {
  211. body: {
  212. rx: 4,
  213. ry: 4,
  214. width:132,
  215. height:28,
  216. fill:'#0052D9',
  217. strokeWidth: 1,
  218. class: 'mindmap-child-datanode-title',
  219. },
  220. text: {
  221. fontSize: 14,
  222. fill: '#fff',
  223. textWrap:{
  224. width:-10
  225. },
  226. class: 'mindmap-child-datanode-title-text',
  227. }
  228. },
  229. }
  230. )
  231. //数据子框节点
  232. Graph.registerNode(
  233. 'mindmap-child-background-datanode',
  234. {
  235. inherit: 'rect',
  236. markup: [
  237. {
  238. tagName: 'rect',
  239. selector: 'body',
  240. },
  241. {
  242. tagName: 'text',
  243. selector: 'text',
  244. },
  245. ],
  246. attrs: {
  247. body: {
  248. strokeWidth: 1,
  249. width:110,
  250. height:66
  251. },
  252. },
  253. },
  254. true,
  255. );
  256. //数据子节点
  257. Graph.registerNode(
  258. 'mindmap-child-datanode',
  259. {
  260. inherit: 'rect',
  261. markup: [
  262. {
  263. tagName: 'rect',
  264. selector: 'body',
  265. },
  266. {
  267. tagName: 'text',
  268. selector: 'text',
  269. },
  270. ],
  271. attrs: {
  272. body: {
  273. rx: 6,
  274. ry: 6,
  275. strokeWidth: 1,
  276. width:110,
  277. height:66
  278. },
  279. text: {
  280. fontSize: 14,
  281. fill:this.$store.state.sand.styleConfig.color,
  282. textWrap:{
  283. width:-10
  284. }
  285. }
  286. },
  287. },
  288. true,
  289. );
  290. //数据子内部节点
  291. Graph.registerNode(
  292. 'mindmap-grandchild-datanode',
  293. {
  294. inherit: 'rect',
  295. markup: [
  296. {
  297. tagName: 'rect',
  298. selector: 'body',
  299. },
  300. {
  301. tagName: "text",
  302. selector: "text",
  303. },
  304. {
  305. tagName: "text",
  306. selector: "value",
  307. },
  308. ],
  309. attrs: {
  310. body: {
  311. width:110,
  312. height:66,
  313. },
  314. text: {
  315. fontSize: 14,
  316. },
  317. value:{
  318. fontSize: 14,
  319. },
  320. },
  321. },
  322. true,
  323. );
  324. // 连接器
  325. Graph.registerConnector(
  326. 'mindmap',
  327. (sourcePoint, targetPoint, routerPoints, options) => {
  328. const midX = sourcePoint.x + 10
  329. // const midY = sourcePoint.y
  330. const ctrX = (targetPoint.x - midX) / 5 + midX
  331. const ctrY = targetPoint.y
  332. const pathData = `
  333. M ${sourcePoint.x} ${sourcePoint.y}
  334. Q ${ctrX} ${ctrY} ${targetPoint.x} ${targetPoint.y}
  335. `
  336. return options.raw ? Path.parse(pathData) : pathData
  337. },
  338. true,
  339. )
  340. // 边
  341. Graph.registerEdge(
  342. 'mindmap-edge',
  343. {
  344. inherit: 'edge',
  345. connector: {
  346. name: 'mindmap',
  347. },
  348. attrs: {
  349. line: {
  350. targetMarker: false,
  351. strokeWidth: 2,
  352. },
  353. },
  354. zIndex: 0,
  355. },
  356. true,
  357. )
  358. },
  359. methods: {
  360. setGraph(){
  361. // 添加左边节点
  362. this.graph.on('add:topic:left', ({ node }) => {
  363. if(this.operationType=='view') return
  364. const { id } = node
  365. this.setCurrent(id)
  366. const type = node.prop('type')
  367. if (this.addChildNode(id, type,'left')) {
  368. this.mindMapRender()
  369. }
  370. })
  371. // 添加右边节点
  372. this.graph.on('add:topic:right', ({ node }) => {
  373. if(this.operationType=='view') return
  374. const { id } = node
  375. this.setCurrent(id)
  376. const type = node.prop('type')
  377. if (this.addChildNode(id, type,'right')) {
  378. this.mindMapRender()
  379. }
  380. })
  381. // 改变大小 - 更新数据源
  382. this.graph.on('node:change:size', (args) => {
  383. if(this.operationType=='view') return
  384. if(args.node.shape.indexOf('mindmap')!==-1){
  385. let ids = args.node.id.split('-')
  386. let mindmapDataIndex = this.mindmapAssistData.mindmapDataUse.findIndex(mindmap => mindmap.mindmapData.id == ids[0])
  387. let mindMapDataCurrent = this.mindmapAssistData.mindmapDataUse[mindmapDataIndex]?this.mindmapAssistData.mindmapDataUse[mindmapDataIndex].mindmapData:{}
  388. let findId = ids[0]
  389. for (let i = 1; i < ids.length; i++) {
  390. const element = ids[i];
  391. findId = findId+'-'+element
  392. mindMapDataCurrent=mindMapDataCurrent.children.find(it => it.id==findId)
  393. }
  394. mindMapDataCurrent.width = args.node.size().width
  395. mindMapDataCurrent.height = args.node.size().height
  396. }
  397. })
  398. // 改变位置 - 更新数据源
  399. this.graph.on('node:change:position', (args) => {
  400. if(this.operationType=='view') return
  401. if(args.node.shape.indexOf('mindmap')!==-1 && Number(args.node.id)){
  402. let index = this.mindmapAssistData.mindmapDataUse.findIndex(it =>it.mindmapData.id == args.node.id)
  403. if(index!=-1){
  404. this.mindmapAssistData.mindmapDataUse[index].position = args.current
  405. }
  406. }
  407. })
  408. // tab 键添加节点-右边的 - 更新数据源
  409. this.graph.bindKey('tab', (e) => {
  410. if(this.operationType=='view') return
  411. e.preventDefault()
  412. const selectedNodes = this.graph.getSelectedCells().filter((item) => {
  413. return item.shape.indexOf('mindmap')!=-1 && item.isNode()
  414. })
  415. if (selectedNodes.length) {
  416. const node = selectedNodes[0]
  417. this.setCurrent(node.id)
  418. let type = node.prop('type')
  419. let direction = node.shape.indexOf('left')!=-1?'left':'right'
  420. if (this.addChildNode(node.id, type,direction)) {
  421. this.mindMapRender()
  422. }
  423. }
  424. })
  425. // 撤销 - 入栈
  426. this.graph.history.on('undo', (args) => {
  427. let mindmapNodes=args.cmds.filter(it => it.data.props && it.data.props.shape.indexOf('mindmap')!=-1 && it.data.node)
  428. if(!(mindmapNodes && mindmapNodes.length>0)) return
  429. let mindmapUndoType=mindmapNodes[0].event
  430. if(mindmapUndoType=="cell:added"){
  431. this.mindmapDeleteRecordPush(mindmapNodes)
  432. }else if(mindmapUndoType=="cell:removed"){
  433. this.mindmapAddRecordPush()
  434. }
  435. })
  436. // 重做,出栈
  437. this.graph.history.on('redo', (args) => {
  438. let mindmapNodes=args.cmds.filter(it => it.data.props && it.data.props.shape.indexOf('mindmap')!=-1 && it.data.node)
  439. if(!(mindmapNodes && mindmapNodes.length>0)) return
  440. this.mindmapRecover()
  441. })
  442. },
  443. // 生成思维导图初始化数据
  444. generateMindmapData(position,addType){
  445. let beId=this.mindmapAssistData.mindmapDataUse.length>0?
  446. parseInt(this.mindmapAssistData.mindmapDataUse[this.mindmapAssistData.mindmapDataUse.length-1].mindmapData.id)+1+'':'1'
  447. let mindmapData={
  448. id: beId,
  449. type: 'topic',
  450. label: this.$t('SandboxManage.SandFlow.center_theme')||'中心主题',
  451. width: 160,
  452. height: 50,
  453. direction:'double',
  454. children: [
  455. {
  456. id: beId+'-1',
  457. type: 'topic-branch',
  458. label: this.$t('SandboxManage.SandFlow.branch_topic')+'1',
  459. width: 100,
  460. height: 40,
  461. direction:'left',
  462. children: [
  463. {
  464. id: beId+'-1-1',
  465. type: 'topic-child',
  466. label: this.$t('SandboxManage.SandFlow.sub_topic')+'1',
  467. width: 60,
  468. height: 30,
  469. direction:'left',
  470. },
  471. {
  472. id: beId+'-1-2',
  473. type: 'topic-child',
  474. label: this.$t('SandboxManage.SandFlow.sub_topic')+'2',
  475. width: 60,
  476. height: 30,
  477. direction:'left',
  478. },
  479. ],
  480. },
  481. {
  482. id: beId+'-2',
  483. type: 'topic-branch',
  484. label: this.$t('SandboxManage.SandFlow.branch_topic')+'2',
  485. width: 100,
  486. height: 40,
  487. direction:'right',
  488. },
  489. ],
  490. }
  491. this.mindmapAssistData.mindmapDataUse.push({mindmapData,position,addType})
  492. },
  493. // 通过id设置当前操作的思维导图
  494. setCurrent(id){
  495. let rootId = id.split('-')[0]
  496. let index = this.mindmapAssistData.mindmapDataUse.findIndex(it =>it.mindmapData.id == rootId)
  497. this.mindMapDataCurrent = this.mindmapAssistData.mindmapDataUse[index]?this.mindmapAssistData.mindmapDataUse[index].mindmapData:{}
  498. this.positionCurrent = this.mindmapAssistData.mindmapDataUse[index]?this.mindmapAssistData.mindmapDataUse[index].position:{x:0,y:0}
  499. this.addTypeCurrent = this.mindmapAssistData.mindmapDataUse[index]?this.mindmapAssistData.mindmapDataUse[index].addType:'singleMindmap'
  500. },
  501. mindMapRender(i){
  502. this.graph.startBatch('renderMindmap')
  503. let mindMapType = i || i==0?this.mindmapAssistData.mindmapDataUse[i].addType:this.addTypeCurrent
  504. this.mindMapDataCurrent = i || i==0?this.mindmapAssistData.mindmapDataUse[i].mindmapData:this.mindMapDataCurrent
  505. this.positionCurrent = i || i==0?this.mindmapAssistData.mindmapDataUse[i].position:this.positionCurrent
  506. const result = Hierarchy.mindmap(this.mindMapDataCurrent, {
  507. direction: 'H',
  508. getHeight(d) {
  509. return d.height
  510. },
  511. getWidth(d) {
  512. return d.width
  513. },
  514. getHGap() {
  515. return 40
  516. },
  517. getVGap() {
  518. return 20
  519. },
  520. getSide: (d) => {
  521. return mindMapType.indexOf('double') != -1?d.data.direction || 'left':'right'
  522. }
  523. })
  524. const cells = []
  525. let xGap = this.positionCurrent?this.positionCurrent.x-result.x:0
  526. let yGap = this.positionCurrent?this.positionCurrent.y-result.y:0
  527. const traverse = (hierarchyItem) => {
  528. if (hierarchyItem) {
  529. const { data, children } = hierarchyItem
  530. let mindmapDirection = mindMapType.indexOf('double') != -1?data.direction:'right'
  531. let currentCell=this.graph.getCellById(data.id)
  532. if(!currentCell){
  533. // 没有 新增
  534. cells.push(
  535. this.graph.createNode({
  536. id: data.id,
  537. shape:mindmapDirection=='right'?'mindmap-topic-right':mindmapDirection=='left'?'mindmap-topic-left':'mindmap-topic',
  538. x: xGap+hierarchyItem.x,
  539. y: yGap+hierarchyItem.y,
  540. width: data.width,
  541. height: data.height,
  542. label: data.label,
  543. type: data.type,
  544. attrs:{
  545. body: {
  546. stroke: this.$store.state.sand.styleConfig.borderColor,
  547. fill: this.$store.state.sand.styleConfig.backgroundColor
  548. },
  549. text:{
  550. fill:this.$store.state.sand.styleConfig.color
  551. }
  552. },
  553. }),
  554. )
  555. }else{
  556. // 有,更新下位置信息
  557. currentCell.position(xGap+hierarchyItem.x,yGap+hierarchyItem.y);
  558. if(currentCell.shape == 'mindmap-child-background-datanode' && currentCell.getChildren().length > 0){
  559. let titleNodeSize = this.getTitleNodeSize(currentCell.data.Name,currentCell.size().width);
  560. let childDataNode = currentCell.getChildren()[0];
  561. childDataNode.position(xGap+hierarchyItem.x,yGap+hierarchyItem.y + titleNodeSize.height)
  562. let childrens = childDataNode.getChildren() || [];
  563. let cs = _.cloneDeep(childrens)
  564. cs.sort((a, b) => {
  565. if (a.shape === 'mindmap-child-datanode-title') return 1;
  566. if (b.shape === 'mindmap-child-datanode-title') return -1;
  567. return 0;
  568. });
  569. cs.map((v,i)=>{
  570. if(v.shape == 'mindmap-grandchild-datanode'){
  571. let m = i % 3;
  572. let w = m == 0 ? 0 : (m == 1 ? cs[0].size().width : cs[0].size().width + cs[1].size().width);
  573. v.position(xGap+hierarchyItem.x + (m + 1) * 10 + w , yGap+hierarchyItem.y + Math.floor(i / 3) * 66 + titleNodeSize.height);
  574. } else {
  575. v.position(xGap+hierarchyItem.x , yGap+hierarchyItem.y);
  576. }
  577. })
  578. }
  579. }
  580. if (children) {
  581. children.forEach((item) => {
  582. const { id, data } = item
  583. let mindmapChildDirection = mindMapType.indexOf('double') != -1?data.direction:'right'
  584. let currentEdge=this.graph.getCellById(data.id)
  585. if(!currentEdge){
  586. cells.push(
  587. this.graph.createEdge({
  588. shape: 'mindmap-edge',
  589. attrs:{
  590. line:{
  591. stroke:this.$store.state.sand.styleConfig.lineColor
  592. }
  593. },
  594. source: {
  595. cell: hierarchyItem.id,
  596. anchor: {
  597. name: 'center',
  598. args: {
  599. dx: mindmapChildDirection=='right'?'25%':mindmapChildDirection=='left'?'-25%':0,
  600. },
  601. },
  602. },
  603. target: {
  604. cell: id,
  605. anchor: {
  606. name: mindmapChildDirection =='left'?'right':'left',
  607. },
  608. },
  609. }),
  610. )
  611. }
  612. traverse(item)
  613. })
  614. }
  615. }
  616. }
  617. traverse(result)
  618. // 排下序,把边放最后面 不然 边 会找不到 节点
  619. let sortCells = cells.sort(cell => cell.shape.indexOf('edge'))
  620. this.graph.addCell(sortCells)
  621. this.graph.stopBatch('renderMindmap')
  622. },
  623. findItem(obj,id){
  624. if (obj.id === id) {
  625. return {
  626. parent: null,
  627. node: obj,
  628. }
  629. }
  630. const { children } = obj
  631. if (children) {
  632. for (let i = 0, len = children.length; i < len; i++) {
  633. const res = this.findItem(children[i], id)
  634. if (res) {
  635. return {
  636. parent: res.parent || obj,
  637. node: res.node,
  638. }
  639. }
  640. }
  641. }
  642. return null
  643. },
  644. addChildNode (id, type,direction='left'){
  645. // 重做不了 清空重做栈
  646. this.mindmapAssistData.mindmapDataRecoverUse = []
  647. const res = this.findItem(this.mindMapDataCurrent, id)
  648. const dataItem = res && res.node
  649. if (dataItem) {
  650. let item = null
  651. let addId ='1'
  652. if(dataItem.children && dataItem.children.length>0){
  653. let ids = dataItem.children[dataItem.children.length-1].id.split('-')
  654. addId = parseInt(ids[ids.length-1])+1+''
  655. }
  656. if (type === 'topic') {
  657. item = {
  658. id: `${id}-${addId}`,
  659. type: 'topic-branch',
  660. label: `分支主题${addId}`,
  661. width: 100,
  662. height: 40,
  663. direction
  664. }
  665. } else if (type === 'topic-branch' || type=='topic-child') {
  666. item = {
  667. id: `${id}-${addId}`,
  668. type: 'topic-child',
  669. label: `子主题${addId}`,
  670. width: 60,
  671. height: 30,
  672. direction
  673. }
  674. }
  675. if (item) {
  676. if (dataItem.children) {
  677. dataItem.children.push(item)
  678. } else {
  679. dataItem.children = [item]
  680. }
  681. return item
  682. }
  683. }
  684. return null
  685. },
  686. //添加数据节点
  687. handleGetDataNodeWidth(list,widthIndex){
  688. let arr = [[],[],[]]; //按列分组,获取当前列的内容的最大长度作为该列的最大长度
  689. list.map((item,index)=>{
  690. arr[index % 3].push(item)
  691. });
  692. let widthArr = [];
  693. arr.map((arrs,arrsIndex)=>{
  694. let widths = [];
  695. arrs.map(item=>{
  696. widths.push(getTextWidth(item.label,'Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, SimSun, sans-serif',14) || 0);
  697. widths.push(getTextWidth(item.value,'Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, SimSun, sans-serif',14) || 0);
  698. })
  699. let max = widths.length > 0 ? Math.max.apply(null,widths) : 0;
  700. if(max) widthArr[arrsIndex] = max > 106 ? max : 106;
  701. });
  702. this.childNodeSizes[widthIndex] = widthArr || [];
  703. let sum = widthArr.reduce((accumulator, current) => {
  704. return accumulator + current;
  705. },0);
  706. sum += list.filter(_=>_).length >= 3 ? 40 : (20 + 10*(list.length - 1));
  707. return sum;
  708. },
  709. getDataNodeSize(data,index){
  710. const list = data.calculationMethod;
  711. let height = Math.ceil(list.length / 3) * 66;
  712. let width = this.handleGetDataNodeWidth(list,index);
  713. return {
  714. height:height || 66,
  715. width:width || 50,
  716. }
  717. },
  718. getTitleNodeSize(name,width){
  719. let nameWidth = getTextWidth(name,'Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, SimSun, sans-serif',14) + 10;
  720. let titleWidth = nameWidth < width ? nameWidth : width;
  721. titleWidth = titleWidth < 126 ? 126 : titleWidth;
  722. let h = getTextSize(name,titleWidth,14,'Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, SimSun, sans-serif');
  723. h.height = h.height < 30 ? 30 : h.height;
  724. return h
  725. },
  726. addMindmapDataNodes(id,list,direction){
  727. // 重做不了 清空重做栈
  728. this.mindmapAssistData.mindmapDataRecoverUse = []
  729. const res = this.findItem(this.mindMapDataCurrent, id);
  730. const dataItem = res && res.node;
  731. if(dataItem.children && dataItem.children.length > 0){ //将原数据清空
  732. const types = ['mindmap-child-background','datanode-title','datanode-grandchild','datanode-child']
  733. dataItem.children = dataItem.children.filter(_=>!types.includes(_.type))
  734. }
  735. this.linkLists = [];
  736. this.childNodeSizes = [];
  737. this.childNodeCells = [];
  738. if (dataItem) {
  739. list.map((_,index)=>{
  740. let item = null
  741. let addId ='1'
  742. if(dataItem.children && dataItem.children.length>0){
  743. let ids = dataItem.children[dataItem.children.length-1].id.split('-')
  744. addId = parseInt(ids[ids.length-1])+1+''
  745. };
  746. let h = {height:30,width:200};
  747. let titleH = {height:0,width:0}
  748. if(!_.calculationMethod || !_.calculationMethod.length){
  749. h = getTextSize(_.Name,200,14,'Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, SimSun, sans-serif');
  750. h.height = h.height < 30 ? 30 : h.height;
  751. } else {
  752. h = this.getDataNodeSize(_,index);
  753. titleH = this.getTitleNodeSize(_.Name,h.width);
  754. }
  755. item = {
  756. id: `${id}-${addId}`,
  757. type: !_.calculationMethod || !_.calculationMethod.length ? 'datanode-title' : 'mindmap-child-background',
  758. label: !_.calculationMethod || !_.calculationMethod.length ? _.Name : '',
  759. width: h.width,
  760. height: h.height + titleH.height,
  761. direction:direction,
  762. data:_,
  763. }
  764. this.linkLists.push(item);
  765. if (item) {
  766. if (dataItem.children) {
  767. dataItem.children.push(item)
  768. } else {
  769. dataItem.children = [item]
  770. }
  771. this.mindMapDataRender();
  772. if(this.childNodeCells.length == list.length){ //添加完成
  773. const select_cell = this.graph.getSelectedCells()[0];
  774. select_cell.setChildren(this.childNodeCells)
  775. }
  776. }
  777. })
  778. }
  779. },
  780. mindMapDataRender(i){ //添加数据节点的渲染
  781. this.graph.startBatch('renderMindmap')
  782. let mindMapType = i || i==0?this.mindmapAssistData.mindmapDataUse[i].addType:this.addTypeCurrent
  783. this.mindMapDataCurrent = i || i==0?this.mindmapAssistData.mindmapDataUse[i].mindmapData:this.mindMapDataCurrent
  784. this.positionCurrent = i || i==0?this.mindmapAssistData.mindmapDataUse[i].position:this.positionCurrent
  785. const result = Hierarchy.mindmap(this.mindMapDataCurrent, {
  786. direction: 'H',
  787. getHeight(d) {
  788. return d.height
  789. },
  790. getWidth(d) {
  791. return d.width
  792. },
  793. getHGap() {
  794. return 40
  795. },
  796. getVGap() {
  797. return 20
  798. },
  799. getSide: (d) => {
  800. return mindMapType.indexOf('double') != -1?d.data.direction || 'left':'right'
  801. }
  802. })
  803. const cells = [];
  804. let xGap = this.positionCurrent?this.positionCurrent.x-result.x:0
  805. let yGap = this.positionCurrent?this.positionCurrent.y-result.y:0
  806. const traverse = (hierarchyItem) => {
  807. if (hierarchyItem) {
  808. const { data, children } = hierarchyItem
  809. let mindmapDirection = mindMapType.indexOf('double') != -1?data.direction:'right'
  810. let currentCell=this.graph.getCellById(data.id)
  811. const shapesObject = {
  812. 'datanode-title':'mindmap-child-datanode-title',
  813. 'datanode-child':'mindmap-child-datanode',
  814. 'datanode-grandchild':'mindmap-grandchild-datanode',
  815. 'mindmap-child-background':'mindmap-child-background-datanode'
  816. }
  817. if(!currentCell){
  818. // 没有 新增
  819. const wrapNode = this.graph.createNode({
  820. id: data.id,
  821. shape:shapesObject[data.type],
  822. x: xGap+hierarchyItem.x,
  823. y: yGap+hierarchyItem.y,
  824. width: data.width,
  825. height: data.height,
  826. label: data.label,
  827. type: data.type,
  828. attrs:{
  829. body: {
  830. stroke: data.type == 'datanode-title' ? '#0052D9' : 'transparent',
  831. fill: data.type == 'datanode-title' ? '#0052D9' : 'transparent',
  832. },
  833. text:{
  834. fill:data.type == 'datanode-title' ? '#FFFFFF' : 'transparent',
  835. }
  836. },
  837. data:data.data,
  838. })
  839. cells.push(wrapNode);
  840. if(data.type == 'mindmap-child-background' || data.type == 'datanode-title') this.childNodeCells.push(wrapNode);
  841. let titleNodeSize = this.getTitleNodeSize(data.data.Name,data.width);
  842. let currentNode = null;
  843. if(data.type == 'mindmap-child-background'){
  844. currentNode = this.graph.createNode({
  845. id: `${data.id}+0` ,
  846. shape:'mindmap-child-datanode',
  847. x: xGap+hierarchyItem.x,
  848. y: yGap+hierarchyItem.y + titleNodeSize.height,
  849. width: data.width,
  850. height: data.height - titleNodeSize.height,
  851. label: '',
  852. type:'datanode-child',
  853. attrs:{
  854. body: {
  855. stroke: this.$store.state.sand.styleConfig.borderColor,
  856. fill: this.$store.state.sand.styleConfig.backgroundColor,
  857. 'pointer-events': 'none',
  858. },
  859. text:{
  860. fill:this.$store.state.sand.styleConfig.color,
  861. 'pointer-events': 'none',
  862. }
  863. },
  864. data:data.data,
  865. })
  866. cells.push(currentNode);
  867. wrapNode.setChildren([currentNode])
  868. }
  869. let titleNode = null;
  870. if(data.data && data.data.detailParams && data.data.detailParams.id && data.data && data.data.calculationMethod && data.data.calculationMethod.length > 0){
  871. titleNode = this.graph.createNode({
  872. id: `${data.id}-0`,
  873. shape:'mindmap-child-datanode-title',
  874. x: xGap+hierarchyItem.x,
  875. y: yGap+hierarchyItem.y,
  876. width: titleNodeSize.width,
  877. height: titleNodeSize.height,
  878. label: data.data.Name,
  879. type: 'datanode-title',
  880. attrs:{
  881. body: {
  882. stroke: '#0052D9',
  883. fill: '#0052D9'
  884. },
  885. text:{
  886. fill:'#FFFFFF'
  887. }
  888. },
  889. data:data.data,
  890. });
  891. cells.push(titleNode);
  892. }
  893. // 处理内部数据节点
  894. if(data.data && data.data.calculationMethod && data.data.calculationMethod.length > 0){
  895. let cNodes = data.data.calculationMethod.map((v,i)=>{
  896. let linkIndex = this.linkLists.findIndex(x=>x.id == data.id);
  897. let widthArr = this.childNodeSizes[linkIndex] ? this.childNodeSizes[linkIndex] : [];
  898. let m = i % 3;
  899. let w = m == 0 ? 0 : (m == 1 ? widthArr[0] : widthArr[0] + widthArr[1])
  900. let innerNode = this.graph.createNode({
  901. id: `${data.id}-${i + 1}`,
  902. shape:'mindmap-grandchild-datanode',
  903. x: xGap+hierarchyItem.x + (m + 1) * 10 + w,
  904. y: yGap+hierarchyItem.y + titleNodeSize.height + Math.floor(i / 3) * 66,
  905. width: widthArr[m],
  906. height: 66,
  907. type: 'datanode-grandchild',
  908. attrs:{
  909. body:{
  910. stroke: '',
  911. strokeWidth: 0,
  912. fill: 'transparent',
  913. 'pointer-events': 'none',
  914. },
  915. text:{
  916. fill:this.$store.state.sand.styleConfig.color,
  917. text:v.label,
  918. refWidth:1,
  919. refY: 10,
  920. textVerticalAnchor:'top',
  921. 'pointer-events': 'none',
  922. },
  923. value:{
  924. fill:this.$store.state.sand.styleConfig.color,
  925. text:v.value,
  926. refWidth:1,
  927. refX:"50%",
  928. refY: '100%',
  929. refY2:-10,
  930. textVerticalAnchor:'bottom',
  931. 'text-anchor':'middle',
  932. 'pointer-events': 'none',
  933. },
  934. },
  935. data:v,
  936. });
  937. cells.push(innerNode);
  938. return innerNode
  939. })
  940. currentNode.setChildren(titleNode ? [...cNodes,titleNode] : cNodes)
  941. }
  942. }else{
  943. // 有,更新下位置信息
  944. currentCell.position(xGap+hierarchyItem.x,yGap+hierarchyItem.y);
  945. if(currentCell.shape == 'mindmap-child-background-datanode' && currentCell.getChildren().length > 0){
  946. let titleNodeSize = this.getTitleNodeSize(currentCell.data.Name,currentCell.size().width);
  947. let childDataNode = currentCell.getChildren()[0];
  948. childDataNode.position(xGap+hierarchyItem.x,yGap+hierarchyItem.y + titleNodeSize.height)
  949. let childrens = childDataNode.getChildren() || [];
  950. let cs = _.cloneDeep(childrens)
  951. cs.sort((a, b) => {
  952. if (a.shape === 'mindmap-child-datanode-title') return 1;
  953. if (b.shape === 'mindmap-child-datanode-title') return -1;
  954. return 0;
  955. });
  956. cs.map((v,i)=>{
  957. if(v.shape == 'mindmap-grandchild-datanode'){
  958. let m = i % 3;
  959. let w = m == 0 ? 0 : (m == 1 ? cs[0].size().width : cs[0].size().width + cs[1].size().width);
  960. v.position(xGap+hierarchyItem.x + (m + 1) * 10 + w , yGap+hierarchyItem.y + Math.floor(i / 3) * 66 + titleNodeSize.height);
  961. } else {
  962. v.position(xGap+hierarchyItem.x , yGap+hierarchyItem.y);
  963. }
  964. })
  965. }
  966. }
  967. if (children) {
  968. children.forEach((item) => {
  969. const { id, data } = item
  970. let mindmapChildDirection = mindMapType.indexOf('double') != -1?data.direction:'right'
  971. let currentEdge=this.graph.getCellById(data.id)
  972. if(!currentEdge){
  973. cells.push(
  974. this.graph.createEdge({
  975. shape: 'mindmap-edge',
  976. attrs:{
  977. line:{
  978. stroke:this.$store.state.sand.styleConfig.lineColor
  979. }
  980. },
  981. source: {
  982. cell: hierarchyItem.id,
  983. anchor: {
  984. name: 'center',
  985. args: {
  986. dx: mindmapChildDirection=='right'?'25%':mindmapChildDirection=='left'?'-25%':0,
  987. },
  988. },
  989. },
  990. target: {
  991. cell: id,
  992. anchor: {
  993. name: mindmapChildDirection =='left'?'right':'left',
  994. },
  995. },
  996. }),
  997. )
  998. }
  999. traverse(item)
  1000. })
  1001. }
  1002. }
  1003. }
  1004. traverse(result)
  1005. // 排下序,把边放最后面 不然 边 会找不到 节点
  1006. let sortCells = cells.sort(cell => cell.shape.indexOf('edge'))
  1007. let cells_result = sortCells.sort(cell => cell.shape.indexOf('mindmap-grandchild-datanode'));
  1008. this.graph.addCell(cells_result)
  1009. this.graph.stopBatch('renderMindmap')
  1010. },
  1011. getMindmapDataUse(){
  1012. return this.mindmapAssistData
  1013. },
  1014. mindmapDeleteRecordPush(mindmapNodes){
  1015. let shouldOperations=[]
  1016. this.mindmapAssistData.mindmapDataUse.map((item,index)=>{
  1017. let levelIds = mindmapNodes.filter(mindMap => mindMap.data.id.startsWith(item.mindmapData.id)).map(mindMap => mindMap.data.id)
  1018. if(!(levelIds && levelIds.length>0)) return
  1019. let mindMapIds=[...levelIds]
  1020. for (let i = 0; i < levelIds.length; i++) {
  1021. const element = levelIds[i]
  1022. mindMapIds=mindMapIds.filter( id => id.indexOf(element) !=0 || id==element)
  1023. }
  1024. shouldOperations.push(mindMapIds)
  1025. })
  1026. // 入栈
  1027. if(!this.canRedo){
  1028. this.mindmapAssistData.mindmapDataRecoverUse=[JSON.stringify(this.mindmapAssistData.mindmapDataUse)]
  1029. }else{
  1030. this.mindmapAssistData.mindmapDataRecoverUse.push(JSON.stringify(this.mindmapAssistData.mindmapDataUse))
  1031. }
  1032. shouldOperations.map(it =>{
  1033. it.map(it1 =>{
  1034. this.deleteMindmapData(it1,this.mindmapAssistData.mindmapDataUse)
  1035. })
  1036. })
  1037. },
  1038. mindmapAddRecordPush(){
  1039. // 入栈
  1040. if(!this.canRedo){
  1041. this.mindmapAssistData.mindmapDataRecoverUse=[JSON.stringify(this.mindmapAssistData.mindmapDataUse)]
  1042. }else{
  1043. this.mindmapAssistData.mindmapDataRecoverUse.push(JSON.stringify(this.mindmapAssistData.mindmapDataUse))
  1044. }
  1045. let recoverDeletedData=this.mindmapAssistData.deletedMindmapData.pop()
  1046. if(recoverDeletedData && recoverDeletedData.length>0){
  1047. // 添加
  1048. recoverDeletedData.map(it =>{
  1049. it.map(it1 =>{
  1050. this.addMindmapData(it1,this.mindmapAssistData.mindmapDataUse)
  1051. })
  1052. })
  1053. }
  1054. },
  1055. deleteMindmapData(id,data){
  1056. let ids = id.split('-')
  1057. let mindmapDataIndex = data.findIndex(mindmap => mindmap.mindmapData.id == ids[0])
  1058. if(ids.length==1){
  1059. data.splice(mindmapDataIndex,1)
  1060. return
  1061. }
  1062. let mindmapData = data[mindmapDataIndex].mindmapData
  1063. let findId = ids[0]
  1064. for (let i = 1; i < ids.length-1; i++) {
  1065. const element = ids[i];
  1066. findId = findId+'-'+element
  1067. mindmapData=mindmapData.children.find(it => it.id==findId)
  1068. }
  1069. let endId = ids[ids.length-1]
  1070. let endIndex = mindmapData.children.findIndex(it => it.id == findId+'-'+endId)
  1071. mindmapData.children.splice(endIndex,1)
  1072. },
  1073. addMindmapData(item,data){
  1074. let isRoot = item.mindmapData?true:false
  1075. let id = isRoot?item.mindmapData.id:item.id
  1076. if(isRoot){
  1077. // 恢复根节点
  1078. let mindmapDataIndexRoot=data.length
  1079. for (let i = 0; i < data.length-1; i++) {
  1080. const element = data[i];
  1081. if((+id) < (+element.mindmapData.id)){
  1082. mindmapDataIndexRoot=0
  1083. break
  1084. }else if((+id) > (+element.mindmapData.id) && (+id) < (+data[i+1].mindmapData.id)){
  1085. mindmapDataIndexRoot=i+1
  1086. break
  1087. }
  1088. }
  1089. data.splice(mindmapDataIndexRoot,0,item)
  1090. return
  1091. }
  1092. let ids = id.split('-')
  1093. let mindmapDataIndex = data.findIndex(mindmap => {
  1094. return mindmap.mindmapData.id == ids[0]
  1095. })
  1096. let mindmapData = data[mindmapDataIndex].mindmapData
  1097. let findId = ids[0]
  1098. for (let i = 1; i < ids.length-1; i++) {
  1099. const element = ids[i];
  1100. findId = findId+'-'+element
  1101. mindmapData=mindmapData.children.find(it => it.id==findId)
  1102. }
  1103. let endId = ids[ids.length-1]
  1104. let endIndex = mindmapData.children.length
  1105. if(mindmapData.children.length==1){
  1106. // 只有一个
  1107. let prevIdx = mindmapData.children[0].id.substring(mindmapData.children[0].id.lastIndexOf('-')+1)
  1108. if((+endId) < (+prevIdx)){
  1109. endIndex=0
  1110. }else{
  1111. endIndex=1
  1112. }
  1113. }else{
  1114. for (let i = 0; i < mindmapData.children.length-1; i++) {
  1115. // 两个及以上
  1116. const element = mindmapData.children[i];
  1117. let prevIdx = element.id.substring(element.id.lastIndexOf('-')+1)
  1118. let nextIdx = mindmapData.children[i+1].id.substring(mindmapData.children[i+1].id.lastIndexOf('-')+1)
  1119. // 找到对应位置,前提 id是按顺序的
  1120. if((+endId) < (+prevIdx)){
  1121. endIndex=0
  1122. break
  1123. }else if((+endId) > (+prevIdx) && (+endId) < (+nextIdx)){
  1124. endIndex=i+1
  1125. break
  1126. }
  1127. }
  1128. }
  1129. mindmapData.children.splice(endIndex,0,item)
  1130. },
  1131. mindmapRecover(){
  1132. // 重做添加时 恢复数据指针前进
  1133. let recoverData = this.mindmapAssistData.mindmapDataRecoverUse.pop()
  1134. if(recoverData) this.mindmapAssistData.mindmapDataUse=JSON.parse(recoverData)
  1135. },
  1136. },
  1137. }