Class: TkComponent::TableViewComponent

Inherits:
Base
  • Object
show all
Defined in:
lib/tk_component/components/table_view_component.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#children, #node, #parent, #parent_node, #tk_item

Instance Method Summary collapse

Methods inherited from Base

#add_child, #build, #emit, #generate, #name, #parse_component, #parse_nodes, #rebuild, #regenerate, #regenerate_after_node, #regenerate_from_index, #regenerate_from_node

Methods included from BasicComponent

#focus

Constructor Details

#initialize(options = {}) ⇒ TableViewComponent

Returns a new instance of TableViewComponent.



10
11
12
13
14
15
16
17
18
# File 'lib/tk_component/components/table_view_component.rb', line 10

def initialize(options = {})
  super
  @data_source = options[:data_source]
  @columns = options[:columns]
  @nested = !!options[:nested]
  @lazy = !!options[:lazy]
  @scrollers = options[:scrollers] || 'y'
  @to_load = {}
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



5
6
7
# File 'lib/tk_component/components/table_view_component.rb', line 5

def columns
  @columns
end

#data_sourceObject

Returns the value of attribute data_source.



4
5
6
# File 'lib/tk_component/components/table_view_component.rb', line 4

def data_source
  @data_source
end

#lazyObject

Returns the value of attribute lazy.



7
8
9
# File 'lib/tk_component/components/table_view_component.rb', line 7

def lazy
  @lazy
end

#nestedObject

Returns the value of attribute nested.



6
7
8
# File 'lib/tk_component/components/table_view_component.rb', line 6

def nested
  @nested
end

#scrollersObject

Returns the value of attribute scrollers.



8
9
10
# File 'lib/tk_component/components/table_view_component.rb', line 8

def scrollers
  @scrollers
end

Instance Method Details

#add_item(tree, item, parent_items = [], parent_item = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tk_component/components/table_view_component.rb', line 36

def add_item(tree, item, parent_items = [], parent_item = nil)
  tree_item = tree.native_item.insert(parent_item || '', 'end', item_to_options(item))
  return unless nested && ((sub_path = parent_items + [ item ]) && data_source.has_sub_items?(sub_path))
  if lazy
    dummy_item = tree.native_item.insert(tree_item, 'end')
    @to_load[tree_item] = sub_path
  else
    sub_items = data_source.items_for_path(sub_path)
    sub_items.each do |sub_item|
      add_item(tree, sub_item, sub_path, tree_item)
    end
  end
end

#component_did_buildObject



29
30
31
32
33
34
# File 'lib/tk_component/components/table_view_component.rb', line 29

def component_did_build
  items = data_source.items_for_path(nil)
  items.each do |item|
    add_item(@tree, item)
  end
end

#item_open(e) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tk_component/components/table_view_component.rb', line 65

def item_open(e)
  open_item = e.sender.native_item.focus_item
  return unless open_item.present?
  path = @to_load.delete(open_item)
  return unless path.present?
  @tree.native_item.delete(open_item.children)
  sub_items = data_source.items_for_path(path)
  sub_items.each do |sub_item|
    add_item(@tree, sub_item, path, open_item)
  end
end

#item_selected(e) ⇒ Object



77
78
# File 'lib/tk_component/components/table_view_component.rb', line 77

def item_selected(e)
end

#item_to_options(item) ⇒ Object



54
55
56
# File 'lib/tk_component/components/table_view_component.rb', line 54

def item_to_options(item)
  { text: item[text_key], values: item.slice(*values_keys).values }
end

#render(p, parent_component) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/tk_component/components/table_view_component.rb', line 20

def render(p, parent_component)
  @to_load = {}
  @tree = p.tree(sticky: 'nsew', x_flex: 1, y_flex: 1, scrollers: @scrollers,
                 column_defs: columns,
                 on_item_open: :item_open,
                 on_select: :item_selected
                )
end

#selected_itemObject



50
51
52
# File 'lib/tk_component/components/table_view_component.rb', line 50

def selected_item
  (tree_item = @tree.native_item.focus_item) && tree_item_to_item(tree_item)
end

#text_keyObject



80
81
82
# File 'lib/tk_component/components/table_view_component.rb', line 80

def text_key
  @text_key ||= columns.first[:key]
end

#tree_item_to_item(tree_item) ⇒ Object



58
59
60
61
62
63
# File 'lib/tk_component/components/table_view_component.rb', line 58

def tree_item_to_item(tree_item)
  columns.map.with_index do |c, i|
    [c[:key],
     i == 0 ? tree_item.text : tree_item.get(c[:key])]
  end.to_h
end

#values_keysObject



84
85
86
# File 'lib/tk_component/components/table_view_component.rb', line 84

def values_keys
  @values_keys ||= columns[1..-1].map { |c| c[:key] }
end