Plushie.Selection (Plushie v0.6.0)

Copy Markdown View Source

Selection state for lists and tables. Pure data structure supporting single, multi, and range selection modes.

Modes

  • :single -- at most one item selected at a time.
  • :multi -- multiple items selectable; extend: true adds to the set.
  • :range -- like multi, but range_select/2 selects a contiguous slice of the order list between the anchor and the target.

Example

sel = Plushie.Selection.new(mode: :multi, order: ["a", "b", "c", "d"])
sel = Plushie.Selection.select(sel, "b")
sel = Plushie.Selection.select(sel, "d", extend: true)
Plushie.Selection.selected(sel)
#=> MapSet.new(["b", "d"])

Summary

Functions

Clears all selected items and resets the anchor.

Removes id from the selection.

Creates a new selection state.

Selects all items in order between the current anchor and id (inclusive). If there is no anchor, selects only id.

Selects id. In :single mode, replaces the selection. In :multi and :range modes, replaces unless extend: true is passed, in which case id is added to the existing selection.

Selects all items in order.

Returns the MapSet of currently selected item IDs.

Returns true if id is currently selected.

Toggles id in the selection. If already selected, removes it; otherwise adds it. In :single mode, toggling a selected item clears the selection entirely.

Types

t()

@type t() :: %Plushie.Selection{
  anchor: term() | nil,
  mode: :single | :multi | :range,
  order: [term()],
  selected: MapSet.t()
}

Functions

clear(sel)

@spec clear(sel :: t()) :: t()

Clears all selected items and resets the anchor.

deselect(sel, id)

@spec deselect(sel :: t(), id :: term()) :: t()

Removes id from the selection.

new(opts \\ [])

@spec new(opts :: keyword()) :: t()

Creates a new selection state.

Options

  • :mode -- selection mode: :single (default), :multi, or :range.
  • :order -- ordered list of item IDs for range selection.

range_select(sel, id)

@spec range_select(sel :: t(), id :: term()) :: t()

Selects all items in order between the current anchor and id (inclusive). If there is no anchor, selects only id.

Requires order to have been set at creation time via new/1.

select(sel, id, opts \\ [])

@spec select(sel :: t(), id :: term(), opts :: keyword()) :: t()

Selects id. In :single mode, replaces the selection. In :multi and :range modes, replaces unless extend: true is passed, in which case id is added to the existing selection.

Sets the anchor to id for subsequent range selections.

select_all(sel)

@spec select_all(sel :: t()) :: t()

Selects all items in order.

Requires order to have been set at creation time via new/1.

selected(selection)

@spec selected(sel :: t()) :: MapSet.t()

Returns the MapSet of currently selected item IDs.

selected?(selection, id)

@spec selected?(sel :: t(), id :: term()) :: boolean()

Returns true if id is currently selected.

toggle(sel, id)

@spec toggle(sel :: t(), id :: term()) :: t()

Toggles id in the selection. If already selected, removes it; otherwise adds it. In :single mode, toggling a selected item clears the selection entirely.