aurita-gui 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,7 +14,7 @@ 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
15
  part of Aurita in a single gem with no dependencies on aurita itself.
16
16
  EOF
17
- s.version = '0.3.6'
17
+ s.version = '0.3.7'
18
18
  s.author = 'Tobias Fuchs'
19
19
  s.email = 'fuchs@wortundform.de'
20
20
  s.date = Time.now
@@ -153,19 +153,18 @@ module GUI
153
153
 
154
154
  def initialize(*args, &block)
155
155
 
156
- params = {}
157
156
  case args[0]
158
157
  when Hash
159
- params = args[0]
158
+ params = args[0]
160
159
  else
161
- params = args[1]
160
+ params = args[1]
162
161
  params ||= {}
163
162
  params[:content] = args[0]
164
163
  end
165
164
 
166
- @@element_count += 1
167
- @id = @@element_count
168
- @parent = params[:parent]
165
+ @@element_count += 1
166
+ @id = @@element_count
167
+ @parent ||= params[:parent]
169
168
  @force_closing_tag = params[:force_closing_tag]
170
169
 
171
170
  params[:tag] = :div if params[:tag].nil?
@@ -222,7 +221,13 @@ module GUI
222
221
  # --> <Element [ <Element 'first'>, <Element 'second'> ] >
223
222
  #
224
223
  def +(other)
225
- return [ self, other ]
224
+ other = [other] unless other.is_a?(Array)
225
+ other.each { |o|
226
+ if o.is_a?(Element) then
227
+ o.parent = @parent if @parent
228
+ end
229
+ }
230
+ return [ self ] + other
226
231
  end
227
232
 
228
233
  # Append object to array of nested elements.
@@ -231,9 +236,13 @@ module GUI
231
236
  # If so, however, other#parent will be set
232
237
  # to this instance.
233
238
  def <<(other)
234
- other.parent = self if other.is_a?(Element)
239
+ if other.is_a?(Element) then
240
+ other.parent = self
241
+ end
235
242
  __getobj__().push(other)
236
243
  end
244
+ alias add_child <<
245
+ alias add_content <<
237
246
 
238
247
  # Returns [ self ], so concatenation with
239
248
  # Arrays and other Element instances works
@@ -303,8 +312,13 @@ module GUI
303
312
  # Set enclosed content of this element.
304
313
  # Will be automatically wrapped in an array.
305
314
  def set_content(obj)
306
- return __setobj__([ obj ]) unless (obj.is_a?(Array))
307
- __setobj__(obj)
315
+ if obj.is_a?(Array) then
316
+ obj.each { |o| o.parent = self if o.is_a?(Element) }
317
+ __setobj__(obj)
318
+ else
319
+ obj.parent = self if obj.is_a?(Element)
320
+ return __setobj__([ obj ])
321
+ end
308
322
  end
309
323
  alias content= set_content
310
324
 
@@ -376,7 +390,7 @@ module GUI
376
390
  }
377
391
 
378
392
  if (!(@force_closing_tag.instance_of?(FalseClass)) &&
379
- [ :div, :label, :button, :textarea ].include?(@tag)) then
393
+ [ :div, :label, :button, :textarea, :ol, :ul ].include?(@tag)) then
380
394
  @force_closing_tag = true
381
395
  end
382
396
  if @force_closing_tag || has_content? then
@@ -469,6 +483,15 @@ module GUI
469
483
  end
470
484
  }
471
485
  end
486
+
487
+ def js_init_code()
488
+ code = js_initialize() if self.respond_to?(:js_initialize)
489
+ code ||= ''
490
+ recurse { |e|
491
+ code << e.js_initialize if e.respond_to?(:js_initialize)
492
+ }
493
+ code
494
+ end
472
495
 
473
496
  end # class
474
497
 
@@ -508,6 +531,13 @@ module GUI
508
531
  end
509
532
 
510
533
  end
534
+
535
+ class PseudoElement < Element
536
+ def initialize(params={}, &block)
537
+ params[:tag] = :pseudo
538
+ super()
539
+ end
540
+ end
511
541
 
512
542
  end # module
513
543
  end # module
@@ -12,6 +12,7 @@ require('aurita-gui/form/checkbox_field')
12
12
  require('aurita-gui/form/fieldset')
