Class: Objs::Field

Inherits:
Gloo::Core::Obj
  • Object
show all
Defined in:
lib/objs/field.rb

Constant Summary collapse

KEYWORD =
'field'.freeze
KEYWORD_SHORT =
'field'.freeze
NAME =

Form

'name'.freeze
ID =
'id'.freeze
TYPE =
'type'.freeze
VALUE =
'value'.freeze
LABEL =
'label'.freeze
PLACEHOLDER =
'placeholder'.freeze
AUTOFOCUS =
'autofocus'.freeze
COLS =
'cols'.freeze
ROWS =
'rows'.freeze
DESCRIPTION =
'description'.freeze
CHECKED =
'checked'.freeze
OPTIONS =
'options'.freeze
FIELD_GROUP =

Style attributes

'field_group'.freeze
FIELD_LABEL =
'field_label'.freeze
FIELD_CONTROL =
'field_control'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.doc_dataObject

Get the object's documentation data.



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/objs/field.rb', line 434

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'An HTML form field. Might be a text field, a ' \
      'checkbox, a select list or other type of form field.',
    :children => [
      'name (string) — Optional; if not specified, the name of the object is used. Used for both the name and the id of the field element.',
      'type (string) — Required. Examples: text, checkbox, hidden, textarea, select, etc. Some other children depend on the type of the field.',
      'value (string) — Optional. The value of the field.',
      'label (string) — Optional; if not specified, the name of the field is used. The label for the field.',
      'autofocus (boolean) — Optional, default false. Whether the field should be focused when the page loads.',
      'placeholder (string) — Optional. The placeholder text for the field.',
      'cols (string) — Optional. The number of columns for the field, appended to the bootstrap column class (can also add offset or other column classes). Example: 6.',
      'rows (integer) — Optional. The number of rows for a textarea field; only used for textarea fields.',
      'field_group (string) — Optional override for form styles: the field group style classes.',
      'field_label (string) — Optional override for form styles: the field label style classes.',
      'field_control (string) — Optional override for form styles: the field control style classes.',
      'options (container) — Optional; only used for select fields. The options for the select field.',
      'checked (boolean) — Optional; only used for checkbox fields. Whether the checkbox field is checked.'
    ],
    :messages => [
      'render — Manually render the HTML field. Normally the render is called by the web server when the page containing the element is requested.'
    ],
    :examples => <<~EXAMPLES.strip
      f [form] :
        name [string] : message_form
        action [string] : /messages
        method [string] : post
        cancel_path [string] : /messages
        styles [alias] : form_styles
        content [can] :
          id [field] :
            type [string] : hidden
            value [string] : 13
          message [field] :
            type [string] : text
            autofocus [boolean] : true
            placeholder [string] : the message goes here…
          description [field] :
            type [string] : text
            placeholder [string] : longer description…
          opt [field] :
            label [string] : Option
            type [string] : select
            value [string] : Option 3
            options [can] :
              1 [string] : Option 1
              2 [string] : Option 2
              3 [string] : Option 3
          box [field] :
            type [string] : checkbox
            checked [boolean] : false
            description [string] : Check this box if you want to box it.
    EXAMPLES
  }
end

.messagesObject

Get a list of message names that this object receives.



318
319
320
# File 'lib/objs/field.rb', line 318

def self.messages
  return super + [ 'render' ]
end

.short_typenameObject

The short name of the object type.



45
46
47
# File 'lib/objs/field.rb', line 45

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



38
39
40
# File 'lib/objs/field.rb', line 38

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:

  • (Boolean)


293
294
295
# File 'lib/objs/field.rb', line 293

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



302
303
304
305
306
307
308
# File 'lib/objs/field.rb', line 302

def add_default_children
  fac = @engine.factory

  # Create attributes with ID and Classes
  fac.create_string NAME, '', self
  fac.create_string TYPE, 'text', self
end

#autofocus?Boolean

Should this field autofocus?

Returns:

  • (Boolean)


140
141
142
143
144
145
# File 'lib/objs/field.rb', line 140

def autofocus?
  o = find_child AUTOFOCUS
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return nil unless o
  return o.value
