Map.js 7.6 KB

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