Anonymous avatar Anonymous committed a2d066a

[svn] Updated scriptaculous helpers to use simplejson dumping for element id's, updated corresponding unit tests.

Comments (0)

Files changed (2)

tests/test_scriptaculous.py

 
 class TestScriptaculousHelper(TestCase):
     def test_effect(self):
-        self.assertEqual("new Effect.Highlight('posts',{});", visual_effect('highlight', "posts"))
-        self.assertEqual("new Effect.Fade('fademe',{duration:4.0});", visual_effect('fade', "fademe", duration=4.0))
+        self.assertEqual("new Effect.Highlight(\"posts\",{});", visual_effect('highlight', "posts"))
+        self.assertEqual("new Effect.Fade(\"fademe\",{duration:4.0});", visual_effect('fade', "fademe", duration=4.0))
         self.assertEqual("new Effect.Shake(element,{});", visual_effect('shake'))
-        self.assertEqual("new Effect.DropOut('dropme',{queue:'end'});", visual_effect('drop_out', 'dropme',queue='end'))
-        self.assertEqual("new Effect.DropOut('dropme',{queue:{scope:'test',limit:2,position:'end'}});",  
+        self.assertEqual("new Effect.DropOut(\"dropme\",{queue:'end'});", visual_effect('drop_out', 'dropme',queue='end'))
+        self.assertEqual("new Effect.DropOut(\"dropme\",{queue:{scope:'test',limit:2,position:'end'}});",  
             visual_effect('drop_out', 'dropme', queue=dict(position="end",scope="test", limit=2)))
-        self.assertEqual("new Effect.DropOut('dropme',{queue:{scope:'list',limit:2}});",
+        self.assertEqual("new Effect.DropOut(\"dropme\",{queue:{scope:'list',limit:2}});",
             visual_effect('drop_out', 'dropme', queue=dict(scope='list',limit=2)))
-        self.assertEqual("new Effect.DropOut('dropme',{queue:{scope:'test',limit:2,position:'end'}});",  
+        self.assertEqual("new Effect.DropOut(\"dropme\",{queue:{scope:'test',limit:2,position:'end'}});",  
             visual_effect('drop_out', 'dropme', queue=dict(position='end',scope='test',limit=2)))
     
     def test_toggle_effects(self):
-        self.assertEqual("Effect.toggle('posts','appear',{});", visual_effect("toggle_appear", "posts"))
-        self.assertEqual("Effect.toggle('posts','slide',{});", visual_effect("toggle_slide", "posts"))
-        self.assertEqual("Effect.toggle('posts','blind',{});", visual_effect("toggle_blind", "posts"))
+        self.assertEqual("Effect.toggle(\"posts\",'appear',{});", visual_effect("toggle_appear", "posts"))
+        self.assertEqual("Effect.toggle(\"posts\",'slide',{});", visual_effect("toggle_slide", "posts"))
+        self.assertEqual("Effect.toggle(\"posts\",'blind',{});", visual_effect("toggle_blind", "posts"))
     
     def test_sortable_element(self):
-        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nSortable.create('mylist', {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize('mylist')})}})\n//]]>\n</script>""", 
+        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nSortable.create("mylist", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize('mylist')})}})\n//]]>\n</script>""", 
             sortable_element("mylist",url='http://www.example.com/order'))
-        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nSortable.create('mylist', {constraint:'horizontal', onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize('mylist')})}, tag:'div'})\n//]]>\n</script>""",
+        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nSortable.create("mylist", {constraint:'horizontal', onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize('mylist')})}, tag:'div'})\n//]]>\n</script>""",
             sortable_element("mylist",tag="div",constraint="horizontal",url='http://www.example.com/order'))
-        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nSortable.create('mylist', {constraint:'horizontal', containment:['list1','list2'], onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize('mylist')})}})\n//]]>\n</script>""",
+        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nSortable.create("mylist", {constraint:'horizontal', containment:['list1','list2'], onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize('mylist')})}})\n//]]>\n</script>""",
             sortable_element("mylist",containment=['list1','list2'], constraint="horizontal",url='http://www.example.com/order'))
