| Module | ScopedSearch::RailsHelper |
| In: |
lib/scoped_search/rails_helper.rb
|
# File lib/scoped_search/rails_helper.rb, line 170
170: def a_link(name, href, html_options)
171: tag_options = tag_options(html_options)
172: link = "<a href=\"#{href}\"#{tag_options}>#{name}</a>"
173: return link.respond_to?(:html_safe) ? link.html_safe : link
174: end
# File lib/scoped_search/rails_helper.rb, line 165
165: def auto_complete_clear_value_button(field_id)
166: html_options = {:tabindex => '-1',:class=>"auto_complete_clear",:title =>'Clear Search', :onclick=>"document.getElementById('#{field_id}').value = '';"}
167: a_link("", "#", html_options)
168: end
Adds AJAX auto complete functionality to the text input field with the DOM ID specified by field_id.
Required options is:
| :url: | URL to call for auto completion results in url_for format. |
Additional options are:
| :update: | Specifies the DOM ID of the element whose innerHTML should be updated with the auto complete entries returned by the AJAX request. Defaults to field_id + ‘_auto_complete‘ |
| :with: | A JavaScript expression specifying the parameters for the XMLHttpRequest. This defaults to ‘fieldname=value’. |
| :frequency: | Determines the time to wait after the last keystroke for the AJAX request to be initiated. |
| :indicator: | Specifies the DOM ID of an element which will be displayed while auto complete is running. |
| :tokens: | A string or an array of strings containing separator tokens for tokenized incremental auto completion. Example: :tokens => ’,’ would allow multiple auto completion entries, separated by commas. |
| :min_chars: | The minimum number of characters that should be in the input field before an Ajax call is made to the server. |
| :on_hide: | A Javascript expression that is called when the auto completion div is hidden. The expression should take two variables: element and update. Element is a DOM element for the field, update is a DOM element for the div from which the innerHTML is replaced. |
| :on_show: | Like on_hide, only now the expression is called then the div is shown. |
| :after_update_element: | A Javascript expression that is called when the user has selected one of the proposed values. The expression should take two variables: element and value. Element is a DOM element for the field, value is the value selected by the user. |
| :select: | Pick the class of the element from which the value for insertion should be extracted. If this is not specified, the entire element is used. |
| :method: | Specifies the HTTP verb to use when the auto completion request is made. Defaults to POST. |
# File lib/scoped_search/rails_helper.rb, line 92
92: def auto_complete_field(field_id, options = {})
93: function = "var #{field_id}_auto_completer = new Ajax.Autocompleter("
94: function << "'#{field_id}', "
95: function << "'" + (options[:update] || "#{field_id}_auto_complete") + "', "
96: function << "'#{url_for(options[:url])}'"
97:
98: js_options = {}
99: js_options[:tokens] = array_or_string_for_javascript(options[:tokens]) if options[:tokens]
100: js_options[:callback] = "function(element, value) { return #{options[:with]} }" if options[:with]
101: js_options[:indicator] = "'#{options[:indicator]}'" if options[:indicator]
102: js_options[:select] = "'#{options[:select]}'" if options[:select]
103: js_options[:paramName] = "'#{options[:param_name]}'" if options[:param_name]
104: js_options[:frequency] = "#{options[:frequency]}" if options[:frequency]
105: js_options[:method] = "'#{options[:method].to_s}'" if options[:method]
106:
107: { :after_update_element => :afterUpdateElement,
108: :on_show => :onShow, :on_hide => :onHide, :min_chars => :minChars }.each do |k,v|
109: js_options[v] = options[k] if options[k]
110: end
111:
112: function << (', ' + options_for_javascript(js_options) + ')')
113:
114: javascript_tag(function)
115: end
# File lib/scoped_search/rails_helper.rb, line 117
117: def auto_complete_field_jquery(method, url, options = {})
118: function = "$.widget( \"custom.catcomplete\", $.ui.autocomplete, {\n_renderMenu: function( ul, items ) {\nvar self = this,\ncurrentCategory = \"\";\n$.each( items, function( index, item ) {\nif ( item.category != undefined && item.category != currentCategory ) {\nul.append( \"<li class='ui-autocomplete-category'>\" + item.category + \"</li>\" );\ncurrentCategory = item.category;\n}\nif ( item.error != undefined ) {\nul.append( \"<li class='ui-autocomplete-error'>\" + item.error + \"</li>\" );\n}\nif( item.completed != undefined ) {\n$( \"<li></li>\" ).data( \"item.autocomplete\", item )\n.append( \"<a>\" + \"<strong class='ui-autocomplete-completed'>\" + item.completed + \"</strong>\" + item.part + \"</a>\" )\n.appendTo( ul );\n} else {\nself._renderItem( ul, item );\n}\n});\n}\n});\n\n$(\"#\#{method}\")\n.catcomplete({\nsource: function( request, response ) { $.getJSON( \"\#{url}\", { \#{method}: request.term }, response ); },\nminLength: \#{options[:min_length] || 0},\ndelay: \#{options[:delay] || 200},\nselect: function(event, ui) { $( this ).catcomplete( \"search\" , ui.item.value); },\nsearch: function(event, ui) { $(\".auto_complete_clear\").hide(); },\nopen: function(event, ui) { $(\".auto_complete_clear\").show(); }\n});\n\n$(\"#\#{method}\").bind( \"focus\", function( event ) {\nif( $( this )[0].value == \"\" ) {\n$( this ).catcomplete( \"search\" );\n}\n});\n\n"
119:
120:
121: javascript_tag(function)
122: end
Wrapper for text_field with added AJAX auto completion functionality.
In your controller, you‘ll need to define an action called auto_complete_method to respond the AJAX calls,
# File lib/scoped_search/rails_helper.rb, line 190
190: def auto_complete_field_tag(method, val,tag_options = {}, completion_options = {})
191: auto_completer_options = { :url => { :action => "auto_complete_#{method}" } }.update(completion_options)
192:
193: text_field_tag(method, val,tag_options.merge(:class => "auto_complete_input")) +
194: auto_complete_clear_value_button(method) +
195: content_tag("div", "", :id => "#{method}_auto_complete", :class => "auto_complete") +
196: auto_complete_field(method, auto_completer_options)
197: end
Wrapper for text_field with added JQuery auto completion functionality.
In your controller, you‘ll need to define an action called auto_complete_method to respond the JQuery calls,
# File lib/scoped_search/rails_helper.rb, line 203
203: def auto_complete_field_tag_jquery(method, val,tag_options = {}, completion_options = {})
204: url = url_for(:action => "auto_complete_#{method}", :filter => completion_options[:filter])
205: options = tag_options.merge(:class => "auto_complete_input")
206: text_field_tag(method, val, options) + auto_complete_clear_value_button(method) +
207: auto_complete_field_jquery(method, url, completion_options)
208: end
Use this method in your view to generate a return for the AJAX auto complete requests.
The auto_complete_result can of course also be called from a view belonging to the auto_complete action if you need to decorate it further.
# File lib/scoped_search/rails_helper.rb, line 180
180: def auto_complete_result(entries, phrase = nil)
181: return unless entries
182: items = entries.map { |entry| content_tag("li", phrase ? highlight(entry, phrase) : h(entry)) }
183: content_tag("ul", items)
184: end
Creates a link that alternates between ascending and descending.
Examples:
sort @search, :by => :username sort @search, :by => :created_at, :as => "Created"
This helper accepts the following options:
# File lib/scoped_search/rails_helper.rb, line 14
14: def sort(field, options = {}, html_options = {})
15:
16: unless options[:as]
17: id = field.to_s.downcase == "id"
18: options[:as] = id ? field.to_s.upcase : field.to_s.humanize
19: end
20:
21: ascend = "#{field} ASC"
22: descend = "#{field} DESC"
23:
24: ascending = params[:order] == ascend
25: new_sort = ascending ? descend : ascend
26: selected = [ascend, descend].include?(params[:order])
27:
28: if selected
29: css_classes = html_options[:class] ? html_options[:class].split(" ") : []
30: if ascending
31: options[:as] = "▲ #{options[:as]}"
32: css_classes << "ascending"
33: else
34: options[:as] = "▼ #{options[:as]}"
35: css_classes << "descending"
36: end
37: html_options[:class] = css_classes.join(" ")
38: end
39:
40: url_options = params.merge(:order => new_sort)
41:
42: options[:as] = raw(options[:as]) if defined?(RailsXss)
43:
44: a_link(options[:as], html_escape(url_for(url_options)),html_options)
45: end