Changeset 2191
- Timestamp:
- 09/11/05 07:52:53 (3 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller.rb (modified) (2 diffs)
- trunk/actionpack/lib/action_controller/macros (added)
- trunk/actionpack/lib/action_controller/macros/auto_complete.rb (moved) (moved from trunk/actionpack/lib/action_controller/auto_complete.rb) (1 diff)
- trunk/actionpack/lib/action_controller/macros/in_place_editing.rb (added)
- trunk/actionpack/lib/action_view/helpers/form_helper.rb (modified) (1 diff)
- trunk/actionpack/lib/action_view/helpers/java_script_macros_helper.rb (added)
- trunk/actionpack/lib/action_view/helpers/javascript_helper.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r2184 r2191 1 1 *SVN* 2 3 * Moved auto-completion and in-place editing into the Macros module and their helper counterparts into JavaScriptMacrosHelper 4 5 * Added in-place editing support in the spirit of auto complete with ActionController::Base.in_place_edit_for, JavascriptHelper#in_place_editor_field, and Javascript support from script.aculo.us #2038 [Jon Tirsen] 2 6 3 7 * Added :disabled option to all data selects that'll make the elements inaccessible for change #2167, #253 [eigentone] trunk/actionpack/lib/action_controller.rb
r1915 r2191 50 50 require 'action_controller/verification' 51 51 require 'action_controller/streaming' 52 require 'action_controller/auto_complete'53 52 require 'action_controller/session_management' 53 require 'action_controller/macros/auto_complete' 54 require 'action_controller/macros/in_place_editing' 54 55 55 56 require 'action_view' … … 72 73 include ActionController::Verification 73 74 include ActionController::Streaming 74 include ActionController::AutoComplete75 75 include ActionController::SessionManagement 76 include ActionController::Macros::AutoComplete 77 include ActionController::Macros::InPlaceEditing 76 78 end trunk/actionpack/lib/action_controller/macros/auto_complete.rb
r1815 r2191 1 1 module ActionController 2 module AutoComplete #:nodoc: 3 def self.append_features(base) #:nodoc: 4 super 5 base.extend(ClassMethods) 6 end 2 # Macros are class-level calls that add pre-defined actions to the controller based on the parameters passed in. 3 # Currently, they're used to bridge the JavaScript macros, like autocompletion and in-place editing, with the controller 4 # backing. 5 module Macros 6 module AutoComplete #:nodoc: 7 def self.append_features(base) #:nodoc: 8 super 9 base.extend(ClassMethods) 10 end 7 11 8 # Example:9 #10 # # Controller11 # class BlogController < ApplicationController12 # auto_complete_for :post, :title13 # end14 #15 # # View16 # <%= text_field_with_auto_complete :post, title %>17 #18 # By default, auto_complete_for limits the results to 10 entries,19 # and sorts by the given field.20 #21 # auto_complete_for takes a third parameter, an options hash to22 # the find method used to search for the records:23 #24 # auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC'25 #26 # For help on defining text input fields with autocompletion,27 # see ActionView::Helpers::JavaScriptHelper.28 #29 # For more examples, see script.aculo.us:30 # * http://script.aculo.us/demos/ajax/autocompleter31 # * http://script.aculo.us/demos/ajax/autocompleter_customized32 module ClassMethods33 def auto_complete_for(object, method, options = {})34 define_method("auto_complete_for_#{object}_#{method}") do35 find_options = {36 :conditions => [ "LOWER(#{method}) LIKE ?", '%' + params[object][method].downcase + '%' ],37 :order => "#{method} ASC",38 :limit => 10 }.merge!(options)12 # Example: 13 # 14 # # Controller 15 # class BlogController < ApplicationController 16 # auto_complete_for :post, :title 17 # end 18 # 19 # # View 20 # <%= text_field_with_auto_complete :post, title %> 21 # 22 # By default, auto_complete_for limits the results to 10 entries, 23 # and sorts by the given field. 24 # 25 # auto_complete_for takes a third parameter, an options hash to 26 # the find method used to search for the records: 27 # 28 # auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC' 29 # 30 # For help on defining text input fields with autocompletion, 31 # see ActionView::Helpers::JavaScriptHelper. 32 # 33 # For more examples, see script.aculo.us: 34 # * http://script.aculo.us/demos/ajax/autocompleter 35 # * http://script.aculo.us/demos/ajax/autocompleter_customized 36 module ClassMethods 37 def auto_complete_for(object, method, options = {}) 38 define_method("auto_complete_for_#{object}_#{method}") do 39 find_options = { 40 :conditions => [ "LOWER(#{method}) LIKE ?", '%' + params[object][method].downcase + '%' ], 41 :order => "#{method} ASC", 42 :limit => 10 }.merge!(options) 39 43 40 @items = object.to_s.camelize.constantize.find(:all, find_options)44 @items = object.to_s.camelize.constantize.find(:all, find_options) 41 45 42 render :inline => "<%= auto_complete_result @items, '#{method}' %>" 46 render :inline => "<%= auto_complete_result @items, '#{method}' %>" 47 end 43 48 end 44 49 end trunk/actionpack/lib/action_view/helpers/form_helper.rb
r2165 r2191 235 235 tag_text << ">True</option></select>" 236 236 end 237 237 238 def to_content_tag(tag_name, options = {}) 239 content_tag(tag_name, value, options) 240 end 241 238 242 def object 239 243 @template_object.instance_variable_get "@#{@object_name}" trunk/actionpack/lib/action_view/helpers/javascript_helper.rb
r2164 r2191 343 343 build_observer('Form.EventObserver', form_id, options) 344 344 end 345 end346 347 348 # Adds AJAX autocomplete functionality to the text input field with the349 # DOM ID specified by +field_id+.350 #351 # This function expects that the called action returns a HTML <ul> list,352 # or nothing if no entries should be displayed for autocompletion.353 #354 # You'll probably want to turn the browser's built-in autocompletion off,355 # su be sure to include a autocomplete="off" attribute with your text356 # input field.357 #358 # Required +options+ are:359 # <tt>:url</tt>:: Specifies the DOM ID of the element whose360 # innerHTML should be updated with the autocomplete361 # entries returned by XMLHttpRequest.362 #363 # Addtional +options+ are:364 # <tt>:update</tt>:: Specifies the DOM ID of the element whose365 # innerHTML should be updated with the autocomplete366 # entries returned by the AJAX request.367 # Defaults to field_id + '_auto_complete'368 # <tt>:with</tt>:: A JavaScript expression specifying the369 # parameters for the XMLHttpRequest. This defaults370 # to 'fieldname=value'.371 # <tt>:indicator</tt>:: Specifies the DOM ID of an elment which will be372 # displayed while autocomplete is running.373 def auto_complete_field(field_id, options = {})374 function = "new Ajax.Autocompleter("375 function << "'#{field_id}', "376 function << "'" + (options[:update] || "#{field_id}_auto_complete") + "', "377 function << "'#{url_for(options[:url])}'"378 379 js_options = {}380 js_options[:tokens] = array_or_string_for_javascript(options[:tokens]) if options[:tokens]381 js_options[:callback] = "function(element, value) { return #{options[:with]} }" if options[:with]382 js_options[:indicator] = "'#{options[:indicator]}'" if options[:indicator]383 function << (', ' + options_for_javascript(js_options) + ')')384 385 javascript_tag(function)386 end387 388 # Use this method in your view to generate a return for the AJAX automplete requests.389 #390 # Example action:391 #392 # def auto_complete_for_item_title393 # @items = Item.find(:all,394 # :conditions => [ 'LOWER(description) LIKE ?',395 # '%' + request.raw_post.downcase + '%' ])396 # render :inline => '<%= auto_complete_result(@items, 'description') %>'397 # end398 #399 # The auto_complete_result can of course also be called from a view belonging to the400 # auto_complete action if you need to decorate it further.401 def auto_complete_result(entries, field, phrase = nil)402 return unless entries403 items = entries.map { |entry| content_tag("li", phrase ? highlight(entry[field], phrase) : h(entry[field])) }404 content_tag("ul", items.uniq)405 end406 407 # Wrapper for text_field with added AJAX autocompletion functionality.408 #409 # In your controller, you'll need to define an action called410 # auto_complete_for_object_method to respond the AJAX calls,411 #412 # See the RDoc on ActionController::AutoComplete to learn more about this.413 def text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})414 (completion_options[:skip_style] ? "" : auto_complete_stylesheet) +415 text_field(object, method, { :autocomplete => "off" }.merge!(tag_options)) +416 content_tag("div", "", :id => "#{object}_#{method}_auto_complete", :class => "auto_complete") +417 auto_complete_field("#{object}_#{method}", { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options))418 345 end 419 346