Guest User

Untitled

a guest
Dec 16th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import PropTypes from 'prop-types';
  2. import React from 'react';
  3. import MapGL from 'react-map-gl';
  4.  
  5. // Note: this is pretty much identical to react-map-gl/utils/automount. Because
  6. // we are subclassing InteractiveMap (and how property inheritance works),
  7. // we need to pass the parent in for autobinding along with the current instance.
  8. const PREDEFINED = [
  9. 'constructor', 'render', 'componentWillMount', 'componentDidMount',
  10. 'componentWillReceiveProps', 'shouldComponentUpdate', 'componentWillUpdate',
  11. 'componentDidUpdate', 'componentWillUnmount'
  12. ];
  13.  
  14. const autobind = (obj, parent) => {
  15. const propNames = Object.getOwnPropertyNames(parent.prototype);
  16. for (const key of propNames) {
  17. if (typeof obj[key] === 'function') {
  18. if (!PREDEFINED.find(name => key === name)) {
  19. obj[key] = obj[key].bind(obj);
  20. }
  21. }
  22. }
  23. }
  24.  
  25. // Adding layers requires a reference to the mapbox object itself. This is
  26. // different than just putting new HTML elements over the map, because a layer
  27. // is part of the map. Instead of making a massive map component, we're able to
  28. // add <Layer>s as subcomponents that (mostly) work the way react and HTML
  29. // elements would expect them to. The layers get the mapbox object via. the
  30. // `loaded` context, which is a promise that is resolved with the map once it
  31. // has finally loaded.
  32. class Map extends MapGL {
  33. static childContextTypes = {
  34. ...MapGL.childContextTypes,
  35. loaded: PropTypes.objectOf(Promise),
  36. }
  37.  
  38. constructor(props) {
  39. super(props);
  40. autobind(this, MapGL);
  41.  
  42. this._loaded = new Promise((resolve) => this._loadedResolve = resolve);
  43. }
  44.  
  45. _staticMapLoaded = (ref) => {
  46. super._staticMapLoaded(ref);
  47. this.getMap().on('load', () => this._loadedResolve(this.getMap()));
  48. }
  49.  
  50. getChildContext() {
  51. return {
  52. ...super.getChildContext(),
  53. loaded: this._loaded,
  54. };
  55. }
  56. }
  57.  
  58. export default Map;
Add Comment
Please, Sign In to add comment