UNPKG

rc-tile-map

Version:
185 lines (156 loc) 8.17 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports['default'] = undefined; 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; }; }(); var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; var _class, _temp2; var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _lodash = require('lodash'); var _MapComponent2 = require('../MapComponent'); var _MapComponent3 = _interopRequireDefault(_MapComponent2); var _layer = require('../propTypes/layer'); var _layer2 = _interopRequireDefault(_layer); var _map = require('../propTypes/map'); var _map2 = _interopRequireDefault(_map); var _index = require('../propTypes/index'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } 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; } var InfoWindow = (_temp2 = _class = function (_MapComponent) { _inherits(InfoWindow, _MapComponent); function InfoWindow() { var _ref; var _temp, _this, _ret; _classCallCheck(this, InfoWindow); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = InfoWindow.__proto__ || Object.getPrototypeOf(InfoWindow)).call.apply(_ref, [this].concat(args))), _this), _this.onClickMarker = function () { var markerInstance = _this.context.markerInstance; markerInstance.openInfoWindow(_this.componentInstance); var dom = _this.getHtmlDomByReactDom(_this.props.children); _this.bindContentEvents(_this.props.contentEvents, dom, markerInstance, _this.componentInstance); _this.componentInstance.setContent(dom); }, _this.removeInfoWindowContent = function () { var _this$context = _this.context, markerInstance = _this$context.markerInstance, map = _this$context.map; if (markerInstance) { markerInstance.closeInfoWindow(); markerInstance.removeEventListener('click', _this.onClickMarker); } else { if (_this.props.position) { map.closeInfoWindow(); } } }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(InfoWindow, [{ key: 'createComponentInstance', value: function createComponentInstance(props) { var instance = new window.BMap.InfoWindow(this.getHtmlDomByReactDom(props.children), this.getOptions(props)); if (!props.show) { instance.hide(); } return instance; } }, { key: 'updateComponentInstance', value: function updateComponentInstance(fromProps, toProps) { this.updatePropsByBoolFun('show', 'hide', fromProps.show, toProps.show); this.updatePropsBySetFun('setTitle', fromProps.title, toProps.title); this.updatePropsBySetFun('setWidth', fromProps.width, toProps.width); this.updatePropsBySetFun('setHeight', fromProps.height, toProps.height); this.updatePropsByBoolFun('enableAutoPan', 'disableAutoPan', fromProps.enableAutoPan, toProps.enableAutoPan); this.updatePropsByBoolFun('enableCloseOnClick', 'disableCloseOnClick', fromProps.enableCloseOnClick, toProps.enableCloseOnClick); } }, { key: 'componentWillMount', value: function componentWillMount() { _get(InfoWindow.prototype.__proto__ || Object.getPrototypeOf(InfoWindow.prototype), 'componentWillMount', this).call(this); this.componentInstance = this.createComponentInstance(this.props); if (this.props.setComponentInstance) { this.props.setComponentInstance(this.componentInstance); } } }, { key: 'componentDidMount', value: function componentDidMount() { var position = this.props.position; var _context = this.context, map = _context.map, markerInstance = _context.markerInstance; var el = this.componentInstance; if (markerInstance) { // Attach to container component markerInstance.addEventListener('click', this.onClickMarker); } else { // Attach to a Map if (position) { map.openInfoWindow(el, position); } } } }, { key: 'componentDidUpdate', value: function componentDidUpdate(prevProps) { this.updateComponentInstance(prevProps, this.props); if (this.props.position && !(0, _lodash.isEqual)(prevProps.position, this.props.position)) { this.context.map.openInfoWindow(this.componentInstance, this.props.position); } if (this.props.show && this.componentInstance.isOpen()) { if (this.props.children == null) { this.removeInfoWindowContent(); } else { var oldContent = this.getHtmlDomByReactDom(prevProps.children).innerText; var newContent = this.getHtmlDomByReactDom(this.props.children).innerText; if (oldContent !== newContent) { var dom = this.getHtmlDomByReactDom(this.props.children); this.bindContentEvents(this.props.contentEvents, dom, this.context.markerInstance, this.componentInstance); this.componentInstance.setContent(dom); } } } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.removeInfoWindowContent(); _get(InfoWindow.prototype.__proto__ || Object.getPrototypeOf(InfoWindow.prototype), 'componentWillUnmount', this).call(this); } }, { key: 'render', value: function render() { return null; } }]); return InfoWindow; }(_MapComponent3['default']), _class.defaultProps = { width: 0, height: 0, enableAutoPan: true, enableCloseOnClick: true, show: true, contentEvents: {} }, _class.propTypes = { children: _propTypes2['default'].node, width: _propTypes2['default'].number, height: _propTypes2['default'].number, maxWidth: _propTypes2['default'].number, offset: _index.size, title: _propTypes2['default'].string, enableAutoPan: _propTypes2['default'].bool, enableCloseOnClick: _propTypes2['default'].bool, position: _index.point, show: _propTypes2['default'].bool, contentEvents: _propTypes2['default'].object }, _class.contextTypes = { map: _map2['default'], markerInstance: _layer2['default'] }, _temp2); exports['default'] = InfoWindow; module.exports = exports['default'];