Class: Plushie::Selection
- Inherits:
-
Object
- Object
- Plushie::Selection
- Defined in:
- lib/plushie/selection.rb,
sig/plushie/selection.rbs
Overview
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: trueadds to the set.:range: like multi, butrange_selectselects a contiguous slice of theorderlist between the anchor and the target.
Defined Under Namespace
Classes: State
Class Method Summary collapse
-
.clear(sel) ⇒ State
Clears all selected items and resets the anchor.
-
.deselect(sel, id) ⇒ State
Removes
idfrom the selection. -
.new(mode: :single, order: []) ⇒ State
Creates a new selection state.
-
.range_select(sel, id) ⇒ State
Selects all items in
orderbetween the current anchor andid(inclusive). -
.select(sel, id, extend: false) ⇒ State
Selects
id. -
.selected(sel) ⇒ Set
Returns the Set of currently selected item IDs.
-
.selected?(sel, id) ⇒ Boolean
Returns true if
idis currently selected. -
.toggle(sel, id) ⇒ State
Toggles
idin the selection.
Class Method Details
.clear(sel) ⇒ State
Clears all selected items and resets the anchor.
89 90 91 |
# File 'lib/plushie/selection.rb', line 89 def self.clear(sel) sel.with(selected: Set.new, anchor: nil) end |
.deselect(sel, id) ⇒ State
Removes id from the selection.
81 82 83 |
# File 'lib/plushie/selection.rb', line 81 def self.deselect(sel, id) sel.with(selected: sel.selected - Set[id]) end |
.new(mode: :single, order: []) ⇒ State
Creates a new selection state.
31 32 33 |
# File 'lib/plushie/selection.rb', line 31 def self.new(mode: :single, order: []) State.new(mode: mode, selected: Set.new, anchor: nil, order: order) end |
.range_select(sel, id) ⇒ State
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.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/plushie/selection.rb', line 101 def self.range_select(sel, id) if sel.anchor.nil? return sel.with(selected: Set[id], anchor: id) end anchor_idx = sel.order.index(sel.anchor) id_idx = sel.order.index(id) if anchor_idx.nil? || id_idx.nil? return sel.with(selected: Set[id], anchor: id) end lo, hi = [anchor_idx, id_idx].sort range_ids = sel.order[lo..hi] #: Array[untyped] sel.with(selected: Set.new(range_ids)) end |
.select(sel, id, extend: false) ⇒ State
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.
45 46 47 48 49 50 51 52 53 |
# File 'lib/plushie/selection.rb', line 45 def self.select(sel, id, extend: false) if sel.mode == :single sel.with(selected: Set[id], anchor: id) elsif extend sel.with(selected: sel.selected | Set[id], anchor: id) else sel.with(selected: Set[id], anchor: id) end end |
.selected(sel) ⇒ Set
Returns the Set of currently selected item IDs.
122 123 124 |
# File 'lib/plushie/selection.rb', line 122 def self.selected(sel) sel.selected end |
.selected?(sel, id) ⇒ Boolean
Returns true if id is currently selected.
131 132 133 |
# File 'lib/plushie/selection.rb', line 131 def self.selected?(sel, id) sel.selected.include?(id) end |
.toggle(sel, id) ⇒ State
Toggles id in the selection. If already selected, removes it;
otherwise adds it. In :single mode, toggling a selected item
clears the selection entirely.
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/plushie/selection.rb', line 62 def self.toggle(sel, id) if sel.mode == :single if sel.selected.include?(id) sel.with(selected: Set.new, anchor: nil) else sel.with(selected: Set[id], anchor: id) end elsif sel.selected.include?(id) sel.with(selected: sel.selected - Set[id]) else sel.with(selected: sel.selected | Set[id], anchor: id) end end |