# `Macaw.Frame.Events`
[🔗](https://github.com/dcoai/macaw_frame/blob/v0.1.0/lib/macaw/frame/events.ex#L1)

The Frame's events: each built as one `Macaw.Data.Event` with its element, identity and
value (spec/01 §5.3), and what the server writes into the drawing so a hook can send a
pick-like event with no resolution against rows (§4.4, §5.3; spec/10 §7.1, §7.3).
Copied from visualize's `Visualize.Event` (`element/2`, `declared_identity/2`, the
declared names); its click decoding and `resolve/3` against a drawing token are not
carried over, because a Macaw event names its item by identity.

| Event | `element` | `identity` | `value` |
|---|---|---|---|
| `"pick"` | the item's frame and its layer's id (else its kind) | the row's declared identity, else `nil` | the row's declared fields as `[field, value]` pairs, `[]` with none |
| `"pick"` of nothing | the frame | `nil` | `[]` |
| `"brush"`, `"zoom"` | the frame | `nil` | `%{from: from, to: to}` |
| `"reset"` | the frame | `nil` | `nil` |
| `"view"` | the geographic frame | `nil` | `%{box: %{west, south, east, north}}` |
| a layer's declared event | as `"pick"` | as `"pick"` | as `"pick"` |

Every event is built through `Macaw.Data.Event.new/1` with the context's instance, its
`now` (the holder's) and `initiator: :visitor`. **Every event but `"brush"`, `"zoom"`,
`"reset"` and `"view"` carries a list value**, so the core's rule (an event with no
identity and a `nil` or map value writes `window`) never reads a pick as a window.

