Map.js 6.2 KB

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