Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/plugins/in_place_editing/lib/in_place_macros_helper.rb

Revision 7441, 4.4 kB (checked in by david, 1 year ago)

Moved in_place editing out of core and into its own plugin #9513

Line 
1 module InPlaceMacrosHelper
2   # Makes an HTML element specified by the DOM ID +field_id+ become an in-place
3   # editor of a property.
4   #
5   # A form is automatically created and displayed when the user clicks the element,
6   # something like this:
7   #   <form id="myElement-in-place-edit-form" target="specified url">
8   #     <input name="value" text="The content of myElement"/>
9   #     <input type="submit" value="ok"/>
10   #     <a onclick="javascript to cancel the editing">cancel</a>
11   #   </form>
12   #
13   # The form is serialized and sent to the server using an AJAX call, the action on
14   # the server should process the value and return the updated value in the body of
15   # the reponse. The element will automatically be updated with the changed value
16   # (as returned from the server).
17   #
18   # Required +options+ are:
19   # <tt>:url</tt>::       Specifies the url where the updated value should
20   #                       be sent after the user presses "ok".
21   #
22   # Addtional +options+ are:
23   # <tt>:rows</tt>::              Number of rows (more than 1 will use a TEXTAREA)
24   # <tt>:cols</tt>::              Number of characters the text input should span (works for both INPUT and TEXTAREA)
25   # <tt>:size</tt>::              Synonym for :cols when using a single line text input.
26   # <tt>:cancel_text</tt>::       The text on the cancel link. (default: "cancel")
27   # <tt>:save_text</tt>::         The text on the save link. (default: "ok")
28   # <tt>:loading_text</tt>::      The text to display while the data is being loaded from the server (default: "Loading...")
29   # <tt>:saving_text</tt>::       The text to display when submitting to the server (default: "Saving...")
30   # <tt>:external_control</tt>::  The id of an external control used to enter edit mode.
31   # <tt>:load_text_url</tt>::     URL where initial value of editor (content) is retrieved.
32   # <tt>:options</tt>::           Pass through options to the AJAX call (see prototype's Ajax.Updater)
33   # <tt>:with</tt>::              JavaScript snippet that should return what is to be sent
34   #                               in the AJAX call, +form+ is an implicit parameter
35   # <tt>:script</tt>::            Instructs the in-place editor to evaluate the remote JavaScript response (default: false)
36   # <tt>:click_to_edit_text</tt>::The text shown during mouseover the editable text (default: "Click to edit")
37   def in_place_editor(field_id, options = {})
38     function =  "new Ajax.InPlaceEditor("
39     function << "'#{field_id}', "
40     function << "'#{url_for(options[:url])}'"
41
42     js_options = {}
43     js_options['cancelText'] = %('#{options[:cancel_text]}') if options[:cancel_text]
44     js_options['okText'] = %('#{options[:save_text]}') if options[:save_text]
45     js_options['loadingText'] = %('#{options[:loading_text]}') if options[:loading_text]
46     js_options['savingText'] = %('#{options[:saving_text]}') if options[:saving_text]
47     js_options['rows'] = options[:rows] if options[:rows]
48     js_options['cols'] = options[:cols] if options[:cols]
49     js_options['size'] = options[:size] if options[:size]
50     js_options['externalControl'] = "'#{options[:external_control]}'" if options[:external_control]
51     js_options['loadTextURL'] = "'#{url_for(options[:load_text_url])}'" if options[:load_text_url]       
52     js_options['ajaxOptions'] = options[:options] if options[:options]
53     js_options['evalScripts'] = options[:script] if options[:script]
54     js_options['callback']   = "function(form) { return #{options[:with]} }" if options[:with]
55     js_options['clickToEditText'] = %('#{options[:click_to_edit_text]}') if options[:click_to_edit_text]
56     function << (', ' + options_for_javascript(js_options)) unless js_options.empty?
57    
58     function << ')'
59
60     javascript_tag(function)
61   end
62  
63   # Renders the value of the specified object and method with in-place editing capabilities.
64   def in_place_editor_field(object, method, tag_options = {}, in_place_editor_options = {})
65     tag = ::ActionView::Helpers::InstanceTag.new(object, method, self)
66     tag_options = {:tag => "span", :id => "#{object}_#{method}_#{tag.object.id}_in_place_editor", :class => "in_place_editor_field"}.merge!(tag_options)
67     in_place_editor_options[:url] = in_place_editor_options[:url] || url_for({ :action => "set_#{object}_#{method}", :id => tag.object.id })
68     tag.to_content_tag(tag_options.delete(:tag), tag_options) +
69     in_place_editor(tag_options[:id], in_place_editor_options)
70   end
71 end
Note: See TracBrowser for help on using the browser.