Class: Gloo::Objs::Field

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/web_svr/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

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, #find_child_resolve_alias, #find_child_value, help, inherited, #initialize, #is_alias?, #is_container?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_unload, #multiline_value?, #pn, #remove_child, #root?, #send_message, #set_parent, #set_value, #sql_value, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Class Method Details

.messagesObject

Get a list of message names that this object receives.



318
319
320
# File 'lib/gloo/objs/web_svr/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/gloo/objs/web_svr/field.rb', line 45

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



38
39
40
# File 'lib/gloo/objs/web_svr/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:



293
294
295
# File 'lib/gloo/objs/web_svr/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/gloo/objs/web_svr/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:



140
141
142
143
144
145
# File 'lib/gloo/objs/web_svr/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/gloo/objs/web_svr/field.rb', line 150

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

#checked?Boolean

Should this field be checked?

Returns:



205
206
207
208
209
210
# File 'lib/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/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/gloo/objs/web_svr/field.rb', line 84

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