# `Visualize.Chart.Builder`
[🔗](https://github.com/dcoai/Visualize/blob/v0.8.3/lib/visualize/chart/builder.ex#L2)

An embeddable editor of typed fragments (spec/14 §18, §19).

A `Phoenix.LiveComponent`, not a `Phoenix.LiveView`: a LiveView owns a route and a
session, and this embeds in whatever page a host already has.

    <.live_component module={Visualize.Chart.Builder} id="builder"
                     layers={@layers} sources={@sources} on_save={:design_saved} />

The host owns the route, the layers the builder starts with, the pool of sample sources
the preview binds to, the library of saved fragments (`Visualize.Chart.Builder.Store`)
and what saving means. The builder owns the editing state alone: a **stack of use
sites** (§19.3) — each a fragment used at a position, with what is masked, bound and
added there — and, when a library entry is opened, that entry's fragment. The one thing
that leaves it is a message: saving sends `{on_save, id, structure}` to the host
LiveView process, where `structure` is the stack as a composite (§19.6); a host that
wants a flat design flattens it with `Visualize.Chart.flatten/2`. It pushes no event
and patches no URL (D-85).

## Assigns

* `id` - the component id (required)
* `layers` - the fragments the stack starts with, in precedence order, the higher
  later: a design map, a `Visualize.Chart` or a `{name, layer}` pair (default `[]`).
  Each is seeded as an inline-only use site (§19.9).
* `sources` - the pool of sample sources the preview binds the design's slots to, as
  `Visualize.Chart.apply/2` takes it (default `%{}`)
* `vars` - the values the preview applies the design's variables with (default `%{}`)
* `theme` - a `Visualize.Theme` for the preview, `nil` for the design's own
* `store` - a module implementing `Visualize.Chart.Builder.Store`, or `{module, context}`
  with what the host wants every callback told (spec/14 §19.7), or `nil` for no
  library (default `nil`)
* `on_save` - the tag of the message saving sends (default `:visualize_chart_saved`)
* `catalog` - the connector catalog of the board mode: each feed the host offers to its
  `Visualize.Data.Schema`, or `nil` to bind a board's slots to the sample pool
  (spec/14 §22.8)
* `board_bindings` - a `Visualize.Board.Binding` the host holds, by the board entry's id
* `virtual_sources` - the modules of the host's virtual-source functions
  (`use Visualize.VirtualSources`) a board's sources may name, beside
  `config :visualize, virtual_sources:` (default `[]`, spec/14 §22.9)
* `on_bind` - the tag of the binding message, `{on_bind, id, board_id, binding}`, sent
  on every change of a board's binding; the binding is never stored in the board
  (default `:visualize_board_bound`)
* `styles` - render the stylesheet in a `<style>` element (default `true`); `false` when
  the host ships `Visualize.Chart.Builder.css/0` through its own asset pipeline
* `class` - appended to the builder's root class

## Handling the message

    def handle_info({:design_saved, "builder", structure}, socket) do
      {:ok, design, _missing} = Visualize.Chart.flatten(structure, &MyStore.fetch/1)
      {:noreply, assign(socket, design: design)}
    end

This module compiles only when `Phoenix.Component` is loaded: `phoenix_live_view` is an
optional dependency, so an application that only renders predefined charts never
compiles a line of the builder, which `scripts/consumer_check.exs` proves on every
pipeline (spec/10 §1.1, D-45, D-85).

# `css`

```elixir
@spec css() :: binary()
```

Returns the builder's stylesheet as a binary (spec/14 §18.13).

The builder renders this itself unless the host passes `styles={false}`;
`Visualize.Chart.Builder.Styles.install!/0` writes it beside the hooks for a host that
would rather own the file.

# `mount`

## Attributes

* `id` (`:string`) (required) - the component id.
* `layers` (`:list`) - the fragments the stack starts with, the higher later. Defaults to `[]`.
* `sources` (`:map`) - the pool the preview binds the design's slots to. Defaults to `%{}`.
* `vars` (`:map`) - the values the preview applies the variables with. Defaults to `%{}`.
* `theme` (`:any`) - a `Visualize.Theme` for the preview. Defaults to `nil`.
* `source_defaults` (`:boolean`) - whether a source's `default` is offered in the form (spec/14 §18.1, §19.10). Defaults to `true`.
* `tick_ms` (`:integer`) - the period of the signals' tick while playing (spec/14 §19.10). Defaults to `500`.
* `store` (`:any`) - a `Visualize.Chart.Builder.Store`, or `{module, context}`. Defaults to `nil`.
* `on_save` (`:any`) - the tag of the save message. Defaults to `:visualize_chart_saved`.
* `on_deploy` (`:any`) - the tag of the deploy message: the flat design, where save's carries structure. Defaults to `:visualize_chart_deployed`.
* `catalog` (`:map`) - the connector catalog: each feed the host offers to its `Visualize.Data.Schema`; `nil` binds a board's slots to the sample pool (spec/14 §22.8). Defaults to `nil`.
* `board_bindings` (`:map`) - a `Visualize.Board.Binding` the host holds, by the board entry's id (spec/14 §22.8). Defaults to `%{}`.
* `virtual_sources` (`:list`) - the modules of the host's virtual-source functions a board's sources may name (`use Visualize.VirtualSources`), beside the configuration's (spec/14 §22.9). Defaults to `[]`.
* `on_bind` (`:any`) - the tag of the binding message, `{on_bind, id, board_id, binding}`. Defaults to `:visualize_board_bound`.
* `styles` (`:boolean`) - render the stylesheet, `false` if the host ships it. Defaults to `true`.
* `class` (`:string`) - appended to the builder's root class. Defaults to `nil`.
* `library_folded` (`:any`) - the library's top-level headers folded at the start, a set of names; a person's folds are the builder's own after that (spec/14 §18.16, #355).

# `styles`

Renders the stylesheet in a `<style>` element (spec/14 §18.13).

# `tick`

```elixir
@spec tick(String.t(), Phoenix.LiveView.Socket.t()) :: Phoenix.LiveView.Socket.t()
```

The host's one line for the signals' tick (spec/14 §18.2): forwards the message the
builder scheduled to itself, `{:visualize_chart_tick, id}`, as an update.

    def handle_info({:visualize_chart_tick, id}, socket),
      do: {:noreply, Visualize.Chart.Builder.tick(id, socket)}

---

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