def options_for_ajax(options): js_options = build_callbacks(options) js_options['asynchronous'] = str( options.get('type') != 'synchronous').lower() if options.get('method'): if isinstance(options['method'], str) and options['method'].startswith("'"): js_options['method'] = options['method'] else: js_options['method'] = "'%s'" % options['method'] if options.get('position'): js_options['insertion'] = "Insertion.%s" % camelize( options['position']) js_options['evalScripts'] = str( options.get('script') is None or options['script']).lower() if options.get('form'): js_options['parameters'] = 'Form.serialize(this)' elif options.get('submit'): js_options['parameters'] = "Form.serialize('%s')" % options['submit'] elif options.get('with_'): js_options['parameters'] = options['with_'] return options_for_javascript(js_options)
def update_element_function(element_id, **options): """ Returns a JavaScript function (or expression) that'll update a DOM element. ``content`` The content to use for updating. ``action`` Valid options are 'update' (assumed by default), 'empty', 'remove' ``position`` If the ``action`` is 'update', you can optionally specify one of the following positions: 'before', 'top', 'bottom', 'after'. Example:: <% javascript_tag(update_element_function("products", position='bottom', content="<p>New product!</p>")) %> This method can also be used in combination with remote method call where the result is evaluated afterwards to cause multiple updates on a page. Example:: # Calling view <% form_remote_tag(url=url(action="buy"), complete=evaluate_remote_response()) %> all the inputs here... # Controller action def buy(self, **params): c.product = Product.find(1) return render_response('/buy.myt') # Returning view (buy.myt) <% update_element_function( "cart", action='update', position='bottom', content="<p>New Product: %s</p>" % c.product.name) %> <% update_element_function("status", binding='binding', content="You've bought a new product!") %> """ content = escape_javascript(options.get('content', '')) opval = options.get('action', 'update') if opval == 'update': if options.get('position'): jsf = "new Insertion.%s('%s','%s')" % (camelize( options['position']), element_id, content) else: jsf = "$('%s').innerHTML = '%s'" % (element_id, content) elif opval == 'empty': jsf = "$('%s').innerHTML = ''" % element_id elif opval == 'remove': jsf = "Element.remove('%s')" % element_id else: raise ValueError( "Invalid action, choose one of update, remove, or empty") jsf += ";\n" if options.get('binding'): return jsf + options['binding'] else: return jsf
def update_element_function(element_id, **options): """ Return a JavaScript function (or expression) that'll update a DOM element. ``content`` The content to use for updating. ``action`` Valid options are 'update' (assumed by default), 'empty', 'remove' ``position`` If the ``action`` is 'update', you can optionally specify one of the following positions: 'before', 'top', 'bottom', 'after'. Example:: <% javascript_tag(update_element_function("products", position='bottom', content="<p>New product!</p>")) %> This method can also be used in combination with remote method call where the result is evaluated afterwards to cause multiple updates on a page. Example:: # Calling view <% form_remote_tag(url=url(action="buy"), complete=evaluate_remote_response()) %> all the inputs here... # Controller action def buy(self, **params): c.product = Product.find(1) return render_response('/buy.myt') # Returning view (buy.myt) <% update_element_function( "cart", action='update', position='bottom', content="<p>New Product: %s</p>" % c.product.name) %> <% update_element_function("status", binding='binding', content="You've bought a new product!") %> """ content = escape_javascript(options.get('content', '')) opval = options.get('action', 'update') if opval == 'update': if options.get('position'): jsf = "new Insertion.%s('%s','%s')" % (camelize(options['position']), element_id, content) else: jsf = "$('%s').innerHTML = '%s'" % (element_id, content) elif opval == 'empty': jsf = "$('%s').innerHTML = ''" % element_id elif opval == 'remove': jsf = "Element.remove('%s')" % element_id else: raise ValueError("Invalid action, choose one of update, remove, or empty") jsf += ";\n" if options.get('binding'): return jsf + options['binding'] else: return jsf
def visual_effect(name, element_id=False, **js_options): """ Returns a JavaScript snippet to be used on the 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 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'): 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 options_for_ajax(options): js_options = build_callbacks(options) js_options['asynchronous'] = str(options.get('type') != 'synchronous').lower() if options.get('method'): if isinstance(options['method'], str) and options['method'].startswith("'"): js_options['method'] = options['method'] else: js_options['method'] = "'%s'" % options['method'] if options.get('position'): js_options['insertion'] = "Insertion.%s" % camelize(options['position']) js_options['evalScripts'] = str(options.get('script') is None or options['script']).lower() if options.get('form'): js_options['parameters'] = 'Form.serialize(this)' elif options.get('submit'): js_options['parameters'] = "Form.serialize('%s')" % options['submit'] elif options.get('with'): js_options['parameters'] = options['with'] return options_for_javascript(js_options)
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))