source: ether_statistics/web/resources/js/windows_js_1.3/helper/prototype_window_class_helper.rb @ 578

Last change on this file since 578 was 569, checked in by vmipsl, 12 years ago

Nouveau projet

File size: 2.5 KB
Line 
1# Prototype Window Class Helper (http://pwc-helper.xurdeonrails.com)
2# by Jorge Díaz (http://xurde.info)
3# thanks to Sebastien Gruhier for his Prototype Window Class (http://prototype-window.xilinus.com/)
4
5#Quick use:
6#Reference this helper in your rails applicaction adding -> helper :prototype_window_class in your application.rb
7#You must include in the template header the prototype window class javascripts and the .css theme you want to use.
8#This code in your template might be enough:
9
10    #  <%= stylesheet_link_tag 'default' %> (or theme you wanna use)
11    #  <%= stylesheet_link_tag 'alert' %>
12    #  <%= javascript_include_tag :defaults %>
13    #  <%= javascript_include_tag 'window'%>
14
15
16module PrototypeWindowClassHelper
17
18    def params_for_javascript(params) #options_for_javascript doesn't works fine
19       
20        '{' + params.map {|k, v| "#{k}: #{ 
21            case v
22              when Hash then params_for_javascript( v )
23              when String then "'#{v}'"         
24            else v   #Isn't neither Hash or String
25            end }"}.sort.join(', ') + '}'
26    end
27   
28   
29   
30    def link_to_prototype_dialog( name, content, dialog_kind = 'alert', options = { :windowParameters => {} } , html_options = {} )
31   
32        #dialog_kind: 'alert' (default), 'confirm' or 'info' (info dialogs should be destroyed with a javascript function call 'win.destroy')
33        #options for this helper depending the dialog_kind: http://prototype-window.xilinus.com/documentation.html#alert (#confirm or #info)
34   
35        js_code ="Dialog.#{dialog_kind}( '#{content}',  #{params_for_javascript(options) } ); "
36        content_tag(
37               "a", name, 
38               html_options.merge({ 
39                 :href => html_options[:href] || "#", 
40                 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + js_code }))
41    end
42   
43   
44   
45    def link_to_prototype_window( name, window_id, options = { :windowParameters => {} } , html_options = {} )
46       
47        #window_id must be unique and it's destroyed on window close.
48        #options for this helper: http://prototype-window.xilinus.com/documentation.html#initialize
49     
50        js_code ="var win = new Window( '#{window_id}', #{params_for_javascript(options) } );  win.show();  win.setDestroyOnClose();"
51        content_tag(
52               "a", name, 
53               html_options.merge({ 
54                 :href => html_options[:href] || "#", 
55                 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + js_code }))
56    end
57
58end
Note: See TracBrowser for help on using the repository browser.