InfoWindow.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  6. import React, { Component, PureComponent } from 'react';
  7. import PropTypes from 'prop-types';
  8. import { createInfoWindow, updateInfoWindow } from './api';
  9. var __com__ = 'InfoWindow';
  10. //const debug = console.log;
  11. var debug = function debug() {};
  12. // https://lbs.amap.com/api/javascript-api/reference/infowindow
  13. export var InfoWindow = function (_Component) {
  14. _inherits(InfoWindow, _Component);
  15. function InfoWindow() {
  16. _classCallCheck(this, InfoWindow);
  17. var _this = _possibleConstructorReturn(this, (InfoWindow.__proto__ || Object.getPrototypeOf(InfoWindow)).call(this));
  18. _this.refElement = null;
  19. _this._entity = null;
  20. debug(__com__, 'constructor', _this._entity);
  21. return _this;
  22. }
  23. _createClass(InfoWindow, [{
  24. key: 'componentDidMount',
  25. value: function componentDidMount() {
  26. debug(__com__, 'componentDidMount', this._entity);
  27. var _props = this.props,
  28. __map__ = _props.__map__,
  29. options = _props.options,
  30. events = _props.events,
  31. children = _props.children;
  32. //let opts = { ...(options || {}), map: __map__, content: children };
  33. var opts = _extends({}, options || {}, { map: __map__ });
  34. this._entity = createInfoWindow(opts, events);
  35. if (this._entity) {
  36. if (this.props.refer) this.props.refer(this._entity);
  37. }
  38. }
  39. }, {
  40. key: 'componentDidUpdate',
  41. value: function componentDidUpdate(prevProps) {
  42. debug(__com__, 'componentDidUpdate', this._entity);
  43. var _props2 = this.props,
  44. __map__ = _props2.__map__,
  45. options = _props2.options,
  46. events = _props2.events,
  47. children = _props2.children;
  48. //let opts = { ...(options || {}), map: __map__, content: children };
  49. var opts = _extends({}, options || {}, { map: __map__ });
  50. if (!this._entity) {
  51. this._entity = createInfoWindow(opts, events);
  52. if (this._entity) {
  53. if (this.props.refer) this.props.refer(this._entity);
  54. }
  55. return;
  56. }
  57. // need check props changes, then update.
  58. var oldOpts = _extends({}, prevProps.options || {}, {
  59. map: prevProps.__map__
  60. });
  61. updateInfoWindow(this._entity, opts, events, oldOpts, prevProps.events);
  62. }
  63. }, {
  64. key: 'componentWillUnmount',
  65. value: function componentWillUnmount() {
  66. debug(__com__, 'componentWillUnmount', this._entity);
  67. if (this._entity) {
  68. // this._entity.clearMap();
  69. this._entity.setMap(null);
  70. // delete this._entity;
  71. this._entity = null;
  72. if (this.props.refer) this.props.refer(this._entity);
  73. }
  74. }
  75. // shouldComponentUpdate(nextProps, nextState) {
  76. // debug(__com__, 'shouldComponentUpdate', this._entity);
  77. // return false;
  78. // }
  79. }, {
  80. key: 'render',
  81. value: function render() {
  82. debug(__com__, 'render', this._entity);
  83. return null;
  84. }
  85. }]);
  86. return InfoWindow;
  87. }(Component);
  88. InfoWindow.propTypes = {
  89. __map__: PropTypes.object,
  90. options: PropTypes.object,
  91. events: PropTypes.object
  92. };
  93. export default InfoWindow;