Module: Nitro::XhtmlHelper

Included in:
Form::Control, Render::Emitter
Defined in:
lib/nitro/helper/xhtml.rb

Overview

A helper mixin for programmatically building XHTML blocks.

Instance Method Summary collapse

Instance Method Details

#date_select(date, options = {}) ⇒ Object

Render a date select. Override to customize this.

Example

#date, :name => ‘brithday’



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/nitro/helper/xhtml.rb', line 125

def date_select(date, options = {})
  raise 'No name provided to date_select' unless name = options[:name]
  date ||= Time.now
  %{
    <select id="#{name}.day" name="#{name}.day">
      #{options(:labels_values => (1..31).to_a, :selected => date.day)}        
    </select>&nbsp;
    <select id="#{name}.month" name="#{name}.month">
      #{options(:labels => Date::MONTHNAMES[1..12], :values => (1..12).to_a, :selected => (date.month))}
    </select>&nbsp;
    <select id="#{name}.year" name="#{name}.year">
      #{options(:labels_values => ((Time.now.year-10)..(Time.now.year+10)).to_a, :selected => date.year)}        
    </select>}
end

#datetime_select(time, options) ⇒ Object

Render a datetime select. Override to customize this.



156
157
158
# File 'lib/nitro/helper/xhtml.rb', line 156

def datetime_select(time, options)
  date_select(time, options) + '&nbsp;at&nbsp;' + time_select(time, options)
end

#hidden(options) ⇒ Object

Render a hidden form input.



94
95
96
97
# File 'lib/nitro/helper/xhtml.rb', line 94

def hidden(options)
  opts = options.collect { |k, v| %[#{k}="#{v}"] }.join(' ')
  %[<input type="hidden" #{opts} />]
end

#href_of(obj, base = nil) ⇒ Object

Creates the href of an Object.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nitro/helper/xhtml.rb', line 10

def href_of(obj, base = nil)
  if obj.is_a?(Symbol) or obj.is_a?(String)
    href = obj.to_s
  elsif obj.respond_to? :to_href
    href = obj.to_href
  else
    href = "#{obj.class.name.pluralize.underscore}/#{obj.oid}"
  end
  
  if base
    base += '/'
  else
    base = "#{self.class.mount_path}/".squeeze
  end
  
  return "#{base}#{href}"
end

#js_popup(options = {}) ⇒ Object

gmosx: keep the leading / to be IE friendly.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/nitro/helper/xhtml.rb', line 163

def js_popup(options = {})
  o = {
    :width => 320,
    :height => 240,
    :title => 'Popup',
    :resizable => false,
    :scrollbars => false,
  }.merge(options)

  poptions = (o[:resizable] ? 'resizable=yes,' : 'resizable=no,')
  poptions << (o[:scrollbars] ? 'scrollbars=yes' : 'scrollbars=no')
  
  url = o[:url] || o[:uri]
  
  %[javascript: var pwl = (screen.width - #{o[:width]}) / 2; var pwt = (screen.height - #{o[:height]}) / 2; window.open('#{url}', '#{o[:title]}', 'width=#{o[:width]},height=#{o[:height]},top='+pwt+',left='+pwl+', #{poptions}'); return false;"]       
end

Creates a link to an Object.



30
31
32
# File 'lib/nitro/helper/xhtml.rb', line 30

def link_to(obj, base = nil)
  %|<a href="#{href_of(obj, base)}">#{obj}</a>|
end

#onclick_popup(options = {}) ⇒ Object

Example

<a href=“#” #‘add-comment’, :scrollbars => true>Hello</a>



184
185
186
# File 'lib/nitro/helper/xhtml.rb', line 184

def onclick_popup(options = {})
  %|onclick="#{js_popup(options)}"|
end

#options(options = {}) ⇒ Object

Render select options. The parameter is a hash of options.

labels

The option labels.

values

The corresponding values.

labels_values

Use when labels == values.

selected

The value of the selected option.

Examples

labels = [‘Male’, ‘Female’] o.select(:name => ‘sex’)

o.options(:labels => labels, :selected => 1)

or

#:labels => labels, :values => [..], :selected => 1 #:options, :labels => labels, :values => [..], :selected => 1



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nitro/helper/xhtml.rb', line 60

def options(options = {})
  if labels = options[:labels] || options[:labels_values]
    str = ''
    
    values = options[:values] || options[:labels_values] || (0...labels.size).to_a
    
    selected = options[:selected]
    selected = selected.to_s if selected
    
    labels.each_with_index do |label, idx|
      value = values[idx]
      if options[:style]
        style = if options[:style].is_a?(Array) 
          options[:style][idx]
        else
          options[:style]
        end
        style = %{ style="#{style}"}
      end
      if value.to_s == selected
        str << %|<option value="#{value}" selected="selected"#{style}>#{label}</option>|
      else
        str << %|<option value="#{value}"#{style}>#{label}</option>|
      end
    end
    
    return str
  else
    raise ArgumentError.new('No labels provided')
  end
end

Emit a link that spawns a popup window.

Example

<a href=“#” #:text => ‘Hello’, :url => ‘add-comment’, :scrollbars => true>Hello</a>



194
195
196
# File 'lib/nitro/helper/xhtml.rb', line 194

def popup(options = {})
  %|<a href="#" #{onclick_popup(options)}>#{options[:text] || 'Popup'}</a>|
end

#submit(label, options = nil) ⇒ Object

Render a submit input.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/nitro/helper/xhtml.rb', line 101

def submit(label, options = nil)
  str = ''
  
  label = options.delete(:value) unless label 
  
  str << '<input type="submit"'
  str << %[ value="#{label}"] if label

  unless options.empty?
    opts = options.collect { |k, v| %[#{k}="#{v}"] }.join(' ')
    str << %[ #{opts} ]
  end
  
  str << ' />'

  return str
end

#time_select(time, options = {}) ⇒ Object

Render a time select. Override to customize this.



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/nitro/helper/xhtml.rb', line 142

def time_select(time, options = {})
  raise 'No name provided to time_select' unless name = options[:name]
  time ||= Time.now
  %{
    <select id="#{name}.hour" name="#{name}.hour">
      #{options(:labels_values => (1..60).to_a, :selected => time.hour)}        
    </select>&nbsp;
    <select id="#{name}.min" name="#{name}.min">
      #{options(:labels_values => (1..60).to_a, :selected => time.min)}        
    </select>}
end