PromucFlow_constructor/app/client/src/widgets/MapWidget.tsx

181 lines
4.8 KiB
TypeScript
Raw Normal View History

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";
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,
onMarkerClick: VALIDATION_TYPES.ACTION_SELECTOR,
onCreateMarker: VALIDATION_TYPES.ACTION_SELECTOR,
};
}
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",
};
}
static getMetaPropertiesMap(): Record<string, undefined> {
return {
center: undefined,
markers: undefined,
};
}
2020-05-07 10:51:37 +00:00
updateCenter = (lat: number, long: number) => {
this.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-04-29 10:29:02 +00:00
const markers: Array<MarkerProps> = [...this.props.markers];
2020-05-07 10:51:37 +00:00
this.disableDrag(false);
2020-04-29 10:29:02 +00:00
this.updateWidgetMetaProperty(
"markers",
markers.map((marker, i) => {
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-05-07 10:51:37 +00:00
onCreateMarker = (lat: number, long: number) => {
this.disableDrag(true);
this.updateWidgetMetaProperty("selectedMarker", {
lat: lat,
long: long,
});
2020-04-15 11:42:11 +00:00
if (this.props.onCreateMarker) {
super.executeAction({
dynamicString: this.props.onCreateMarker,
event: {
type: EventType.ON_CREATE_MARKER,
},
});
}
};
2020-05-07 10:51:37 +00:00
onMarkerClick = (lat: number, long: number, title: string) => {
2020-04-15 11:42:11 +00:00
this.updateWidgetMetaProperty("selectedMarker", {
lat: lat,
2020-05-07 10:51:37 +00:00
long: long,
2020-04-15 11:42:11 +00:00
title: title,
});
2020-05-07 10:51:37 +00:00
this.disableDrag(true);
2020-04-15 11:42:11 +00:00
if (this.props.onMarkerClick) {
super.executeAction({
dynamicString: this.props.onMarkerClick,
event: {
type: EventType.ON_MARKER_CLICK,
},
});
}
};
2020-04-17 16:15:09 +00:00
// componentDidMount() {
// super.componentDidMount();
// if (this.props.mapCenter) {
// this.updateWidgetMetaProperty("center", this.props.mapCenter);
// }
// }
//
// componentDidUpdate(prevProps: MapWidgetProps) {
// super.componentDidUpdate(prevProps);
// if (
// this.props.mapCenter &&
// prevProps.mapCenter &&
// (this.props.mapCenter.lat !== prevProps.mapCenter.lat ||
// this.props.mapCenter.lng !== prevProps.mapCenter.lng)
// ) {
// this.updateWidgetMetaProperty("center", this.props.mapCenter);
// }
// }
2020-04-15 11:42:11 +00:00
getPageView() {
return (
<MapComponent
widgetId={this.props.widgetId}
isVisible={this.props.isVisible}
zoomLevel={this.props.zoomLevel}
allowZoom={this.props.allowZoom}
center={this.props.center || this.props.mapCenter}
2020-05-07 10:51:37 +00:00
enableCreateMarker
2020-04-29 10:29:02 +00:00
selectedMarker={this.props.selectedMarker}
2020-04-15 11:42:11 +00:00
updateCenter={this.updateCenter}
isDisabled={this.props.isDisabled}
enableSearch={this.props.enableSearch}
enablePickLocation={this.props.enablePickLocation}
saveMarker={this.onCreateMarker}
2020-04-29 10:29:02 +00:00
updateMarker={this.updateMarker}
2020-04-15 11:42:11 +00:00
selectMarker={this.onMarkerClick}
markers={this.props.markers || []}
2020-05-07 10:51:37 +00:00
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;
}
export interface MapWidgetProps extends WidgetProps {
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;