aurita-gui 0.3.5 → 0.3.6
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.
- data/History.txt +12 -0
- data/README +10 -0
- data/aurita-gui.gemspec +4 -4
- data/lib/aurita-gui/builtest.rb +15 -0
- data/lib/aurita-gui/element.rb +37 -0
- data/lib/aurita-gui/form.rb +1 -0
- data/lib/aurita-gui/form/boolean_field.rb +14 -0
- data/lib/aurita-gui/form/checkbox_field.rb +15 -32
- data/lib/aurita-gui/form/date_field.rb +1 -0
- data/lib/aurita-gui/form/form_field.rb +1 -1
- data/lib/aurita-gui/form/options_field.rb +58 -51
- data/lib/aurita-gui/form/radio_field.rb +8 -14
- data/lib/aurita-gui/form/select_field.rb +10 -16
- data/lib/aurita-gui/form/selection_list.rb +181 -0
- data/lib/aurita-gui/html.rb +39 -6
- data/lib/aurita-gui/selection_list_test.rb +12 -0
- data/spec/javascript.rb +2 -2
- data/spec/options.rb +44 -0
- data/spec/selection_list.rb +17 -0
- metadata +20 -6
data/History.txt
CHANGED
@@ -1,4 +1,16 @@
|
|
1
1
|
|
2
|
+
== 0.3.6 / 2009-01-31
|
3
|
+
|
4
|
+
* Added Selection_List_Field.
|
5
|
+
* Several minor fixes in Options_Field, and Form
|
6
|
+
* Added more specs.
|
7
|
+
|
8
|
+
== 0.3.5 / 2009-01-27
|
9
|
+
|
10
|
+
* Element instances now behave like arrays (nested content is delegated).
|
11
|
+
* Object trees now can be iterated recursively via Element#recurse { |element| ... }
|
12
|
+
* Added more specs.
|
13
|
+
|
2
14
|
=== 0.3.4 / 2009-01-26
|
3
15
|
|
4
16
|
* Added retreiving elements from object tree
|
data/README
ADDED
data/aurita-gui.gemspec
CHANGED
@@ -12,14 +12,14 @@ of primitive and complex HTML elements, such as tables and forms.
|
|
12
12
|
It is a core module of the Aurita application framework, but it can be used
|
13
13
|
as stand-alone library in any context (such as rails).
|
14
14
|
As there seems to be a lack of ruby form generators, i decided to release this
|
15
|
-
part of Aurita in a single gem with no dependencies.
|
15
|
+
part of Aurita in a single gem with no dependencies on aurita itself.
|
16
16
|
EOF
|
17
|
-
s.version = '0.3.
|
17
|
+
s.version = '0.3.6'
|
18
18
|
s.author = 'Tobias Fuchs'
|
19
|
-
s.email = 'fuchs@
|
19
|
+
s.email = 'fuchs@wortundform.de'
|
20
20
|
s.date = Time.now
|
21
21
|
s.files = '*.rb'
|
22
|
-
|
22
|
+
s.add_dependency('arrayfields', '>= 4.6.0')
|
23
23
|
s.files = FileList['*',
|
24
24
|
'lib/*',
|
25
25
|
'lib/aurita-gui/*',
|
data/lib/aurita-gui/element.rb
CHANGED
@@ -471,6 +471,43 @@ module GUI
|
|
471
471
|
end
|
472
472
|
|
473
473
|
end # class
|
474
|
+
|
475
|
+
class Buffered_Element < Element
|
476
|
+
|
477
|
+
def initialize(buffer, *args, &block)
|
478
|
+
@output_buffer = buffer
|
479
|
+
super(*args, &block)
|
480
|
+
end
|
481
|
+
|
482
|
+
def method_missing(meth, value=nil, &block)
|
483
|
+
if block_given? then
|
484
|
+
@attrib[:class] = meth
|
485
|
+
@attrib.update(value) if value.is_a? Hash
|
486
|
+
c = yield
|
487
|
+
c = [ c ] unless c.is_a?(Array)
|
488
|
+
__setobj__(c)
|
489
|
+
|
490
|
+
return string()
|
491
|
+
elsif !value.nil? && !meth.to_s.include?('=') then
|
492
|
+
@attrib[:class] = meth
|
493
|
+
case value
|
494
|
+
when Hash then
|
495
|
+
@attrib.update(value)
|
496
|
+
c = value[:content]
|
497
|
+
c = [ c ] if (c && !c.is_a?(Array))
|
498
|
+
__setobj__(c) if c
|
499
|
+
when String then
|
500
|
+
__setobj__([value])
|
501
|
+
end
|
502
|
+
|
503
|
+
return string()
|
504
|
+
else
|
505
|
+
return @attrib[meth] unless value or meth.to_s.include? '='
|
506
|
+
@attrib[meth.to_s.gsub('=','').intern] = value
|
507
|
+
end
|
508
|
+
end
|
509
|
+
|
510
|
+
end
|
474
511
|
|
475
512
|
end # module
|
476
513
|
end # module
|
data/lib/aurita-gui/form.rb
CHANGED
@@ -11,6 +11,20 @@ module GUI
|
|
11
11
|
params[:options] = [ 1 ]
|
12
12
|
super(params, &block)
|
13
13
|
end
|
14
|
+
def option_elements
|
15
|
+
elements = []
|
16
|
+
options().each_pair { |k,v|
|
17
|
+
box_attribs = { :type => :checkbox,
|
18
|
+
:value => k,
|
19
|
+
:name => @attrib[:name] }
|
20
|
+
checked = (@value && k.to_s == @value.to_s)? true : nil
|
21
|
+
box_attribs[:checked] = checked
|
22
|
+
element = []
|
23
|
+
element << HTML.input(box_attribs)
|
24
|
+
elements << element
|
25
|
+
}
|
26
|
+
elements
|
27
|
+
end
|
14
28
|
end
|
15
29
|
|
16
30
|
end
|
@@ -10,40 +10,23 @@ module GUI
|
|
10
10
|
#
|
11
11
|
class Checkbox_Field < Options_Field
|
12
12
|
def option_elements
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
:value => k,
|
23
|
-
:name => @attrib[:name] }
|
24
|
-
box_attribs[:checked] = true if (v.to_s == @value.to_s)
|
25
|
-
options << HTML.input(box_attribs)
|
26
|
-
}
|
27
|
-
elsif option.kind_of? Element then
|
28
|
-
# Option element has been built externally, like in
|
29
|
-
#
|
30
|
-
# @options = [ HTML.option(:class => :custom, :label => 'built manually') ]
|
31
|
-
#
|
32
|
-
option.name = @attrib[:name]
|
33
|
-
option.tag = :input
|
34
|
-
option.type = :checkbox
|
35
|
-
options << option
|
36
|
-
else
|
37
|
-
# For @options = [ :foo, :bar, 'wombat' ],
|
38
|
-
#
|
39
|
-
box_attribs = { :type => :checkbox,
|
40
|
-
:value => option.to_s,
|
41
|
-
:name => @attrib[:name] }
|
42
|
-
box_attribs[:checked] = true if (option.to_s == @value.to_s)
|
43
|
-
options << HTML.input(box_attribs)
|
13
|
+
elements = []
|
14
|
+
options().each_pair { |k,v|
|
15
|
+
box_attribs = { :type => :checkbox,
|
16
|
+
:value => k,
|
17
|
+
:name => @attrib[:name] }
|
18
|
+
if @value.is_a?(Array) then
|
19
|
+
checked = (@value.map { |v| v.to_s }.include?(k))? true : nil
|
20
|
+
else
|
21
|
+
checked = (@value && k.to_s == @value.to_s)? true : nil
|
44
22
|
end
|
23
|
+
box_attribs[:checked] = checked
|
24
|
+
element = []
|
25
|
+
element << HTML.input(box_attribs)
|
26
|
+
element << HTML.label(:for => option_id) { v } if v
|
27
|
+
elements << element
|
45
28
|
}
|
46
|
-
|
29
|
+
elements
|
47
30
|
end
|
48
31
|
|
49
32
|
def element
|
@@ -1,5 +1,6 @@
|
|
1
1
|
|
2
2
|
require('aurita-gui/form/form_field')
|
3
|
+
require('arrayfields')
|
3
4
|
|
4
5
|
module Aurita
|
5
6
|
module GUI
|
@@ -69,14 +70,9 @@ module GUI
|
|
69
70
|
# Set option values as array, labels as hash:
|
70
71
|
#
|
71
72
|
# s1 = Select_Field.new(:name => :test,
|
72
|
-
# :label => { 'Pick one' => [ 'foo', 'bar', 'wombat' ] },
|
73
|
-
# :options => [ 1,2,3 ])
|
74
|
-
# Same as
|
75
|
-
#
|
76
|
-
# s1 = Select_Field.new(:name => :test,
|
77
73
|
# :label => 'Pick one',
|
78
|
-
# :option_labels => [ 'foo', 'bar', 'wombat' ],
|
79
|
-
# :
|
74
|
+
# :option_labels => [ 'foo', 'bar', 'wombat' ] },
|
75
|
+
# :option_values => [ 1,2,3 ])
|
80
76
|
# Ranges are ok, too:
|
81
77
|
#
|
82
78
|
# s1 = Select_Field.new(:name => :test,
|
@@ -93,20 +89,11 @@ module GUI
|
|
93
89
|
# Change option labels using a hash.
|
94
90
|
# Compared to using an array, this is useful
|
95
91
|
# in case you don't know the order of option
|
96
|
-
# values.
|
92
|
+
# values. You can also rename some or all option
|
93
|
+
# labels this way.
|
97
94
|
#
|
98
95
|
# s1.option_labels = { 1 => 'ras', 2 => 'dwa', 3 => 'tri' }
|
99
96
|
#
|
100
|
-
# Overwriting the labels field does the same,
|
101
|
-
# but this way, you also can change the
|
102
|
-
# field label:
|
103
|
-
#
|
104
|
-
# s1.label = { 'Select one' => [ 'foo', 'bar', 'wombat' ] }
|
105
|
-
#
|
106
|
-
# Or
|
107
|
-
#
|
108
|
-
# s1.label = { 'Select one' => { 1 => 'foo', 2 => 'bar', 3 => 'wombat' } }
|
109
|
-
#
|
110
97
|
# Of yourse you can replace all option values
|
111
98
|
# and their labels at once by overwriting
|
112
99
|
# the options field:
|
@@ -116,20 +103,18 @@ module GUI
|
|
116
103
|
#
|
117
104
|
class Options_Field < Form_Field
|
118
105
|
|
119
|
-
attr_accessor :
|
106
|
+
attr_accessor :option_values, :option_labels
|
107
|
+
attr_reader :value
|
120
108
|
|
121
109
|
def initialize(params, &block)
|
122
|
-
@options = params[:options]
|
123
|
-
@options = @options.to_a if @options.kind_of? Range
|
124
|
-
@options_range = params[:options_range]
|
125
110
|
@option_labels = params[:option_labels]
|
126
|
-
set_option_labels(params[:option_labels]) if params[:option_labels]
|
127
111
|
@option_labels ||= []
|
128
|
-
@
|
129
|
-
@
|
130
|
-
@option_elements ||= []
|
112
|
+
@option_values = params[:option_values]
|
113
|
+
@option_values ||= []
|
131
114
|
@value = params[:value]
|
132
115
|
|
116
|
+
set_options(params[:options]) if params[:options]
|
117
|
+
|
133
118
|
if block_given? then
|
134
119
|
yield.each { |option|
|
135
120
|
add_option(option)
|
@@ -140,52 +125,74 @@ module GUI
|
|
140
125
|
params.delete(:options)
|
141
126
|
# Option fields don't have a value attribute themselves
|
142
127
|
params.delete(:value)
|
143
|
-
params.delete(:
|
128
|
+
params.delete(:option_values)
|
144
129
|
params.delete(:option_labels)
|
145
130
|
super(params)
|
146
131
|
end
|
147
132
|
|
133
|
+
def options
|
134
|
+
@option_labels = @option_values.dup unless @option_labels.length > 0
|
135
|
+
@option_labels.fields = @option_values.map { |v| v.to_s }
|
136
|
+
@option_labels
|
137
|
+
end
|
138
|
+
|
139
|
+
def option_hash
|
140
|
+
options().to_hash
|
141
|
+
end
|
142
|
+
|
148
143
|
def option_labels=(labels)
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
144
|
+
# [ :red, :blue, :green, :yellow ]
|
145
|
+
if labels.kind_of? Array then
|
146
|
+
@option_labels = labels
|
147
|
+
# { 0 => :red, 1 => :blue }
|
148
|
+
elsif labels.kind_of? Hash then
|
149
|
+
@option_labels ||= []
|
150
|
+
@option_values ||= []
|
151
|
+
# Ensure keys and values are strings:
|
152
|
+
labels.each_pair { |k,v|
|
153
|
+
labels.delete(k)
|
154
|
+
labels[k.to_s] = v
|
156
155
|
}
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
renamed_options[value] = labels[index]
|
161
|
-
index += 1
|
156
|
+
opt_field = options()
|
157
|
+
labels.sort.each { |pair|
|
158
|
+
opt_field[pair[0].to_s] = pair[1]
|
162
159
|
}
|
163
160
|
end
|
164
|
-
@options = renamed_options
|
165
|
-
@option_elements = [ @options ]
|
166
161
|
end
|
167
162
|
alias set_option_labels option_labels=
|
168
163
|
|
169
164
|
def add_option(option={})
|
170
165
|
if option.kind_of? Array then
|
171
|
-
@
|
172
|
-
elsif option.kind_of? Hash then
|
173
|
-
@option_elements << options
|
166
|
+
@option_values += option
|
174
167
|
elsif option.kind_of? Range then
|
175
|
-
@
|
168
|
+
@option_values += option.to_a
|
169
|
+
elsif option.kind_of? Hash then
|
170
|
+
# @option_elements << options
|
176
171
|
end
|
177
|
-
# @option_elements << option
|
178
172
|
end
|
179
173
|
|
180
174
|
def options=(options)
|
181
175
|
if options.kind_of? Array then
|
182
|
-
@
|
183
|
-
|
184
|
-
@option_elements = [ options ]
|
176
|
+
@option_values = options
|
177
|
+
@option_labels = options
|
185
178
|
elsif options.kind_of? Range then
|
186
|
-
@
|
179
|
+
@option_values = options.to_a
|
180
|
+
@option_labels = options.to_a
|
181
|
+
elsif options.kind_of? Hash then
|
182
|
+
@option_values = []
|
183
|
+
@option_labels = []
|
184
|
+
options.sort.each { |pair|
|
185
|
+
@option_values << pair[0]
|
186
|
+
@option_labels << pair[1]
|
187
|
+
}
|
187
188
|
end
|
188
189
|
end
|
190
|
+
alias set_options options=
|
191
|
+
|
192
|
+
def value=(value)
|
193
|
+
@value = value
|
194
|
+
end
|
195
|
+
alias set_value value=
|
189
196
|
|
190
197
|
def [](index)
|
191
198
|
@option_elements[index]
|
@@ -195,7 +202,7 @@ module GUI
|
|
195
202
|
end
|
196
203
|
|
197
204
|
def element
|
198
|
-
raise Form_Error.new('Method #element from Abstract class Options_Field has not been overloaded.
|
205
|
+
raise Form_Error.new('Method #element from Abstract class Options_Field has not been overloaded in ' << self.class.to_s)
|
199
206
|
end
|
200
207
|
|
201
208
|
def content
|
@@ -24,24 +24,18 @@ module GUI
|
|
24
24
|
class Radio_Field < Options_Field
|
25
25
|
|
26
26
|
# Returns array of option elements for this
|
27
|
-
# radio field.
|
27
|
+
# radio field, i.e. an array of <input type="checkbox" ... />.
|
28
28
|
# Each option is a radio input field with
|
29
29
|
# @name as common name attribute.
|
30
30
|
def option_elements
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
elsif option.kind_of? Element then
|
38
|
-
option.name = @attrib[:name]
|
39
|
-
option.tag = :input
|
40
|
-
option.type = :radio
|
41
|
-
options << option
|
42
|
-
end
|
31
|
+
elements = []
|
32
|
+
options().each_pair { |k,v|
|
33
|
+
element = []
|
34
|
+
element << HTML.input(:type => :radio, :value => k, :name => @attrib[:name] )
|
35
|
+
element << HTML.label(:for => option_id) { v } if v
|
36
|
+
elements << element
|
43
37
|
}
|
44
|
-
|
38
|
+
elements
|
45
39
|
end
|
46
40
|
|
47
41
|
# Renders this radio field to a HTML.ul element,
|
@@ -5,24 +5,18 @@ module Aurita
|
|
5
5
|
module GUI
|
6
6
|
|
7
7
|
class Select_Field < Options_Field
|
8
|
+
|
9
|
+
# Returns array of option elements for this
|
10
|
+
# select field.
|
8
11
|
def option_elements
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
selected = (@value && k.to_s == @value.to_s)? true : nil
|
14
|
-
options << HTML.option(:value => k, :selected => selected) { v }
|
15
|
-
}
|
16
|
-
elsif option.kind_of? Element then
|
17
|
-
option.tag = :option
|
18
|
-
options << option
|
19
|
-
else
|
20
|
-
selected = (@value && option.to_s == @value.to_s)? true : nil
|
21
|
-
options << HTML.option(:value => option, :selected => selected) { option }
|
22
|
-
end
|
12
|
+
elements = []
|
13
|
+
options().each_pair { |k,v|
|
14
|
+
selected = (@value && k.to_s == @value.to_s)? true : nil
|
15
|
+
elements << HTML.option(:value => k, :selected => selected) { v }
|
23
16
|
}
|
24
|
-
|
17
|
+
elements
|
25
18
|
end
|
19
|
+
|
26
20
|
def element
|
27
21
|
HTML.select(@attrib) {
|
28
22
|
option_elements()
|
@@ -30,7 +24,7 @@ module GUI
|
|
30
24
|
end
|
31
25
|
def readonly_element
|
32
26
|
HTML.div(@attrib) {
|
33
|
-
|
27
|
+
options()[@value]
|
34
28
|
}
|
35
29
|
end
|
36
30
|
end
|
@@ -0,0 +1,181 @@
|
|
1
|
+
|
2
|
+
require('aurita-gui/form/form_field')
|
3
|
+
require('aurita-gui/form/select_field')
|
4
|
+
require('aurita-gui/form/options_field')
|
5
|
+
|
6
|
+
module Aurita
|
7
|
+
module GUI
|
8
|
+
|
9
|
+
class Selection_List_Option_Field < Form_Field
|
10
|
+
def initialize(params={})
|
11
|
+
params.delete(:selection_list_params)
|
12
|
+
super(params)
|
13
|
+
end
|
14
|
+
def element
|
15
|
+
HTML.div(:id => "selection_option_#{@attrib[:name]}_#{@value}", :class => :selection_list_option) {
|
16
|
+
HTML.label { @label.to_s } +
|
17
|
+
Hidden_Field.new(:value => @value, :name => @attrib[:name].to_s + '_selected[]')
|
18
|
+
}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# A selection list maintains a list of
|
23
|
+
# available options and a select field of
|
24
|
+
# further available options.
|
25
|
+
#
|
26
|
+
# == Example:
|
27
|
+
#
|
28
|
+
# Selection_List_Field.new(:name => :the_list,
|
29
|
+
# :value => ['10','30' ] # Active selection from options
|
30
|
+
# :options => { '10' => :blue, # List of all options
|
31
|
+
# '20' => :red,
|
32
|
+
# '30' => :green }
|
33
|
+
#
|
34
|
+
# In the example, any combination of 'blue',
|
35
|
+
# 'red' and 'green' could be selected,
|
36
|
+
# here it is 'blue' and 'green'.
|
37
|
+
# The select field contains 20 => 'red',
|
38
|
+
# the only additionally available option
|
39
|
+
# (as it is not already set in value).
|
40
|
+
#
|
41
|
+
# Use case: Assign user to categories.
|
42
|
+
# Options is all available categories,
|
43
|
+
# value is an array of category ids
|
44
|
+
# already assigned to this user.
|
45
|
+
#
|
46
|
+
# == Customization
|
47
|
+
#
|
48
|
+
# You can override the Form_Field class
|
49
|
+
# rendering the option list elements, as
|
50
|
+
# well as the Form_Field class rendering
|
51
|
+
# the select field.
|
52
|
+
#
|
53
|
+
# Use #option_field_decorator to override
|
54
|
+
# rendering of option fields.
|
55
|
+
# Default is Selection_List_Option_Field.
|
56
|
+
#
|
57
|
+
# Example for custom option field decorator:
|
58
|
+
#
|
59
|
+
# class Delete_Option_Decorator < Form_Field
|
60
|
+
# def element
|
61
|
+
# HTML.div {
|
62
|
+
# HTML.img(:src => '/images/delete.png') + @label
|
63
|
+
# }
|
64
|
+
# end
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# selection_list.option_field_decorator = Delete_Option_Decorator
|
68
|
+
#
|
69
|
+
# Example for custom select field class:
|
70
|
+
#
|
71
|
+
# Note that Selection_List_Field will initialize
|
72
|
+
# @select_field_class with parameter :options, a
|
73
|
+
# key / label map of available options.
|
74
|
+
#
|
75
|
+
# class Ajax_Selection_Field < Form_Field
|
76
|
+
# def initialize(params={})
|
77
|
+
# super(params)
|
78
|
+
# params[:onchange] = js.do_something.ajaxian.with(:this, @options)
|
79
|
+
# end
|
80
|
+
# def element
|
81
|
+
# HTML.div {
|
82
|
+
# Input_Field.new(@attrib)
|
83
|
+
# }
|
84
|
+
# end
|
85
|
+
# end
|
86
|
+
#
|
87
|
+
# selection_list.select_field_class = Ajax_Selection_Field
|
88
|
+
#
|
89
|
+
class Selection_List_Field < Options_Field
|
90
|
+
|
91
|
+
attr_accessor :option_field_decorator, :select_field_class, :selectable_options
|
92
|
+
|
93
|
+
def initialize(params={})
|
94
|
+
@option_field_decorator = params[:option_field]
|
95
|
+
@select_field_class = params[:select_field]
|
96
|
+
@option_field_decorator ||= Selection_List_Option_Field
|
97
|
+
@select_field_class ||= Select_Field
|
98
|
+
@selectable_options = params[:selectable_options]
|
99
|
+
@selectable_options ||= []
|
100
|
+
|
101
|
+
params.delete(:option_field)
|
102
|
+
params.delete(:select_field)
|
103
|
+
params.delete(:selectable_options)
|
104
|
+
super(params)
|
105
|
+
set_value(@value)
|
106
|
+
add_css_class(:selection_list_field)
|
107
|
+
end
|
108
|
+
|
109
|
+
# Set list of all options as value / label hash.
|
110
|
+
def options=(options)
|
111
|
+
super(options)
|
112
|
+
if !@selectable_options || @selectable_options.length == 0 then
|
113
|
+
options().each_pair { |value, name|
|
114
|
+
@selectable_options << value unless (@value.is_a?(Array) && @value.map { |v| v.to_s }.include?(value.to_s))
|
115
|
+
}
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# Set list of active selection options as array.
|
120
|
+
def value=(value)
|
121
|
+
super(value)
|
122
|
+
@selectable_options = []
|
123
|
+
options().each_pair { |value, name|
|
124
|
+
@selectable_options << value unless @value.map { |v| v.to_s }.include? value.to_s
|
125
|
+
}
|
126
|
+
end
|
127
|
+
alias set_value value=
|
128
|
+
|
129
|
+
# Returns array of available options,
|
130
|
+
# decorated by @option_field_decorator
|
131
|
+
# (see comments on Selection_List_Field).
|
132
|
+
def option_elements
|
133
|
+
id_prefix = "#{@attrib[:id]}_" if @attrib[:id]
|
134
|
+
elements = []
|
135
|
+
options().each_pair { |opt_value, opt_label|
|
136
|
+
selected = @value.map { |v| v.to_s }.include?(opt_value.to_s)
|
137
|
+
if selected then
|
138
|
+
elements << HTML.li(:id => "#{id_prefix}#{@attrib[:name]}_#{opt_value}") {
|
139
|
+
@option_field_decorator.new(:name => @attrib[:name],
|
140
|
+
:value => opt_value,
|
141
|
+
:label => opt_label,
|
142
|
+
:parent => self)
|
143
|
+
}
|
144
|
+
end
|
145
|
+
}
|
146
|
+
elements
|
147
|
+
end
|
148
|
+
|
149
|
+
# Renders list of active options, as well
|
150
|
+
# as select field element containing additionally
|
151
|
+
# available options.
|
152
|
+
def element
|
153
|
+
select_options = {}
|
154
|
+
@selectable_options.each { |v|
|
155
|
+
select_options[v] = @option_labels[v]
|
156
|
+
}
|
157
|
+
id_prefix = "#{@attrib[:id]}_" if @attrib[:id]
|
158
|
+
|
159
|
+
if @value && @value.length > 0 then
|
160
|
+
HTML.div(@attrib) {
|
161
|
+
HTML.ul(:id => "#{id_prefix}#{@attrib[:name]}") {
|
162
|
+
option_elements()
|
163
|
+
} +
|
164
|
+
@select_field_class.new(:id => "#{id_prefix}#{@attrib[:name]}_select",
|
165
|
+
:options => select_options,
|
166
|
+
:name => "#{@attrib[:name]}" )
|
167
|
+
}
|
168
|
+
else
|
169
|
+
HTML.div(@attrib) {
|
170
|
+
@select_field_class.new(:id => "#{id_prefix}#{@attrib[:name]}_select",
|
171
|
+
:options => select_options,
|
172
|
+
:name => "#{@attrib[:name]}" )
|
173
|
+
}
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
data/lib/aurita-gui/html.rb
CHANGED
@@ -298,11 +298,6 @@ module GUI
|
|
298
298
|
Element.new(:tag => :pseudo) { self.class_eval(&block) }
|
299
299
|
end
|
300
300
|
|
301
|
-
|
302
|
-
def self.a(*attribs, &block)
|
303
|
-
render(:a, *attribs, &block)
|
304
|
-
end
|
305
|
-
|
306
301
|
# p is defined in Kernel, so we have to
|
307
302
|
# redirect it manually (method_missing won't be
|
308
303
|
# triggered for it)
|
@@ -315,7 +310,7 @@ module GUI
|
|
315
310
|
end
|
316
311
|
|
317
312
|
def self.puts(arg)
|
318
|
-
arg
|
313
|
+
HTML.build { arg }
|
319
314
|
end
|
320
315
|
|
321
316
|
=begin
|
@@ -331,6 +326,44 @@ EOC
|
|
331
326
|
=end
|
332
327
|
|
333
328
|
end # class
|
329
|
+
|
330
|
+
class Builder
|
331
|
+
|
332
|
+
def initialize(&block)
|
333
|
+
raise ::Exception.new('Missing block for Builder') unless block_given?
|
334
|
+
@output_buffer = ''
|
335
|
+
instance_eval(&block)
|
336
|
+
end
|
337
|
+
|
338
|
+
# Statically defined as <br /> must not have any attributes.
|
339
|
+
def br
|
340
|
+
@output_buffer << '<br />'
|
341
|
+
end
|
342
|
+
|
343
|
+
def render(meth_name, *args, &block)
|
344
|
+
raise ::Exception.new('Missing attributes for HTML.' << meth_name.inspect) unless args
|
345
|
+
e = Buffered_Element.new(@output_buffer, *args, &block)
|
346
|
+
e.tag = meth_name
|
347
|
+
@output_buffer << e.string
|
348
|
+
e
|
349
|
+
end
|
350
|
+
|
351
|
+
# p is defined in Kernel, so we have to
|
352
|
+
# redirect it manually (method_missing won't be
|
353
|
+
# triggered for it)
|
354
|
+
def p(*attribs, &block)
|
355
|
+
render(:p, *attribs, &block)
|
356
|
+
end
|
357
|
+
|
358
|
+
def method_missing(meth_name, *attribs, &block)
|
359
|
+
render(meth_name, *attribs, &block)
|
360
|
+
end
|
361
|
+
|
362
|
+
def to_s
|
363
|
+
@output_buffer
|
364
|
+
end
|
365
|
+
|
366
|
+
end
|
334
367
|
|
335
368
|
end # module
|
336
369
|
end # module
|
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
require('aurita')
|
3
|
+
Aurita.load_project :default
|
4
|
+
Aurita.import_module :gui, :custom_form_elements
|
5
|
+
|
6
|
+
include Aurita::GUI
|
7
|
+
|
8
|
+
sl = Selection_List_Field.new(:name => :the_list,
|
9
|
+
:value => ['10','30' ]
|
10
|
+
:options => { '10' => :blue, '20' => :red, '30' => :green }
|
11
|
+
|
12
|
+
puts sl.to_s.gsub('><',">\n<")
|
data/spec/javascript.rb
CHANGED
@@ -24,11 +24,11 @@ describe Aurita::GUI::Javascript, "basic rendering" do
|
|
24
24
|
|
25
25
|
it "should extend class HTML by a helper method '.js'" do
|
26
26
|
e = HTML.build {
|
27
|
-
div.outer(:onclick => js.Wombat.alert('message')) {
|
27
|
+
div.outer(:id => :unit, :onclick => js.Wombat.alert('message')) {
|
28
28
|
p.inner 'click me'
|
29
29
|
}
|
30
30
|
}
|
31
|
-
e.to_s.should == '
|
31
|
+
e[:unit].onclick.to_s.should == 'Wombat.alert(\'message\'); '
|
32
32
|
end
|
33
33
|
|
34
34
|
end
|
data/spec/options.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
|
2
|
+
require('rubygems')
|
3
|
+
require('aurita-gui/form')
|
4
|
+
|
5
|
+
include Aurita::GUI
|
6
|
+
|
7
|
+
describe Aurita::GUI::Options_Field, "values and labels" do
|
8
|
+
before do
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should allow setting option as array, using values as labels" do
|
12
|
+
s = Select_Field.new(:name => :color)
|
13
|
+
s.value = :red
|
14
|
+
s.options = [ :blue, :red, :green, :yellow ]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should allow setting values as Hash (key / value map)" do
|
18
|
+
s = Select_Field.new(:name => :color)
|
19
|
+
s.value = 20
|
20
|
+
s.options = { 10 => :blue, 20 => :red, 30 => :green, 40 => :yellow }
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be possible to set values and labels seperately" do
|
24
|
+
s = Select_Field.new(:name => :color)
|
25
|
+
s.value = 20
|
26
|
+
s.option_values = [ 10, 20, 30, 40 ]
|
27
|
+
s.option_labels = [ :apple, :orange, :pear, :banana ]
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should be possible to set/rename labels with a hash, mapping { value => label }" do
|
31
|
+
s = Radio_Field.new(:name => :color)
|
32
|
+
s.value = 'foo'
|
33
|
+
s.option_values = [ 10, 'foo', 30, 40 ]
|
34
|
+
s.option_labels = { 10 => :bleu, 'foo' => :rouge }
|
35
|
+
s.options['10'].should == :bleu
|
36
|
+
s.options['10'] = :noire
|
37
|
+
s.options['10'].should == :noire
|
38
|
+
s.options['foo'].should == :rouge
|
39
|
+
s.options['30'].should == 30
|
40
|
+
s.options['40'].should == 40
|
41
|
+
s.options[s.value.to_s].should == :rouge
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
|
2
|
+
require('rubygems')
|
3
|
+
require('aurita-gui/form')
|
4
|
+
|
5
|
+
include Aurita::GUI
|
6
|
+
|
7
|
+
describe Aurita::GUI::Options_Field, "values and labels" do
|
8
|
+
before do
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should provide the same interface as an option field" do
|
12
|
+
s = Selection_List_Field.new(:name => :the_list)
|
13
|
+
s.options = { 0 => :foo, 1 => :bar, 2 => :batz, 3 => :bla, 4 => :gna}
|
14
|
+
s.value = [ 1, 3 ]
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aurita-gui
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tobias Fuchs
|
@@ -9,12 +9,20 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-01-
|
12
|
+
date: 2009-01-31 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: arrayfields
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 4.6.0
|
23
|
+
version:
|
24
|
+
description: Aurita::GUI provides an intuitive and flexible API for object-oriented creation of primitive and complex HTML elements, such as tables and forms. It is a core module of the Aurita application framework, but it can be used as stand-alone library in any context (such as rails). As there seems to be a lack of ruby form generators, i decided to release this part of Aurita in a single gem with no dependencies on aurita itself.
|
25
|
+
email: fuchs@wortundform.de
|
18
26
|
executables: []
|
19
27
|
|
20
28
|
extensions: []
|
@@ -26,6 +34,7 @@ files:
|
|
26
34
|
- History.txt
|
27
35
|
- lib
|
28
36
|
- samples
|
37
|
+
- README
|
29
38
|
- aurita-gui.gemspec
|
30
39
|
- bin
|
31
40
|
- TODO
|
@@ -38,9 +47,11 @@ files:
|
|
38
47
|
- lib/aurita-gui/button.rb
|
39
48
|
- lib/aurita-gui/form.rb
|
40
49
|
- lib/aurita-gui/html.rb
|
50
|
+
- lib/aurita-gui/builtest.rb
|
41
51
|
- lib/aurita-gui/table.rb
|
42
52
|
- lib/aurita-gui/form
|
43
53
|
- lib/aurita-gui/element.rb
|
54
|
+
- lib/aurita-gui/selection_list_test.rb
|
44
55
|
- lib/aurita-gui/form/password_field.rb
|
45
56
|
- lib/aurita-gui/form/options_field.rb
|
46
57
|
- lib/aurita-gui/form/file_field.rb
|
@@ -51,6 +62,7 @@ files:
|
|
51
62
|
- lib/aurita-gui/form/datetime_field.rb
|
52
63
|
- lib/aurita-gui/form/boolean_field.rb
|
53
64
|
- lib/aurita-gui/form/fieldset.rb
|
65
|
+
- lib/aurita-gui/form/selection_list.rb
|
54
66
|
- lib/aurita-gui/form/select_field.rb
|
55
67
|
- lib/aurita-gui/form/radio_field.rb
|
56
68
|
- lib/aurita-gui/form/hidden_field.rb
|
@@ -67,6 +79,8 @@ files:
|
|
67
79
|
- spec/javascript.rb
|
68
80
|
- spec/form.rb
|
69
81
|
- spec/html.rb
|
82
|
+
- spec/options.rb
|
83
|
+
- spec/selection_list.rb
|
70
84
|
- spec/element.rb
|
71
85
|
has_rdoc: true
|
72
86
|
homepage: http://aurita.wortundform.de/
|