Skip to content
This repository
Browse code

Added :popup option to UrlHelper#link_to #1996 [gabriel.gironda@gmail…

….com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2129 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 258bacbf8221dc20a83cfaa03decfb522bda3ffb 1 parent 9e803e0
David Heinemeier Hansson authored September 04, 2005
5  actionpack/CHANGELOG
... ...
@@ -1,5 +1,10 @@
1 1
 *SVN*
2 2
 
  3
+* Added :popup option to UrlHelper#link_to #1996 [gabriel.gironda@gmail.com]. Examples:
  4
+
  5
+    link_to "Help", { :action => "help" }, :popup => true
  6
+    link_to "Busy loop", { :action => "busy" }, :popup => ['new_window', 'height=300,width=600']
  7
+
3 8
 * Drop trailing \000 if present on RAW_POST_DATA (works around bug in Safari Ajax implementation) #918
4 9
 
5 10
 * Fix observe_field to fall back to event-based observation if frequency <= 0 #1916 [michael@schubert.cx]
26  actionpack/lib/action_view/helpers/url_helper.rb
@@ -19,14 +19,20 @@ def url_for(options = {}, *parameters_for_method_reference)
19 19
       # Creates a link tag of the given +name+ using an URL created by the set of +options+. See the valid options in
20 20
       # link:classes/ActionController/Base.html#M000021. It's also possible to pass a string instead of an options hash to
21 21
       # get a link tag that just points without consideration. If nil is passed as a name, the link itself will become the name.
22  
-      # The html_options have a special feature for creating javascript confirm alerts where if you pass :confirm => 'Are you sure?',
  22
+      #
  23
+      # The html_options has two special features. One for creating javascript confirm alerts where if you pass :confirm => 'Are you sure?',
23 24
       # the link will be guarded with a JS popup asking that question. If the user accepts, the link is processed, otherwise not.
24 25
       #
25  
-      # Example:
  26
+      # The other for creating a popup window, which is done by either passing :popup with true or the options of the window in 
  27
+      # Javascript form.
  28
+      #
  29
+      # Examples:
26 30
       #   link_to "Delete this page", { :action => "destroy", :id => @page.id }, :confirm => "Are you sure?"
  31
+      #   link_to "Help", { :action => "help" }, :popup => true
  32
+      #   link_to "Busy loop", { :action => "busy" }, :popup => ['new_window', 'height=300,width=600']
27 33
       def link_to(name, options = {}, html_options = nil, *parameters_for_method_reference)
28 34
         html_options = (html_options || {}).stringify_keys
29  
-        convert_confirm_option_to_javascript!(html_options)
  35
+        convert_options_to_javascript!(html_options)
30 36
         if options.is_a?(String)
31 37
           content_tag "a", name || options, (html_options || {}).merge("href" => options)
32 38
         else
@@ -224,9 +230,23 @@ def current_page?(options)
224 230
         def convert_confirm_option_to_javascript!(html_options)
225 231
           if confirm = html_options.delete("confirm")
226 232
             html_options["onclick"] = "return confirm('#{escape_javascript(confirm)}');"
  233
+            return confirm
227 234
           end
228 235
         end
  236
+        
  237
+        def convert_popup_option_to_javascript!(html_options, confirm_message = false)
  238
+          if popup = html_options.delete("popup")
  239
+            popup_js = popup.is_a?(Array) ? "window.open(this.href,'#{popup.first}','#{popup.last}');" : "window.open(this.href);"
  240
+            html_options["onclick"] = popup_js + 'return false;' unless confirm_message
  241
+            html_options["onclick"] = "if (confirm('#{escape_javascript(confirm_message)}')) { #{popup_js} };return false;" if confirm_message
  242
+          end    
  243
+        end
229 244
 
  245
+        def convert_options_to_javascript!(html_options)
  246
+          confirm_message = convert_confirm_option_to_javascript!(html_options)
  247
+          convert_popup_option_to_javascript!(html_options, confirm_message)
  248
+        end
  249
+        
230 250
         # Processes the _html_options_ hash, converting the boolean
231 251
         # attributes from true/false form into the form required by
232 252
         # HTML/XHTML.  (An attribute is considered to be boolean if
26  actionpack/test/template/url_helper_test.rb
@@ -62,6 +62,32 @@ def test_link_tag_with_javascript_confirm
62 62
     )
63 63
   end
64 64
 
  65
+  def test_link_tag_with_popup
  66
+    assert_equal(
  67
+      "<a href=\"http://www.example.com\" onclick=\"window.open(this.href);return false;\">Hello</a>",
  68
+      link_to("Hello", "http://www.example.com", :popup => true)
  69
+    )
  70
+    assert_equal(
  71
+      "<a href=\"http://www.example.com\" onclick=\"window.open(this.href);return false;\">Hello</a>", 
  72
+      link_to("Hello", "http://www.example.com", :popup => 'true')
  73
+    )
  74
+    assert_equal(
  75
+      "<a href=\"http://www.example.com\" onclick=\"window.open(this.href,'window_name','width=300,height=300');return false;\">Hello</a>", 
  76
+      link_to("Hello", "http://www.example.com", :popup => ['window_name', 'width=300,height=300'])
  77
+    )
  78
+  end
  79
+  
  80
+  def test_link_tag_with_popup_and_javascript_confirm
  81
+    assert_equal(
  82
+      "<a href=\"http://www.example.com\" onclick=\"if (confirm('Fo\\' sho\\'?')) { window.open(this.href); };return false;\">Hello</a>",
  83
+      link_to("Hello", "http://www.example.com", { :popup => true, :confirm => "Fo' sho'?" })
  84
+    )
  85
+    assert_equal(
  86
+      "<a href=\"http://www.example.com\" onclick=\"if (confirm('Are you serious?')) { window.open(this.href,'window_name','width=300,height=300'); };return false;\">Hello</a>",
  87
+      link_to("Hello", "http://www.example.com", { :popup => ['window_name', 'width=300,height=300'], :confirm => "Are you serious?" })
  88
+    )
  89
+  end
  90
+  
65 91
   def test_link_to_unless
66 92
     assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog")
67 93
     assert "<a href=\"http://www.example.com\">Listing</a>", link_to_unless(false, "Listing", :action => "list", :controller => "weblog")

0 notes on commit 258bacb

Please sign in to comment.
Something went wrong with that request. Please try again.