Module: Nitro::XhtmlHelper
- Included in:
- Control, Render::Emitter
- Defined in:
- lib/nitro/helper/xhtml.rb
Overview
A helper mixin for programmatically building XHTML blocks.
Instance Method Summary collapse
-
#date_select(date, options = {}) ⇒ Object
Render a date select.
-
#datetime_select(time, options) ⇒ Object
Render a datetime select.
-
#hidden(name, value) ⇒ Object
Render a hidden form input.
-
#href_of(obj, base = nil) ⇒ Object
Creates the href of an Object.
-
#js_popup(options = {}) ⇒ Object
gmosx: keep the leading / to be IE friendly.
-
#link_to(obj, base = nil) ⇒ Object
Creates a link to an Object.
-
#objects_to_options(objs, params = {}) ⇒ Object
Convert a collection of objects to options.
-
#onclick_popup(options = {}) ⇒ Object
Example.
-
#options(options = {}) ⇒ Object
Render select options.
-
#popup(options = {}) ⇒ Object
Emit a link that spawns a popup window.
-
#submit(label, options = nil) ⇒ Object
Render a submit input.
-
#time_select(time, options = {}) ⇒ Object
Render a time select.
Instance Method Details
#date_select(date, options = {}) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/nitro/helper/xhtml.rb', line 142 def date_select(date, = {}) raise 'No name provided to date_select' unless name = [:name] date ||= Time.now %{ <select id="#{name}.day" name="#{name}.day"> #{(:labels_values => (1..31).to_a, :selected => date.day)} </select> <select id="#{name}.month" name="#{name}.month"> #{(:labels => Date::MONTHNAMES[1..12], :values => (1..12).to_a, :selected => (date.month))} </select> <select id="#{name}.year" name="#{name}.year"> #{(: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/nitro/helper/xhtml.rb', line 173 def datetime_select(time, ) date_select(time, ) + ' at ' + time_select(time, ) end |
#hidden(name, value) ⇒ Object
Render a hidden form input.
111 112 113 114 |
# File 'lib/nitro/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/nitro/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/nitro/helper/xhtml.rb', line 180 def js_popup( = {}) o = { :width => 320, :height => 240, :title => 'Popup', :resizable => false, :scrollbars => false, }.merge() = (o[:resizable] ? 'resizable=yes,' : 'resizable=no,') << (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+', #{}'); return false;"] end |
#link_to(obj, base = nil) ⇒ Object
Creates a link to an Object.
33 34 35 |
# File 'lib/nitro/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/nitro/helper/xhtml.rb', line 97 def (objs, params = {}) labels = [] values = [] for obj in objs labels << obj.to_s values << obj.pk end params[:labels] = labels params[:values] = values (params) end |
#onclick_popup(options = {}) ⇒ Object
Example
<a href=“#” #‘add-comment’, :scrollbars => true>Hello</a>
201 202 203 |
# File 'lib/nitro/helper/xhtml.rb', line 201 def onclick_popup( = {}) %|onclick="#{js_popup()}"| 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.(: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/nitro/helper/xhtml.rb', line 63 def ( = {}) if labels = [:labels] || [:labels_values] str = '' values = [:values] || [:labels_values] || (0...labels.size).to_a selected = [:selected] selected = selected.to_s if selected labels.each_with_index do |label, idx| value = values[idx] if [:style] style = if [:style].is_a?(Array) [:style][idx] else [: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 |
#popup(options = {}) ⇒ Object
Emit a link that spawns a popup window.
Example
<a href=“#” #:text => ‘Hello’, :url => ‘add-comment’, :scrollbars => true>Hello</a>
211 212 213 |
# File 'lib/nitro/helper/xhtml.rb', line 211 def popup( = {}) %|<a href="#" #{onclick_popup()}>#{[: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/nitro/helper/xhtml.rb', line 118 def submit(label, = nil) str = '' label = .delete(:value) unless label str << '<input type="submit"' str << %[ value="#{label}"] if label unless .empty? opts = .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/nitro/helper/xhtml.rb', line 159 def time_select(time, = {}) raise 'No name provided to time_select' unless name = [:name] time ||= Time.now %{ <select id="#{name}.hour" name="#{name}.hour"> #{(:labels_values => (1..60).to_a, :selected => time.hour)} </select> <select id="#{name}.min" name="#{name}.min"> #{(:labels_values => (1..60).to_a, :selected => time.min)} </select>} end |