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): """ 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 drop_receiving_element_js(element_id, **options): options.setdefault('with_', "'id=' + encodeURIComponent(element.id)") options.setdefault('onDrop', "function(element){%s}" % remote_function(**options)) for k in options.keys(): if k in AJAX_OPTIONS: del options[k] if options.has_key('accept'): options['accept'] = array_or_string_for_javascript(options['accept']) if options.has_key('hoverclass'): options['hoverclass'] = "'%s'" % options['hoverclass'] return "Droppables.add(%s, %s)" % (_elements_to_js(element_id), options_for_javascript(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))
def parallel_effects(*effects, **js_options): """ Wraps 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 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_js(element_id, **options): options.setdefault('with', "Sortable.serialize('%s')" % element_id) options.setdefault('onUpdate', "function(){%s}" % remote_function(**options)) for k in options.keys(): if k in AJAX_OPTIONS: del options[k] for option in ['tag', 'overlap', 'constraint', 'handle']: if options.has_key(option) and options[option]: options[option] = "'%s'" % options[option] if options.has_key('containment'): options['containment'] = array_or_string_for_javascript(options['containment']) if options.has_key('only'): options['only'] = array_or_string_for_javascript(options['only']) return "Sortable.create(%s, %s)" % (json.dumps(element_id), options_for_javascript(options))
def sortable_element_js(element_id, **options): if not isinstance(element_id, basestring): raise ValueError('Argument element_id must be a string') options.setdefault('with_', "Sortable.serialize('%s')" % element_id) options.setdefault('onUpdate', "function(){%s}" % remote_function(**options)) for k in options.keys(): if k in AJAX_OPTIONS: del options[k] for option in ['tag', 'overlap', 'constraint', 'handle']: if options.has_key(option) and options[option]: options[option] = "'%s'" % options[option] if options.has_key('containment'): options['containment'] = array_or_string_for_javascript(options['containment']) if options.has_key('only'): options['only'] = array_or_string_for_javascript(options['only']) return "Sortable.create(%s, %s)" % (_elements_to_js(element_id), options_for_javascript(options))
def sortable_element_js(element_id, **options): options.setdefault('with', "Sortable.serialize('%s')" % element_id) options.setdefault('onUpdate', "function(){%s}" % remote_function(**options)) for k in options.keys(): if k in AJAX_OPTIONS: del options[k] for option in ['tag', 'overlap', 'constraint', 'handle']: if options.has_key(option) and options[option]: options[option] = "'%s'" % options[option] if options.has_key('containment'): options['containment'] = array_or_string_for_javascript( options['containment']) if options.has_key('only'): options['only'] = array_or_string_for_javascript(options['only']) return "Sortable.create(%s, %s)" % (json.dumps(element_id), options_for_javascript(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 draggable_element_js(element_id, **options): return "new Draggable(%s, %s)" % (_elements_to_js(element_id), options_for_javascript(options))