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
-
-
# Based on Prototype Window Class Helper (http://pwc-helper.xurdeonrails.com)
-
-
module LitboxHelper
-
-
def params_for_javascript(params) #options_for_javascript doesn’t works fine
-
‘{’ + params.map {|k, v| "#{k}: #{
-
case v
-
when Hash then params_for_javascript( v )
-
when String then "‘#{v}’"
-
else v #Isn’t neither Hash or String
-
end }"}.sort.join(‘, ‘) + ‘}’
-
end
-
-
-
# Returns a default LITBox window.
-
def litbox_window(link_text, href, html_options={}, params={})
-
prefix = "<a "
-
# Add all html options as key="value" pairs
-
html_options.each do |k, v|
-
prefix += "#{k}=\"#{v}\" "
-
end
-
prefix += "href=\"javascript:void(null);\" onclick=\""
-
-
# JS Payload
-
payload = "new LITBox(’#{href}’, #{params_for_javascript(params)});"
-
-
# Rest of the stuff including link text
-
suffix = "\">#{link_text}</a>"
-
-
return prefix + payload + suffix
-
end
-
-
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