Spread and use Firefox
Advertise on this blog!
-->

Subscribe to my posts via Email


Delivered by FeedBurner

I'm an Indian
Darn proud of being an Indian!

Links...

Categories

Archives

Friends



Subscribe





External Links


April 24, 2008

(Posted at 3:13 am)

LITBox helper for Rails

Introducing - LITBox Helper for Rails!

Disclaimer: This is a work in progress and is available on “as is” basis.

For long, I was trying to integrate Criag Ambros’s RedBox plugin into ApnaBill.com project for implementing modal dialogs but somehow, the CSS always used to get upset, rendering the lightbox in such a way that the content which is to be shown inside it, shows below it.

Then I came across Ryan J Lowe’s LITBox(The site seems to down, try Google cache)- and boy, it rocks! Just 10 seconds setup, very light weight and looks brilliant! The code is nicely written too. But the only drawback was that it was plain JS stuff - so that means no Rails helpers available.

So, once the test implementation was done with, I decided to write a small helper myself. I’ve seen how Jorge’s Prototype Window Class’s rails helper works - so decided to base my code on the same lines. And since he’s already written the Ruby->JS part for converting between optional arguments, this didn’t seemed a daunting task even for a Wednesday night :)

  1.  
  2. # Based on Prototype Window Class Helper (http://pwc-helper.xurdeonrails.com)
  3.  
  4. module LitboxHelper
  5.  
  6.     def params_for_javascript(params) #options_for_javascript doesn’t works fine
  7.         ‘{’ + params.map {|k, v| "#{k}: #{
  8.        case v
  9.          when Hash then params_for_javascript( v )
  10.          when String then "‘#{v}’"          
  11.        else v   #Isn’t neither Hash or String
  12.        end }"}.sort.join(‘, ‘) + ‘}’
  13.     end
  14.    
  15.  
  16.     # Returns a default LITBox window.
  17.     def litbox_window(link_text, href, html_options={}, params={})
  18.       prefix = "<a "
  19.       # Add all html options as key="value" pairs
  20.       html_options.each do |k, v|
  21.         prefix += "#{k}=\"#{v}\" "
  22.       end
  23.       prefix += "href=\"javascript:void(null);\" onclick=\""
  24.      
  25.       # JS Payload
  26.       payload = "new LITBox(’#{href}’, #{params_for_javascript(params)});"
  27.      
  28.       # Rest of the stuff including link text
  29.       suffix = "\">#{link_text}</a>"
  30.      
  31.       return prefix + payload + suffix
  32.     end
  33.  
  34. end

I’m trying to make it feature complete so that I can make it available for download.

Till then… happy hacking!

The params_for_javascript method is bluntly copied from Jorge’s PWC - http://pwc-helper.xurdeonrails.com

Comment by Mayank Jain (makuchaku) — April 24, 2008 @ 3:20 am

Usage is as simple as
< %= litbox_window("link text", url_for(:controller => “test”), {”id” => “html_id”}, {”type” => “window”}) %>

Comment by Mayank Jain (makuchaku) — April 24, 2008 @ 3:21 am

Hey,

I got the same problem for Criag Ambros’s RedBox plugin, where the text was falling out of boxes.
I solved it by replacing the redbox.css and redbox.js by the same files from the demo page: http://www.craigambrose.com/javascripts/redbox.js.

Anup

Comment by Anup — April 28, 2008 @ 12:29 pm

Awesome!
I’ll give it a shot tonight then… :)

Thanks Anup!

Comment by Mayank Jain (makuchaku) — April 28, 2008 @ 1:27 pm

Thanks for the reference. I celebrate my helper was useful for you. :)
Best.
xurde

Comment by xurde — May 13, 2008 @ 3:50 am

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image