aurita-gui 0.3.7 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -390,6 +390,7 @@ module GUI
390
390
  # TODO: Should overwrite previous field element
391
391
  # with same field name.
392
392
  def add(form_field_element)
393
+ touch()
393
394
  field_name = form_field_element.name.to_s
394
395
  form_field_element.value = @values[field_name] unless form_field_element.value.to_s != ''
395
396
  if !form_field_element.dom_id then
@@ -410,6 +411,7 @@ module GUI
410
411
  # form.fields = [ :name, :description, :date ]
411
412
  #
412
413
  def fields=(attrib_array)
414
+ touch()
413
415
  @custom_fields = true
414
416
  @fields = attrib_array.flatten.collect { |fieldname| fieldname.to_s }
415
417
  @elements.each { |field|
@@ -425,6 +427,7 @@ module GUI
425
427
  # form.values = { :name => 'Foo', :description => 'Bar', :date => '20081012' }
426
428
  #
427
429
  def values=(value_hash={})
430
+ touch()
428
431
  @values = value_hash
429
432
  @values.each_pair { |field_name, value|
430
433
  element = @element_map[field_name.to_s]
@@ -435,6 +438,7 @@ module GUI
435
438
 
436
439
  # Set all elements to readonly rendering mode.
437
440
  def readonly!
441
+ touch()
438
442
  @elements.each { |e|
439
443
  e.readonly!
440
444
  }
@@ -30,7 +30,10 @@ module GUI
30
30
  end
31
31
 
32
32
  def element
33
- HTML.ul(:class => :checkbox_options) {
33
+ element_attrib = @attrib.dup.update({ :class => :checkbox_options })
34
+ element_attrib.delete(:name)
35
+ element_attrib.delete(:value)
36
+ HTML.ul(element_attrib) {
34
37
  option_elements().map { |o| HTML.li() { o } }
35
38
  }
36
39
  end
@@ -172,7 +172,10 @@ module GUI
172
172
  end
173
173
 
174
174
  def options=(options)
175
- if options.kind_of? Array then
175
+ if options.kind_of? ArrayFields then
176
+ @option_values = options.fields
177
+ @option_labels = options.values
178
+ elsif options.kind_of? Array then
176
179
  @option_values = options
177
180
  @option_labels = options
178
181
  elsif options.kind_of? Range then
@@ -102,7 +102,7 @@ module GUI
102
102
  params.delete(:select_field)
103
103
  params.delete(:selectable_options)
104
104
  super(params)
105
- set_value(@value)
105
+ set_value(@value)
106
106
  add_css_class(:selection_list_field)
107
107
  end
108
108
 
@@ -118,6 +118,7 @@ module GUI
118
118
 
119
119
  # Set list of active selection options as array.
120
120
  def value=(value)
121
+ return unless value
121
122
  super(value)
122
123
  @selectable_options = []
123
124
  options().each_pair { |value, name|
@@ -130,12 +131,13 @@ module GUI
130
131
  # decorated by @option_field_decorator
131
132
  # (see comments on Selection_List_Field).
132
133
  def option_elements
133
- id_prefix = "#{@attrib[:id]}_" if @attrib[:id]
134
+ base_id = @attrib[:id]
135
+ base_id ||= @attrib[:name]
134
136
  elements = []
135
137
  options().each_pair { |opt_value, opt_label|
136
138
  selected = @value.map { |v| v.to_s }.include?(opt_value.to_s)
137
139
  if selected then
138
- elements << HTML.li(:id => "#{id_prefix}#{@attrib[:name]}_#{opt_value}") {
140
+ elements << HTML.li(:id => "#{base_id}_#{opt_value}") {
139
141
  @option_field_decorator.new(:name => @attrib[:name],
140
142
  :value => opt_value,
141
143
  :label => opt_label,
@@ -150,31 +152,51 @@ module GUI
150
152
  # as select field element containing additionally
151
153
  # available options.
152
154
  def element
153
- select_options = {}
155
+ select_options = []
156
+ select_option_ids = []
154
157
  @selectable_options.each { |v|
155
- select_options[v] = @option_labels[v]
158
+ select_option_ids << v
159
+ select_options << @option_labels[v]
156
160
  }
157
- id_prefix = "#{@attrib[:id]}_" if @attrib[:id]
161
+ select_options.fields = select_option_ids
162
+
163
+ base_id = @attrib[:id]
164
+ base_id ||= @attrib[:name]
158
165
 
159
166
  if @value && @value.length > 0 then
160
167
  HTML.div(@attrib) {
161
- HTML.ul(:id => "#{id_prefix}#{@attrib[:name]}") {
168
+ HTML.ul(:id => "#{base_id}_selected_options") {
162
169
  option_elements()
163
170
  } +
164
- @select_field_class.new(:id => "#{id_prefix}#{@attrib[:name]}_select",
171
+ @select_field_class.new(:id => "#{base_id}_select",
165
172
  :options => select_options,
173
+ :parent => self,
166
174
  :name => "#{@attrib[:name]}" )
167
175
  }
168
176
  else
169
177
  HTML.div(@attrib) {
170
- HTML.ul(:id => "#{id_prefix}#{@attrib[:name]}", :force_closing_tag => true) +
171
- @select_field_class.new(:id => "#{id_prefix}#{@attrib[:name]}_select",
178
+ HTML.ul(:id => "#{base_id}_selected_options", :force_closing_tag => true) +
179
+ @select_field_class.new(:id => "#{base_id}_select",
172
180
  :options => select_options,
181
+ :parent => self,
173
182
  :name => "#{@attrib[:name]}" )
174
183
  }
175
184
  end
176
185
  end
177
186
 
187
+ def readonly_element
188
+ base_id = @attrib[:id]
189
+ base_id ||= @attrib[:name]
190
+ elements = HTML.ul.readonly_selection_list { }
191
+ options().each_pair { |opt_value, opt_label|
192
+ selected = @value.map { |v| v.to_s }.include?(opt_value.to_s)
193
+ if selected then
194
+ elements << HTML.li(:id => "#{base_id}_#{opt_value}") { opt_label }
195
+ end
196
+ }
197
+ elements
198
+ end
199
+
178
200
  end
179
201
 
180
202
  end
@@ -119,9 +119,9 @@ module GUI
119
119
  args_string = args.join(',') if args.size > 0
120
120
  meth = meth.to_s
121
121
  # Uppercase method => method call
122
- meth << '(' << args_string.to_s << '); ' if meth[0] > 96
122
+ meth << '(' << args_string.to_s << '); ' if meth[0].to_i > 96
123
123
  # Uppercase method => Namespace
124
- meth << '.' if meth[0] < 97
124
+ meth << '.' if meth[0].to_i < 97
125
125
  @script << meth
126
126
  return self
127
127
  end
@@ -134,19 +134,19 @@ module GUI
134
134
 
135
135
  def to_js(arg)
136
136
  case arg
137
- when String:
137
+ when String then
138
138
  arg.gsub!("'","\\\\'")
139
139
  "'#{arg}'"
140
- when Hash:
140
+ when Hash then
141
141
  obj = []
142
142
  arg.each_pair { |k,v|
143
143
  v = to_js(v)
144
144
  obj << "#{k}: #{v}"
145
145
  }
146
146
  "{#{obj.join(', ')}}"
147
- when Symbol
147
+ when Symbol then
148
148
  arg.to_s
149
- else
149
+ else
150
150
  arg
151
151
  end
152
152
  end
@@ -8,7 +8,7 @@ module GUI
8
8
  # Usage:
9
9
  #
10
10
  # table = Table.new(:headers => [ 'user', 'phone', 'mobile', 'email' ])
11
- # table.add_row('fuchsto', '+49 89 123456', '+49 89 987654', 'fuchst@wortundform.de')
11
+ # table.add_row('fuchsto', '+49 89 123456', '+49 89 987654', 'fuchs@wortundform.de')
12
12
  #
13
13
  # A row element may be of type Aurita::GUI::Element.
14
14
  # In this case, method #string is invoked to render the cell.
@@ -16,43 +16,73 @@ module GUI
16
16
  # More examples:
17
17
  #
18
18
  # t = Table.new(:headers => ['user', 'phone', 'email'],
19
- # :options => { :class => 'css_class', :id => 'test_table' })
20
- # t.add_row([ 'a','b','c' ])
21
- # t.add_row([ 'd','e','f' ])
22
-
19
+ # :class => 'css_class',
20
+ # :id => 'test_table' )
21
+ # t.add_row('a','b','c' )
22
+ #
23
+ # t[0].class == Table_Cell
23
24
  # t[0][0].value = 'foo'
24
25
  # t[0][0].value = 45
25
26
  # t[1][0].onclick = 'test();'
26
27
  # t[0][1] = HTML.a(:href => 'http://google.com') { 'google' }
27
-
28
+ #
28
29
  # t[0][1].value.href = 'other'
29
30
  # t[0][1].value.content = 'clickme'
30
-
31
+ #
31
32
  # puts t[0][1].value.string
32
-
33
+ #
33
34
  # t[0].class = 'highlighted'
34
35
  #
35
36
  class Table < Element
36
37
 
37
- attr_accessor :columns, :headers, :rows, :template, :row_css_classes, :column_css_classes, :options
38
+ attr_accessor :columns, :headers, :rows, :template, :row_css_classes, :column_css_classes, :row_class
38
39
 
39
40
  def initialize(params={}, &block)
40
- @options = params[:options]
41
- @options[:tag] = 'table'
42
- @headers = params[:headers]
43
- @num_columns = params[:num_columns]
44
- @num_columns = @headers.length if (!@columns && @headers)
45
- @columns = []
46
- @rows = []
41
+ params[:tag] = :table
42
+ @headers ||= params[:headers]
43
+ @num_columns ||= params[:num_columns]
44
+ @num_columns ||= @headers.length if (!@columns && @headers)
45
+ @columns ||= []
46
+ @rows ||= []
47
+ @headers ||= []
48
+ @row_class ||= params[:row_class]
49
+ @row_class ||= Table_Row
47
50
  @row_css_classes = params[:row_css_classes]
48
51
  @row_css_classes ||= []
52
+ @row_css_classes = [ @row_css_classes ] unless @row_css_classes.is_a?(Array)
49
53
  @column_css_classes = params[:column_css_classes]
50
54
  @column_css_classes ||= []
51
- super(@options, &block)
55
+ @column_css_classes = [ @column_css_classes ] unless @column_css_classes.is_a?(Array)
56
+ params[:cellpadding] = 0 unless params[:cellpadding]
57
+ params[:cellspacing] = 0 unless params[:cellspacing]
58
+ params.delete(:headers)
59
+ params.delete(:num_columns)
60
+ params.delete(:row_css_classes)
61
+ params.delete(:column_css_classes)
62
+ set_headers(@headers)
63
+ super(params, &block)
64
+ end
65
+
66
+ def headers=(headers)
67
+ @headers = headers
68
+ if @headers.length > 0 then
69
+ @headers.map! { |cell|
70
+ if cell.is_a? Element then
71
+ cell
72
+ else
73
+ HTML.th { cell }
74
+ end
75
+ }
76
+ end
52
77
  end
78
+ alias set_headers headers=
53
79
 
54
- def add_row(row_data)
55
- row = Table_Row.new(row_data, :parent => self)
80
+ def add_row(*row_data)
81
+ if row_data.first.is_a?(Array) then
82
+ row_data = row_data.first
83
+ end
84
+ # TODO: This should happen in #string
85
+ row = @row_class.new(row_data, :parent => self)
56
86
  @rows << row
57
87
 
58
88
  # Add row content to columns
@@ -64,9 +94,12 @@ module GUI
64
94
  end
65
95
 
66
96
  def string
67
- @content = "\n"
68
- @content << HTML.tr { @headers.collect { |cell| HTML.th { cell } }.join }.string
69
- @content << @rows.collect { |row| row.string }.join("\n")
97
+ t = []
98
+ if @headers.length > 0 then
99
+ t = HTML.tr { @headers.collect { |cell| if cell.is_a? Element then cell else HTML.th { cell } end } }
100
+ end
101
+ t += rows()
102
+ set_content(t)
70
103
  super()
71
104
  end
72
105
  alias to_s string
@@ -75,20 +108,21 @@ module GUI
75
108
  end
76
109
 
77
110
  def set_data(row_array)
111
+ @rows = []
78
112
  row_array.each { |row|
79
- @rows << Table_Row.new(row, :parent => self)
113
+ @rows << @row_class.new(row, :parent => self)
80
114
  }
81
115
  end
82
116
 
83
117
  def cell(column, row)
84
- @rows[row][column]
118
+ rows[row][column]
85
119
  end
86
120
 
87
121
  def [](row_index)
88
- @rows[row_index]
122
+ rows[row_index]
89
123
  end
90
124
  def []=(row_index, row_data)
91
- @rows[row_index] = row_data
125
+ rows[row_index] = row_data
92
126
  end
93
127
 
94
128
  def columns
@@ -98,34 +132,57 @@ module GUI
98
132
  end
99
133
 
100
134
  class Table_Row < Element
135
+
136
+ attr_accessor :cells, :parent
137
+
101
138
  def initialize(cell_data, params={})
102
- @cell_data = cell_data
103
- @cells = []
139
+ params[:tag] = :tr
140
+
141
+ @parent ||= params[:parent]
142
+ @cell_class ||= Table_Cell
143
+ @cell_data ||= cell_data
144
+ @cells ||= []
145
+ @touched = false
146
+ column_index = 0
147
+
104
148
  @cell_data.each { |cell|
105
- @cells << Table_Cell.new(cell, :parent => self)
149
+ @cells << @cell_class.new(cell, :parent => self, :column_index => column_index)
150
+ column_index += 1
106
151
  }
107
- params[:tag] = 'tr'
152
+ set_content(@cells)
153
+ @content = @cells
154
+
108
155
  super(params)
156
+ add_css_classes(@parent.row_css_classes) if @parent.row_css_classes.length > 0
109
157
  end
110
158
 
111
159
  def table
112
- @params[:parent]
160
+ @parent
113
161
  end
114
162
 
115
163
  def [](column_index)
116
164
  @cells[column_index]
117
165
  end
118
166
  def []=(column_index, cell_data)
167
+ touch()
119
168
  @cells[column_index].value = cell_data
120
169
  end
121
170
 
122
171
  def string
123
- @content = "\n" << @cells.collect { |c| c.string }.join()
172
+ if @touched then
173
+ @cells = []
174
+ @cell_data.each { |cell|
175
+ @cells << @cell_class.new(cell, :parent => self, :column_index => column_index)
176
+ column_index += 1
177
+ }
178
+ set_content(@cells)
179
+ @touched = false
180
+ end
124
181
  super()
125
182
  end
126
183
 
127
184
  def inspect
128
- '[' << @cells.collect { |c| c.value }.join(',') + ']'
185
+ 'Row[' << @cells.collect { |c| c.inspect }.join(',') + ']'
129
186
  end
130
187
  end
131
188
 
@@ -152,37 +209,34 @@ module GUI
152
209
  #
153
210
  class Table_Cell < Element
154
211
 
155
- attr_accessor :value, :presentation_class
212
+ attr_accessor :value, :presentation_class, :parent
156
213
 
157
214
  def initialize(cell_element, params={})
158
- params[:tag] = 'td'
159
- @presentation_class = false
215
+ params[:tag] = :td
216
+ @content ||= cell_element
217
+ @value ||= cell_element
218
+ @presentation_class ||= false
219
+ @column_index = params[:column_index]
220
+ @parent ||= params[:parent]
221
+ params.delete(:column_index)
160
222
  super(params)
161
- @value = cell_element
162
- @content = cell_element
223
+ column_css_classes = @parent.parent.column_css_classes[@column_index]
224
+ add_css_classes(column_css_classes) if column_css_classes
163
225
  end
164
226
 
165
- def value=(val)
166
- @value = val
167
- @content = val.to_s
168
- if @presentation_class then
169
- @content = @presentation_class.new(@value).string
170
- else
171
- @content = @value.to_s
172
- end
173
- end
174
-
175
227
  def set_presentation(presentation_class)
176
228
  @presentation_class = presentation_class
177
229
  end
230
+ alias presentation= set_presentation
178
231
 
179
- def string
180
- if @presentation_class then
181
- @content = @presentation_class.new(@value).string
182
- else
183
- @content = @value.to_s
184
- end
185
- super()
232
+ def value=(val)
233
+ @value = val
234
+ set_content(@value) unless @presentation_class
235
+ set_content(@presentation_class.new(@value)) if @presentation_class
236
+ end
237
+
238
+ def inspect
239
+ 'Cell[' << @value + ']'
186
240
  end
187
241
 
188
242
  end
@@ -0,0 +1,27 @@
1
+
2
+ require('delegate')
3
+ require('aurita-gui/element')
4
+
5
+ module Aurita
6
+ module GUI
7
+
8
+ class Widget < DelegateClass(Element)
9
+ include Aurita::GUI
10
+
11
+ def initialize()
12
+ super(element())
13
+ end
14
+
15
+ def element
16
+ raise ::Exception.new('Missing method #element for ' << self.class.to_s)
17
+ end
18
+
19
+ def js_initialize
20
+ ''
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+ end
27
+