Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3568: prototype_helper_behavior_methods_patch.diff

File prototype_helper_behavior_methods_patch.diff, 4.1 kB (added by devslashnull@gmail.com, 3 years ago)

Patch for the trunk directory

  • actionpack/test/template/prototype_helper_test.rb

    old new  
    111111    assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Observer('cart', 2, function(element, value) {new Ajax.Request('http://www.example.com/cart_changed', {asynchronous:true, evalScripts:true})})\n//]]>\n</script>), 
    112112      observe_form("cart", :frequency => 2, :url => { :action => "cart_changed" }) 
    113113  end 
     114   
     115  def test_observe_elements 
     116    assert_equal %(new Behavior("a", {name: "show_href_on_click", callbacks: [function(element) {Event.observe(element, "click", function() {alert(element.href)});}]});), 
     117      observe_elements("a", {:click => "alert(element.href)"}, :name => "show_href_on_click") 
     118  end 
     119   
     120  def test_register_behaviors 
     121    assert_equal %(new Behavior("a", {name: "a", callbacks: [function(element) {if (/\\.pdf$/i.test(element.href)) element.target = "_blank";}]});), 
     122      register_behaviors('a' => 'if (/\.pdf$/i.test(element.href)) element.target = "_blank";') 
     123  end 
    114124 
    115125  def test_update_element_function 
    116126    assert_equal %($('myelement').innerHTML = 'blub';\n), 
  • actionpack/lib/action_view/helpers/prototype_helper.rb

    old new  
    365365        end 
    366366      end 
    367367       
     368      # Applies callbacks for all elements matching the given CSS selector. For example, 
     369      # to make all +h1+ tags green on mouseover you could do this: 
     370      # 
     371      #   observe_elements 'h1', 
     372      #                    :mouseover => 'Element.setStyle(element, {color: "green"})', 
     373      #                    :mouseout  => 'Element.setStyle(element, {color: null})' 
     374      # 
     375      # Event names may optionally start with 'on'. 
     376      def observe_elements(selector, callbacks, options={}) 
     377        options = { 
     378          :callback => (callbacks || {}).map { |(event,callback)| 
     379            %(Event.observe(element, #{event.to_s.sub(/^on/, '').to_json}, function() {#{callback}});) 
     380          } * "\n", 
     381          :name => selector 
     382        }.merge(options) 
     383 
     384        register_behaviors(selector => options) 
     385      end 
     386 
     387      # Applies behaviors to elements matching certain CSS selectors. For example, 
     388      # to make all paragraph elements fade in you could do this: 
     389      #  
     390      #   register_behaviors 'p' => visual_effect(:appear) 
     391      # 
     392      # Or to make all links to PDF documents open in a new window: 
     393      # 
     394      #   register_behaviors 'a' => 'if (/\.pdf$/i.test(element.href)) element.target = "_blank";' 
     395      def register_behaviors(rules) 
     396        js = [] 
     397        rules.dup.each do |(selector,options)| 
     398          options = {:name => selector, :callbacks => [options].flatten} unless options.is_a? Hash 
     399          options[:callbacks] ||= [options.delete(:callback)] 
     400          options[:callbacks].map! { |callback| "function(element) {#{callback}}" } 
     401           
     402          js << "new Behavior(#{selector.to_json}, {name: #{options[:name].to_json}, callbacks: [#{options[:callbacks] * ','}]});" 
     403        end 
     404        js * "\n" 
     405      end 
     406 
    368407      # JavaScriptGenerator generates blocks of JavaScript code that allow you  
    369408      # to change the content and presentation of multiple DOM elements.  Use  
    370409      # this in your Ajax response bodies, either in a <script> tag or as plain 
     
    501540          yield 
    502541          record "}, #{(seconds * 1000).to_i})" 
    503542        end 
    504  
     543         
     544        # Forces existing and new behaviors to be applied. This is required  
     545        # if any new page elements are to exibit existing behaviors or if  
     546        # this request has created any new behaviors. 
     547        def apply_behaviors 
     548          record "Behavior.apply()" 
     549        end 
     550         
    505551      private 
    506552        def method_missing(method, *arguments, &block) 
    507553          record(@context.send(method, *arguments, &block))