From 1d8e4788c6ea487e35d1a71862b3a8e2fa675944 Mon Sep 17 00:00:00 2001 From: Rishabh Jain Date: Mon, 24 Oct 2016 14:56:10 -0400 Subject: [PATCH] feat(FusionTablesLayer): add component * Original commit: fd01d445d5cbe012253af8acf17b5b72e4853888 * Original author: @RishabhJain96 * Closes #370 --- src/lib/FusionTablesLayer.js | 98 +++++++++++++++++++++++++++++++++ src/lib/__tests__/index.test.js | 2 + src/lib/constants.js | 2 + src/lib/index.js | 4 ++ 4 files changed, 106 insertions(+) create mode 100644 src/lib/FusionTablesLayer.js diff --git a/src/lib/FusionTablesLayer.js b/src/lib/FusionTablesLayer.js new file mode 100644 index 00000000..63193a59 --- /dev/null +++ b/src/lib/FusionTablesLayer.js @@ -0,0 +1,98 @@ +/* global google */ +import _ from 'lodash'; + +import { + default as React, + PropTypes, +} from 'react'; + +import { + MAP, + FUSION_TABLES_LAYER, +} from './constants'; + +import { + addDefaultPrefixToPropTypes, + collectUncontrolledAndControlledProps, + default as enhanceElement, +} from './enhanceElement'; + +const controlledPropTypes = { + // 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#FusionTablesLayer + options: PropTypes.object, +} + +const defaultUncontrolledPropTypes = addDefaultPrefixToPropTypes(controlledPropTypes); + +const eventMap = { + // https://developers.google.com/maps/documentation/javascript/3.exp/reference#FusionTablesLayer + // [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; }) + onClick: `click`, +}; + +const publicMethodMap = { + // Public APIs + // + // https://developers.google.com/maps/documentation/javascript/3.exp/reference#FusionTablesLayer + // + // [].map.call($0.querySelectorAll("tr>td>code"), function(it){ return it.textContent; }) + // .filter(function(it){ return it.match(/^get/) && !it.match(/Map$/); }) + // END - Public APIs +} + +const controlledPropUpdaterMap = { + options(fusionTablesLayer, options) { fusionTablesLayer.setOptions(options); }, +}; + +function getInstanceFromComponent(component) { + return component.state[FUSION_TABLES_LAYER]; +} + +export default _.flowRight( + React.createClass, + enhanceElement(getInstanceFromComponent, publicMethodMap, eventMap, controlledPropUpdaterMap), +)({ + displayName: `FusionTablesLayer`, + + propTypes: { + ...controlledPropTypes, + ...defaultUncontrolledPropTypes, + }, + + contextTypes: { + [MAP]: PropTypes.object, + }, + + getInitialState() { + const fusionTablesLayer = new google.maps.FusionTablesLayer({ + map: this.context[MAP], + ...collectUncontrolledAndControlledProps( + defaultUncontrolledPropTypes, + controlledPropTypes, + this.props + ), + }); + + return { + [FUSION_TABLES_LAYER]: fusionTablesLayer, + }; + }, + + componentWillUnmount() { + const fusionTablesLayer = getInstanceFromComponent(this); + if (fusionTablesLayer) { + fusionTablesLayer.setMap(null); + } + }, + + render() { + return false; + }, +}); \ No newline at end of file diff --git a/src/lib/__tests__/index.test.js b/src/lib/__tests__/index.test.js index d66294bd..a2e2aa17 100644 --- a/src/lib/__tests__/index.test.js +++ b/src/lib/__tests__/index.test.js @@ -7,6 +7,7 @@ import { HeatmapLayer, InfoWindow, KmlLayer, + FusionTablesLayer, Marker, OverlayView, Polygon, @@ -24,6 +25,7 @@ describe(`index`, () => { expect(HeatmapLayer).toBeDefined(); expect(InfoWindow).toBeDefined(); expect(KmlLayer).toBeDefined(); + expect(FusionTablesLayer).toBeDefined(); expect(Marker).toBeDefined(); expect(OverlayView).toBeDefined(); expect(Polygon).toBeDefined(); diff --git a/src/lib/constants.js b/src/lib/constants.js index f9917d41..9ec6a799 100644 --- a/src/lib/constants.js +++ b/src/lib/constants.js @@ -18,6 +18,8 @@ export const DIRECTIONS_RENDERER = `__SECRET_DIRECTIONS_RENDERER_DO_NOT_USE_OR_Y export const HEATMAP_LAYER = `__SECRET_HEATMAP_LAYER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`; +export const FUSION_TABLES_LAYER = `__SECRET_FUSION_TABLES_LAYER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`; + export const ANCHOR = `__SECRET_ANCHOR_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`; export const INFO_WINDOW = `__SECRET_INFO_WINDOW_DO_NOT_USE_OR_YOU_WILL_BE_FIRED`; diff --git a/src/lib/index.js b/src/lib/index.js index 11b2c7a1..9d610d52 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -30,6 +30,10 @@ export { default as KmlLayer, } from "./KmlLayer"; +export { + default as FusionTablesLayer, +} from "./FusionTablesLayer"; + export { default as DirectionsRenderer, } from "./DirectionsRenderer";