-        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nSortable.create('mylist', {constraint:'horizontal', containment:'list1', onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize('mylist')})}})\n//]]>\n</script>""", 
+        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nSortable.create("mylist", {constraint:'horizontal', containment:'list1', onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize('mylist')})}})\n//]]>\n</script>""", 
             sortable_element("mylist",containment='list1',constraint="horizontal",url='http://www.example.com/order'))
 
     def test_draggable_element(self):
-        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nnew Draggable('product_13', {})\n//]]>\n</script>""",
+        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nnew Draggable("product_13", {})\n//]]>\n</script>""",
             draggable_element('product_13'))
-        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nnew Draggable('product_13', {revert:true})\n//]]>\n</script>""",
+        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nnew Draggable("product_13", {revert:true})\n//]]>\n</script>""",
             draggable_element('product_13',revert=True))
 
     def test_drop_receiving_element(self):
-        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nDroppables.add('droptarget1', {onDrop:function(element){new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>""",
+        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nDroppables.add("droptarget1", {onDrop:function(element){new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>""",
             drop_receiving_element('droptarget1',url='http://www.example.com/'))
-        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nDroppables.add('droptarget1', {accept:'products', onDrop:function(element){new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>""",
+        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nDroppables.add("droptarget1", {accept:'products', onDrop:function(element){new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>""",
             drop_receiving_element('droptarget1',url='http://www.example.com/',accept='products'))
-        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nDroppables.add('droptarget1', {accept:'products', onDrop:function(element){new Ajax.Updater('infobox', 'http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>""",
+        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nDroppables.add("droptarget1", {accept:'products', onDrop:function(element){new Ajax.Updater('infobox', 'http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>""",
             drop_receiving_element('droptarget1',accept='products',update='infobox',url='http://www.example.com/'))
-        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nDroppables.add('droptarget1', {accept:['tshirts','mugs'], onDrop:function(element){new Ajax.Updater('infobox', 'http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>""",
+        self.assertEqual("""<script type="text/javascript">\n//<![CDATA[\nDroppables.add("droptarget1", {accept:['tshirts','mugs'], onDrop:function(element){new Ajax.Updater('infobox', 'http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>""",
             drop_receiving_element('droptarget1',accept=['tshirts','mugs'],update='infobox',url='http://www.example.com/'))
 
 

webhelpers/rails/scriptaculous.py

 using these helpers in your application.
 """
 # Last synced with Rails copy at Revision 3772 on Aug 19th, 2006.
-
+import simplejson as json
 from prototype import *
 from javascript import options_for_javascript, array_or_string_for_javascript
 from prototype import AJAX_OPTIONS, javascript_tag
     You can change the behaviour with various options, see
     http://script.aculo.us for more documentation.
     """
-    element = (element_id and "'%s'" % element_id) or "element"
+    element = (element_id and json.dumps(element_id)) or "element"
     if isinstance(js_options.get('queue'), dict):
         js_options['queue'] = '{%s}' % ','.join(["%s:%s" % (k,(k == 'limit' and v) or "'%s'" % v) for k,v in js_options['queue'].iteritems()])
     elif js_options.has_key('queue'):
     if options.has_key('only'):
         options['only'] = array_or_string_for_javascript(options['only'])
     
-    return "Sortable.create('%s', %s)" % (element_id, options_for_javascript(options))
+    return "Sortable.create(%s, %s)" % (json.dumps(element_id), options_for_javascript(options))
 
 def draggable_element(element_id, **options):
     """
     return javascript_tag(draggable_element_js(element_id, **options))
 
 def draggable_element_js(element_id, **options):
-    return "new Draggable('%s', %s)" % (element_id, options_for_javascript(options))
+    return "new Draggable(%s, %s)" % (json.dumps(element_id), options_for_javascript(options))
 
 def drop_receiving_element(element_id, **options):
     """
     if options.has_key('hoverclass'):
         options['hoverclass'] = "'%s'" % options['hoverclass']
     
-    return "Droppables.add('%s', %s)" % (element_id, options_for_javascript(options))
+    return "Droppables.add(%s, %s)" % (json.dumps(element_id), options_for_javascript(options))
 
 __all__ = ['visual_effect', 'parallel_effects', 'sortable_element', 'draggable_element', 'drop_receiving_element']
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.