Class: Sirens::TreeChoice

Inherits:
PrimitiveComponent show all
Defined in:
lib/components/widgets/tree_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

#create_viewObject

Returns a WindowView.



6
7
8
9
10
11
12
13
# File 'lib/components/widgets/tree_choice.rb', line 6

def create_view()
    TreeView.new
        .on_selection_changed_block { |selection_items:, selection_paths:|
                on_selection_changed(selection_items: selection_items, selection_paths: selection_paths)
            }
        .get_item_block { |path:| model.item_at(path: path) }
        .get_children_block { |path:| model.children_at(path: path) }
end

#default_modelObject

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



31
32
33
# File 'lib/components/widgets/tree_choice.rb', line 31

def default_model()
    TreeChoiceModel.new(roots: [], get_children_block: nil)
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.

Raises:

  • (RuntimeError)


19
20
21
22
23
24
25
26
# File 'lib/components/widgets/tree_choice.rb', line 19

def define_columns(columns_props_array)
    raise RuntimeError.new("The #{self.class.name} must have at least one column.") if columns_props_array.empty?

    view.define_columns(columns_props_array)

    # Sync again after adding the columns.
    sync_ui_from_model
end

#on_selected_value_changed(announcement) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/components/widgets/tree_choice.rb', line 63

def on_selected_value_changed(announcement)
    selection_hierarchy = announcement.new_value

    indices_path = model.path_of(selection_hierarchy)

    view.set_selection_indices(indices_path)
end

#on_selection_changed(selection_items:, selection_paths:) ⇒ Object



71
72
73
# File 'lib/components/widgets/tree_choice.rb', line 71

def on_selection_changed(selection_items:, selection_paths:)
    model.set_selection_from_path(path: selection_paths.first) unless model.nil?
end

#on_tree_changed(announcement) ⇒ Object



59
60
61
# File 'lib/components/widgets/tree_choice.rb', line 59

def on_tree_changed(announcement)
    sync_ui_from_model
end

#root_itemsObject

Returns the root items of the tree.



38
39
40
# File 'lib/components/widgets/tree_choice.rb', line 38

def root_items()
    model.tree.roots
end

#subscribe_to_model_eventsObject

Subscribes this component to the model events



54
55
56
57
# File 'lib/components/widgets/tree_choice.rb', line 54

def subscribe_to_model_events()
    model.tree.add_observer(self, :on_tree_changed)
    model.selection.add_observer(self, :on_selected_value_changed)
end

#sync_ui_from_modelObject

Syncs the ui from the model.



45
46
47
# File 'lib/components/widgets/tree_choice.rb', line 45

def sync_ui_from_model()
    view.set_roots(model.tree.roots)
end