require File.dirname(__FILE__) + '/tag_helper' module ActionView module Helpers # Provides a set of helpers for creating JavaScript macros that rely on and often bundle methods from JavaScriptHelper into # larger units. These macros also rely on counterparts in the controller that provide them with their backing. The in-place # editing relies on ActionController::Base.in_place_edit_for and the autocompletion relies on # ActionController::Base.auto_complete_for. module JavaScriptMacrosHelper # Makes an HTML element specified by the DOM ID +field_id+ become an in-place # editor of a property. # # A form is automatically created and displayed when the user clicks the element, # something like this: #
# # # cancel #
# # The form is serialized and sent to the server using an AJAX call, the action on # the server should process the value and return the updated value in the body of # the reponse. The element will automatically be updated with the changed value # (as returned from the server). # # Required +options+ are: # :url:: Specifies the url where the updated value should # be sent after the user presses "ok". # # Addtional +options+ are: # :rows:: Number of rows (more than 1 will use a TEXTAREA) # :cancel_text:: The text on the cancel link. (default: "cancel") # :save_text:: The text on the save link. (default: "ok") # :external_control:: The id of an external control used to enter edit mode. # :options:: Pass through options to the AJAX call (see prototype's Ajax.Updater) # :with:: JavaScript snippet that should return what is to be sent # in the AJAX call, +form+ is an implicit parameter def in_place_editor(field_id, options = {}) function = "new Ajax.InPlaceEditor(" function << "'#{field_id}', " function << "'#{url_for(options[:url])}'" js_options = {} js_options['cancelText'] = %('#{options[:cancel_text]}') if options[:cancel_text] js_options['okText'] = %('#{options[:save_text]}') if options[:save_text] js_options['rows'] = options[:rows] if options[:rows] js_options['externalControl'] = options[:external_control] if options[:external_control] js_options['ajaxOptions'] = options[:options] if options[:options] js_options['callback'] = "function(form) { return #{options[:with]} }" if options[:with] function << (', ' + options_for_javascript(js_options)) unless js_options.empty? function << ')' javascript_tag(function) end # Renders the value of the specified object and method with in-place editing capabilities. # # See the RDoc on ActionController::InPlaceEditing to learn more about this. def in_place_editor_field(object, method, tag_options = {}, in_place_editor_options = {}) tag = ::ActionView::Helpers::InstanceTag.new(object, method, self) tag_options = {:tag => "span", :id => "#{object}_#{method}_#{tag.object.id}_in_place_editor", :class => "in_place_editor_field"}.merge!(tag_options) in_place_editor_options[:url] = in_place_editor_options[:url] || url_for({ :action => "set_#{object}_#{method}", :id => tag.object.id }) tag.to_content_tag(tag_options.delete(:tag), tag_options) + in_place_editor(tag_options[:id], in_place_editor_options) end # Adds AJAX autocomplete functionality to the text input field with the # DOM ID specified by +field_id+. # # This function expects that the called action returns a HTML