""" Scriptaculous Helpers 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 pages. The Scriptaculous helpers' behavior can be tweaked with various options. See the documentation at http://script.aculo.us for more information on using these helpers in your application. """ # Last synced with Rails copy at Revision 6057 on Feb 9th, 2007. from prototype import * from javascript import options_for_javascript, array_or_string_for_javascript from prototype import AJAX_OPTIONS, javascript_tag from tags import camelize def _elements_to_js(elements): if isinstance(elements, str): return "'%s'" % elements elif isinstance(elements, list): return "[" + ",".join([_elements_to_js(e) for e in elements]) + "]" def visual_effect(name, element_id=False, **js_options): """ Return JavaScript for Ajax callbacks for starting visual effects. Example:: <% link_to_remote("Reload", dict(url=url(action="reload"), update="posts", 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 used for example with drop_receiving_element:: <% drop_receving_element('some_element', loading=visual_effect('fade')) %> 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 ``toggle_blind`` which will alternate between appear/fade, slidedown/slideup, and blinddown/blindup respectively. You can change the behaviour with various options, see http://script.aculo.us for more documentation. """ element = (element_id and _elements_to_js(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'): js_options['queue'] = "'%s'" % js_options['queue'] if 'toggle' in name: return "Effect.toggle(%s,'%s',%s);" % (element, name.replace('toggle_',''), options_for_javascript(js_options)) return "new Effect.%s(%s,%s);" % (camelize(name), element, options_for_javascript(js_options)) def parallel_effects(*effects, **js_options): """ Wrap visual effects so they occur in parallel. Example:: parallel_effects( visual_effect('highlight, 'dom_id'), visual_effect('fade', 'dom_id'), ) """ str_effects = [e[:e.rindex(';')] for e in effects] # Remove trailing ';' return "new Effect.Parallel([%s], %s)" % (','.join(str_effects), options_for_javascript(js_options)) def sortable_element(element_id, **options): """ Make the element with the DOM ID specified by ``element_id`` sortable. Uses drag-and-drop and makes an Ajax call whenever the sort order has changed. By default, the action called gets the serialized sortable element as parameters. Example:: <% sortable_element("my_list", url=url(action="order")) %> In the example, the server-side action gets a "my_list" array parameter containing the values of the ids of elements the sortable consists of, in the current order (like ``mylist=item1&mylist=item2``, where ``item1`` and ``item2`` are the ids of the ``