Class: Weaver::FormElements

Inherits:
Elements show all
Defined in:
lib/weaver/element_types/form_elements.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Elements

#_button, #accordion, #background, #badge, #big_button, #big_embossed_button, #block_button, #breadcrumb, #center, #circle_button, #col, #crossfade_image, #embossed_button, #gallery, #generate, #half, #hyperlink, #ibox, #icon, #image, #jumbotron, #math, #method_missing, #modal, #normal_button, #on_page_load, #outline_button, #p, #panel, #quarter, #request_css, #request_js, #root, #rounded_button, #row, #set_favicon_path, #set_favicon_type, #small_button, #syntax, #table, #table_from_hashes, #table_from_source, #tabs, #text, #third, #tiny_button, #twothirds, #wform, #widget, #write_script_once

Constructor Details

#initialize(page, anchors, formName, options = {}) ⇒ FormElements



9
10
11
12
13
14
# File 'lib/weaver/element_types/form_elements.rb', line 9

def initialize(page, anchors, formName, options = {})
  super(page, anchors)
  @formName = formName
  @options = options
  @scripts = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Weaver::Elements

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/weaver/element_types/form_elements.rb', line 7

def options
  @options
end

#scriptsObject

Returns the value of attribute scripts.



7
8
9
# File 'lib/weaver/element_types/form_elements.rb', line 7

def scripts
  @scripts
end

Instance Method Details

#boolean_element(checkbox_label, options = {}) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/weaver/element_types/form_elements.rb', line 270

def boolean_element(checkbox_label, options = {})
  @page.request_css 'css/plugins/iCheck/custom.css'
  @page.request_js 'js/plugins/iCheck/icheck.min.js'

  @page.write_script_once "    $(document).ready(function () {\n        $('.i-checks').iCheck({\n            checkboxClass: 'icheckbox_square-green',\n            radioClass: 'iradio_square-green',\n        });\n    });\n  SCRIPT\n\n  label_options = {}\n  elem = Elements.new(@page, @anchors)\n  elem.instance_eval do\n    if options[:form] == :button\n      options.delete(:form)\n      label class: 'btn btn-primary btn-block btn-outline' do\n        input options\n        text checkbox_label.to_s\n      end\n    else\n      div class: 'i-checks' do\n        label label_options do\n          input options do\n            text \" \#{checkbox_label}\"\n          end\n        end\n      end\n    end\n  end\n\n  elem.generate\nend\n"

#checkbox(name, checkbox_label, options = {}) ⇒ Object



249
250
251
252
253
254
255
256
257
258
# File 'lib/weaver/element_types/form_elements.rb', line 249

def checkbox(name, checkbox_label, options = {})
  checkbox_name = options[:id] || @page.create_anchor('checkbox')
  options[:type] = 'checkbox'
  options[:name] = name
  options[:id] = checkbox_name
  text boolean_element(checkbox_label, options)
  @scripts << "  object[\"\#{name}\"] = $('#\#{checkbox_name}').is(\":checked\");\n  SCRIPT\nend\n"

#credit_card(options = {}, &block) ⇒ Object



306
307
308
309
310
# File 'lib/weaver/element_types/form_elements.rb', line 306

def credit_card(options = {}, &block)
  elem = CreditCardForm.new(@page, @anchors, options, &block)
  elem.apply_script(@scripts)
  text elem.generate
end


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
169
# File 'lib/weaver/element_types/form_elements.rb', line 107

def dropdown(name, dropdown_label, choice_array, options = {})
  select_name = options[:id] || @page.create_anchor('select')

  options[:class] = 'form-control'
  options[:name] = name
  options[:id] = select_name
  options[:placeholder] ||= ' '

  form_options = options.clone

  if options[:multiple]

    if options[:multiple_style] == :chosen
      @page.request_css 'css/plugins/chosen/chosen.css'
      @page.request_js 'js/plugins/chosen/chosen.jquery.js'

      @page.write_script_once "  var config = {\n    '.chosen-select'           : {placeholder_text_multiple: \"\#{options[:placeholder]}\"},\n    '.chosen-select-deselect'  : {allow_single_deselect:true},\n    '.chosen-select-no-single' : {disable_search_threshold:10},\n    '.chosen-select-no-results': {no_results_text:'Oops, nothing found!'},\n    '.chosen-select-width'     : {width:\"95%\"}\n  }\n  for (var selector in config) {\n    $(selector).chosen(config[selector]);\n  }\n      SCRIPT\n\n      form_options[:class] = 'chosen-select'\n      form_options[:style] = 'width: 100%'\n    end\n\n    @scripts << <<-SCRIPT\n  var selections = [];\n  $(\"#\#{select_name} option:selected\").each(function(i, selected){\nselections[i] = $(selected).text();\n  });\n  object[\"\#{name}\"] = selections;\n    SCRIPT\n\n  else\n    @scripts << <<-SCRIPT\n  object[\"\#{name}\"] = $( \"#\#{select_name} option:selected\" ).text();\n    SCRIPT\n  end\n\n  div class: 'form-group' do\n    label dropdown_label, class: 'control-label'\n\n    div class: 'input-group', style: 'width: 100%' do\n      method_missing :select, form_options do\n        choice_array.each do |choice|\n          if (options[:value]).to_s == choice.to_s\n            option choice, selected: true\n          else\n            option choice\n          end\n        end\n      end\n    end\n  end\nend\n"

#hiddenfield(name, value, options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/weaver/element_types/form_elements.rb', line 91

