module Nitro
# A helper mixin for programmatically building XHTML
# blocks.
module XhtmlMixin
private
# 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
#
# #{options :labels => labels, :values => [..], :selected => 1}
# #{build :options, :labels => labels, :values => [..], :selected => 1}
def options(options = {})
if labels = options[:labels] || options[:labels_values]
str = ''
unless values = options[:values]
if values = options[:labels_values]
else
values = (0...labels.size).to_a
end
end
selected = (options[:selected] || -1).to_i
labels.each_with_index do |label, idx|
value = values[idx]
if value == selected
str << %||
else
str << %||
end
end
return str
else
raise ArgumentError.new('No labels provided')
end
end
# Render a submit input.
def submit(options = nil)
str = ''
if options
opts = options.collect { |k, v| %[#{k}="#{v}"] }.join(' ')
str << %[]
else
str << %||
end
return str
end
# Render a date select. Override to customize this.
#
# === Example
#
# #{date_select date, :name => 'brithday'}
def date_select(date, options = {})
raise 'No name provided to date_select' unless name = options[:name]
date ||= Time.now
%{
}
end
# Render a time select. Override to customize this.
def time_select(time, options = {})
raise 'No name provided to time_select' unless name = options[:name]
time ||= Time.now
%{
}
end
# Render a datetime select. Override to customize this.
def datetime_select(time, options)
date_select(time, options) + ' at ' + time_select(time, options)
end
# gmosx: keep the leading / to be IE friendly.
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
# === Example
#
# true}>Hello
def onclick_popup(options = {})
%|onclick="#{js_popup(options)}"|
end
# Emit a link that spawns a popup window.
#
# === Example
#
# 'Hello', :url => 'add-comment', :scrollbars => true}>Hello
def popup(options = {})
%|#{options[:text] || 'Popup'}|
end
end
end
# * George Moschovitis