Module: Spark::InputHelper

Defined in:
app/helpers/spark/input_helper.rb

Instance Method Summary collapse

Instance Method Details

#base_input_tag(name, value, options, type, &block) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/helpers/spark/input_helper.rb', line 97

def base_input_tag( name, value, options, type, &block )
  case type
  when :range
    range_input_tag( name, options )
  when :select
    value = value.html_safe if value
    select_tag( name, value, options )
  when :textarea
    value ||= capture(&block).html_safe if block_given?
    options[:class] = "#{options[:class]} empty".strip if value.nil?
    text_area_tag( name, value, options )
  else
    options[:class] = "#{options[:class]} empty".strip if value.nil?
    text_field_tag( name, value, options )
  end

end

#input_tag(type, name, value, options = nil, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
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
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
# File 'app/helpers/spark/input_helper.rb', line 4

def input_tag(type, name, value, options=nil, &block)
  type.to_sym! if type.is_a?( String )

  if value.is_a? Hash
    options = value
    value = nil
  end

  options = Spark::Helpers.set_type_defaults( type, options )
  options = Spark::Helpers.set_data_keys( options, 'default' )
  options = Spark::Helpers.set_aria_keys( options )

  icon = Esvg.use( icon ) if icon = options.delete('icon')
  icon_after = Esvg.use( icon_after ) if icon_after = options.delete('icon_after')

  options['data']['unique-id'] = "input-#{SecureRandom.hex(8)}"
  options['autocomplete'] ||= 'off' if type != :textarea

  label_options = { 
    data: { 'input-type' => type },
    class: [ label_class( type ), options.delete( 'class' ) ]
  }

  # Move modal options to label
  %w(show-panel show-dialog show-menu).each do |opt|
    if val = options['data'].delete(opt)
      label_options[:data][opt] = val
    end
  end

  # Add a placeholder element unless textarea
  if type != :textarea && placeholder = options['placeholder']
    placeholder = (:span, class: 'input-placeholder-text') { placeholder }
    label_options = add_class label_options, 'placeholder-label'
  end

  if label_description = options.delete('description')
    options['aria-describedby'] = options['data']['unique-id'] + '-description'
    label_description = (:span, id: options['aria-describedby'], class: 'label-description') { label_description }
    label_options = add_class label_options, 'has-description'
  end

  if options['label'] && label_text = options.delete('label')
    options['aria-labelledby'] = options['data']['unique-id'] + '-label'

    label_text = (:span, class: 'input-label-text') {
      (:span, id: options['aria-labelledby']){ label_text }
    }
  end

  # Be sure the input has a label if there is no label text
  options['aria-label'] = options['placeholder'] if placeholder && !label_text

  input = base_input_tag( name, value, options, type, &block )

  (:div, label_options) do
    concat( label_text )
    concat( label_description )

    concat ( :span, class: 'input-wrapper' ) {
      if type == :select
        concat( input )
        #concat placeholder
      else
        concat ( :span, class: 'input-icon' ) { icon } if icon

        concat ( :span, class: 'input-container' ) {
          concat( input )
          #concat placeholder
        }

        if type == :search 
          concat button_tag( type: 'button', class: 'clear-search', "aria-label" => 'Clear search', disabled: !value, "data-clear-input" => "[data-unique-id=#{options['data']['unique-id']}]" ) { 'x' }
        end
        concat ( :span, class: 'input-icon after' ) { icon_after } if icon_after

        if block_given? && type != :textarea
          html = capture(&block)

          if html.match(/<select.*?>/)
            selected = html.match(/<option.*?selected.*?>(.+?)<\/option>/m) || html.match(/<option.*?>(.+?)<\/option/m)
            html.sub!(/<select/, "<select data-sync-selected='[data-sync=#{options['data']['unique-id']}]' ")
            html << "<span class='select-text' data-sync='#{options['data']['unique-id']}'>#{selected[1]}</span>"
          end

          concat ( :span, class: 'input-extra' ) { html.html_safe }
        end
      end
    }
  end

end

#label_class(type) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/helpers/spark/input_helper.rb', line 121

def label_class( type )
  type = case type
  when :tel, :password, :number, :url, :email, :search
    "text"
  when :checkbox, :radio
    "tick"
  else
    type.to_s
  end

  "#{type}-input-label"
end

#range_input_tag(name, options) ⇒ Object



115
116
117
118
119
# File 'app/helpers/spark/input_helper.rb', line 115

def range_input_tag(name, options)
  options.merge!({ type: "range", })

  tag :input, options
end

#switch_input_tag(name, checked = false, options = {}) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/helpers/spark/input_helper.rb', line 134

def switch_input_tag(name, checked = false, options = {})
  type = :checkbox

  if checked.is_a? Hash
    options = checked
    checked = false
  end

  options = add_class( options, [ label_class( type ), 'check-switch switch-label'] )

  label_options = { 
    data: { 
      'input-type' => type,
      'input' => 'checkbox'
    },
    class: options.delete('class')
  }

  if label_text = options.delete('label')
    label_text = (:span, class: 'label-text') { label_text }
  end

  (:label, label_options ) do
    concat tag :input, name: name, type: :hidden, value: false
    concat label_text
    concat check_box_tag(name, true, checked, options)

    concat (:span, class: 'check-switch-panel') {
      concat (:span, class: 'check-switch-tick') { '' }
    }

    concat (:span, class: 'check-switch-label') { 'Enable' }
  end

end