Sha256: 0b10c65b1d80e56143e668f4401d3510ed7a81f0aaa451964d5ab01053131a3c

Contents?: true

Size: 1.92 KB

Versions: 5

Compression:

Stored size: 1.92 KB

Contents

module Nitro

# Add Prototype methods to the ScriptGenerator.
# Prototype.js is a third-party library that provides a number 
# of functions for AJAX-style interaction with the browser. 
# Prototype's homepage is http://prototype.conio.net/

module ScriptGenerator

  # html = A string or a symbol to an action for rendering.
  #--
  # TODO: resolve html.
  #++
  
  def insert_html(id, html, options = {})
    position = options.fetch(:where, :before)
    js "new Insertion.#{position.to_s.camelize}('#{id}', '#{html}');"
  end

  def replace_html(id, html, options = {})
    js "Element.update('#{id}', '#{html}');"
  end
  
  # Hide a DOM element.
  
  def hide(id)
    js "$('#{id}').style.display = 'none';"
  end

  # Show a DOM element.
  
  def show(id)
    js "$('#{id}').style.display = 'block';"
  end

  # Toggle a DOM element.
  
  def toggle(id)
    js "Element.toggle('#{id}');"
  end

  # Perform an ajax, async update.

  def ajax_update(id, options = {})
    code = %~
      new Ajax.Updater(
        { success: '#{id}' },
        '#{options[:action] || options[:url]}', 
        {
          method: '#{options.fetch(:method, :post)}',
          parameters: #{options[:params] || options[:parameters]}, 
    ~

    if before = options[:before]
      if before.is_a? Proc
        old_buffer = @buffer
        @buffer = ''
        before.call
        before = @buffer
        @buffer = old_buffer + @buffer
      end
      code << %~
        onLoading: function(request) {
          #{before}
        },
      ~
    end

    if success = options[:success]
      if success.is_a? Proc      
        old_buffer = @buffer
        @buffer = ''
        success.call
        success = @buffer
        @buffer = old_buffer
      end
      code << %~
        onComplete: function(request) {
          #{success}
        }        
      ~
    end

    code << %~
        }
      );
    ~
    
    js(code)
  end

end

end

# * George Moschovitis <gm@navel.gr>

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nitro-0.27.0 lib/nitro/helper/javascript/prototype.rb
nitro-0.28.0 lib/nitro/helper/javascript/prototype.rb
nitro-0.29.0 lib/nitro/helper/javascript/prototype.rb
nitro-0.30.0 lib/nitro/helper/javascript/prototype.rb
nitro-0.31.0 lib/nitro/helper/javascript/prototype.rb