end

#autofocus_tagObject

Get the autofocus tag for the form field.



150
151
152
153
# File 'lib/objs/field.rb', line 150

def autofocus_tag
  return "autofocus='autofocus'" if autofocus?
  return ''
end

#checked?Boolean

Should this field be checked?

Returns:

  • (Boolean)


205
206
207
208
209
210
# File 'lib/objs/field.rb', line 205

def checked?
  o = find_child CHECKED
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return nil unless o
  return o.value
end

#checked_tagObject

Get the checked tag for the form field.



215
216
217
218
# File 'lib/objs/field.rb', line 215

def checked_tag
  return "checked='checked'" if checked?
  return ''
end

#cols_tagObject

Get the cols tag for the form field.



167
168
169
170
171
# File 'lib/objs/field.rb', line 167

def cols_tag
  cols = cols_value
  return "col-#{cols}" if cols
  return ''
end

#cols_valueObject

Get the cols for the form field.



158
159
160
161
162
# File 'lib/objs/field.rb', line 158

def cols_value
  o = find_child COLS
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o ? o.value : nil
end

#description_valueObject

Get the description for the form field.



196
197
198
199
200
# File 'lib/objs/field.rb', line 196

def description_value
  o = find_child DESCRIPTION
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o ? o.value : nil
end

#field_control_stylesObject

Get the field control styles.



273
274
275
276
277
278
279
280
281
# File 'lib/objs/field.rb', line 273

def field_control_styles
  o = find_child FIELD_CONTROL
  if o
    o = Gloo::Objs::Alias.resolve_alias( @engine, o )
    return o ? o.value : ''
  end
  
  return @styles[FIELD_CONTROL] || ''
end

#field_group_stylesObject

Get the field group styles.



247
248
249
250
251
252
253
254
255
# File 'lib/objs/field.rb', line 247

def field_group_styles
  o = find_child FIELD_GROUP
  if o
    o = Gloo::Objs::Alias.resolve_alias( @engine, o )
    return o ? o.value : ''
  end
  
  return @styles[FIELD_GROUP] || ''
end

#field_label_stylesObject

Get the field label styles.



260
261
262
263
264
265
266
267
268
# File 'lib/objs/field.rb', line 260

def field_label_styles
  o = find_child FIELD_LABEL
  if o
    o = Gloo::Objs::Alias.resolve_alias( @engine, o )
    return o ? o.value : ''
  end
  
  return @styles[FIELD_LABEL] || ''
end

#field_valueObject

Get the value for the form field.



75
76
77
78
79
# File 'lib/objs/field.rb', line 75

def field_value
  o = find_child VALUE
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o ? o.value : nil
end

#label_tagObject

Get the label tag for the form field.



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/objs/field.rb', line 106

def label_tag
  label = label_value
  label_data = ''
  if label
    label_data = <<~HTML
      <label class="#{field_label_styles}" for="#{name_value}">
        #{label}
      </label>
    HTML
  end
  return label_data
end

#label_valueObject

Get the label for the form field.



93
94
95
96
97
98
99
100
101
# File 'lib/objs/field.rb', line 93

def label_value
  o = find_child LABEL

  # If there is no child, use the obj's name
  return self.name_value.capitalize unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o ? o.value : nil
end

#msg_renderObject

Render the form field.



325
326
327
328
329
# File 'lib/objs/field.rb', line 325

def msg_render
  content = self.render
  @engine.heap.it.set_to content 
  return content
end

#name_valueObject

Get the name for the form field.



52
53
54
55
56
57
58
59
60
# File 'lib/objs/field.rb', line 52

def name_value
  o = find_child NAME

  # If there is no child, use the obj's name
  return self.name unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o ? o.value : nil
end

#placeholder_tagObject

Get the placeholder tag for the form field.



131
132
133
134
135
# File 'lib/objs/field.rb', line 131

def placeholder_tag
  placeholder = placeholder_value
  return "placeholder='#{placeholder}'" if placeholder
  return ''
end

#placeholder_valueObject

Get the placeholder for the form field.



122
123
124
125
126
# File 'lib/objs/field.rb', line 122

