Class: Sirens::ListChoice

Inherits:
PrimitiveComponent show all
Defined in:
lib/components/widgets/list_choice.rb

Instance Method Summary collapse

Methods inherited from PrimitiveComponent

#apply_props, #initialize, #on_component_added, #on_model_changed, #on_value_changed, #populate_popup_menu, #set_props

Methods inherited from AbstractComponent

#add_all_components, #add_component, #child_components, #initialize, #model, #on_component_added, #on_model_changed, open, #props, #remove_component_at, #remove_last_component, #set_model, #set_props, #view

Constructor Details

This class inherits a constructor from Sirens::PrimitiveComponent

Instance Method Details

#choicesObject

Returns the choices list



35
36
37
# File 'lib/components/widgets/list_choice.rb', line 35

def choices()
    model.choices
end

#create_viewObject

Returns a WindowView.



6
7
8
9
10
11
12
# 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_modelObject

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



28
29
30
# File 'lib/components/widgets/list_choice.rb', line 28

def default_model()
    ChoiceModel.new
end

#define_columns(columns_props_array) ⇒ Object

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.



18
19
20
21
22
23
# 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) ⇒ Object

Method called when the choices list changes in the model.



50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object

Method called when the selected choice changes in the model.



67
68
69
70
71
72
73
74
# 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:) ⇒ Object

Events



85
86
87
# 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_eventsObject

Subscribes this component to the model events



42
43
44
45
# 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_modelObject



76
77
78
79
80
81
# 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