DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Jsfx.rb 0.1: Ruby Module For Inserting JS Code In Rails Views With A Ruby Syntax

04.18.2005
| 4848 views |
  • submit to reddit
        module Jsfx
  
  # by Matt Lyon, sanehatter@gmail.com
  # written on 17 April, after I spent an hour hunting down a missing apostraphe

  # f.e. <%= link_to_function thing.title, Jsfx.toggle("thing-#{thing.id}-insides") %>
  # or multiples: Jsfx.toggle(["id-1","id-2"])
  def Jsfx.toggle(ids=[])
    s = ids.collect {|i| "\'#{i.to_s}\'"}.join(', ')
    "Toggle.display(#{s});"
  end
  
  # insert anywhere you'd put the equivalent javascript.
  # f.e.:
  # form_remote_tag(:url => "/c/a/i", :update => "id", :complete => Jsfx.highlight("thing-#{thing.id}")
  #
  # You can combine multiple effects like so: 
  # Jsfx.highlight("thing-#{thing.id}") + Jsfx.toggle("thing-#{thing.id}-insides")
  def Jsfx.highlight(id)
    "new Effect.Highlight(\'#{id}');"
  end
  def Jsfx.fade(id)
    "new Effect.Fade(\'#{id}\');"
  end
  def Jsfx.scale(id,percent)
    "new Effect.Scale(\'#{id}\',#{percent});"
  end
  def Jsfx.squish(id)
    "new Effect.Squish(\'#{id}\');"
  end
  def Jsfx.puff(id)
    "new Effect.Puff(\'#{id}\')"
  end
  def Jsfx.appear(id)
    "new Effect.Appear(\'#{id}\')"
  end
  
  # Non-Prototype DOM manipulation
  # f.e. Jsfx.change("thing-#{thing.id}-description","Saving...")
  def Jsfx.change(id,new)
    "$(\'#{id}\').innerHTML = '#{new}'"
  end
  
  # Form Helpers
  def Jsfx.form_disable(id)
    "Form.disable(\'#{id}\');"
  end
  def Jsfx.form_reset(id)
    "Form.reset(\'#{id}\');"
  end
  def Jsfx.form_focus(id)
    "Form.focus_first(\'#{id}\');"
  end
end