# Module: BivouacHelpers::FormView [ "README", "AUTHORS", "COPYING", "lib/bivouac/helpers/view/goh/base.rb", "lib/bivouac/helpers/view/goh/form.rb", "lib/bivouac/helpers/view/goh/html.rb", "lib/bivouac/helpers/view/goh/tooltip.rb", "lib/bivouac/helpers/view/goh/scriptaculous.rb", "lib/bivouac/helpers/view/goh/javascript.rb", nil].each do JavaScriptGenerator.view_html BivouacHelpers.view_html BivouacHelpers::TooltipView.view_html BivouacHelpers::ScriptAculoUsView.view_html BivouacHelpers::BaseView.view_html BivouacHelpers::JavaScriptView.view_html BivouacHelpers::HtmlView.view_html BivouacHelpers::FormView.view_html end |
bivouac/helpers/view/html
Adds AJAX autocomplete functionality to the text input field with the DOM ID specified by field_id.
[ show source ]
# File lib/bivouac/helpers/view/goh/form.rb, line 6 6: def auto_complete_field( field_id, options = nil ) 7: if options.has_key?( :indicator ) 8: data = options[:indicator] 9: options[:indicator] = "indicator_#{field_id}" 10: span( data, :id => options[:indicator], :style => "display: none" ) 11: end 12: 13: choises_id = "choises_#{field_id}" 14: div( :id => choises_id, :class => "autocomplete" ) do; end 15: url = options.delete( :url ) 16: javascript_tag "new Ajax.Autocompleter( '#{field_id}', '#{choises_id}', '#{url}', #{options_for_javascript(options)} );" 17: end
Returns a button that‘ll trigger a JavaScript function using the onclick handler.
The function argument can be omitted in favor of an update_page block, which evaluates to a string when the template is rendered (instead of making an Ajax request first).
[ show source ]
# File lib/bivouac/helpers/view/goh/form.rb, line 63 63: def button_to_function(name, *args, &block) 64: html_options = args.last.is_a?(Hash) ? args.pop : {} 65: function = args[0] || '' 66: 67: function = update_page(&block) if block_given? 68: 69: input( html_options.merge({ 70: :type => "button", :value => name, 71: :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" 72: })) 73: end
Returns a form tag that will submit using XMLHttpRequest in the background instead of the regular reloading POST arrangement. Even though it‘s using JavaScript to serialize the form elements, the form submission will work just like a regular submission as viewed by the receiving side. The options for specifying the target with :url and defining callbacks is the same as link_to_remote.
A "fall-through" target for browsers that doesn‘t do JavaScript can be specified with the :action/:method options on :html.
Example:
form_remote_tag :html => { :url => R(SomePlace) }
The Hash passed to the :html key is equivalent to the options (2nd) argument in the form_tag method.
By default the fall-through action is the same as the one specified in the :url (and the default method is :post).
form_remote_tag takes a block, like form_tag:
form_remote_tag :url => '/posts' do div do; input( :type => 'submit', :name => 'submit' :value => 'Save' ); end end
[ show source ]
# File lib/bivouac/helpers/view/goh/form.rb, line 159 159: def form_remote_tag(options = {}, &block) 160: options[:form] = true 161: 162: options[:html] ||= {} 163: options[:html][:onsubmit] = 164: (options[:html][:onsubmit] ? options[:html][:onsubmit] + "; " : "") + 165: "#{remote_function(options)}; return false;" 166: 167: form_tag( options[:html].delete(:url), options[:html], &block ) 168: end
Starts a form tag that points the action to an url. The method for the form defaults to POST.
Examples:
Example:
form_tag R(Post) do div do; input( :type => 'submit', :name => 'submit' :value => 'Save' ); end end
Will output:
<form action="/posts" method="post"><input type="submit" name="submit" value="Save" /></div></form>
Options:
If "put", "delete", or another verb is used, a hidden input with name _method is added to simulate the verb over post.
[ show source ]
# File lib/bivouac/helpers/view/goh/form.rb, line 109 109: def form_tag(url, options = {}, &block) 110: options[:enctype] = "multipart/form-data" if options.delete(:multipart) 111: options[:action] = url 112: 113: method_tag = false 114: 115: case method = options.delete(:method).to_s 116: when /^get$/i # must be case-insentive, but can't use downcase as might be nil 117: options[:method] = "get" 118: when /^post$/i, "", nil 119: options[:method] = "post" 120: else 121: options[:method] = "post" 122: method_tag = true 123: end 124: 125: if block_given? 126: form( options ) do 127: yield( ) 128: input( :type => "hidden", :name => "_method", :value => method ) if method_tag 129: end 130: else 131: form( options ) do; end 132: end 133: end
Returns a button input tag that will submit form using XMLHttpRequest in the background instead of regular reloading POST arrangement. options argument is the same as in form_remote_tag.
[ show source ]
# File lib/bivouac/helpers/view/goh/form.rb, line 78 78: def submit_to_remote(name, value, options = {}) 79: options[:with] ||= 'Form.serialize(this.form)' 80: 81: options[:html] ||= {} 82: options[:html][:type] = 'button' 83: options[:html][:onclick] = "#{remote_function(options)}; return false;" 84: options[:html][:name] = name 85: options[:html][:value] = value 86: 87: input options[:html] 88: end
Autocomplete options :
+:url+: URL to call for autocompletion results +:tokens+: +:frequency+: +:minChars+: +:indicator+: When sending the Ajax request Autocompleter shows this +:updateElement+: Hook for a custom function called after the element has been updated (i.e. when the user has selected an entry). +:afterUpdateElement+: Hook for a custom function called after the element has been updated (i.e. when the user has selected an entry). +:callback+: This function is called just before the Request is actually made, allowing you to modify the querystring that is sent to the server. +:parameters+: If you need to send any additional parameters through your search form, add them here, in the usual {field: ‘value’,another: ‘value’} or ‘field=value&another=value’ manner.
[ show source ]
# File lib/bivouac/helpers/view/goh/form.rb, line 30 30: def text_field(field_name, value = "", options = {}) 31: autocomplete_options = nil 32: 33: if options.has_key?( :autocomplete ) 34: unless ["on", "off"].include?( options[:autocomplete] ) 35: autocomplete_options = Hash.new( ) 36: autocomplete_options[:url] = options.delete( :autocomplete ) 37: # options[:autocomplete] = "off" 38: end 39: end 40: 41: 42: [:tokens, :frequency, :minChars, :indicator, :updateElement, :afterUpdateElement, :callback, :parameters].each do |op| 43: if options.has_key?( op ) 44: if autocomplete_options.nil? 45: options.delete( op ) 46: else 47: autocomplete_options[op] = options.delete( op ) 48: end 49: end 50: end 51: 52: options = { :id => "default_#{Time.now.to_i}", :type => "text", :name => field_name, :value => value }.merge( options ) 53: input( options ) 54: auto_complete_field( options[:id], autocomplete_options ) unless autocomplete_options.nil? 55: end