module Merb module JavascriptMixin # escape text for javascript. def escape_js(javascript) (javascript || '').gsub('\\','\0\0').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" } end def link_to_function(name, function) %{#{name}} end def js(data) if data.respond_to? :to_json data.to_json else data.inspect.to_json end end def require_js(*scripts) return nil if scripts.empty? scripts.inject('') do |memo,script| script = script.to_s memo << %Q|\n| end end def require_css(*scripts) return nil if scripts.empty? scripts.inject('') do |memo,script| script = script.to_s memo << %Q|\n| end end def js_hash(options) '{' + options.map {|k, v| "#{k}:#{v}"}.join(', ') + '}' end def insert_html(id, html, options = {}) position = options.fetch(:where, :before) "new Insertion.#{position.to_s.camel_case}('#{id}', '#{escape_js html}');" end def replace_html(id, html, options = {}) "Element.update('#{id}', '#{escape_js html}');" end def hide(id) "$('#{id}').style.display = 'none';" end def show(id) "$('#{id}').style.display = 'block';" end def toggle(id) "Element.toggle('#{id}');" end end end