Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restyle Map: add tooltip and popup templating #4444

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions client/app/assets/less/inc/visualizations/map.less
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
z-index: 0;
}
}

.leaflet-popup-content img {
max-width: 100%;
height: auto;
}
68 changes: 68 additions & 0 deletions client/app/visualizations/map/Editor/FormatSettings.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React from "react";
import { useDebouncedCallback } from "use-debounce";
import { Section, Input, Checkbox, TextArea, ContextHelp } from "@/components/visualizations/editor";
import { EditorPropTypes } from "@/visualizations";

function TemplateFormatHint() {
// eslint-disable-line react/prop-types
return (
<ContextHelp placement="topLeft" arrowPointAtCenter>
<div className="p-b-5">
All query result columns can be referenced using <code>{"{{ column_name }}"}</code> syntax.
</div>
</ContextHelp>
);
}

export default function FormatSettings({ options, onOptionsChange }) {
const [onOptionsChangeDebounced] = useDebouncedCallback(onOptionsChange, 200);

const templateFormatHint = <TemplateFormatHint />;

return (
<div className="map-visualization-editor-format-settings">
<Section>
<Checkbox
data-test="Map.Editor.TooltipEnabled"
checked={options.tooltip.enabled}
onChange={event => onOptionsChange({ tooltip: { enabled: event.target.checked } })}>
Show tooltip
</Checkbox>
</Section>

<Section>
<Input
label={<React.Fragment>Tooltip template {templateFormatHint}</React.Fragment>}
className="w-100"
data-test="Map.Editor.TooltipTemplate"
disabled={!options.tooltip.enabled}
defaultValue={options.tooltip.template}
onChange={event => onOptionsChangeDebounced({ tooltip: { template: event.target.value } })}
/>
</Section>

<Section>
<Checkbox
data-test="Map.Editor.PopupEnabled"
checked={options.popup.enabled}
onChange={event => onOptionsChange({ popup: { enabled: event.target.checked } })}>
Show popup
</Checkbox>
</Section>

<Section>
<TextArea
label={<React.Fragment>Popup template {templateFormatHint}</React.Fragment>}
className="w-100"
data-test="Map.Editor.PopupTemplate"
disabled={!options.popup.enabled}
rows={4}
defaultValue={options.popup.template}
onChange={event => onOptionsChangeDebounced({ popup: { template: event.target.value } })}
/>
</Section>
</div>
);
}

FormatSettings.propTypes = EditorPropTypes;
2 changes: 2 additions & 0 deletions client/app/visualizations/map/Editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import createTabbedEditor from "@/components/visualizations/editor/createTabbedE

import GeneralSettings from "./GeneralSettings";
import GroupsSettings from "./GroupsSettings";
import FormatSettings from "./FormatSettings";
import StyleSettings from "./StyleSettings";

export default createTabbedEditor([
{ key: "General", title: "General", component: GeneralSettings },
{ key: "Groups", title: "Groups", component: GroupsSettings },
{ key: "Format", title: "Format", component: FormatSettings },
{ key: "Style", title: "Style", component: StyleSettings },
]);
8 changes: 8 additions & 0 deletions client/app/visualizations/map/getOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ const DEFAULT_OPTIONS = {
backgroundColor: "#356AFF",
borderColor: "#356AFF",
bounds: null,
tooltip: {
enabled: false,
template: "",
},
popup: {
enabled: true,
template: "",
},
};

export default function getOptions(options) {
Expand Down
30 changes: 24 additions & 6 deletions client/app/visualizations/map/initMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import markerIconRetina from "leaflet/dist/images/marker-icon-2x.png";
import markerShadow from "leaflet/dist/images/marker-shadow.png";
import "leaflet-fullscreen";
import "leaflet-fullscreen/dist/leaflet.fullscreen.css";
import { formatSimpleTemplate } from "@/lib/value-format";
import { $sanitize } from "@/services/ng";
import resizeObserver from "@/services/resizeObserver";
import chooseTextColorForBackground from "@/lib/chooseTextColorForBackground";

Expand Down Expand Up @@ -107,12 +109,28 @@ function createMarkersLayer(options, { color, points }) {
}
}

marker.bindPopup(`
<ul style="list-style-type: none; padding-left: 0">
<li><strong>${lat}, ${lon}</strong>
${map(row, (v, k) => `<li>${k}: ${v}</li>`).join("")}
</ul>
`);
if (options.tooltip.enabled) {
if (options.tooltip.template !== "") {
marker.bindTooltip($sanitize(formatSimpleTemplate(options.tooltip.template, row)));
} else {
marker.bindTooltip(`
<strong>${lat}, ${lon}</strong>
`);
}
}

if (options.popup.enabled) {
if (options.popup.template !== "") {
marker.bindPopup($sanitize(formatSimpleTemplate(options.popup.template, row)));
} else {
marker.bindPopup(`
<ul style="list-style-type: none; padding-left: 0">
<li><strong>${lat}, ${lon}</strong>
${map(row, (v, k) => `<li>${k}: ${v}</li>`).join("")}
</ul>
`);
}
}
result.addLayer(marker);
});

Expand Down