Method: Conjoin::FormBuilder::SelectInput#display

Defined in:
lib/conjoin/inputs/select.rb

#displayObject



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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/conjoin/inputs/select.rb', line 15

def display
  # We don't need the type
  options.delete :type

  append_button       = options.delete :append_button
  append_split_button = options.delete :append_split_button

  if options[:multiple]
    options[:name] += '[]'
  end

  content = mab do
    # automatically add a prompt by default
    options[:prompt] = 'true' unless options.key? :prompt
    options[:class] += ' selectize' unless options.delete(:selectize) == false
    selected_value = options.delete :value

    select options do
      opts = {
        value: ''
      }

      if prompt = options.delete(:prompt)
        opts['selected'] = 'selected' unless selected_value
        option opts do
          text! prompt.to_s == 'true' ? 'Please Choose One.' : prompt
        end
      end

      if not options[:group]
        select_options.invert.each do |name, value|
          option render_opts(value, selected_value, opts) do
            text (name == name.downcase ? name.titleize : name)
          end
        end
      else
        select_options.each do |group_select, group|
          optgroup label: group.to_s.titleize do
            group_select.each do |value, name|
              option render_opts(value, selected_value, opts) do
                text name.titleize
              end
            end
          end
        end
      end
    end
  end

  if append_button
    mab do
      div class: 'input-group' do
        text! content
        div class: 'input-group-btn' do
          button class: 'btn btn-primary', type: 'button', 'on-click-get' => append_button[:href] do
            text append_button[:text]
          end
        end
      end
    end
  elsif append_split_button
    first_button = append_split_button.shift

    mab do
      div class: 'input-group' do
        text! content
        div class: 'input-group-btn' do
          button class: 'btn btn-primary', type: 'button', 'on-click-get' => first_button[:href] do
            text first_button[:text]
          end
          if append_split_button.length
            button class: 'btn btn-primary dropdown-toggle', 'data-toggle' => "dropdown", type: 'button' do
              span class: 'caret'
            end
            ul class: 'dropdown-menu pull-right', role: 'menu' do
              append_split_button.each do |b|
                li do
                  a href: 'javascript:{};', 'on-click-get' => b[:href] do
                    text b[:text]
                  end
                end
              end
            end
          end
        end
      end
    end
  else
    content
  end
end