Class: TkComponent::BrowserColumnComponent

Inherits:
Base
  • Object
show all
Defined in:
lib/tk_component/components/browser_column_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 = {}) ⇒ BrowserColumnComponent

Returns a new instance of BrowserColumnComponent.



7
8
9
10
11
# File 'lib/tk_component/components/browser_column_component.rb', line 7

def initialize(options = {})
  super
  @browser = options[:browser]
  @column_index = options[:column_index] || 0
end

Instance Attribute Details

#browserObject

Returns the value of attribute browser.



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

def browser
  @browser
end

#column_indexObject

Returns the value of attribute column_index.



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

def column_index
  @column_index
end

Instance Method Details

#component_did_buildObject



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

def component_did_build
  show_current_selection
end

#render(p, parent_component) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tk_component/components/browser_column_component.rb', line 13

def render(p, parent_component)
  if @column_index <= @browser.selected_path.size
    current_item = @browser.selected_path[@column_index]
    path_so_far = @browser.selected_path.slice(0, @column_index)
    items = @browser.data_source.items_for_path(path_so_far)
    items ||= []
  else
    items = []
    current_item = nil
  end
  command = @browser.paned ? :hpaned : :hframe
  p.send(command, sticky: 'nsew', x_flex: 1, y_flex: 1) do |f|
    @tree = f.tree(sticky: 'nsew', x_flex: 1, y_flex: 1,
                   on_select: :select_item,
                   scrollers: 'y', heading: @browser.data_source.title_for_path(path_so_far, items)) do |t|
      items.each do |item|
        t.tree_node(at: 'end',
                    text: item,
                    selected: item == current_item)
      end
    end
    if (@browser.max_columns.blank? || @browser.max_columns > @column_index + 1) &&
       (@column_index < @browser.selected_path.size || items.present?)               
      f.hframe(sticky: 'nsew', x_flex: 1, y_flex: 1) do |hf|
        @next_column = hf.insert_component(TkComponent::BrowserColumnComponent, self,
                                           browser: @browser,
                                           column_index: @column_index + 1,
                                           sticky: 'nsew', x_flex: 1, y_flex: 1) do |bc|
          bc.on_event 'ItemSelected', ->(e) do
            emit('ItemSelected')
          end
        end
      end
    end
  end
end

#select_item(e) ⇒ Object



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

def select_item(e)
  item = e.sender.native_item.selection&.first.text.to_s
  return if @browser.selected_path[@column_index] == item
  @browser.selected_path[@column_index] = item
  @browser.selected_path.slice!(@column_index + 1..-1) if @column_index < @browser.selected_path.size - 1
  puts "New selected path: #{@browser.selected_path}"
  @next_column&.regenerate
  emit('ItemSelected')
end

#show_current_selectionObject



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

def show_current_selection
  @tree.tk_item.scroll_to_selection
end