Module: Raw::XhtmlHelper

Included in:
Control
Defined in:
lib/raw/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’



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

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.



173
174
175
# File 'lib/raw/helper/xhtml.rb', line 173

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

#hidden(name, value) ⇒ Object

Render a hidden form input.



111
112
113
114
# File 'lib/raw/helper/xhtml.rb', line 111

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

#href_of(obj, base = nil) ⇒ Object

Creates the href of an Object. – gmosx: this duplicates R functionality, merge! ++



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/raw/helper/xhtml.rb', line 13

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.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/raw/helper/xhtml.rb', line 180

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

Creates a link to an Object.



33
34
35
# File 'lib/raw/helper/xhtml.rb', line 33

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

#objects_to_options(objs, params = {}) ⇒ Object

Convert a collection of objects to options.



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/raw/helper/xhtml.rb', line 97

def objects_to_options(objs, params = {})
  labels = []
  values = []
  for obj in objs
    labels << obj.to_s
    values << obj.pk
  end
  params[:labels] = labels
  params[:values] = values
  options(params)
end

#onclick_popup(options = {}) ⇒ Object

Example

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



201
202
203
# File 'lib/raw/helper/xhtml.rb', line 201

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



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
91
92
93
# File 'lib/raw/helper/xhtml.rb', line 63

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’, :uri => ‘add-comment’, :scrollbars => true>Hello</a>



211
212
213
# File 'lib/raw/helper/xhtml.rb', line 211

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

#submit(label, options = nil) ⇒ Object

Render a submit input.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/raw/helper/xhtml.rb', line 118

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.



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/raw/helper/xhtml.rb', line 159

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