From d11f8d551640c94e22c221c3bee39ab572b1dc72 Mon Sep 17 00:00:00 2001 From: Thomas Fuchs Date: Sun, 26 Feb 2006 14:20:21 +0000 Subject: Added script.aculo.us drag and drop helpers to RJS [Thomas Fuchs] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3667 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/action_view/helpers/prototype_helper.rb | 22 +++++++++++++++++++- .../action_view/helpers/scriptaculous_helper.rb | 24 ++++++++++++++++------ 2 files changed, 39 insertions(+), 7 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 8a4498ae89..4c6c020ebe 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -565,12 +565,32 @@ module ActionView record "}, #{(seconds * 1000).to_i})" end - # Starts a Scriptaculous visual effect. See + # Starts a script.aculo.us visual effect. See # ActionView::Helpers::ScriptaculousHelper for more information. def visual_effect(name, id, options = {}) record @context.send(:visual_effect, name, id, options) end + # Creates a script.aculo.us sortable element. Useful + # to recreate sortable elements after items get added + # or deleted. + # See ActionView::Helpers::ScriptaculousHelper for more information. + def sortable(id, options = {}) + record @context.send(:sortable_element_js, id, options) + end + + # Creates a script.aculo.us draggable element. + # See ActionView::Helpers::ScriptaculousHelper for more information. + def draggable(id, options = {}) + record @context.send(:draggable_element_js, id, options) + end + + # Creates a script.aculo.us drop receiving element. + # See ActionView::Helpers::ScriptaculousHelper for more information. + def drop_receiving(id, options = {}) + record @context.send(:drop_receiving_element_js, id, options) + end + private def include_helpers_from_context @context.extended_by.each do |mod| diff --git a/actionpack/lib/action_view/helpers/scriptaculous_helper.rb b/actionpack/lib/action_view/helpers/scriptaculous_helper.rb index e64966d919..5498ae1cd9 100644 --- a/actionpack/lib/action_view/helpers/scriptaculous_helper.rb +++ b/actionpack/lib/action_view/helpers/scriptaculous_helper.rb @@ -67,6 +67,10 @@ module ActionView # You can change the behaviour with various options, 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('#{element_id}')" options[:onUpdate] ||= "function(){" + remote_function(options) + "}" options.delete_if { |key, value| PrototypeHelper::AJAX_OPTIONS.include?(key) } @@ -78,7 +82,7 @@ module ActionView options[:containment] = array_or_string_for_javascript(options[:containment]) if options[:containment] options[:only] = array_or_string_for_javascript(options[:only]) if options[:only] - javascript_tag("Sortable.create('#{element_id}', #{options_for_javascript(options)})") + %(Sortable.create('#{element_id}', #{options_for_javascript(options)});) end # Makes the element with the DOM ID specified by +element_id+ draggable. @@ -87,9 +91,13 @@ module ActionView # <%= draggable_element("my_image", :revert => true) # # You can change the behaviour with various options, see - # http://script.aculo.us for more documentation. + # http://script.aculo.us for more documentation. def draggable_element(element_id, options = {}) - javascript_tag("new Draggable('#{element_id}', #{options_for_javascript(options)})") + javascript_tag(draggable_element_js(element_id, options).chop!) + end + + def draggable_element_js(element_id, options = {}) #:nodoc: + %(new Draggable('#{element_id}', #{options_for_javascript(options)});) end # Makes the element with the DOM ID specified by +element_id+ receive @@ -104,14 +112,18 @@ module ActionView # You can change the behaviour with various options, see # http://script.aculo.us for more documentation. 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[:hoverclass] = "'#{options[:hoverclass]}'" if options[:hoverclass] - - javascript_tag("Droppables.add('#{element_id}', #{options_for_javascript(options)})") + + %(Droppables.add('#{element_id}', #{options_for_javascript(options)});) end end end -- cgit v1.2.3