13
13
  require('aurita-gui/form/textarea_field')
14
14
  require('aurita-gui/form/date_field')
15
+ require('aurita-gui/form/time_field')
15
16
  require('aurita-gui/form/datetime_field')
16
17
  require('aurita-gui/form/file_field')
17
18
  require('aurita-gui/form/boolean_field')
@@ -74,7 +75,12 @@ module GUI
74
75
  @content = field
75
76
  end
76
77
  field.dom_id = field.name.to_s.gsub('.','_') unless field.dom_id
77
- css_classes = field.class.to_s.split('::')[-1].downcase + '_wrap form_field'
78
+
79
+ # Inherit css classes from decorated field, if any:
80
+ css_classes = field.css_class.map { |c| c.to_s + '_wrap' if c }
81
+ css_classes ||= []
82
+
83
+ css_classes << field.class.to_s.split('::')[-1].downcase + '_wrap form_field'
78
84
  css_classes << ' required' if field.required?
79
85
  css_classes << ' invalid' if field.invalid?
80
86
  params = { :tag => :li,
@@ -14,7 +14,7 @@ module GUI
14
14
  @year_range = params[:year_range]
15
15
  @year_range ||= (2009..2020)
16
16
 
17
- if params[:value] then set_date(params[:value]) end
17
+ set_value(params[:value])
18
18
 
19
19
  params.delete(:value)
20
20
  params.delete(:date_format)
@@ -69,6 +69,8 @@ module GUI
69
69
  end
70
70
 
71
71
  def value=(date)
72
+ return unless date
73
+
72
74
  case date
73
75
  when Hash then
74
76
  @value = date
@@ -81,7 +83,7 @@ module GUI
81
83
  :month => date.month,
82
84
  :day => date.day }
83
85
  when String then
84
- date = (::DateTime).strptime(date, "%Y%m%d")
86
+ date = (::DateTime).strptime(date, "%Y-%m-%d")
85
87
  @value = { :year => date.year,
86
88
  :month => date.month,
87
89
  :day => date.day }
@@ -91,6 +93,7 @@ module GUI
91
93
  @year, @month, @day = @value[:year], @value[:month], @value[:day]
92
94
  end
93
95
  alias set_date value=
96
+ alias set_value value=
94
97
 
95
98
  def value
96
99
  { :day => @day, :month => @month, :year => @year }
@@ -205,6 +205,12 @@ module GUI
205
205
  raise Form_Error.new('Method #element from Abstract class Options_Field has not been overloaded in ' << self.class.to_s)
206
206
  end
207
207
 
208
+ def readonly_element
209
+ opt = options()
210
+ return HTML.div(@attrib) { opt[@value] } if (opt && @value.to_s != '' && opt[@value])
211
+ return HTML.div(@attrib) { @value }
212
+ end
213
+
208
214
  def content
209
215
  option_elements()
210
216
  end
@@ -31,7 +31,8 @@ module GUI
31
31
  elements = []
32
32
  options().each_pair { |k,v|
33
33
  element = []
34
- element << HTML.input(:type => :radio, :value => k, :name => @attrib[:name] )
34
+ selected = (@value.to_s == k.to_s)? true : nil
35
+ element << HTML.input(:type => :radio, :value => k, :name => @attrib[:name], :checked => selected )
35
36
  element << HTML.label(:for => option_id) { v } if v
36
37
  elements << element
37
38
  }
@@ -91,11 +91,11 @@ module GUI
91
91
  attr_accessor :option_field_decorator, :select_field_class, :selectable_options
92
92
 
93
93
  def initialize(params={})
94
- @option_field_decorator = params[:option_field]
95
- @select_field_class = params[:select_field]
94
+ @option_field_decorator ||= params[:option_field]
95
+ @select_field_class ||= params[:select_field]
96
96
  @option_field_decorator ||= Selection_List_Option_Field
97
97
  @select_field_class ||= Select_Field
98
- @selectable_options = params[:selectable_options]
98
+ @selectable_options ||= params[:selectable_options]
99
99
  @selectable_options ||= []
100
100
 
101
101
  params.delete(:option_field)
@@ -136,9 +136,9 @@ module GUI
136
136
  selected = @value.map { |v| v.to_s }.include?(opt_value.to_s)
