Skip to content

Commit

Permalink
Add HeatmapLayer Addon
Browse files Browse the repository at this point in the history
Allows a HeatmapLayer to be created. Takes an array of
`google.maps.LatLng` objects to the prop `data`.
  • Loading branch information
zolrath committed May 7, 2016
1 parent 5c77cb9 commit b8d8623
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/addons/HeatmapLayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
default as React,
Component,
} from "react";

import {
default as canUseDOM,
} from "can-use-dom";

import {
default as HeatmapLayerCreator,
heatmapLayerDefaultPropTypes,
heatmapLayerControlledPropTypes,
heatmapLayerEventPropTypes,
} from "./addonsCreators/HeatmapLayerCreator";

export default class HeatmapLayer extends Component {
static propTypes = {
// Uncontrolled default[props] - used only in componentDidMount
...heatmapLayerDefaultPropTypes,
// Controlled [props] - used in componentDidMount/componentDidUpdate
...heatmapLayerControlledPropTypes,
// Event [onEventName]
...heatmapLayerEventPropTypes,
}

// Public APIs
//
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#HeatmapLayer
//
// [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; }).filter(function(it){ return it.match(/^get/) && !it.match(/^getMap/); })
getData() { return this.state.directionsRenderer.getDirections(); }

// END - Public APIs
//
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#HeatmapLayer

state = {
}

componentWillMount() {
if (!canUseDOM) {
return;
}
const heatmapLayer = HeatmapLayerCreator._createHeatmapLayer(this.props);

this.setState({ heatmapLayer });
}

render() {
if (this.state.heatmapLayer) {
return (
<HeatmapLayerCreator heatmapLayer={this.state.heatmapLayer} {...this.props}>
{this.props.children}
</HeatmapLayerCreator>
);
} else {
return (<noscript />);
}
}
}
67 changes: 67 additions & 0 deletions src/addons/addonsCreators/HeatmapLayerCreator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
default as React,
PropTypes,
Component,
} from "react";

import { default as HeatmapLayerEventList } from "../addonsEventLists/HeatmapLayerEventList";
import { default as eventHandlerCreator } from "../../utils/eventHandlerCreator";
import { default as defaultPropsCreator } from "../../utils/defaultPropsCreator";
import { default as composeOptions } from "../../utils/composeOptions";
import { default as componentLifecycleDecorator } from "../../utils/componentLifecycleDecorator";

import { default as GoogleMapHolder } from "../../creators/GoogleMapHolder";

export const heatmapLayerControlledPropTypes = {
// NOTICE!!!!!!
//
// Only expose those with getters & setters in the table as controlled props.
//
// [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; }).filter(function(it){ return it.match(/^set/) && !it.match(/^setMap/); })
//
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#HeatmapLayer
data: PropTypes.any,
options: PropTypes.object,
};

export const heatmapLayerDefaultPropTypes = defaultPropsCreator(heatmapLayerControlledPropTypes);

const heatmapLayerUpdaters = {
data(data, component) { component.getHeatmapLayer().setData(data); },
options(options, component) { component.getHeatmapLayer().setOptions(options); },
};

const { eventPropTypes, registerEvents } = eventHandlerCreator(HeatmapLayerEventList);

export const heatmapLayerEventPropTypes = eventPropTypes;

@componentLifecycleDecorator({
registerEvents,
instanceMethodName: `getHeatmapLayer`,
updaters: heatmapLayerUpdaters,
})
export default class HeatmapLayerCreator extends Component {

static propTypes = {
mapHolderRef: PropTypes.instanceOf(GoogleMapHolder).isRequired,
heatmapLayer: PropTypes.object.isRequired,
}

static _createHeatmapLayer(heatmapLayerProps) {
const { mapHolderRef } = heatmapLayerProps;
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#HeatmapLayer
const heatmapLayer = new google.maps.visualization.HeatmapLayer(composeOptions(heatmapLayerProps, heatmapLayerControlledPropTypes));

heatmapLayer.setMap(mapHolderRef.getMap());

return heatmapLayer;
}

getHeatmapLayer() {
return this.props.heatmapLayer;
}

render() {
return (<noscript />);
}
}
5 changes: 5 additions & 0 deletions src/addons/addonsEventLists/HeatmapLayerEventList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// https://developers.google.com/maps/documentation/javascript/3.exp/reference#Polyline
// [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; })
export default [
`zoom_changed`,
];

0 comments on commit b8d8623

Please sign in to comment.