2020-04-15 11:42:11 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
|
|
|
|
|
import { WidgetType } from "constants/WidgetConstants";
|
|
|
|
|
import MapComponent from "components/designSystems/appsmith/MapComponent";
|
|
|
|
|
import { WidgetPropertyValidationType } from "utils/ValidationFactory";
|
|
|
|
|
import { VALIDATION_TYPES } from "constants/WidgetValidation";
|
|
|
|
|
import { EventType } from "constants/ActionConstants";
|
|
|
|
|
import { TriggerPropertiesMap } from "utils/WidgetFactory";
|
2020-07-07 10:22:17 +00:00
|
|
|
import { getAppsmithConfigs } from "configs";
|
|
|
|
|
import styled from "styled-components";
|
2020-08-28 17:23:07 +00:00
|
|
|
import * as Sentry from "@sentry/react";
|
2020-10-06 09:01:51 +00:00
|
|
|
import withMeta, { WithMeta } from "./MetaHOC";
|
2020-04-15 11:42:11 +00:00
|
|
|
|
2020-07-07 10:22:17 +00:00
|
|
|
const { google } = getAppsmithConfigs();
|
|
|
|
|
|
|
|
|
|
const DisabledContainer = styled.div`
|
|
|
|
|
background-color: white;
|
|
|
|
|
height: 100%;
|
|
|
|
|
text-align: center;
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
h1 {
|
|
|
|
|
margin-top: 15%;
|
|
|
|
|
margin-bottom: 10%;
|
|
|
|
|
color: #7c7c7c;
|
|
|
|
|
}
|
|
|
|
|
p {
|
|
|
|
|
color: #0a0b0e;
|
|
|
|
|
}
|
|
|
|
|
`;
|
2020-04-15 11:42:11 +00:00
|
|
|
class MapWidget extends BaseWidget<MapWidgetProps, WidgetState> {
|
|
|
|
|
static getPropertyValidationMap(): WidgetPropertyValidationType {
|
|
|
|
|
return {
|
|
|
|
|
defaultMarkers: VALIDATION_TYPES.MARKERS,
|
|
|
|
|
isDisabled: VALIDATION_TYPES.BOOLEAN,
|
|
|
|
|
isVisible: VALIDATION_TYPES.BOOLEAN,
|
|
|
|
|
enableSearch: VALIDATION_TYPES.BOOLEAN,
|
|
|
|
|
enablePickLocation: VALIDATION_TYPES.BOOLEAN,
|
|
|
|
|
allowZoom: VALIDATION_TYPES.BOOLEAN,
|
|
|
|
|
zoomLevel: VALIDATION_TYPES.NUMBER,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getTriggerPropertyMap(): TriggerPropertiesMap {
|
|
|
|
|
return {
|
|
|
|
|
onMarkerClick: true,
|
|
|
|
|
onCreateMarker: true,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 16:15:09 +00:00
|
|
|
static getDefaultPropertiesMap(): Record<string, string> {
|
|
|
|
|
return {
|
|
|
|
|
markers: "defaultMarkers",
|
|
|
|
|
center: "mapCenter",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 07:54:23 +00:00
|
|
|
static getMetaPropertiesMap(): Record<string, undefined> {
|
|
|
|
|
return {
|
|
|
|
|
center: undefined,
|
|
|
|
|
markers: undefined,
|
2020-10-06 09:01:51 +00:00
|
|
|
selectedMarker: undefined,
|
2020-04-21 07:54:23 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-07 10:51:37 +00:00
|
|
|
updateCenter = (lat: number, long: number) => {
|
2020-10-06 09:01:51 +00:00
|
|
|
this.props.updateWidgetMetaProperty("center", { lat, long });
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
2020-05-07 10:51:37 +00:00
|
|
|
updateMarker = (lat: number, long: number, index: number) => {
|
2020-10-12 13:06:05 +00:00
|
|
|
const markers: Array<MarkerProps> = [...(this.props.markers || [])].map(
|
2020-10-06 09:01:51 +00:00
|
|
|
(marker, i) => {
|
2020-04-29 10:29:02 +00:00
|
|
|
if (index === i) {
|
|
|
|
|
marker.lat = lat;
|
2020-05-07 10:51:37 +00:00
|
|
|
marker.long = long;
|
2020-04-29 10:29:02 +00:00
|
|
|
}
|
|
|
|
|
return marker;
|
2020-10-06 09:01:51 +00:00
|
|
|
},
|
2020-04-29 10:29:02 +00:00
|
|
|
);
|
2020-10-06 09:01:51 +00:00
|
|
|
this.disableDrag(false);
|
|
|
|
|
this.props.updateWidgetMetaProperty("markers", markers);
|
2020-04-29 10:29:02 +00:00
|
|
|
};
|
|
|
|
|
|
2020-05-07 10:51:37 +00:00
|
|
|
onCreateMarker = (lat: number, long: number) => {
|
|
|
|
|
this.disableDrag(true);
|
2020-10-06 16:47:16 +00:00
|
|
|
this.props.updateWidgetMetaProperty(
|
|
|
|
|
"selectedMarker",
|
|
|
|
|
{
|
|
|
|
|
lat,
|
|
|
|
|
long,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-04-15 11:42:11 +00:00
|
|
|
dynamicString: this.props.onCreateMarker,
|
|
|
|
|
event: {
|
|
|
|
|
type: EventType.ON_CREATE_MARKER,
|
|
|
|
|
},
|
2020-10-06 16:47:16 +00:00
|
|
|
},
|
|
|
|
|
);
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
2020-05-07 10:51:37 +00:00
|
|
|
onMarkerClick = (lat: number, long: number, title: string) => {
|
2020-10-06 09:01:51 +00:00
|
|
|
this.disableDrag(true);
|
|
|
|
|
const selectedMarker = {
|
2020-04-15 11:42:11 +00:00
|
|
|
lat: lat,
|
2020-05-07 10:51:37 +00:00
|
|
|
long: long,
|
2020-04-15 11:42:11 +00:00
|
|
|
title: title,
|
2020-10-06 09:01:51 +00:00
|
|
|
};
|
2020-10-06 16:47:16 +00:00
|
|
|
this.props.updateWidgetMetaProperty("selectedMarker", selectedMarker, {
|
|
|
|
|
dynamicString: this.props.onMarkerClick,
|
|
|
|
|
event: {
|
|
|
|
|
type: EventType.ON_MARKER_CLICK,
|
|
|
|
|
},
|
|
|
|
|
});
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getPageView() {
|
|
|
|
|
return (
|
2020-07-07 10:22:17 +00:00
|
|
|
<>
|
|
|
|
|
{!google.enabled && (
|
|
|
|
|
<DisabledContainer>
|
|
|
|
|
<h1>{"Map Widget disabled"}</h1>
|
2020-07-17 09:59:28 +00:00
|
|
|
<p>{"Map widget requires a Google Maps API Key"}</p>
|
2020-07-07 10:22:17 +00:00
|
|
|
<p>
|
2020-07-17 09:59:28 +00:00
|
|
|
{"See our"}
|
2020-07-07 10:22:17 +00:00
|
|
|
<a
|
|
|
|
|
target="_blank"
|
|
|
|
|
rel="noopener noreferrer"
|
2020-07-17 09:59:28 +00:00
|
|
|
href="https://docs.appsmith.com/third-party-services/google-maps"
|
2020-07-07 10:22:17 +00:00
|
|
|
>
|
2020-08-06 06:36:12 +00:00
|
|
|
{" documentation "}
|
2020-07-07 10:22:17 +00:00
|
|
|
</a>
|
2020-07-17 09:59:28 +00:00
|
|
|
{"to configure API Keys"}
|
2020-07-07 10:22:17 +00:00
|
|
|
</p>
|
|
|
|
|
</DisabledContainer>
|
|
|
|
|
)}
|
|
|
|
|
{google.enabled && (
|
|
|
|
|
<MapComponent
|
2020-07-10 07:35:27 +00:00
|
|
|
apiKey={google.apiKey}
|
2020-07-07 10:22:17 +00:00
|
|
|
widgetId={this.props.widgetId}
|
|
|
|
|
isVisible={this.props.isVisible}
|
|
|
|
|
zoomLevel={this.props.zoomLevel}
|
|
|
|
|
allowZoom={this.props.allowZoom}
|
|
|
|
|
center={this.props.center || this.props.mapCenter}
|
|
|
|
|
enableCreateMarker
|
|
|
|
|
selectedMarker={this.props.selectedMarker}
|
|
|
|
|
updateCenter={this.updateCenter}
|
|
|
|
|
isDisabled={this.props.isDisabled}
|
|
|
|
|
enableSearch={this.props.enableSearch}
|
|
|
|
|
enablePickLocation={this.props.enablePickLocation}
|
|
|
|
|
saveMarker={this.onCreateMarker}
|
|
|
|
|
updateMarker={this.updateMarker}
|
|
|
|
|
selectMarker={this.onMarkerClick}
|
|
|
|
|
markers={this.props.markers || []}
|
|
|
|
|
disableDrag={() => {
|
|
|
|
|
this.disableDrag(false);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2020-04-15 11:42:11 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getWidgetType(): WidgetType {
|
|
|
|
|
return "MAP_WIDGET";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface MarkerProps {
|
|
|
|
|
lat: number;
|
2020-05-07 10:51:37 +00:00
|
|
|
long: number;
|
2020-04-15 11:42:11 +00:00
|
|
|
title?: string;
|
|
|
|
|
description?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-06 09:01:51 +00:00
|
|
|
export interface MapWidgetProps extends WidgetProps, WithMeta {
|
2020-04-15 11:42:11 +00:00
|
|
|
isDisabled?: boolean;
|
|
|
|
|
isVisible?: boolean;
|
|
|
|
|
enableSearch: boolean;
|
|
|
|
|
zoomLevel: number;
|
|
|
|
|
allowZoom: boolean;
|
|
|
|
|
enablePickLocation: boolean;
|
|
|
|
|
mapCenter: {
|
|
|
|
|
lat: number;
|
2020-05-07 10:51:37 +00:00
|
|
|
long: number;
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
center?: {
|
|
|
|
|
lat: number;
|
2020-05-07 10:51:37 +00:00
|
|
|
long: number;
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
defaultMarkers?: Array<MarkerProps>;
|
|
|
|
|
markers?: Array<MarkerProps>;
|
2020-04-29 10:29:02 +00:00
|
|
|
selectedMarker?: {
|
|
|
|
|
lat: number;
|
|
|
|
|
long: number;
|
|
|
|
|
title?: string;
|
|
|
|
|
};
|
2020-04-15 11:42:11 +00:00
|
|
|
onMarkerClick?: string;
|
|
|
|
|
onCreateMarker?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MapWidget;
|
2020-10-06 09:01:51 +00:00
|
|
|
export const ProfiledMapWidget = Sentry.withProfiler(withMeta(MapWidget));
|