require 'mega/dictionary' require 'nitro/compiler/morphing' # Javascript related morphers. module Nitro #-- # A useful superclass for morphers. Provides easy access to # js/css shared buffers. #++ class SharedMorpher < Morpher # :nodoc: all include JavascriptUtils # Record javascript to be injected in the template. def record_js(code) (@compiler.shared[:js_buffer] ||= '') << code end alias_method :js, :record_js # Record css to be injected in the template. def record_css(css) (@compiler.shared[:css_buffer] ||= '') << css end alias_method :css, :record_css # Require javascript files. def require_script_file(*names) files = (@compiler.shared[:js_required_files] ||= Dictionary.new) for name in names files[name] = true end end alias_method :add_script_file, :require_script_file end # Transform a normal achor into an asynchronous request: # ... # becomes # ... class AsyncMorpher < SharedMorpher def before_start(buffer) require_script_file :prototype href = @attributes['href'] @attributes['href'] = '#' @attributes['onclick'] = "new Ajax.Request('#{href}'); return false;" @attributes.delete(@key) end end # Transform script attributes. # #