[3] | 1 | """ |
---|
| 2 | Javascript Helpers |
---|
| 3 | |
---|
| 4 | Provides functionality for working with JavaScript in your views. |
---|
| 5 | |
---|
| 6 | Ajax, controls and visual effects |
---|
| 7 | --------------------------------- |
---|
| 8 | |
---|
| 9 | * For information on using Ajax, see `Prototype Helpers <module-railshelpers.helpers.prototype.html>`_. |
---|
| 10 | * For information on using controls and visual effects, see `Scriptaculous Helpers <module-railshelpers.helpers.scriptaculous.html>`_. |
---|
| 11 | """ |
---|
| 12 | # Last synced with Rails copy at Revision 4235 on Aug 19th, 2006. |
---|
| 13 | |
---|
| 14 | import re |
---|
| 15 | from tags import * |
---|
| 16 | |
---|
| 17 | def link_to_function(name, function, **html_options): |
---|
| 18 | """ |
---|
| 19 | Returns a link that'll trigger a JavaScript ``function`` using the |
---|
| 20 | onclick handler and return false after the fact. |
---|
| 21 | |
---|
| 22 | Example:: |
---|
| 23 | |
---|
| 24 | link_to_function("Greeting", "alert('Hello world!')") |
---|
| 25 | """ |
---|
| 26 | options = dict(href="#", onclick="%s; return false;" % function) |
---|
| 27 | options.update(html_options) |
---|
| 28 | return content_tag("a", name, **options) |
---|
| 29 | |
---|
| 30 | def button_to_function(name, function, **html_options): |
---|
| 31 | """ |
---|
| 32 | Returns a link that'll trigger a JavaScript ``function`` using the |
---|
| 33 | onclick handler and return false after the fact. |
---|
| 34 | |
---|
| 35 | Example:: |
---|
| 36 | |
---|
| 37 | button_to_function("Greeting", "alert('Hello world!')") |
---|
| 38 | """ |
---|
| 39 | options = dict(type_="button", value=name, onclick="%s; " % function) |
---|
| 40 | options.update(html_options) |
---|
| 41 | return content_tag("input", "", **options) |
---|
| 42 | |
---|
| 43 | def escape_javascript(javascript): |
---|
| 44 | """ |
---|
| 45 | Escape carriage returns and single and double quotes for JavaScript segments. |
---|
| 46 | """ |
---|
| 47 | javascript = re.sub(r'\r\n|\n|\r', r'\\n', (javascript or '')) |
---|
| 48 | javascript = re.sub(r'(["\'])', r'\\\1', javascript) |
---|
| 49 | return javascript |
---|
| 50 | |
---|
| 51 | def javascript_tag(content): |
---|
| 52 | """ |
---|
| 53 | Returns a JavaScript tag with the ``content`` inside. |
---|
| 54 | |
---|
| 55 | Example:: |
---|
| 56 | |
---|
| 57 | >>> javascript_tag("alert('All is good')" |
---|
| 58 | '<script type="text/javascript">alert('All is good')</script>' |
---|
| 59 | """ |
---|
| 60 | return content_tag("script", javascript_cdata_section(content), type="text/javascript") |
---|
| 61 | |
---|
| 62 | def javascript_cdata_section(content): |
---|
| 63 | return "\n//%s\n" % cdata_section("\n%s\n//" % content) |
---|
| 64 | |
---|
| 65 | def options_for_javascript(options): |
---|
| 66 | optionlist = [] |
---|
| 67 | for key, value in options.iteritems(): |
---|
| 68 | if isinstance(value, bool): |
---|
| 69 | value = str(value).lower() |
---|
| 70 | optionlist.append('%s:%s' % (key, value)) |
---|
| 71 | optionlist.sort() |
---|
| 72 | return '{' + ', '.join(optionlist) + '}' |
---|
| 73 | |
---|
| 74 | def array_or_string_for_javascript(option): |
---|
| 75 | jsoption = None |
---|
| 76 | if isinstance(option, list): |
---|
| 77 | jsoption = "['%s']" % '\',\''.join(option) |
---|
| 78 | elif isinstance(option, bool): |
---|
| 79 | jsoption = str(option).lower() |
---|
| 80 | else: |
---|
| 81 | jsoption = "'%s'" % option |
---|
| 82 | return jsoption |
---|
| 83 | |
---|
| 84 | __all__ = ['link_to_function', 'button_to_function', 'escape_javascript', 'javascript_tag'] |
---|