Map.js 7.7 KB

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