137
137
  if selected then
138
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,
139
+ @option_field_decorator.new(:name => @attrib[:name],
140
+ :value => opt_value,
141
+ :label => opt_label,
142
142
  :parent => self)
143
143
  }
144
144
  end
@@ -155,21 +155,22 @@ module GUI
155
155
  select_options[v] = @option_labels[v]
156
156
  }
157
157
  id_prefix = "#{@attrib[:id]}_" if @attrib[:id]
158
-
158
+
159
159
  if @value && @value.length > 0 then
160
160
  HTML.div(@attrib) {
161
161
  HTML.ul(:id => "#{id_prefix}#{@attrib[:name]}") {
162
162
  option_elements()
163
163
  } +
164
- @select_field_class.new(:id => "#{id_prefix}#{@attrib[:name]}_select",
164
+ @select_field_class.new(:id => "#{id_prefix}#{@attrib[:name]}_select",
165
165
  :options => select_options,
166
- :name => "#{@attrib[:name]}" )
166
+ :name => "#{@attrib[:name]}" )
167
167
  }
168
168
  else
169
169
  HTML.div(@attrib) {
170
- @select_field_class.new(:id => "#{id_prefix}#{@attrib[:name]}_select",
170
+ HTML.ul(:id => "#{id_prefix}#{@attrib[:name]}", :force_closing_tag => true) +
171
+ @select_field_class.new(:id => "#{id_prefix}#{@attrib[:name]}_select",
171
172
  :options => select_options,
172
- :name => "#{@attrib[:name]}" )
173
+ :name => "#{@attrib[:name]}" )
173
174
  }
174
175
  end
175
176
  end
@@ -92,9 +92,14 @@ module GUI
92
92
  end
93
93
 
94
94
  def custom(params, &block)
95
- klass = params[:element]
96
- params.delete(:element)
97
- @field_decorator.new(klass.new(params, &block)).string
95
+ if params.is_a?(Element) then
96
+ element = params
97
+ else
98
+ klass = params[:element]
99
+ params.delete(:element)
100
+ element = klass.new(params, &block)
101
+ end
102
+ @field_decorator.new(element).string
98
103
  end
99
104
 
100
105
  end