def hiddenfield(name, value, options = {})
  hiddenfield_name = options[:id] || @page.create_anchor('hiddenfield')

  input_options = {}
  input_options[:type] = 'hidden'
  input_options[:value] = value
  input_options[:id] = hiddenfield_name
  input_options[:name] = name

  input input_options

  @scripts << "  object[\"\#{name}\"] = $('#\#{hiddenfield_name}').val();\n  SCRIPT\nend\n"

#knob(name, options = {}) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/weaver/element_types/form_elements.rb', line 171

def knob(name, options = {})
  knob_name = @page.create_anchor 'knob'

  @page.request_js 'js/plugins/jsKnob/jquery.knob.js'
  @page.write_script_once "  $(\".dial\").knob();\n  SCRIPT\n\n  knob_options = {}\n\n  knob_options[:id] = knob_name\n  knob_options[:type] = 'text'\n  knob_options[:value] = options[:value] || '0'\n  knob_options[:class] = 'dial'\n\n  options.each do |key, value|\n    knob_options[\"data-\#{key}\".to_sym] = value\n  end\n\n  knob_options[:\"data-fgColor\"] = '#1AB394'\n  knob_options[:\"data-width\"] = '85'\n  knob_options[:\"data-height\"] = '85'\n\n  input knob_options\n\n  @scripts << <<-SCRIPT\n  object[\"\#{name}\"] = $('#\#{knob_name}').val();\n  SCRIPT\nend\n"

#passwordfield(name, textfield_label = nil, options = {}, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/weaver/element_types/form_elements.rb', line 16

def passwordfield(name, textfield_label = nil, options = {}, &block)
  if textfield_label.is_a? Hash
    options = textfield_label
    textfield_label = nil
  end

  options[:type] = 'password'
  textfield(name, textfield_label, options, &block)
end

#radio(name, choice_array, options = {}) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/weaver/element_types/form_elements.rb', line 201

def radio(name, choice_array, options = {})
  radio_name = @page.create_anchor 'radio'

  choice_array = choice_array.map do |choice|
    if choice.is_a? Hash
      { value: choice[:value], label: choice[:label] }
    else
      { value: choice, label: choice }
    end
  end

  active = choice_array[0][:value]
  if options[:value] && (choice_array.index { |x| x[:value] == options[:value] } != nil)
    active = options[:value]
  end

  div_options = {}
  curobject = self
  div_options[:"data-toggle"] = 'buttons' if options[:form] == :button
  div div_options do
    choice_array.each do |choice|
      value = choice[:value]
      label = choice[:label]

      the_options = Hash.new(options)

      the_options[:checked] = '' if active == value

      if options[:form] == :button
        the_options[:type] = 'radio'
        the_options[:value] = value
        the_options[:name] = name
        the_options[:form] = :button
        text curobject.boolean_element(label, the_options)
      else
        the_options[:type] = 'radio'
        the_options[:value] = value
        the_options[:name] = name
        text curobject.boolean_element(label, the_options)
      end
    end
  end

  @scripts << "  object[\"\#{name}\"] = $('input[name=\#{name}]:checked', '#\#{@formName}').val()\n  SCRIPT\nend\n"

#submit(anIcon, title = {}, options = {}, &block) ⇒ Object



260
261
262
263
264
265
266
267
268
# File 'lib/weaver/element_types/form_elements.rb', line 260

def submit(anIcon, title = {}, options = {}, &block)
  options[:id] = @page.create_anchor('submit_button')
  options[:icon] = anIcon
  options[:title] = title
  options[:type] = 'submit'
  options[:data] = "get_#{@formName}_object()"
  options[:nosubmit] = true if block
  _button(options, &block)
end

#textfield(name, textfield_label = nil, options = {}, &block) ⇒ Object



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
# File 'lib/weaver/element_types/form_elements.rb', line 26

def textfield(name, textfield_label = nil, options = {}, &block)
  if textfield_label.is_a? Hash
    options = textfield_label
    textfield_label = nil
  end

  textfield_name = options[:id] || @page.create_anchor('textfield')
  options[:type] ||= 'text'
  options[:placeholder] ||= ''
  options[:name] = name

  input_options = {}
  input_options[:type] = options[:type]
  input_options[:placeholder] = options[:placeholder]
  input_options[:id] = textfield_name
  input_options[:name] = options[:name]
  input_options[:rows] = options[:rows]
  input_options[:class] = 'form-control'
  input_options[:value] = options[:value]
  input_options[:style] = options[:style]

  input_options[:autocomplete] = options[:autocomplete] || 'on'
  input_options[:autocorrect] = options[:autocorrect] || 'on'
  input_options[:autocapitalize] = options[:autocapitalize] || 'off'

  if options[:mask]
    @page.request_css 'css/plugins/jasny/jasny-bootstrap.min.css'
    @page.request_js 'js/plugins/jasny/jasny-bootstrap.min.js'

    input_options[:"data-mask"] = options[:mask]
  end

  div class: "form-group #{options[:extra_class]}", id: "#{input_options[:id]}-group" do
    label textfield_label if textfield_label

    div_class = ' '
    if options[:front_text] || options[:back_text]
      div_class = 'input-group m-b'
     end

    div "class": div_class do
      if options[:front_text]
        span (options[:front_text]).to_s, class: 'input-group-addon'
      end
      if input_options[:rows] && (input_options[:rows] > 1)
        textarea input_options do
        end
      else
        input input_options
      end
      if options[:back_text]
        span (options[:back_text]).to_s, class: 'input-group-addon'
      end
    end
  end

  textjs = TextfieldJavascript.new(input_options[:id])

  @page.on_page_load textjs.generate(&block) if block

  @scripts << "  object[\"\#{name}\"] = $('#\#{textfield_name}').val();\n  SCRIPT\nend\n"