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!

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

  • http://www.apnabill.com Mayank Jain (makuchaku)

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

  • http://www.apnabill.com Mayank Jain (makuchaku)

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

  • http://anup.info Anup

    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

  • http://www.apnabill.com Mayank Jain (makuchaku)

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

    Thanks Anup!

  • http://xurde.info xurde

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