@@ -0,0 +1,118 @@
1
+
2
+ require('aurita-gui/form/date_field')
3
+ require('aurita-gui/form/select_field')
4
+
5
+ module Aurita
6
+ module GUI
7
+
8
+ class Time_Field < Form_Field
9
+ attr_accessor :hour, :minute, :second, :hour_element, :minute_element, :second_element, :time_format, :minute_range
10
+
11
+ def initialize(params, &block)
12
+ @time_format = params[:time_format]
13
+ @time_format ||= 'hms'
14
+
15
+ @value = {}
16
+ @minute_range = params[:minute_range]
17
+ set_value(params[:value])
18
+
19
+ params.delete(:minute_range)
20
+ params.delete(:time_format)
21
+ params.delete(:hour)
22
+ params.delete(:minute)
23
+ params.delete(:second)
24
+ params.delete(:value)
25
+ super(params, &block)
26
+ end
27
+
28
+ def hour_element
29
+ if !@hour_element then
30
+ options = (0..23)
31
+ options = options.to_a.map { |e| (e < 10)? ('0' << e.to_s) : e.to_s }
32
+ @hour_element = Select_Field.new(:name => @attrib[:name].to_s + '_hour',
33
+ :class => :hour_select,
34
+ :value => @hour, :options => options)
35
+ end
36
+ @hour_element
37
+ end
38
+ def minute_element
39
+ if !@minute_element then
40
+ options = @minute_range
41
+ options ||= (0..59)
42
+ options = options.to_a.map { |e| (e < 10)? ('0' << e.to_s) : e.to_s }
43
+ @minute_element = Select_Field.new(:name => @attrib[:name].to_s + '_minute',
44
+ :class => :minute_select,
45
+ :value => @minute, :options => options)
46
+ end
47
+ end
48
+ def second_element
49
+ if !@second_element then
50
+ options = (0..59)
51
+ options = options.to_a.map { |e| (e < 10)? ('0' << e.to_s) : e.to_s }
52
+ @second_element = Select_Field.new(:name => @attrib[:name].to_s + '_second',
53
+ :class => :second_select,
54
+ :value => @second, :options => options)
55
+ end
56
+ @second_element
57
+ end
58
+
59
+ def element
60
+ select_fields = []
61
+ @time_format.scan(/./).each { |c|
62
+ case c
63
+ when 'h' then
64
+ select_fields << hour_element()
65
+ when 'm' then
66
+ select_fields << minute_element()
67
+ when 's' then
68
+ select_fields << second_element()
69
+ end
70
+ }
71
+ HTML.div(@attrib) {
72
+ select_fields
73
+ }
74
+ end
75
+
76
+ def value=(time)
77
+ case time
78
+ when Hash then
79
+ @value = time
80
+ when Datetime then
81
+ @value = { :hour => time.hour,
82
+ :minute => time.month,
83
+ :second => time.second }
84
+ when Date then
85
+ @value = { :hour => time.hour,
86
+ :minute => time.minute,
87
+ :second => time.second }
88
+ when String then
89
+ value_parts = time.split(':')
90
+ count = 0
91
+ @time_format.scan(/./).each { |c|
92
+ case c
93
+ when 'h' then
94
+ @hour = value_parts[count]
95
+ @value[:hour] = @hour
96
+ count += 1
97
+ when 'm' then
98
+ @minute = value_parts[count]
99
+ @value[:minute] = @minute
100
+ count += 1
101
+ when 's' then
102
+ @second = value_parts[count]
103
+ @value[:second] = @second
104
+ count += 1
105
+ end
106
+ }
107
+ else
108
+ end
109
+ end
110
+ alias set_value value=
111
+
112
+ def value
113
+ { :second => @second, :minute => @minute, :hour => @hour }
114
+ end
115
+ end
116
+
117
+ end
118
+ end
@@ -0,0 +1,46 @@
1
+
2
+ require('rubygems')
3
+ require('aurita-gui/element')
4
+ require('aurita-gui/html')
5
+
6
+ include Aurita::GUI
7
+
8
+ class Unit_1 < Element
9
+ def js_initialize
10
+ "alert('init me');"
11
+ end
12
+ end
13
+ class Unit_2 < Element
14
+ def js_initialize
15
+ "init(this);"
16
+ end
17
+ end
18
+
19
+ describe Aurita::GUI::Element, "managing javascript init codes" do
20
+ before do
21
+ @u1 = Unit_1.new(:tag => :div)
22
+ @u2 = Unit_2.new(:tag => :a)
23
+ end
24
+
25
+ it "should be possible to apply javascript code to be eval'ed on rendering an element" do
26
+ @u1.js_initialize.should == "alert('init me');"
27
+ @u1.js_init_code.should == "alert('init me');"
28
+ end
29
+
30
+ it "should propagate its init code to its parent element" do
31
+ e = HTML.div {
32
+ HTML.span { 'bla' } + Unit_1.new(:tag => :div)
33
+ }
34
+ e.js_init_code.should == "alert('init me');"
35
+ end
36
+
37
+ it "should concatenate all init codes from its children" do
38
+ e = HTML.div {
39
+ HTML.span { 'bla' } + Unit_1.new(:tag => :inner) { Unit_2.new(:tag => :a) }
40
+ }
41
+ e.js_init_code.should == "alert('init me');init(this);"
42
+ end
43
+
44
+ end
45
+
46
+
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.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Fuchs
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-31 00:00:00 +01:00
12
+ date: 2009-02-03 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -61,6 +61,7 @@ files:
61
61
  - lib/aurita-gui/form/textarea_field.rb
62
62
  - lib/aurita-gui/form/datetime_field.rb
63
63
  - lib/aurita-gui/form/boolean_field.rb
64
+ - lib/aurita-gui/form/time_field.rb
64
65
  - lib/aurita-gui/form/fieldset.rb
65
66
  - lib/aurita-gui/form/selection_list.rb
66
67
  - lib/aurita-gui/form/select_field.rb
@@ -78,6 +79,7 @@ files:
78
79
  - spec/marshal.rb
79
80
  - spec/javascript.rb
80
81
  - spec/form.rb
82
+ - spec/init_code.rb
81
83
  - spec/html.rb
82
84
  - spec/options.rb
83
85
  - spec/selection_list.rb