From b3787643ec4b24b4628e29ebccc5fb517b01b824 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 30 Jan 2010 15:20:00 -0600 Subject: Move link_to_function and link_to_remote into prototype_legacy_helper plugin --- .../lib/action_view/helpers/javascript_helper.rb | 54 -------- .../lib/action_view/helpers/prototype_helper.rb | 141 --------------------- .../action_view/helpers/scriptaculous_helper.rb | 101 +++++++-------- 3 files changed, 48 insertions(+), 248 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 06a9d3405a..f9249e667d 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -41,60 +41,6 @@ module ActionView include PrototypeHelper - # Returns a link of the given +name+ that will trigger a JavaScript +function+ using the - # onclick handler and return false after the fact. - # - # The first argument +name+ is used as the link text. - # - # The next arguments are optional and may include the javascript function definition and a hash of html_options. - # - # 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). - # - # The +html_options+ will accept a hash of html attributes for the link tag. Some examples are :class => "nav_button", :id => "articles_nav_button" - # - # Note: if you choose to specify the javascript function in a block, but would like to pass html_options, set the +function+ parameter to nil - # - # - # Examples: - # link_to_function "Greeting", "alert('Hello world!')" - # Produces: - # Greeting - # - # link_to_function(image_tag("delete"), "if (confirm('Really?')) do_delete()") - # Produces: - # - # Delete - # - # - # link_to_function("Show me more", nil, :id => "more_link") do |page| - # page[:details].visual_effect :toggle_blind - # page[:more_link].replace_html "Show me less" - # end - # Produces: - # Show me more - # - def link_to_function(name, *args, &block) - html_options = args.extract_options!.symbolize_keys - - function = block_given? ? update_page(&block) : args[0] || '' - onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;" - href = html_options[:href] || '#' - - content_tag(:a, name, html_options.merge(:href => href, :onclick => onclick)) - end - # Returns a button with the given +name+ text that'll trigger a JavaScript +function+ using the # onclick handler. # diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 0ec7b48c25..56a7198792 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -102,147 +102,6 @@ module ActionView :form, :with, :update, :script, :type ]).merge(CALLBACKS) end - # Returns a link to a remote action defined by options[:url] - # (using the url_for format) that's called in the background using - # XMLHttpRequest. The result of that request can then be inserted into a - # DOM object whose id can be specified with options[:update]. - # Usually, the result would be a partial prepared by the controller with - # render :partial. - # - # Examples: - # # Generates: Delete this post - # link_to_remote "Delete this post", :update => "posts", - # :url => { :action => "destroy", :id => post.id } - # - # # Generates: Refresh - # link_to_remote(image_tag("refresh"), :update => "emails", - # :url => { :action => "list_emails" }) - # - # You can override the generated HTML options by specifying a hash in - # options[:html]. - # - # link_to_remote "Delete this post", :update => "posts", - # :url => post_url(@post), :method => :delete, - # :html => { :class => "destructive" } - # - # You can also specify a hash for options[:update] to allow for - # easy redirection of output to an other DOM element if a server-side - # error occurs: - # - # Example: - # # Generates: Delete this post - # link_to_remote "Delete this post", - # :url => { :action => "destroy", :id => post.id }, - # :update => { :success => "posts", :failure => "error" } - # - # Optionally, you can use the options[:position] parameter to - # influence how the target DOM element is updated. It must be one of - # :before, :top, :bottom, or :after. - # - # The method used is by default POST. You can also specify GET or you - # can simulate PUT or DELETE over POST. All specified with options[:method] - # - # Example: - # # Generates: Destroy - # link_to_remote "Destroy", :url => person_url(:id => person), :method => :delete - # - # By default, these remote requests are processed asynchronous during - # which various JavaScript callbacks can be triggered (for progress - # indicators and the likes). All callbacks get access to the - # request object, which holds the underlying XMLHttpRequest. - # - # To access the server response, use request.responseText, to - # find out the HTTP status, use request.status. - # - # Example: - # # Generates: hello - # word = 'hello' - # link_to_remote word, - # :url => { :action => "undo", :n => word_counter }, - # :complete => "undoRequestCompleted(request)" - # - # The callbacks that may be specified are (in order): - # - # :loading:: Called when the remote document is being - # loaded with data by the browser. - # :loaded:: Called when the browser has finished loading - # the remote document. - # :interactive:: Called when the user can interact with the - # remote document, even though it has not - # finished loading. - # :success:: Called when the XMLHttpRequest is completed, - # and the HTTP status code is in the 2XX range. - # :failure:: Called when the XMLHttpRequest is completed, - # and the HTTP status code is not in the 2XX - # range. - # :complete:: Called when the XMLHttpRequest is complete - # (fires after success/failure if they are - # present). - # - # You can further refine :success and :failure by - # adding additional callbacks for specific status codes. - # - # Example: - # # Generates: hello - # link_to_remote word, - # :url => { :action => "action" }, - # 404 => "alert('Not found...? Wrong URL...?')", - # :failure => "alert('HTTP Error ' + request.status + '!')" - # - # A status code callback overrides the success/failure handlers if - # present. - # - # If you for some reason or another need synchronous processing (that'll - # block the browser while the request is happening), you can specify - # options[:type] = :synchronous. - # - # You can customize further browser side call logic by passing in - # JavaScript code snippets via some optional parameters. In their order - # of use these are: - # - # :confirm:: Adds confirmation dialog. - # :condition:: Perform remote request conditionally - # by this expression. Use this to - # describe browser-side conditions when - # request should not be initiated. - # :before:: Called before request is initiated. - # :after:: Called immediately after request was - # initiated and before :loading. - # :submit:: Specifies the DOM element ID that's used - # as the parent of the form elements. By - # default this is the current form, but - # it could just as well be the ID of a - # table row or any other DOM element. - # :with:: A JavaScript expression specifying - # the parameters for the XMLHttpRequest. - # Any expressions should return a valid - # URL query string. - # - # Example: - # - # :with => "'name=' + $('name').value" - # - # You can generate a link that uses AJAX in the general case, while - # degrading gracefully to plain link behavior in the absence of - # JavaScript by setting html_options[:href] to an alternate URL. - # Note the extra curly braces around the options hash separate - # it as the second parameter from html_options, the third. - # - # Example: - # link_to_remote "Delete this post", - # { :update => "posts", :url => { :action => "destroy", :id => post.id } }, - # :href => url_for(:action => "destroy", :id => post.id) - def link_to_remote(name, options = {}, html_options = nil) - link_to_function(name, remote_function(options), html_options || options.delete(:html)) - end - # Creates a button with an onclick event which calls a remote action # via XMLHttpRequest # The options for specifying the target with :url diff --git a/actionpack/lib/action_view/helpers/scriptaculous_helper.rb b/actionpack/lib/action_view/helpers/scriptaculous_helper.rb index 04af2781d7..d30164e402 100644 --- a/actionpack/lib/action_view/helpers/scriptaculous_helper.rb +++ b/actionpack/lib/action_view/helpers/scriptaculous_helper.rb @@ -3,11 +3,11 @@ require 'active_support/json' module ActionView module Helpers - # Provides a set of helpers for calling Scriptaculous JavaScript + # Provides a set of helpers for calling Scriptaculous JavaScript # functions, including those which create Ajax controls and visual effects. # - # To be able to use these helpers, you must include the Prototype - # JavaScript framework and the Scriptaculous JavaScript library in your + # To be able to use these helpers, you must include the Prototype + # JavaScript framework and the Scriptaculous JavaScript library in your # pages. See the documentation for ActionView::Helpers::JavaScriptHelper # for more information on including the necessary JavaScript. # @@ -18,22 +18,17 @@ module ActionView unless const_defined? :TOGGLE_EFFECTS TOGGLE_EFFECTS = [:toggle_appear, :toggle_slide, :toggle_blind] end - + # Returns a JavaScript snippet to be used on the Ajax callbacks for # starting visual effects. # - # Example: - # <%= link_to_remote "Reload", :update => "posts", - # :url => { :action => "reload" }, - # :complete => visual_effect(:highlight, "posts", :duration => 0.5) - # # If no +element_id+ is given, it assumes "element" which should be a local - # variable in the generated JavaScript execution context. This can be + # variable in the generated JavaScript execution context. This can be # used for example with +drop_receiving_element+: # # <%= drop_receiving_element (...), :loading => visual_effect(:fade) %> # - # This would fade the element that was dropped on the drop receiving + # This would fade the element that was dropped on the drop receiving # element. # # For toggling visual effects, you can use :toggle_appear, :toggle_slide, and @@ -44,13 +39,13 @@ module ActionView # http://script.aculo.us for more documentation. def visual_effect(name, element_id = false, js_options = {}) element = element_id ? ActiveSupport::JSON.encode(element_id) : "element" - + js_options[:queue] = if js_options[:queue].is_a?(Hash) '{' + js_options[:queue].map {|k, v| k == :limit ? "#{k}:#{v}" : "#{k}:'#{v}'" }.join(',') + '}' elsif js_options[:queue] "'#{js_options[:queue]}'" end if js_options[:queue] - + [:endcolor, :direction, :startcolor, :scaleMode, :restorecolor].each do |option| js_options[option] = "'#{js_options[option]}'" if js_options[option] end @@ -61,7 +56,7 @@ module ActionView "new Effect.#{name.to_s.camelize}(#{element},#{options_for_javascript(js_options)});" end end - + # Makes the element with the DOM ID specified by +element_id+ sortable # by drag-and-drop and make an Ajax call whenever the sort order has # changed. By default, the action called gets the serialized sortable @@ -71,84 +66,84 @@ module ActionView # # <%= sortable_element("my_list", :url => { :action => "order" }) %> # - # In the example, the action gets a "my_list" array parameter - # containing the values of the ids of elements the sortable consists + # In the example, the action gets a "my_list" array parameter + # containing the values of the ids of elements the sortable consists # of, in the current order. # # Important: For this to work, the sortable elements must have id # attributes in the form "string_identifier". For example, "item_1". Only # the identifier part of the id attribute will be serialized. - # + # # Additional +options+ are: # # * :format - A regular expression to determine what to send as the # serialized id to the server (the default is /^[^_]*_(.*)$/). - # + # # * :constraint - Whether to constrain the dragging to either # :horizontal or :vertical (or false to make it unconstrained). - # + # # * :overlap - Calculate the item overlap in the :horizontal # or :vertical direction. - # + # # * :tag - Which children of the container element to treat as # sortable (default is li). - # + # # * :containment - Takes an element or array of elements to treat as # potential drop targets (defaults to the original target element). - # + # # * :only - A CSS class name or array of class names used to filter # out child elements as candidates. - # + # # * :scroll - Determines whether to scroll the list during drag # operations if the list runs past the visual border. - # + # # * :tree - Determines whether to treat nested lists as part of the # main sortable list. This means that you can create multi-layer lists, # and not only sort items at the same level, but drag and sort items # between levels. - # + # # * :hoverclass - If set, the Droppable will have this additional CSS class - # when an accepted Draggable is hovered over it. - # + # when an accepted Draggable is hovered over it. + # # * :handle - Sets whether the element should only be draggable by an # embedded handle. The value may be a string referencing a CSS class value # (as of script.aculo.us V1.5). The first child/grandchild/etc. element # found within the element that has this CSS class value will be used as # the handle. - # + # # * :ghosting - Clones the element and drags the clone, leaving # the original in place until the clone is dropped (default is false). - # + # # * :dropOnEmpty - If true the Sortable container will be made into # a Droppable, that can receive a Draggable (as according to the containment # rules) as a child element when there are no more elements inside (default # is false). - # + # # * :onChange - Called whenever the sort order changes while dragging. When # dragging from one Sortable to another, the callback is called once on each # Sortable. Gets the affected element as its parameter. - # + # # * :onUpdate - Called when the drag ends and the Sortable's order is # changed in any way. When dragging from one Sortable to another, the callback # is called once on each Sortable. Gets the container as its parameter. - # + # # See http://script.aculo.us for more documentation. def sortable_element(element_id, options = {}) javascript_tag(sortable_element_js(element_id, options).chop!) end - + def sortable_element_js(element_id, options = {}) #:nodoc: options[:with] ||= "Sortable.serialize(#{ActiveSupport::JSON.encode(element_id)})" options[:onUpdate] ||= "function(){" + remote_function(options) + "}" options.delete_if { |key, value| PrototypeHelper::AJAX_OPTIONS.include?(key) } - + [:tag, :overlap, :constraint, :handle].each do |option| options[option] = "'#{options[option]}'" if options[option] end - + options[:containment] = array_or_string_for_javascript(options[:containment]) if options[:containment] options[:only] = array_or_string_for_javascript(options[:only]) if options[:only] - + %(Sortable.create(#{ActiveSupport::JSON.encode(element_id)}, #{options_for_javascript(options)});) end @@ -156,24 +151,24 @@ module ActionView # # Example: # <%= draggable_element("my_image", :revert => true) - # + # # You can change the behaviour with various options, see # http://script.aculo.us for more documentation. def draggable_element(element_id, options = {}) javascript_tag(draggable_element_js(element_id, options).chop!) end - + def draggable_element_js(element_id, options = {}) #:nodoc: %(new Draggable(#{ActiveSupport::JSON.encode(element_id)}, #{options_for_javascript(options)});) end # Makes the element with the DOM ID specified by +element_id+ receive # dropped draggable elements (created by +draggable_element+). - # and make an AJAX call. By default, the action called gets the DOM ID + # and make an AJAX call. By default, the action called gets the DOM ID # of the element as parameter. # # Example: - # <%= drop_receiving_element("my_cart", :url => + # <%= drop_receiving_element("my_cart", :url => # { :controller => "cart", :action => "add" }) %> # # You can change the behaviour with various options, see @@ -181,41 +176,41 @@ module ActionView # # Some of these +options+ include: # * :accept - Set this to a string or an array of strings describing the - # allowable CSS classes that the +draggable_element+ must have in order + # allowable CSS classes that the +draggable_element+ must have in order # to be accepted by this +drop_receiving_element+. - # + # # * :confirm - Adds a confirmation dialog. Example: - # + # # :confirm => "Are you sure you want to do this?" - # + # # * :hoverclass - If set, the +drop_receiving_element+ will have # this additional CSS class when an accepted +draggable_element+ is - # hovered over it. - # + # hovered over it. + # # * :onDrop - Called when a +draggable_element+ is dropped onto - # this element. Override this callback with a JavaScript expression to + # this element. Override this callback with a JavaScript expression to # change the default drop behaviour. Example: - # + # # :onDrop => "function(draggable_element, droppable_element, event) { alert('I like bananas') }" - # + # # This callback gets three parameters: The Draggable element, the Droppable # element and the Event object. You can extract additional information about # the drop - like if the Ctrl or Shift keys were pressed - from the Event object. - # + # # * :with - A JavaScript expression specifying the parameters for # the XMLHttpRequest. Any expressions should return a valid URL query string. def drop_receiving_element(element_id, options = {}) javascript_tag(drop_receiving_element_js(element_id, options).chop!) end - + def drop_receiving_element_js(element_id, options = {}) #:nodoc: options[:with] ||= "'id=' + encodeURIComponent(element.id)" options[:onDrop] ||= "function(element){" + remote_function(options) + "}" options.delete_if { |key, value| PrototypeHelper::AJAX_OPTIONS.include?(key) } - options[:accept] = array_or_string_for_javascript(options[:accept]) if options[:accept] + options[:accept] = array_or_string_for_javascript(options[:accept]) if options[:accept] options[:hoverclass] = "'#{options[:hoverclass]}'" if options[:hoverclass] - + # Confirmation happens during the onDrop callback, so it can be removed from the options options.delete(:confirm) if options[:confirm] -- cgit v1.2.3