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
上级 9e803e08
*SVN*
* Added :popup option to UrlHelper#link_to #1996 [gabriel.gironda@gmail.com]. Examples:
link_to "Help", { :action => "help" }, :popup => true
link_to "Busy loop", { :action => "busy" }, :popup => ['new_window', 'height=300,width=600']
* Drop trailing \000 if present on RAW_POST_DATA (works around bug in Safari Ajax implementation) #918
* Fix observe_field to fall back to event-based observation if frequency <= 0 #1916 [michael@schubert.cx]
......
......@@ -19,14 +19,20 @@ def url_for(options = {}, *parameters_for_method_reference)
# Creates a link tag of the given +name+ using an URL created by the set of +options+. See the valid options in
# link:classes/ActionController/Base.html#M000021. It's also possible to pass a string instead of an options hash to
# get a link tag that just points without consideration. If nil is passed as a name, the link itself will become the name.
# The html_options have a special feature for creating javascript confirm alerts where if you pass :confirm => 'Are you sure?',
#
# The html_options has two special features. One for creating javascript confirm alerts where if you pass :confirm => 'Are you sure?',
# the link will be guarded with a JS popup asking that question. If the user accepts, the link is processed, otherwise not.
#
# Example:
# The other for creating a popup window, which is done by either passing :popup with true or the options of the window in
# Javascript form.
#
# Examples:
# link_to "Delete this page", { :action => "destroy", :id => @page.id }, :confirm => "Are you sure?"
# link_to "Help", { :action => "help" }, :popup => true
# link_to "Busy loop", { :action => "busy" }, :popup => ['new_window', 'height=300,width=600']
def link_to(name, options = {}, html_options = nil, *parameters_for_method_reference)
html_options = (html_options || {}).stringify_keys
convert_confirm_option_to_javascript!(html_options)
convert_options_to_javascript!(html_options)
if options.is_a?(String)
content_tag "a", name || options, (html_options || {}).merge("href" => options)
else
......@@ -224,9 +230,23 @@ def current_page?(options)
def convert_confirm_option_to_javascript!(html_options)
if confirm = html_options.delete("confirm")
html_options["onclick"] = "return confirm('#{escape_javascript(confirm)}');"
return confirm
end
end
def convert_popup_option_to_javascript!(html_options, confirm_message = false)
if popup = html_options.delete("popup")
popup_js = popup.is_a?(Array) ? "window.open(this.href,'#{popup.first}','#{popup.last}');" : "window.open(this.href);"
html_options["onclick"] = popup_js + 'return false;' unless confirm_message
html_options["onclick"] = "if (confirm('#{escape_javascript(confirm_message)}')) { #{popup_js} };return false;" if confirm_message
end
end
def convert_options_to_javascript!(html_options)
confirm_message = convert_confirm_option_to_javascript!(html_options)
convert_popup_option_to_javascript!(html_options, confirm_message)
end
# Processes the _html_options_ hash, converting the boolean
# attributes from true/false form into the form required by
# HTML/XHTML. (An attribute is considered to be boolean if
......
......@@ -62,6 +62,32 @@ def test_link_tag_with_javascript_confirm
)
end
def test_link_tag_with_popup
assert_equal(
"<a href=\"http://www.example.com\" onclick=\"window.open(this.href);return false;\">Hello</a>",
link_to("Hello", "http://www.example.com", :popup => true)
)
assert_equal(
"<a href=\"http://www.example.com\" onclick=\"window.open(this.href);return false;\">Hello</a>",
link_to("Hello", "http://www.example.com", :popup => 'true')
)
assert_equal(
"<a href=\"http://www.example.com\" onclick=\"window.open(this.href,'window_name','width=300,height=300');return false;\">Hello</a>",
link_to("Hello", "http://www.example.com", :popup => ['window_name', 'width=300,height=300'])
)
end
def test_link_tag_with_popup_and_javascript_confirm
assert_equal(
"<a href=\"http://www.example.com\" onclick=\"if (confirm('Fo\\' sho\\'?')) { window.open(this.href); };return false;\">Hello</a>",
link_to("Hello", "http://www.example.com", { :popup => true, :confirm => "Fo' sho'?" })
)
assert_equal(
"<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>",
link_to("Hello", "http://www.example.com", { :popup => ['window_name', 'width=300,height=300'], :confirm => "Are you serious?" })
)
end
def test_link_to_unless
assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog")
assert "<a href=\"http://www.example.com\">Listing</a>", link_to_unless(false, "Listing", :action => "list", :controller => "weblog")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册