Map.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import React, { Component, PureComponent } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { loadMap, createMap, updateMap } from './api';
  4. var __com__ = 'Map';
  5. //const debug = console.log;
  6. var debug = function debug() {};
  7. export var Map = function (_Component) {
  8. babelHelpers.inherits(Map, _Component);
  9. function Map() {
  10. babelHelpers.classCallCheck(this, Map);
  11. var _this = babelHelpers.possibleConstructorReturn(this, (Map.__proto__ || Object.getPrototypeOf(Map)).call(this));
  12. _this.refElement = null;
  13. _this._entity = null;
  14. debug(__com__, 'component constructor', _this.refElement, _this._entity);
  15. return _this;
  16. }
  17. babelHelpers.createClass(Map, [{
  18. key: 'componentWillMount',
  19. value: function componentWillMount() {
  20. debug(__com__, 'componentWillMount', this.refElement, this._entity);
  21. }
  22. }, {
  23. key: 'componentDidMount',
  24. value: function componentDidMount() {
  25. debug(__com__, 'componentDidMount', this.refElement, this._entity);
  26. var _props = this.props,
  27. AMap = _props.AMap,
  28. options = _props.options,
  29. events = _props.events;
  30. this._entity = createMap(AMap, this.refElement, options, events);
  31. if (this._entity) {
  32. if (this.props.refer) this.props.refer(this._entity);
  33. this.setState({ __map__: this._entity });
  34. }
  35. }
  36. }, {
  37. key: 'componentWillReceiveProps',
  38. value: function componentWillReceiveProps(nextProps) {
  39. debug(__com__, 'componentWillReceiveProps', this.refElement, this._entity);
  40. }
  41. }, {
  42. key: 'componentWillUpdate',
  43. value: function componentWillUpdate() {
  44. debug(__com__, 'componentWillUpdate', this.refElement, this._entity);
  45. }
  46. }, {
  47. key: 'componentDidUpdate',
  48. value: function componentDidUpdate(prevProps) {
  49. debug(__com__, 'componentDidUpdate', this.refElement, this._entity);
  50. var _props2 = this.props,
  51. AMap = _props2.AMap,
  52. options = _props2.options,
  53. events = _props2.events;
  54. if (!this._entity) {
  55. this._entity = createMap(AMap, this.refElement, options, events);
  56. if (this._entity) {
  57. if (this.props.refer) this.props.refer(this._entity);
  58. this.setState({ __map__: this._entity });
  59. }
  60. return;
  61. }
  62. // need check props changes, then update.
  63. //updateMap(this._entity, this.props, prevProps);
  64. updateMap(this._entity, options, events, prevProps.options, prevProps.events);
  65. }
  66. }, {
  67. key: 'componentWillUnmount',
  68. value: function componentWillUnmount() {
  69. debug(__com__, 'componentWillUnmount', this.refElement, this._entity);
  70. if (this._entity) {
  71. var refer = this.props.refer;
  72. // this._entity.clearMap();
  73. this._entity.destroy();
  74. // delete this._entity;
  75. this._entity = null;
  76. if (this.props.refer) this.props.refer(this._entity);
  77. }
  78. }
  79. }, {
  80. key: 'renderChildren',
  81. value: function renderChildren(children, __map__) {
  82. return React.Children.map(children, function (child) {
  83. if (child) {
  84. var cType = child.type;
  85. /* 针对下面两种组件不注入地图相关属性
  86. * 1. 明确声明不需要注入的
  87. * 2. DOM 元素
  88. */
  89. if (cType.preventAmap || typeof cType === 'string') {
  90. debug(__com__, 'component renderChildren orig');
  91. return child;
  92. }
  93. debug(__com__, 'component renderChildren add __map__');
  94. return React.cloneElement(child, {
  95. __map__: __map__
  96. });
  97. }
  98. debug(__com__, 'component renderChildren null');
  99. return child;
  100. });
  101. }
  102. // shouldComponentUpdate(nextProps, nextState) {
  103. // debug(__com__, 'shouldComponentUpdate', this.refElement, this._entity);
  104. // let { AMap: oldAMap, refer: oldRefer, options: oldOptions, events: oldEvents } = this.props;
  105. // let { AMap: newAMap, refer: newRefer, options: newOptions, events: newEvents } = nextProps;
  106. // if (oldAMap === newAMap && oldRefer === newRefer && oldOptions === newOptions && oldEvents === newEvents) {
  107. // debug(__com__, 'shouldComponentUpdate', false);
  108. // return false;
  109. // }
  110. // debug(__com__, 'shouldComponentUpdate', true);
  111. // return true;
  112. // }
  113. }, {
  114. key: 'render',
  115. value: function render() {
  116. var _this2 = this;
  117. debug(__com__, 'component render', this._entity);
  118. var _props3 = this.props,
  119. AMap = _props3.AMap,
  120. options = _props3.options,
  121. events = _props3.events,
  122. match = _props3.match,
  123. location = _props3.location,
  124. history = _props3.history,
  125. children = _props3.children,
  126. staticContext = _props3.staticContext,
  127. rest = babelHelpers.objectWithoutProperties(_props3, ['AMap', 'options', 'events', 'match', 'location', 'history', 'children', 'staticContext']);
  128. return React.createElement(
  129. 'div',
  130. babelHelpers.extends({
  131. ref: function ref(ele) {
  132. _this2.refElement = ele;
  133. }
  134. }, rest),
  135. this.renderChildren(children, this._entity)
  136. );
  137. }
  138. }]);
  139. return Map;
  140. }(Component);
  141. Map.propTypes = {
  142. AMap: PropTypes.object,
  143. refer: PropTypes.func, // 类似ref的函数形式,可以让父组件调用_entity
  144. options: PropTypes.object,
  145. events: PropTypes.object
  146. // zoom: PropTypes.number, // 10, //设置地图显示的缩放级别
  147. // center: PropTypes.array, // [116.397428, 39.90923],//设置地图中心点坐标
  148. // layers: PropTypes.array, // [new AMap.TileLayer.Satellite()], //设置图层,可设置成包含一个或多个图层的数组
  149. // mapStyle: PropTypes.string, // 'amap://styles/whitesmoke', //设置地图的显示样式
  150. // viewMode: PropTypes.string, // '2D', //设置地图模式
  151. // lang: PropTypes.string, // 'zh_cn', //设置地图语言类型
  152. // events: PropTypes.object, // {'click': function}, // 事件map
  153. };
  154. export default Map;