Class: WhirledPeas::Component::ListWithActive

Inherits:
Object
  • Object
show all
Defined in:
lib/whirled_peas/component/list_with_active.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#active_indexObject

Returns the value of attribute active_index.



4
5
6
# File 'lib/whirled_peas/component/list_with_active.rb', line 4

def active_index
  @active_index
end

#flowObject



14
15
16
# File 'lib/whirled_peas/component/list_with_active.rb', line 14

def flow
  @flow || :l2r
end

#itemsObject

Returns the value of attribute items.



6
7
8
# File 'lib/whirled_peas/component/list_with_active.rb', line 6

def items
  @items
end

#separatorObject

Returns the value of attribute separator.



4
5
6
# File 'lib/whirled_peas/component/list_with_active.rb', line 4

def separator
  @separator
end

#viewport_sizeObject



28
29
30
# File 'lib/whirled_peas/component/list_with_active.rb', line 28

def viewport_size
  @viewport_size || total_size
end

Instance Method Details

#compose(composer, settings) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/whirled_peas/component/list_with_active.rb', line 32

def compose(composer, settings)
  %i[items].each do |required_attr|
    if send(required_attr).nil?
      raise ArgumentError, "Required field #{required_attr} missing"
    end
  end

  composer.add_box('ListWithActive') do |composer, settings|
    settings.flow = flow
    settings.align = :left
    settings.full_border
    curr_index = active_index || 0
    if flow == :l2r
      settings.width = viewport_size
      active_start = separator.nil? ? 0 : curr_index * separator.length
      items.first(curr_index).each do |item|
        active_start += item.length
      end
      curr_size = items[curr_index].length
    else
      settings.height = viewport_size
      active_start = curr_index + (separator.nil? ? 0 : curr_index)
      curr_size = 1
    end

    if viewport_size < total_size
      front_padding = (viewport_size - curr_size) * 0.667
      offset = (active_start - front_padding).round
      if offset < 0
        offset = 0
      elsif offset > total_size - viewport_size
        offset = total_size - viewport_size
      end

      if flow == :l2r
        settings.content_start.left = -offset
        settings.scrollbar.horiz = true
      else
        settings.content_start.top = -offset
        settings.scrollbar.vert = true
      end
    end

    items.each.with_index do |item, index|
      composer.add_text { separator } if !separator.nil? && index > 0
      composer.add_text do |_, settings|
        if index == active_index
          settings.bg_color = :highlight
          settings.color = :highlight
        end
        item
      end
    end
  end
end

#total_sizeObject



18
19
20
21
22
23
24
25
26
# File 'lib/whirled_peas/component/list_with_active.rb', line 18

def total_size
  @total_size ||= if flow == :l2r
    size = separator.nil? ? 0 : (items.count - 1) * separator.length
    items.each { |item| size += item.length }
    size
  else
    items.count + (separator.nil? ? 0 : items.count - 1)
  end
end