Posted By


xurde on 09/27/06

Tagged


Statistics


Viewed 1030 times
Favorited by 2 user(s)

prototype_window_class_helper.rb


/ Published in: Ruby
Save to your folder(s)

by xurde


Copy this code and paste it in your HTML
  1. # Prototype Window Class Helper v0.11.0
  2. # By Jorge Díaz - http://xurde.info
  3. # email: jorgedf@gmail.com
  4. # Thanks to Sebastien Gruhier for his Prototype Window Class (http://prototype-window.xilinus.com/)
  5. # Samples: http://pwc-helper.xurdeonrails.com
  6.  
  7. #Quick use:
  8. #Reference this helper in your rails applicaction adding -> helper :prototype_window_class in your application.rb
  9. #You must include in the template header the prototype window class javascripts and the .css theme you want to use.
  10. #This code in your template might be enough:
  11.  
  12. # <%= stylesheet_link_tag 'default' %> (or theme you wanna use)
  13. # <%= stylesheet_link_tag 'alert' %>
  14. # <%= javascript_include_tag :defaults %>
  15. # <%= javascript_include_tag 'window'%>
  16.  
  17.  
  18.  
  19. class JsCode < String # for JavaScript Code Handling purpose
  20. end
  21.  
  22.  
  23. module PrototypeWindowClassHelper
  24.  
  25. #support methods
  26.  
  27. def params_for_javascript(params) #options_for_javascript doesn't works fine
  28.  
  29. '{' + params.map {|k, v| "#{k}: #{
  30. case v
  31. when Hash then params_for_javascript( v )
  32. when JsCode then v
  33. # when Array then...
  34. when String then "'#{v}'"
  35. else v #Isn't neither Hash or String
  36. end }"}.sort.join(', ') + '}'
  37. end
  38.  
  39.  
  40. def content_for_window( content ) #converts
  41.  
  42. case content
  43. when Hash then params_for_javascript( content )
  44. when String then "'#{content}'"
  45. else
  46. nil
  47. end
  48. end
  49.  
  50.  
  51. #helper methods
  52.  
  53. def link_to_prototype_dialog( name, content, dialog_kind = 'alert', options = {} , html_options = {} )
  54.  
  55. #dialog_kind: 'alert' (default), 'confirm' or 'info' (info dialogs should be destroyed with a javascript function call 'win.destroy')
  56. #options for this helper depending the dialog_kind: http://prototype-window.xilinus.com/documentation.html#alert (#confirm or #info)
  57.  
  58. options.merge!( :windowParameters => {} ) if !options.has_key?(:windowParameters)
  59.  
  60. js_code ="Dialog.#{dialog_kind}( #{content_for_window(content)}, #{params_for_javascript(options) } ); "
  61. content_tag(
  62. "a", name,
  63. html_options.merge({
  64. :href => html_options[:href] || "#",
  65. :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + js_code }))
  66. end
  67.  
  68.  
  69.  
  70.  
  71. def link_to_prototype_confirm_url( name, content, ok_url, cancel_url = nil , options = {} , html_options = {} )
  72.  
  73. #options for this helper: http://prototype-window.xilinus.com/documentation.html#confirm
  74.  
  75. js_ok = "function(){document.location=\'#{ok_url}\';}"
  76. js_cancel = "function(){document.location=\'#{cancel_url}\';}"
  77.  
  78. options.merge!( :ok => JsCode.new(js_ok) )
  79. options.merge!( :cancel => JsCode.new(js_cancel) ) if cancel_url
  80. options.merge!( :windowParameters => {} ) if !options.has_key?(:windowParameters)
  81.  
  82. js_code = "Dialog.confirm( '#{content}', #{params_for_javascript( options )} ); "
  83. content_tag(
  84. "a", name,
  85. html_options.merge({
  86. :href => html_options[:href] || "#",
  87. :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + js_code }))
  88. end
  89.  
  90.  
  91.  
  92. def link_to_prototype_window( name, window_id, options = {} , html_options = {} )
  93.  
  94. #window_id must be unique and it's destroyed on window close.
  95. #options for this helper: http://prototype-window.xilinus.com/documentation.html#initialize
  96.  
  97. options.merge!( :windowParameters => {} ) if !options.has_key?(:windowParameters)
  98.  
  99. js_code ="var win = new Window( '#{window_id}', #{params_for_javascript(options) } ); win.show(); win.setDestroyOnClose();"
  100. content_tag(
  101. "a", name,
  102. html_options.merge({
  103. :href => html_options[:href] || "#",
  104. :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + js_code }))
  105. end
  106.  
  107. end

URL: http://pwc-helper.xurdeonrails.com/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.