def placeholder_value
  o = find_child PLACEHOLDER
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o ? o.value : nil
end

#render(styles = {}) ⇒ Object

Render the field, switch on type.



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/objs/field.rb', line 338

def render styles = {}
  @styles = styles

  case type_value
  when 'text'
    return render_text
  when 'hidden'
    return render_hidden
  when 'textarea'
    return render_textarea
  when 'checkbox'
    return render_checkbox
  when 'search'
    return render_text
  when 'select'
    return render_select
  end
end

#render_checkboxObject

Render the checkbox field as HTML.



398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/objs/field.rb', line 398

def render_checkbox
  return <<~HTML
    <div class="#{field_group_styles} #{cols_tag}">
      #{label_tag}
      <label class="checkbox #{field_control_styles}">
        <input type="checkbox" 
          #{checked_tag} value="true"
          name="#{name_value}" id="#{name_value}" />
          #{description_value}
      </label>
    </div>
  HTML
end

#render_hiddenObject

Render the hidden field as HTML.



360
361
362
363
364
# File 'lib/objs/field.rb', line 360

def render_hidden
  return <<~HTML
    <input type="hidden" #{value_tag} name="#{name_value}" id="#{name_value}" />
  HTML
end

#render_selectObject

Render the select field as HTML.



415
416
417
418
419
420
421
422
423
424
425
# File 'lib/objs/field.rb', line 415

def render_select
  return <<~HTML
    <div class="#{field_group_styles} #{cols_tag}">
      #{label_tag}
      <select class="#{field_control_styles}"
        name="#{name_value}" id="#{name_value}">
        #{select_options}
      </select>
    </div>
  HTML
end

#render_textObject

Render the text field as HTML.



369
370
371
372
373
374
375
376
377
378
379
# File 'lib/objs/field.rb', line 369

def render_text
  return <<~HTML
    <div class="#{field_group_styles} #{cols_tag}">
      #{label_tag}
      <input #{placeholder_tag} #{autofocus_tag} #{rows_tag}
        class="#{field_control_styles}" 
        type="#{type_value}" #{value_tag}
        name="#{name_value}" id="#{name_value}" />
    </div>
  HTML
end

#render_textareaObject

Render the textarea field as HTML.



384
385
386
387
388
389
390
391
392
393
# File 'lib/objs/field.rb', line 384

def render_textarea
  return <<~HTML
    <div class="#{field_group_styles} #{cols_tag}">
      #{label_tag}
      <textarea #{placeholder_tag} #{autofocus_tag} #{rows_tag}
        class="#{field_control_styles}" 
        name="#{name_value}" id="#{name_value}">#{field_value}</textarea>
    </div>
  HTML
end

#rows_tagObject

Get the rows tag for the form field. Only applies to textarea fields.



187
188
189
190
191
# File 'lib/objs/field.rb', line 187

def rows_tag
  rows = rows_value
  return "rows='#{rows}'" if rows
  return ''
end

#rows_valueObject

Get the rows for the form field. Only applies to textarea fields.



177
178
179
180
181
# File 'lib/objs/field.rb', line 177

def rows_value
  o = find_child ROWS
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o ? o.value : nil
end

#select_optionsObject

Get options for the select list.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/objs/field.rb', line 223

def select_options
  o = find_child OPTIONS
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return nil unless o

  selected_value = field_value

  options = ''
  o.children.each do |child|
    if selected_value == child.name || selected_value == child.value
      selected = 'selected="selected"'
    else
      selected = ''
    end
    options += <<~HTML
      <option value="#{child.name}" #{selected}>#{child.value}</option>
    HTML
  end
  return options
end

#type_valueObject

Get the type for the form field. For example, 'text', 'password', 'checkbox', etc.



66
67
68
69
70
# File 'lib/objs/field.rb', line 66

def type_value
  o = find_child TYPE
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o ? o.value : nil
end

#value_tagObject

Get the value tag for the form field.



84
85
86
87
88
# File 'lib/objs/field.rb', line 84

def value_tag
  value = field_value
  return "value='#{value}'" if value
  return ''
end