draw
EstablePermite al usuario dibujar y editar formas en el mapa, como geocercas, desde una barra de herramientas propia o los botones de la aplicación, las envía al LiveView como GeoJSON y muestra las formas que el LiveView le da.
El modo lo fija
Sin mode, la barra de herramientas del dibujo fija el modo en el navegador, y se lo dice al LiveView con draw:mode_changed.
Nueva geocerca
Empieza una forma en el mapa y aquí se abre un formulario para su nombre.
fill_color
@geofences
Elige una herramienta en la esquina superior izquierda del mapa. Un polígono lleva un clic por vértice y se cierra en el primero; un rectángulo o un círculo lleva dos clics. Con select, arrastra una geocerca o sus vértices, o el punto medio de un lado de un polígono para añadir un vértice, y pulsa Supr para quitarla.
- @set_by
- toolbar
- @mode
- nil
- @drawing
- nil
- @cancelled
- 0
- @fill_color
- #7c5cd6
- @geofences
- 2
- @next_id
- 3
<div id="draw-example">
<.example_frame error={@map_error} error_id="example-draw-map-error">
<.map
id="example-draw-map"
center={[4.8434, 45.7565]}
zoom={12.8}
style={basemap(@theme)}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- The drawing's own toolbar, in the map's top-left corner. No mode is
given: the toolbar sets it in the browser, and draw:mode_changed
tells the LiveView. Its labels are the page's, in its language.
draw:started and draw:cancelled open and close the form below. Its
hook comes from maplibrex/draw (assets/js/app.js). --%>
<.draw
:if={@set_by == "toolbar"}
id="example-draw"
map_id="example-draw-map"
modes={[:polygon, :rectangle, :circle, :select]}
features={@geofences}
fill_color={@fill_color}
fill_opacity={0.25}
outline_color={@fill_color}
outline_width={2}
toolbar="top-left"
toolbar_labels={toolbar_labels()}
events={[
"draw:created",
"draw:updated",
"draw:deleted",
"draw:started",
"draw:cancelled",
"draw:mode_changed"
]}
/>
<%!-- The LiveView sets the mode, from @mode: with the toolbar, a button
asks for its mode with draw:mode_changed; without it, the page's own
buttons below set it. The geofences are the truth for the ids they
name: an edit the LiveView puts back stays, a geofence taken out
goes. --%>
<.draw
:if={@set_by != "toolbar"}
id="example-draw"
map_id="example-draw-map"
mode={@mode}
modes={[:polygon, :rectangle, :circle, :select]}
features={@geofences}
fill_color={@fill_color}
fill_opacity={0.25}
outline_color={@fill_color}
outline_width={2}
toolbar={if(@set_by == "toolbar_mode", do: "top-left")}
toolbar_labels={toolbar_labels()}
events={[
"draw:created",
"draw:updated",
"draw:deleted",
"draw:started",
"draw:cancelled",
"draw:mode_changed"
]}
/>
<%!-- The page's own toolbar, when the drawing has none: each button sets
the mode, and the active one is tinted. --%>
<.control_group
:if={@set_by == "buttons"}
id="example-draw-tools"
map_id="example-draw-map"
position="top-left"
>
<.control_button
:for={mode <- draw_modes()}
id={"example-draw-tool-#{mode}"}
map_id="example-draw-map"
tooltip={tooltip(mode)}
active={@mode == mode}
class="grid! place-items-center"
phx-click="draw_mode"
phx-value-mode={mode}
>
<.tool_icon mode={mode} />
</.control_button>
</.control_group>
<div class="space-y-4 border-t border-line px-4 py-4">
<%!-- Who sets the mode. --%>
<.example_group id="example-draw-setter" label={gettext("The mode is set by")}>
<.example_button
:for={setter <- setters()}
id={"example-draw-setter-#{setter}"}
pressed={@set_by == setter}
phx-click="set_by"
phx-value-setter={setter}
>
{setter_label(setter)}
</.example_button>
<p id="example-draw-setter-note" class="w-full pt-1 text-label text-ink-muted">
{setter_note(@set_by)}
</p>
</.example_group>
<%!-- The new geofence: draw:started opens the form, draw:created and
draw:cancelled close it. --%>
<div
id="example-draw-draft"
role="group"
aria-labelledby="example-draw-draft-label"
class="min-w-0"
>
<p id="example-draw-draft-label" class="mb-2 text-label font-bold text-ink-subtle">
{gettext("New geofence")}
</p>
<p :if={!@draft} id="example-draw-draft-idle" class="text-ui text-ink-muted">
{gettext("Start a shape on the map, and a form for its name opens here.")}
</p>
<.form
:if={@draft}
for={%{}}
id="example-draw-draft-form"
phx-change="draft"
phx-submit="draft"
class="flex flex-wrap items-end gap-x-4 gap-y-2"
>
<.input
type="text"
id="example-draw-draft-name"
name="name"
value={@draft.name}
label={gettext("Name")}
autocomplete="off"
wrapper_class="w-56 text-label font-bold text-ink-subtle"
/>
<p class="pb-2 text-ui text-ink-muted">
{gettext("A %{mode} under way. Finish it on the map, or press Escape to give it up.",
mode: @draft.mode
)}
</p>
</.form>
</div>
<div class="grid gap-x-8 gap-y-4 lg:grid-cols-[auto_1fr]">
<.example_group id="example-draw-colors" label="fill_color" code>
<.example_button
:for={{name, hex} <- colors()}
id={"example-draw-color-#{name}"}
code
pressed={@fill_color == hex}
phx-click="fill_color"
phx-value-color={hex}
>
<.example_swatch color={hex} />{hex}
</.example_button>
</.example_group>
<%!-- @geofences as the LiveView stores them: id, mode and, for a
circle, its radius in metres. --%>
<.example_group id="example-draw-fences" label="@geofences" code>
<span
:for={fence <- @geofences}
id={"example-draw-fence-#{fence["id"]}"}
class="inline-flex h-8 items-center gap-1.5 rounded-ui border border-line px-2.5 font-mono text-label text-ink"
>
<span class="text-ink-subtle">{fence["id"]}</span>
<span :if={name(fence)} class="font-sans font-bold">{name(fence)}</span>
{kind(fence)}
<span :if={radius(fence)} class="text-ink-muted">{radius(fence)}</span>
</span>
</.example_group>
</div>
<.example_note id="example-draw-hint" icon="hero-cursor-arrow-rays-micro">
{gettext(
"Choose a tool in the map's top-left corner. A polygon takes a click per corner and closes on its first one; a rectangle or a circle takes two clicks. With select, drag a geofence or its corners, or the middle of a polygon's edge to add a corner, and press Delete to remove it."
)}
</.example_note>
</div>
<%!-- draw:created, draw:updated and draw:deleted keep @geofences current;
draw:started and draw:cancelled set drawing and count the shapes
given up. --%>
<.example_readout values={[
set_by: @set_by,
mode: @mode || "nil",
drawing: (@draft && @draft.mode) || "nil",
cancelled: @cancelled,
fill_color: @fill_color,
geofences: length(@geofences),
next_id: @next_id
]} />
</div>