index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import React from 'react';
  2. import { pick, merge } from '@utils';
  3. import {
  4. GRAPH_MOUSE_EVENTS,
  5. GRAPH_OTHER_EVENTS,
  6. PAGE_EVENTS,
  7. GRAPH_MOUSE_REACT_EVENTS,
  8. GRAPH_OTHER_REACT_EVENTS,
  9. PAGE_REACT_EVENTS,
  10. } from '@common/constants';
  11. class Page extends React.Component {
  12. page;
  13. get pageId() {
  14. return '';
  15. }
  16. config = {};
  17. componentDidMount() {
  18. this.init();
  19. this.bindEvent();
  20. this.forceUpdate();
  21. }
  22. shouldComponentUpdate(props) {
  23. const { data: newData } = props;
  24. const { data: oldData } = this.props;
  25. const { mode: newMode } = props.graph || {};
  26. const { mode: oldMode } = this.props.graph || {};
  27. if (newMode !== oldMode) {
  28. this.page.changeMode(newMode);
  29. }
  30. if (newData !== oldData) {
  31. // Remove the arrow after the connection point of the compensation type
  32. newData.edges.forEach((item) => {
  33. if (item.type === 'Compensation') {
  34. item.style = {
  35. ...item.style,
  36. endArrow: false,
  37. };
  38. }
  39. });
  40. this.page.read(newData);
  41. return true;
  42. }
  43. if (props.className !== this.props.className) return true;
  44. return false;
  45. }
  46. get graph() {
  47. return this.page.getGraph();
  48. }
  49. initPage() { }
  50. readData() {
  51. const { data } = this.config;
  52. if (data) {
  53. this.page.read(data);
  54. }
  55. }
  56. addListener = (target, eventName, handler) => {
  57. if (typeof handler === 'function') target.on(eventName, handler);
  58. };
  59. init() {
  60. merge(this.config, this.props, {
  61. graph: {
  62. container: this.pageId,
  63. },
  64. });
  65. this.initPage();
  66. this.readData();
  67. }
  68. bindEvent() {
  69. const { addListener } = this;
  70. GRAPH_MOUSE_EVENTS.forEach((event) => {
  71. const eventName = GRAPH_MOUSE_REACT_EVENTS[event];
  72. addListener(this.graph, `${event}`, this.props[`on${eventName}`]);
  73. addListener(this.graph, `node:${event}`, this.props[`onNode${eventName}`]);
  74. addListener(this.graph, `edge:${event}`, this.props[`onEdge${eventName}`]);
  75. addListener(this.graph, `group:${event}`, this.props[`onGroup${eventName}`]);
  76. addListener(this.graph, `guide:${event}`, this.props[`onGuide${eventName}`]);
  77. addListener(this.graph, `anchor:${event}`, this.props[`onAnchor${eventName}`]);
  78. });
  79. GRAPH_OTHER_EVENTS.forEach((event) => {
  80. addListener(this.graph, [event], this.props[GRAPH_OTHER_REACT_EVENTS[event]]);
  81. });
  82. PAGE_EVENTS.forEach((event) => {
  83. addListener(this.page, [event], this.props[PAGE_REACT_EVENTS[event]]);
  84. });
  85. }
  86. render() {
  87. const { page, pageId } = this;
  88. const { children } = this.props;
  89. return (
  90. <div id={pageId} {...pick(this.props, ['style', 'className'])}>
  91. {page ? children : null}
  92. </div>
  93. );
  94. }
  95. }
  96. export default Page;