Class: Gloo::Objs::Form

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/web_svr/form.rb

Constant Summary collapse

KEYWORD =
'form'.freeze
KEYWORD_SHORT =
'form'.freeze
NAME =

Form

'name'.freeze
ID =
'id'.freeze
METHOD =
'method'.freeze
METHOD_DEFAULT =
'post'.freeze
ACTION =
'action'.freeze
CANCEL_PATH =
'cancel_path'.freeze
CONTENT =
'content'.freeze
STYLES =
'styles'.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.



168
169
170
# File 'lib/gloo/objs/web_svr/form.rb', line 168

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

.short_typenameObject

The short name of the object type.



37
38
39
# File 'lib/gloo/objs/web_svr/form.rb', line 37

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



30
31
32
# File 'lib/gloo/objs/web_svr/form.rb', line 30

def self.typename
  return KEYWORD
end

Instance Method Details

#action_valueObject

Get the action for the form. This is the path to POST to, for example.



64
65
66
67
68
# File 'lib/gloo/objs/web_svr/form.rb', line 64

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

#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:



139
140
141
# File 'lib/gloo/objs/web_svr/form.rb', line 139

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.



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/gloo/objs/web_svr/form.rb', line 148

def add_default_children
  fac = @engine.factory

  # Create attributes with ID and Classes
  fac.create_string NAME, '', self
  fac.create_string METHOD, 'post', self
  fac.create_string ACTION, '', self
  fac.create_string CANCEL_PATH, '', self

  fac.create_can CONTENT, self
end

#cancel_button_stylesObject

Get the cancel button styles.



125
126
127
# File 'lib/gloo/objs/web_svr/form.rb', line 125

def cancel_button_styles
  return @styles['cancel'] || ''
end

#cancel_path_valueObject

Get the cancel path for the form.



73
74
75
76
77
# File 'lib/gloo/objs/web_svr/form.rb', line 73

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

#close_formObject

Close the form.



221
222
223
# File 'lib/gloo/objs/web_svr/form.rb', line 221

def close_form
  return "</form>"
end

#form_contentObject

Get all the form content, the collection of form fields.



82
83
84
85
86
# File 'lib/gloo/objs/web_svr/form.rb', line 82

def form_content
  o = find_child CONTENT
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o
end

#form_stylesObject

Get the form styles. Use the name if none is provided.



111
112
113
# File 'lib/gloo/objs/web_svr/form.rb', line 111

def form_styles
  return @styles['form'] || name_value
end

#method_valueObject

Get the method for the form. ‘post’ is the default.



54
55
56
57
58
# File 'lib/gloo/objs/web_svr/form.rb', line 54

def method_value
  o = find_child METHOD
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o.value || METHOD_DEFAULT
end

#msg_renderObject

Render the form and all contained fields.



175
176
177
178
179
# File 'lib/gloo/objs/web_svr/form.rb', line 175

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

#name_valueObject

Get the name for the form.



44
45
46
47
48
# File 'lib/gloo/objs/web_svr/form.rb', line 44

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

#open_formObject

Open the form.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/gloo/objs/web_svr/form.rb', line 189

def open_form
  name = name_value

  cancel_button = ""
  if cancel_path_value
    cancel_button = "      <a class=\"\#{cancel_button_styles}\"\n        href=\"\#{cancel_path_value}\"> \n        Cancel</a>\n    HTML\n  end\n  return <<~HTML\n    <form class='\#{form_styles}'\n          id='\#{name}'\n          method='\#{method_value}'\n          action='\#{action_value}'\n          accept-charset='UTF-8'>\n      <div class=\"actions\">\n        <input type=\"submit\" \n          name=\"commit\" \n          value=\"Save\" \n          class=\"\#{submit_button_styles}\" \n          data-disable-with=\"Saving...\" />\n\n      \#{cancel_button}\n      </div>\n  HTML\nend\n"

#renderObject

Render the Form as HTML.



228
229
230
231
# File 'lib/gloo/objs/web_svr/form.rb', line 228

def render
  @styles = styles
  return open_form + render_content + close_form
end

#render_contentObject

Render the element content using the specified render function. This is a recursive function (through one of the other render functions).



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/gloo/objs/web_svr/form.rb', line 237

def render_content 
  fields = ""
  field_can = form_content
  return "" if field_can.nil?

  field_can.children.each do |o|
    o = Gloo::Objs::Alias.resolve_alias( @engine, o )
    if o.class == Field
      fields << o.render( @styles )
    elsif o.class == Element
      fields << o.render_html
    elsif o
      data = render_thing o
      ( fields << data ) if data 
    end
  end

  return fields
end

#render_thing(e) ⇒ Object

Render a string or other object.



260
261
262
263
264
265
266
267
# File 'lib/gloo/objs/web_svr/form.rb', line 260

def render_thing e
  begin
    return e.render( 'render_html' )
  rescue => e
    @engine.log_exception e
    return ''
  end
end

#stylesObject

Get the styles for the form. Retuns styles in the form of a hash: { ‘field_group’ => ‘form-group mt-3’, … }



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gloo/objs/web_svr/form.rb', line 93

def styles
  style_h = {} 
  o = find_child STYLES
  return style_h unless o
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )

  o.children.each do |c|
    style_h[ c.name ] = c.value
  end

  # puts "styles: #{style_h}"
  return style_h
end

#submit_button_stylesObject

Get the submit button styles.



118
119
120
# File 'lib/gloo/objs/web_svr/form.rb', line 118

def submit_button_styles
  return @styles['submit'] || ''
end