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";
|
2021-07-26 05:50:46 +00:00
|
|
|
import { ValidationTypes } from "constants/WidgetValidation";
|
2021-03-30 05:29:03 +00:00
|
|
|
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
|
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";
|
2021-01-19 07:29:15 +00:00
|
|
|
import { DEFAULT_CENTER } from "constants/WidgetConstants";
|
2021-02-08 07:30:01 +00:00
|
|
|
import { getBorderCSSShorthand } from "constants/DefaultTheme";
|
2021-06-01 04:59:45 +00:00
|
|
|
import { EvaluationSubstitutionType } from "entities/DataTree/dataTreeFactory";
|
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;
|
2021-02-08 07:30:01 +00:00
|
|
|
border: ${(props) => getBorderCSSShorthand(props.theme.borders[2])};
|
|
|
|
|
border-radius: 0;
|
2020-07-07 10:22:17 +00:00
|
|
|
h1 {
|
|
|
|
|
margin-top: 15%;
|
|
|
|
|
margin-bottom: 10%;
|
|
|
|
|
color: #7c7c7c;
|
|
|
|
|
}
|
|
|
|
|
p {
|
|
|
|
|
color: #0a0b0e;
|
|
|
|
|
}
|
|
|
|
|
`;
|
2020-10-26 10:01:01 +00:00
|
|
|
|
2021-01-19 07:29:15 +00:00
|
|
|
const DefaultCenter = { ...DEFAULT_CENTER, long: DEFAULT_CENTER.lng };
|
|
|
|
|
|
|
|
|
|
type Center = {
|
|
|
|
|
lat: number;
|
|
|
|
|
long: number;
|
|
|
|
|
[x: string]: any;
|
|
|
|
|
};
|
2020-04-15 11:42:11 +00:00
|
|
|
class MapWidget extends BaseWidget<MapWidgetProps, WidgetState> {
|
2021-02-16 10:29:08 +00:00
|
|
|
static getPropertyPaneConfig() {
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
sectionName: "General",
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
propertyName: "mapCenter",
|
|
|
|
|
label: "Initial location",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
controlType: "LOCATION_SEARCH",
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
2021-07-26 05:50:46 +00:00
|
|
|
validation: {
|
|
|
|
|
type: ValidationTypes.OBJECT,
|
|
|
|
|
params: {
|
|
|
|
|
required: true,
|
|
|
|
|
allowedKeys: [
|
|
|
|
|
{
|
|
|
|
|
name: "lat",
|
|
|
|
|
type: ValidationTypes.NUMBER,
|
|
|
|
|
params: {
|
|
|
|
|
min: -90,
|
|
|
|
|
max: 90,
|
|
|
|
|
default: 0,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "long",
|
|
|
|
|
type: ValidationTypes.NUMBER,
|
|
|
|
|
params: {
|
|
|
|
|
min: -180,
|
|
|
|
|
max: 180,
|
|
|
|
|
default: 0,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "defaultMarkers",
|
|
|
|
|
label: "Default markers",
|
|
|
|
|
controlType: "INPUT_TEXT",
|
|
|
|
|
inputType: "ARRAY",
|
|
|
|
|
helpText: "Sets the default markers on the map",
|
2021-09-09 08:18:16 +00:00
|
|
|
placeholderText: 'Enter [{ "lat": "val1", "long": "val2" }]',
|
2021-02-16 10:29:08 +00:00
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
2021-07-26 05:50:46 +00:00
|
|
|
validation: {
|
|
|
|
|
type: ValidationTypes.ARRAY,
|
|
|
|
|
params: {
|
|
|
|
|
children: {
|
|
|
|
|
type: ValidationTypes.OBJECT,
|
|
|
|
|
params: {
|
2021-08-17 11:35:51 +00:00
|
|
|
required: true,
|
2021-07-26 05:50:46 +00:00
|
|
|
allowedKeys: [
|
|
|
|
|
{
|
|
|
|
|
name: "lat",
|
|
|
|
|
type: ValidationTypes.NUMBER,
|
|
|
|
|
params: {
|
|
|
|
|
min: -90,
|
|
|
|
|
max: 90,
|
|
|
|
|
default: 0,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "long",
|
|
|
|
|
type: ValidationTypes.NUMBER,
|
|
|
|
|
params: {
|
|
|
|
|
min: -180,
|
|
|
|
|
max: 180,
|
|
|
|
|
default: 0,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "title",
|
|
|
|
|
type: ValidationTypes.TEXT,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-06-01 04:59:45 +00:00
|
|
|
evaluationSubstitutionType:
|
|
|
|
|
EvaluationSubstitutionType.SMART_SUBSTITUTE,
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "enableSearch",
|
|
|
|
|
label: "Enable search location",
|
|
|
|
|
helpText: "Enables locaton search",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isBindProperty: false,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "enablePickLocation",
|
|
|
|
|
label: "Enable pick location",
|
|
|
|
|
helpText: "Allows a user to pick their location",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isBindProperty: false,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "enableCreateMarker",
|
|
|
|
|
label: "Create new marker",
|
|
|
|
|
helpText: "Allows users to mark locations on the map",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isBindProperty: false,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
},
|
2021-09-09 08:18:16 +00:00
|
|
|
{
|
|
|
|
|
propertyName: "zoomLevel",
|
|
|
|
|
label: "Zoom Level",
|
|
|
|
|
controlType: "STEP",
|
|
|
|
|
helpText: "Changes the default zoom of the map",
|
|
|
|
|
stepType: "ZOOM_PERCENTAGE",
|
|
|
|
|
isBindProperty: false,
|
|
|
|
|
isTriggerProperty: false,
|
|
|
|
|
},
|
2021-02-16 10:29:08 +00:00
|
|
|
{
|
|
|
|
|
propertyName: "isVisible",
|
|
|
|
|
label: "Visible",
|
|
|
|
|
helpText: "Controls the visibility of the widget",
|
|
|
|
|
controlType: "SWITCH",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: false,
|
2021-07-26 05:50:46 +00:00
|
|
|
validation: { type: ValidationTypes.BOOLEAN },
|
2021-02-16 10:29:08 +00:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
sectionName: "Actions",
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
propertyName: "onMarkerClick",
|
|
|
|
|
label: "onMarkerClick",
|
|
|
|
|
controlType: "ACTION_SELECTOR",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
propertyName: "onCreateMarker",
|
|
|
|
|
label: "onCreateMarker",
|
|
|
|
|
controlType: "ACTION_SELECTOR",
|
|
|
|
|
isJSConvertible: true,
|
|
|
|
|
isBindProperty: true,
|
|
|
|
|
isTriggerProperty: true,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
}
|
2020-04-15 11:42:11 +00:00
|
|
|
|
2021-06-18 07:42:57 +00:00
|
|
|
static getDefaultPropertiesMap(): Record<string, any> {
|
2020-04-17 16:15:09 +00:00
|
|
|
return {
|
|
|
|
|
markers: "defaultMarkers",
|
|
|
|
|
center: "mapCenter",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-18 07:42:57 +00:00
|
|
|
static getMetaPropertiesMap(): Record<string, any> {
|
2020-04-21 07:54:23 +00:00
|
|
|
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) {
|
2021-01-12 04:17:28 +00:00
|
|
|
marker = { lat, 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-11-30 07:23:05 +00:00
|
|
|
const marker = { lat, long, title: "" };
|
|
|
|
|
|
|
|
|
|
const markers = [];
|
2020-12-24 04:32:25 +00:00
|
|
|
(this.props.markers || []).forEach((m) => {
|
2020-11-30 07:23:05 +00:00
|
|
|
markers.push(m);
|
|
|
|
|
});
|
|
|
|
|
markers.push(marker);
|
|
|
|
|
this.props.updateWidgetMetaProperty("markers", markers);
|
|
|
|
|
this.props.updateWidgetMetaProperty("selectedMarker", marker, {
|
2021-04-23 13:50:55 +00:00
|
|
|
triggerPropertyName: "onCreateMarker",
|
2020-11-30 07:23:05 +00:00
|
|
|
dynamicString: this.props.onCreateMarker,
|
|
|
|
|
event: {
|
|
|
|
|
type: EventType.ON_CREATE_MARKER,
|
2020-10-06 16:47:16 +00:00
|
|
|
},
|
2020-11-30 07:23:05 +00:00
|
|
|
});
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
2020-11-06 15:09:35 +00:00
|
|
|
unselectMarker = () => {
|
|
|
|
|
this.props.updateWidgetMetaProperty("selectedMarker", undefined);
|
|
|
|
|
};
|
|
|
|
|
|
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, {
|
2021-04-23 13:50:55 +00:00
|
|
|
triggerPropertyName: "onMarkerClick",
|
2020-10-06 16:47:16 +00:00
|
|
|
dynamicString: this.props.onMarkerClick,
|
|
|
|
|
event: {
|
|
|
|
|
type: EventType.ON_MARKER_CLICK,
|
|
|
|
|
},
|
|
|
|
|
});
|
2020-04-15 11:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
2021-01-19 07:29:15 +00:00
|
|
|
getCenter(): Center {
|
|
|
|
|
return this.props.center || this.props.mapCenter || DefaultCenter;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 06:55:43 +00:00
|
|
|
componentDidUpdate(prevProps: MapWidgetProps) {
|
|
|
|
|
//remove selectedMarker when map initial location is updated
|
|
|
|
|
if (
|
|
|
|
|
JSON.stringify(prevProps.center) !== JSON.stringify(this.props.center) &&
|
|
|
|
|
this.props.selectedMarker
|
|
|
|
|
) {
|
|
|
|
|
this.unselectMarker();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2021-02-03 15:15:16 +00:00
|
|
|
href="https://docs.appsmith.com/v/v1.2.1/setup/docker/google-maps"
|
2021-04-28 10:28:39 +00:00
|
|
|
rel="noopener noreferrer"
|
|
|
|
|
target="_blank"
|
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
|
|
|
|
|
allowZoom={this.props.allowZoom}
|
2021-04-28 10:28:39 +00:00
|
|
|
apiKey={google.apiKey}
|
2021-01-19 07:29:15 +00:00
|
|
|
center={this.getCenter()}
|
2020-11-30 07:23:05 +00:00
|
|
|
enableCreateMarker={this.props.enableCreateMarker}
|
2021-04-28 10:28:39 +00:00
|
|
|
enableDrag={() => {
|
|
|
|
|
this.disableDrag(false);
|
|
|
|
|
}}
|
2020-07-07 10:22:17 +00:00
|
|
|
enablePickLocation={this.props.enablePickLocation}
|
2021-04-28 10:28:39 +00:00
|
|
|
enableSearch={this.props.enableSearch}
|
|
|
|
|
isDisabled={this.props.isDisabled}
|
|
|
|
|
isVisible={this.props.isVisible}
|
|
|
|
|
markers={this.props.markers}
|
2020-07-07 10:22:17 +00:00
|
|
|
saveMarker={this.onCreateMarker}
|
|
|
|
|
selectMarker={this.onMarkerClick}
|
2021-04-28 10:28:39 +00:00
|
|
|
selectedMarker={this.props.selectedMarker}
|
2020-11-06 15:09:35 +00:00
|
|
|
unselectMarker={this.unselectMarker}
|
2021-04-28 10:28:39 +00:00
|
|
|
updateCenter={this.updateCenter}
|
|
|
|
|
updateMarker={this.updateMarker}
|
|
|
|
|
widgetId={this.props.widgetId}
|
|
|
|
|
zoomLevel={this.props.zoomLevel}
|
2020-07-07 10:22:17 +00:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
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-11-24 08:43:45 +00:00
|
|
|
title?: string;
|
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));
|