Class: Lore::GUI::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/lore/gui/form.rb

Overview

Form class returned by Cuba::GUI::Form_Generator. Usage:

generator = Form_Generator.new(Table_Accessor, Lang, :readonly | :mutable)
form =  generator.generate

Setting templates is optional (uses System_Templates ‘form/element.amr.html’ and ‘form/table.arm.html’ by default):

form.element_template Some_Template.new('template/element.amr.html') 
form.form_template Some_Template.new('template/table.amr.html')

Set a form title (optional):

form.set_title('An example form')

Fill form values if present:

values = [Some Cuba::Attributes hash like $cb__libparams]
form.set_values(values)

Add additional hidden fields Form_Generator can’t resolve itself:

form.add_hidden(Table_Accessor.table_name, :attribute_name, value)

Add buttons:

form.add_button(:submit, 'click here to submit data')
form.add_button(:clear, 'click here to clear form')

Configure attribute order and grouping (optional). See method documentation for #set_groups for details:

form.set_groups(attribute_array)

form.print # alias for puts form.string

Constant Summary collapse

@@logger =
Lore.logger

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeForm

{{{



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lore/gui/form.rb', line 51

def initialize # {{{

  @elements           = Array.new
  @buttons            = Array.new
  @indexed_elements   = Hash.new
  @element_template   = ERB_Template.new('form_element.rhtml')
  @form_template      = ERB_Template.new('form_table.rhtml')
  @form_values        = Hash.new
  @element_groups     = nil
  @hidden_fields      = Array.new
  @title              = nil

end

Instance Attribute Details

#element_templateObject

Returns the value of attribute element_template.



47
48
49
# File 'lib/lore/gui/form.rb', line 47

def element_template
  @element_template
end

#form_templateObject

Returns the value of attribute form_template.



47
48
49
# File 'lib/lore/gui/form.rb', line 47

def form_template
  @form_template
end

#form_valuesObject

Returns the value of attribute form_values.



47
48
49
# File 'lib/lore/gui/form.rb', line 47

def form_values
  @form_values
end

Instance Method Details

#[](key) ⇒ Object



154
155
156
# File 'lib/lore/gui/form.rb', line 154

def [](key)
  @form_values[key.to_s]
end

#[]=(key, value) ⇒ Object



158
159
160
# File 'lib/lore/gui/form.rb', line 158

def []=(key, value)
  @form_values[key.to_s] = value
end

#add(form_element) ⇒ Object

adders and setters # {{{



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/lore/gui/form.rb', line 66

def add(form_element)
  if form_element.instance_of? Hidden then
    @hidden_fields.push(form_element)
  else
    if form_element.attribute_table then
      index = form_element.attribute_table+'.'+form_element.attribute_name
    else
      index = form_element.attribute_name
    end
    # If there is an element for this attribute present already, delete 
    # it and use the new one only: 
    if !@indexed_elements[index].nil? then
      @elements.delete_if { |e| e.attribute_table == form_element.attribute_table && 
                                e.attribute_name == form_element.attribute_name }
    end
    @indexed_elements[index] = form_element
    @elements.push(form_element)
  end
end

#add_button(type, label) ⇒ Object

Add buttons:

form.add_button(:submit, 'click here to submit data')
form.add_button(:clear, 'click here to clear form')


140
141
142
143
# File 'lib/lore/gui/form.rb', line 140

def add_button(type, label)
  button = Button.new(type, label)
  @buttons.push(button)
end

#add_duplicate(form_element) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/lore/gui/form.rb', line 86

def add_duplicate(form_element)
  if form_element.instance_of? Hidden then
    @hidden_fields.push(form_element)
  else
    index = form_element.attribute_table+'.'+form_element.attribute_name
    # If there is an element for this attribute present already, delete 
    # it and use the new one only: 
    @indexed_elements[index] = form_element
    @elements.push(form_element)
  end
end

#add_hidden(name, value = nil) ⇒ Object

Add additional hidden fields Form_Generator can’t resolve itself:

form.add_hidden(Table_Accessor.table_name, :attribute_name, value)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/lore/gui/form.rb', line 108

def add_hidden(name, value=nil)
  if name.instance_of? Lore::Clause then
    value = value.to_s
    name_parts = name.to_s.split('.')
    table = name_parts[0..1].join('.')
    name = name_parts[2]
  else
    table = nil
    name = name.to_s
  end
  hidden = Hidden.new(table, name.to_s)
  if !value.nil? then
    hidden.set_attribute_value(value.to_s)
  end
  @hidden_fields.push(hidden)
end

#add_hidden_fields(params = {}) ⇒ Object



125
126
127
128
129
# File 'lib/lore/gui/form.rb', line 125

def add_hidden_fields(params={})
  params.each_pair { |k,v|
    add_hidden(k, v)
  }
end

#drop_hidden_fieldsObject



131
132
133
# File 'lib/lore/gui/form.rb', line 131

def drop_hidden_fields
  @hidden_fields = []
end

#keysObject



166
167
168
# File 'lib/lore/gui/form.rb', line 166

def keys
  @form_values.keys
end

Alias for puts form.string



280
281
282
# File 'lib/lore/gui/form.rb', line 280

def print
  puts string()
end

#set_groups(groups) ⇒ Object

Configure attribute order and grouping (optional). Array argument is a one- or two-dimensional array of full attribute names this form should print. This way, order of input fields can be specified, and attributes can be filtered. Two-dimensional arrays provide grouping of form elements. Example:

qttribute_array = 
[
  [ Klass_A.foo, Klass_A.bar ], 
  [ Klass_B.wombat, Klass_C.bla ]
]

form.set_groups(attribute_array)

Add filtered attribute as hidden field:

form.add_hidden(Klass_A.table_name, :wombat)


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/lore/gui/form.rb', line 189

def set_groups(groups)
  if(groups.nil? || groups.at(0).nil?) then
    @@logger.warn('Form groups are nil or empty') 
    return
  end
  groups.map! { |entry|
    if entry.instance_of? Array then
      entry.map! { |field| field.to_s }
    else 
      entry.to_s
    end
  }
  @@logger.debug('Form groups set: '+groups.inspect)
  @element_groups = groups
end

#set_readonly(*attributes) ⇒ Object



98
99
100
101
102
# File 'lib/lore/gui/form.rb', line 98

def set_readonly(*attributes)
  attributes.each { |a| 
    @indexed_elements[a.to_s].set_mode(:readonly)
  }
end

#set_title(title) ⇒ Object

Set a form title (optional):

form.set_title('An example form')


209
210
211
# File 'lib/lore/gui/form.rb', line 209

def set_title(title)
  @title = title
end

#set_values(values) ⇒ Object

Fill form values if present:

values = [Some Cuba::Attributes hash like $cb__libparams]
form.set_values(values)


150
151
152
# File 'lib/lore/gui/form.rb', line 150

def set_values(values)
  @form_values = values
end

#stringObject

Return form as XHTML string based on current configuration



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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/lore/gui/form.rb', line 217

def string # {{{

  elements = ''

  if !@title.nil? then
    title_string = '<div class="form_header">'+@title.to_s+'</div>' 
  else 
    title_string = ''
  end
  
  if @element_groups.nil? then
    @elements.each { |element|
      elements += process_element(element)
    }
  else
    @element_groups.each { |attribs|
      group = ''
      attribs.each { |attrib|
      attrib = attrib.to_s
      if !@indexed_elements[attrib].nil? then
        element = @indexed_elements[attrib]
        group += process_element(element)
      end
      }
      if attribs.instance_of? Array then
        elements += '<div class="form_delimiter"></div>'+group
      else 
        elements += group
      end
    }
    # Check for explicit (expected/required) attributes 
    # group configuration doesn't contain. Add those as 
    # hidden field: 
    # TODO: Cannot be realized in current solution! 
    
  end
  @form_template.set_data({
    :form => elements
  })

  buttons = ''
  @buttons.each { |button|
    data = {
      :label => '', 
      :element => button.string
    }
    @element_template.set_data(data)
    buttons += @element_template.string
  }
  
  hidden_fields_string = ''
  @hidden_fields.each { |element|
    set_element_value(element)
    hidden_fields_string += element.string
  }
  
  result = title_string + hidden_fields_string + @form_template.string + buttons

  return result

end

#valuesObject



162
163
164
# File 'lib/lore/gui/form.rb', line 162

def values
  @form_values
end