Posted By


xurde on 09/27/06

Tagged


Statistics


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

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.