class Sirens::ListChoice

Public Instance Methods

choices() click to toggle source

Returns the choices list

# File lib/components/widgets/list_choice.rb, line 35
def choices()
    model.choices
end
create_view() click to toggle source

Returns a WindowView.

# File lib/components/widgets/list_choice.rb, line 6
def create_view()
    ListView.new
        .on_selection_changed_block { |selection_items:, selection_indices:|
                on_selection_changed(selection_items: selection_items, selection_indices: selection_indices)
            }
        .get_item_block { |index| model.item_at(index: index) }
end
default_model() click to toggle source

Returns a default model if none is given during the initialization of this component.

# File lib/components/widgets/list_choice.rb, line 28
def default_model()
    ChoiceModel.new
end
define_columns(columns_props_array) click to toggle source

Defines the columns in the list with the given columns_props. column_props is an Array of props Hashes, one Hash for each column to define.

# File lib/components/widgets/list_choice.rb, line 18
def define_columns(columns_props_array)
    view.define_columns(columns_props_array)

    # Sync again after adding the columns.
    sync_ui_from_model
end
on_choices_changed(announcement) click to toggle source

Method called when the choices list changes in the model.

# File lib/components/widgets/list_choice.rb, line 50
def on_choices_changed(announcement)
    if announcement.kind_of?(ListChanged)
        sync_ui_from_model
    elsif announcement.kind_of?(ItemsAdded)
        view.add_items(items: announcement.items, index: announcement.index)
    elsif announcement.kind_of?(ItemsUpdated)
        view.update_items(items: announcement.items, indices: announcement.indices)
    elsif announcement.kind_of?(ItemsRemoved)
        view.remove_items(items: announcement.items, indices: announcement.indices)
    else
        raise RuntimeError.new("Unknown announcement #{announcement.class.name}.")
    end
end
on_selected_value_changed(announcement) click to toggle source

Method called when the selected choice changes in the model.

# File lib/components/widgets/list_choice.rb, line 67
def on_selected_value_changed(announcement)
    selected_value = announcement.new_value

    selection = choices.list.index(selected_value)

    selection.nil? ?
        view.set_selection_indices([]) : view.set_selection_indices([selection])
end
on_selection_changed(selection_items:, selection_indices:) click to toggle source

Events

# File lib/components/widgets/list_choice.rb, line 85
def on_selection_changed(selection_items:, selection_indices:)
    model.set_selection(selection_items.first) unless model.nil?
end
subscribe_to_model_events() click to toggle source

Subscribes this component to the model events

# File lib/components/widgets/list_choice.rb, line 42
def subscribe_to_model_events()
    model.choices.add_observer(self, :on_choices_changed)
    model.selection.add_observer(self, :on_selected_value_changed)
end
sync_ui_from_model() click to toggle source
# File lib/components/widgets/list_choice.rb, line 76
def sync_ui_from_model()
    return if view.nil?

    view.clear_items
    view.add_items(items: choices.list, index: 0)
end