HoloMap.Layer.Base (HoloMap v0.3.0)

Copy Markdown View Source

The shared body of every layer component.

MapLibre's ten layer types differ mostly in which paint and layout properties they accept. The specification shape, the update rules and the event surface are the same. Rather than repeat that ten times, each layer component is one use away from the whole thing:

defmodule HoloMap.Layer.Fill do
  use HoloMap.Layer.Base, type: "fill"
end

Passing no :type produces HoloMap.Layer, where the caller supplies it.

A background layer is the one that draws no source: it fills the whole map. source: false leaves out the props that only a layer drawing a source can use, source, source_layer and filter, and the event props: MapLibre finds no feature on such a layer, so it reports no event for it.

How the type is fixed

A type-specific component does not declare a type prop at all. The literal is baked into its template when the macro expands. That is deliberate: a values: ["fill"] constraint would only be checked while rendering, and this way there is nothing to check, because there is no way to pass a type in.

What the injected component does

It renders one definition element whose data-hm-spec is a MapLibre layer object. When a prop changes, the reconciler picks the narrowest update available: setPaintProperty for a paint change, setLayoutProperty for layout, setFilter for a filter, setLayerZoomRange for a zoom range, moveLayer for before_id. Only a change to type, source or source_layer, none of which MapLibre can change in place, forces a remove-and-re-add, and the layer goes back to its place in the stack.

Summary

Functions

Injects the props, template and specification builder of a layer component.

Builds the JSON layer specification for a definition element.

Encodes a layer's data-hm-events: its event handlers, as HoloMap.Def.events/1 encodes them, and "expand_cluster_on_click": true when the layer has the runtime expand a clicked cluster, which needs no round trip to an action. A change to either rebinds the layer's listeners.

Functions

__using__(opts)

(macro)

Injects the props, template and specification builder of a layer component.

Options

  • :type, the MapLibre layer type this component is locked to. When given, the type is a literal in the generated template and there is no type prop. When omitted, type becomes a required prop.
  • :source, false for a layer type that draws no source. The component then has no source, source_layer, filter, event or expand_cluster_on_click prop. Defaults to true.

build(props)

@spec build(map()) :: String.t()

Builds the JSON layer specification for a definition element.

Paint and layout maps are dasherized (fill_color becomes "fill-color"), the visible prop is folded into layout.visibility, and empty paint or layout maps are dropped so an untouched layer keeps MapLibre's defaults.

iex> HoloMap.Layer.Base.build(%{type: "fill", source: "parcels", paint: %{fill_color: "#f00"},
...>   layout: %{}, filter: nil, min_zoom: nil, max_zoom: nil, before_id: nil,
...>   visible: nil, metadata: nil, source_layer: nil})
~s({"paint":{"fill-color":"#f00"},"source":"parcels","type":"fill"})

events(handlers, expand_cluster_on_click)

@spec events(
  keyword(),
  boolean() | String.t() | nil
) :: String.t()

Encodes a layer's data-hm-events: its event handlers, as HoloMap.Def.events/1 encodes them, and "expand_cluster_on_click": true when the layer has the runtime expand a clicked cluster, which needs no round trip to an action. A change to either rebinds the layer's listeners.

iex> HoloMap.Layer.Base.events([click: :picked], "")
~s({"click":{"action":"picked","params":{},"target":"page"},"expand_cluster_on_click":true})

iex> HoloMap.Layer.Base.events([click: nil], false)
"{}"