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 --- actionpack/CHANGELOG | 6 +++++ .../lib/action_view/helpers/prototype_helper.rb | 22 +++++++++++++++++- .../action_view/helpers/scriptaculous_helper.rb | 24 +++++++++++++++----- actionpack/test/template/prototype_helper_test.rb | 26 ++++++++++++++++++++++ 4 files changed, 71 insertions(+), 7 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 1e3934d512..e804343f9f 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,11 @@ *SVN* +* Added script.aculo.us drag and drop helpers to RJS [Thomas Fuchs]. Examples: + + page.draggable 'product-1' + page.drop_receiving 'wastebasket', :url => { :action => 'delete' } + page.sortable 'todolist', :url => { action => 'change_order' } + * Fixed that form elements would strip the trailing [] from the first parameter #3545 [ruby@bobsilva.com] * During controller resolution, update the NameError suppression to check for the expected constant. [Nicholas Seckar] 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 diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index 326363dc58..5a4ec61b39 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../abstract_unit' module BaseTest include ActionView::Helpers::JavaScriptHelper include ActionView::Helpers::PrototypeHelper + include ActionView::Helpers::ScriptaculousHelper include ActionView::Helpers::UrlHelper include ActionView::Helpers::TagHelper @@ -255,4 +256,29 @@ Element.update("baz", "

This is a test

"); @generator.select('p.welcome b').first.hide assert_equal %($$('p.welcome b').first().hide();), @generator.to_s end + + def test_visual_effect + assert_equal %(new Effect.Puff('blah',{});), + @generator.visual_effect(:puff,'blah') + end + + def test_visual_effect_toggle + assert_equal %(Effect.toggle('blah','appear',{});), + @generator.visual_effect(:toggle_appear,'blah') + end + + def test_sortable + assert_equal %(Sortable.create('blah', {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize('blah')})}});), + @generator.sortable('blah', :url => { :action => "order" }) + end + + def test_draggable + assert_equal %(new Draggable('blah', {});), + @generator.draggable('blah') + end + + def test_drop_receiving + assert_equal %(Droppables.add('blah', {onDrop:function(element){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}});), + @generator.drop_receiving('blah', :url => { :action => "order" }) + end end \ No newline at end of file -- cgit v1.2.3