Polygon.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React, { Component, PureComponent } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { createPolygon, updatePolygon } from './api';
  4. const __com__ = 'Polygon';
  5. //const debug = console.log;
  6. const debug = () => {};
  7. export class Polygon extends Component {
  8. static propTypes = {
  9. __map__: PropTypes.object,
  10. options: PropTypes.object,
  11. events: PropTypes.object
  12. };
  13. constructor() {
  14. super();
  15. this.refElement = null;
  16. this._entity = null;
  17. debug(__com__, 'constructor', this._entity);
  18. }
  19. componentDidMount() {
  20. debug(__com__, 'componentDidMount', this._entity);
  21. let { __map__, options, events, children } = this.props;
  22. //let opts = { ...(options || {}), map: __map__, content: children };
  23. let opts = { ...(options || {}), map: __map__ };
  24. this._entity = createPolygon(opts, events);
  25. if (this._entity) {
  26. if (this.props.refer) this.props.refer(this._entity);
  27. }
  28. }
  29. componentDidUpdate(prevProps) {
  30. debug(__com__, 'componentDidUpdate', this._entity);
  31. let { __map__, options, events, children } = this.props;
  32. //let opts = { ...(options || {}), map: __map__, content: children };
  33. let opts = { ...(options || {}), map: __map__ };
  34. if (!this._entity) {
  35. this._entity = createPolygon(opts, events);
  36. if (this._entity) {
  37. if (this.props.refer) this.props.refer(this._entity);
  38. }
  39. return;
  40. }
  41. // need check props changes, then update.
  42. let oldOpts = {
  43. ...(prevProps.options || {}),
  44. map: prevProps.__map__
  45. };
  46. updatePolygon(this._entity, opts, events, oldOpts, prevProps.events);
  47. }
  48. componentWillUnmount() {
  49. debug(__com__, 'componentWillUnmount', this._entity);
  50. if (this._entity) {
  51. this._entity.setMap(null);
  52. // delete this._entity;
  53. this._entity = null;
  54. if (this.props.refer) this.props.refer(this._entity);
  55. }
  56. }
  57. // shouldComponentUpdate(nextProps, nextState) {
  58. // debug(__com__, 'shouldComponentUpdate', this._entity);
  59. // return false;
  60. // }
  61. render() {
  62. debug(__com__, 'render', this._entity);
  63. return null;
  64. }
  65. }
  66. export default Polygon;