WebHelpers / webhelpers / rails / javascript.py

Javascript Helpers

Provides functionality for working with JavaScript in your views.

Ajax, controls and visual effects

* For information on using Ajax, see `Prototype Helpers <module-railshelpers.helpers.prototype.html>`_.
* For information on using controls and visual effects, see `Scriptaculous Helpers <module-railshelpers.helpers.scriptaculous.html>`_.
import re
from tags import *

def link_to_function(name, function, **html_options):
    Returns a link that'll trigger a JavaScript ``function`` using the 
    onclick handler and return false after the fact.
        link_to_function("Greeting", "alert('Hello world!')")
    options = dict(href="#", onclick="%s; return false;" % function)
    return content_tag("a", name, **options)

def button_to_function(name, function, **html_options):
    Returns a link that'll trigger a JavaScript ``function`` using the 
    onclick handler and return false after the fact.
        button_to_function("Greeting", "alert('Hello world!')")
    options = dict(type_="button", value=name, onclick="%s; " % function)
    return content_tag("input", "", **options)

def escape_javascript(javascript):
    Escape carriage returns and single and double quotes for JavaScript segments.
    javascript = re.sub(r'\r\n|\n|\r', r'\\n', (javascript or ''))
    javascript = re.sub(r'(["\'])', r'\\\1', javascript)
    return javascript

def javascript_tag(content):
    Returns a JavaScript tag with the ``content`` inside.
        >>> javascript_tag("alert('All is good')"
        '<script type="text/javascript">alert('All is good')</script>'
    return content_tag("script", javascript_cdata_section(content), type="text/javascript")

def javascript_cdata_section(content):
    return "\n//%s\n" % cdata_section("\n%s\n//" % content)

def options_for_javascript(options):
    optionlist = []
    for key, value in options.iteritems():
        if isinstance(value, bool):
            value = str(value).lower()
        optionlist.append('%s:%s' % (key, value))
    return '{' + ', '.join(optionlist) + '}'

def array_or_string_for_javascript(option):
    jsoption = None
    if isinstance(option, list):
        jsoption = "['%s']" % '\',\''.join(option)
    elif isinstance(option, bool):
        jsoption = str(option).lower()
        jsoption = "'%s'" % option
    return jsoption

__all__ = ['link_to_function', 'button_to_function', 'escape_javascript', 'javascript_tag']
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.