Module: Mjs::Helper
- Defined in:
- lib/mjs/helper.rb,
lib/mjs/sinatra.rb
Constant Summary collapse
- JS_ESCAPE_MAP =
derived from actionpack
{ '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
Class Method Summary collapse
Instance Method Summary collapse
-
#button_to(name, url = '', opts = {}) ⇒ Object
experimental: not tested yet.
-
#escape_javascript(javascript) ⇒ Object
Escape carrier returns and single and double quotes for JavaScript segments.
-
#link_to(name, url = '', opts = {}) ⇒ Object
override! :link_to # for Ajax.
- #page ⇒ Object
- #remote_function(opts) ⇒ Object
Class Method Details
Instance Method Details
#button_to(name, url = '', opts = {}) ⇒ Object
experimental: not tested yet
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/mjs/helper.rb', line 22 def (name, url='', opts={}) opts[:type] = 'button' opts[:value] = name if opts.delete(:remote) || opts[:submit] || opts[:update] ajax = remote_function(opts) opts[:onclick] = "#{opts.delete(:onclick)}; #{ajax}; return false;" end %{<input #{ opts.to_xml_attributes }>} end |
#escape_javascript(javascript) ⇒ Object
Escape carrier returns and single and double quotes for JavaScript segments.
59 60 61 62 63 64 65 |
# File 'lib/mjs/helper.rb', line 59 def escape_javascript(javascript) if javascript javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] } else '' end end |
#link_to(name, url = '', opts = {}) ⇒ Object
override! :link_to # for Ajax
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mjs/helper.rb', line 34 def link_to(name, url='', opts={}) opts[:href] ||= url if opts.delete(:remote) || opts[:submit] || opts[:update] ajax = remote_function(opts) opts[:onclick] = "#{opts.delete(:onclick)}; #{ajax}; return false;" opts[:href] = '#' else opts[:href] ||= url end %{<a #{ opts.to_xml_attributes }>#{name}</a>} end |
#page ⇒ Object
6 7 8 |
# File 'lib/mjs/helper.rb', line 6 def page @page ||= Mjs::JavaScriptContext.new end |
#remote_function(opts) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mjs/helper.rb', line 10 def remote_function(opts) build_href(opts) unless opts[:submit] opts[:url] ||= opts[:href] end function = "jQuery.ajax(%s);" % (opts) confirm = opts.delete(:confirm) function = "if (confirm('#{escape_javascript(confirm)}')) { #{function}; }" if confirm return function end |