HoloMap.Control.Custom (HoloMap v0.4.0)

Copy Markdown View Source

A control the application builds in JavaScript.

<HoloMap.Control.Custom factory="legend" position="bottom-left" options={%{title: "Zones"}} />

MapLibre takes a control as an object with onAdd(map), which returns the control's element, and onRemove(map), MapLibre's IControl. No prop can carry one, so the application registers a function that builds it, under a name, from a script its layout loads, and factory names it:

(window.HoloMap.registry ||= []).push((register) => {
  register.control("legend", (options, { map, cid, id }) => ({
    onAdd() {
      this.element = document.createElement("div");
      this.element.className = "maplibregl-ctrl maplibregl-ctrl-group legend";
      this.element.textContent = options.title;
      return this.element;
    },
    update(options) {
      this.element.textContent = options.title;
    },
    onRemove() {
      this.element.remove();
    },
  }));
});

The Registering JavaScript guide shows how to load that script with Hologram.

Props

PropTypeNotes
factorystringRequired. The name the factory is registered under with register.control
optionsmapHanded to the factory, and to update. Sent as written: show_counts stays show_counts
positionstringCorner to place the control in. Default "top-right"
idstringTells two controls of one factory in one corner apart. Default the factory's name

options crosses into the browser as JSON, so it holds what JSON can: maps, lists, strings, numbers, booleans and nil.

Lifecycle

The control is added once the map is built, as the built-in controls are, and keeps its place in its corner among them. The factory is called with a copy of options and %{map, cid, id}, and MapLibre then calls the control's onAdd. Removing the component calls onRemove, and takes the control's element out of the page even if onRemove leaves it.

An options change calls the control's update(options), when it has one, and leaves the control in place. A control without update, and a factory change, are rebuilt: the old control's onRemove, then the factory again. A position or id change is a new control. The control stays through a style change.

A factory nothing has registered yet leaves the control off the map, with a console warning once for each name on each map, and the control is added as soon as one is registered. A factory that throws, or returns something that is not a control, and a control whose onAdd, update or onRemove throws are reported through the map's on_error, or the browser console without one. The rest of the map is not affected.

Summary

Functions

Returns true to indicate that the callee module is a component module (has "use Hologram.Component" directive).

Returns the list of property definitions for the compiled component.

Functions

__is_hologram_component__()

@spec __is_hologram_component__() :: boolean()

Returns true to indicate that the callee module is a component module (has "use Hologram.Component" directive).

Examples

iex> __is_hologram_component__()
true

__props__()

@spec __props__() :: [{atom(), atom(), keyword()}]

Returns the list of property definitions for the compiled component.