**Identities are written into the drawing** (`write/3`): each element a pickable layer
draws from a row carries `data-mw-item` (its key in the drawing, the hooks' own, never
sent), `data-mw-value` (the row's declared fields, JSON pairs) and `data-mw-identity`
(the row's declared identity as JSON, absent where the slot declares none). A layer is
pickable when the Frame picks (`interaction` enables `pick`) or it declares an event.
`items/1` reads them back from a drawing, and `click/3` builds the event a click on one
emits, as the hook sends it on the event wire (macaw_component spec/02 §8.3).

A row's fields are named by the source's fields, read through the cell's binding of the
slot's roles, so a selection names the fields every component sharing it reads.

# `item`

```elixir
@type item() :: %{
  item: String.t(),
  frame: String.t(),
  layer: non_neg_integer(),
  identity: map() | nil,
  value: list()
}
```

A drawn item, as `items/1` reads it from a drawing.

# `brush`

```elixir
@spec brush(Macaw.Component.Context.t(), String.t(), term(), term()) ::
  {:ok, Macaw.Data.Event.t()} | {:error, list()}
```

A finished brush over `frame` (§5.3): the band `%{from, to}` in the `x` scale's domain.

# `build`

```elixir
@spec build(Macaw.Component.Context.t(), String.t(), keyword()) ::
  {:ok, Macaw.Data.Event.t()} | {:error, list()}
```

One event named `name` in `context` (§5.3): `{:ok, %Macaw.Data.Event{}}` with the
context's instance, its `now` and `initiator: :visitor`, or the faults
`Macaw.Data.Event.new/1` answers. Options: `element:` (`%{frame, item, path}`),
`identity:`, `value:`, `point:`, `modifiers:` and `row:`.

# `click`

```elixir
@spec click(map(), String.t(), keyword()) ::
  {:ok, Macaw.Data.Event.t()} | {:error, list()} | :error
```

The event a click on the drawn item `key` of `value`'s drawing emits (§5.3), as its hook
sends it: the layer's declared event, else `"pick"` where the Frame picks, with the
item's element, identity and value. `:error` for a key the drawing does not hold or an
item no declared event names. `opts` are `build/3`'s (`modifiers:`, `point:`).

# `element`

```elixir
@spec element(String.t() | nil, map()) :: Macaw.Data.Event.element()
```

An event's `element` (§5.3): `%{frame, item, path}`, the frame's name and the layer's
`id`, else its kind, and `path` the item out to the frame, `[item]`, or `[]` for none.

    iex> Macaw.Frame.Events.element("main", %{"kind" => "symbol", "id" => "points"})
    %{frame: "main", item: "points", path: ["points"]}
    iex> Macaw.Frame.Events.element("main", %{kind: "line"})
    %{frame: "main", item: "line", path: ["line"]}

# `event`

```elixir
@spec event(map(), map()) :: String.t() | nil
```

The event a click on a compiled layer's item emits: its declared event, else `"pick"`
where the Frame picks, else `nil`.

# `fields`

```elixir
@spec fields(map(), Macaw.Data.Schema.t() | nil) :: [[term()]]
```

A row's declared fields as `[field, value]` pairs (§5.3), in the schema's field order
(sorted by name), each field the row holds; `[]` with no schema.

# `gesture`

```elixir
@spec gesture(map(), String.t(), map()) :: {:ok, Macaw.Data.Event.t()} | :none
```

What a finished gesture on `frame` of `value`'s drawing emits (spec/05 §6), as its hook
decides it on the client: `{:ok, event}` or `:none`. The gesture is the frame model's
(`Macaw.Frame.Writes`): `kind` (`:brush`, `:zoom`, `:pan`, `:reset`, `:pick`, `:view`)
and its domain values (`from`, `to`) or a view's projection (`scale`, `translate`); the
context's instance and now, and the served window of the frame's slot (`served`, what
a zoom compares with), are given here. A frame the drawing does not hold is `:none`.

- a brush under five pixels emits nothing, so a click on the plot keeps the window
  (§6.1); a zoom within the served window at half its span or more emits nothing, the
  client's transform of what is drawn (§6.3); a pan or zoom of a geographic frame emits
  `"view"` with its box (§6.4).

# `identity`

```elixir
@spec identity(map(), Macaw.Data.Schema.t() | nil) :: map() | nil
```

A row's declared identity (§5.3, macaw_data spec/01 §8.1): `%{field => value}` of the
fields `schema` declares `identity`, or `nil` when it declares none, there is no schema,
or the row lacks one. It never infers one.

    iex> schema = Macaw.Data.Schema.new!(%{"host" => %{type: :category, identity: true}, "v" => %{type: :number}})
    iex> Macaw.Frame.Events.identity(%{"host" => "h1", "v" => 3}, schema)
    %{"host" => "h1"}
    iex> Macaw.Frame.Events.identity(%{"v" => 3}, Macaw.Data.Schema.new!(%{"v" => %{type: :number}}))
    nil

# `items`

```elixir
@spec items(map() | nil) :: [item()]
```

The items a drawing's scene carries (§5.3), as `write/3` wrote them, in drawing order:
each `%{item, frame, layer, identity, value}`, its identity and value as the wire
carries them (a time as its UTC ISO 8601 spelling). A drawing's canvases are drawn
beneath its SVG (spec/10 §2.6), so their items come first.

# `json`

```elixir
@spec json(term()) :: String.t()
```

A term as the event wire spells it (macaw_component spec/02 §8.3), JSON: a `DateTime`
as its ISO 8601 spelling in UTC, a `Date` or `NaiveDateTime` as its ISO 8601, a point
`{lon, lat}` as `[lon, lat]`, a literal color as its CSS hex, an atom as its name, and a
term with no wire form as `null`.

    iex> Macaw.Frame.Events.json([["at", ~U[2026-01-01 00:00:00Z]], ["v", 2]])
    ~s([["at","2026-01-01T00:00:00Z"],["v",2]])

# `nothing`

```elixir
@spec nothing(Macaw.Component.Context.t(), String.t()) ::
  {:ok, Macaw.Data.Event.t()} | {:error, list()}
```

A pick of nothing in `frame` (§5.3): identity `nil`, value `[]`.

# `pick`

```elixir
@spec pick(Macaw.Component.Context.t(), map(), keyword()) ::
  {:ok, Macaw.Data.Event.t()} | {:error, list()}
```

The pick of an item (§5.3), or the event its layer declares under `name:`: its element
the item's frame and layer, its identity and value as the drawing wrote them.

# `pickable?`

```elixir
@spec pickable?(map(), map()) :: boolean()
```

Whether a compiled layer's items are pickable: the Frame picks, or the layer declares an event.

# `reset`

```elixir
@spec reset(Macaw.Component.Context.t(), String.t()) ::
  {:ok, Macaw.Data.Event.t()} | {:error, list()}
```

A double-click on `frame`'s plot (§5.3): value `nil`, which clears `window`.

# `source`

```elixir
@spec source(map(), map()) :: map()
```

A row read through a cell's binding of its slot's roles (`%{role => field}`), keyed by
the source's fields: each bound role's value under its field, every other key as it is.

# `view`

```elixir
@spec view(Macaw.Component.Context.t(), String.t(), map()) ::
  {:ok, Macaw.Data.Event.t()} | {:error, list()}
```

A finished geographic pan or zoom of `frame` (§5.3): `%{box: box}` in degrees.

# `window_writers`

```elixir
@spec window_writers() :: [String.t(), ...]
```

The Frame's events whose value may be a window, never a list: brush, zoom, reset and view.

# `write`

```elixir
@spec write(Macaw.Draw.IR.Element.t(), Macaw.Data.Table.t(), map()) ::
  Macaw.Draw.IR.Element.t()
```

A pickable layer's drawn `group` with its items' identities written (§5.3): every
element carrying `data-row` gains `data-mw-item` (`<frame>.<layer>.<row>`),
`data-mw-value` and, where the slot declares one, `data-mw-identity`, read from the row
of `table` (the rows it was drawn from) at that index. `about` holds `frame`, `layer`
(the layer's index), `schema` and `bindings` (the slot's).

**It is written by column** (spec/12 §5.1): each declared field's column is read once
(`Macaw.Frame.Columns.read/3`), each distinct value of it spelled for the wire once, and
a row's two texts are put together from those spellings, so the cost per item is a
join, never a row map, a sort of its fields and an encoding of each of them. The
texts are those `fields/2`, `identity/2` and `json/1` give the row, byte for byte.

# `zoom`

```elixir
@spec zoom(Macaw.Component.Context.t(), String.t(), term(), term()) ::
  {:ok, Macaw.Data.Event.t()} | {:error, list()}
```

A zoom of `frame` that needs more detail (§5.3): the zoomed `%{from, to}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
