Class: Sirens::List

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

Instance Method Summary collapse

Methods inherited from PrimitiveComponent

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

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

#create_viewObject

Returns a ListView.



6
7
8
9
10
11
12
# File 'lib/components/widgets/list.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.rb', line 28

def default_model()
    ListModel.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.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_selection_changed(selection_items:, selection_indices:) ⇒ Object

Events



58
59
60
61
62
63
64
65
66
# File 'lib/components/widgets/list.rb', line 58

def on_selection_changed(selection_items:, selection_indices:)
    return if props[:on_selection_changed].nil?

    props[:on_selection_changed].call(
        selection: selection_items,
        indices: selection_indices,
        list: self
    )
end

#on_value_changed(announcement) ⇒ Object

Hook method called when the model value changes.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/components/widgets/list.rb', line 35

def on_value_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

#sync_ui_from_modelObject



49
50
51
52
53
54
# File 'lib/components/widgets/list.rb', line 49

def sync_ui_from_model()
    return if view.nil?

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