# bivouac/helpers/view/html module BivouacHelpers module HtmlView @@javascript_default_sources = %w{prototype.js scriptaculous.js} # Returns a stylesheet link tag for the sources specified as arguments. If # you don't specify an extension, .css will be appended automatically. # You can modify the link attributes by passing a hash as the last argument. # # stylesheet_link_tag "style" # => # # # stylesheet_link_tag "style", :media => "all" # => # # # stylesheet_link_tag "random.styles", "/css/stylish" # => # # def stylesheet_link_tag( *data ) options = data.last.is_a?(Hash) ? data.pop : { } options[:rel] = "Stylesheet" options[:type] = "text/css" data.collect do |file| file << ".css" if File.extname(file).blank? options[:href] = "/public/stylesheets/#{file}" link( options ) end end # Returns an html script tag for each of the +sources+ provided. You # can pass in the filename (.js extension is optional) of javascript files # that exist in your public/javascripts directory for inclusion into the # current page. To include the Prototype and Scriptaculous javascript # libraries in your application, pass :defaults as the source. # # javascript_include_tag "xmlhr" # => # # # javascript_include_tag "common.javascript", "elsewhere/cools" # => # # def javascript_include_tag( *sources ) options = sources.last.is_a?(Hash) ? sources.pop : { } options[:type] = "text/javascript" if sources.include?(:defaults) sources = sources[0..(sources.index(:defaults))] + @@javascript_default_sources.dup + sources[(sources.index(:defaults) + 1)..sources.length] sources.delete(:defaults) end sources.collect do |file| file = file.to_s file << ".js" if File.extname(file).blank? options[:src] = "/public/javascripts/#{file}" script( options ) {} end end # Returns an html image tag for the +source+. The +source+ must be a file # that exists in your public images directory (public/images). # You can add html attributes using the +options+. If no alt text is given, # the file name part of the +source+ is used (capitalized and without the # extension) # # image_tag("icon.png") # => # Icon # # image_tag("icon.png", :alt => "Edit Entry") # => # Edit Entry def image_tag( source, options = {} ) options[:src] = "/public/images/#{source}" options[:alt] ||= File.basename(options[:src], '.*').split('.').first.capitalize img( options ) end # Computes the path to an image asset in the public images directory. # Full paths from the document root will be passed through. # Used internally by image_tag to build the image path. # # image_path("edit") # => /public/images/edit # image_path("edit.png") # => /public/images/edit.png # image_path("icons/edit.png") # => /public/images/icons/edit.png # image_path("/icons/edit.png") # => /icons/edit.png # image_path("http://www.railsapplication.com/img/edit.png") # => http://www.railsapplication.com/img/edit.png def image_path( source ) if /^http:\/\//.match( source ) || /^\//.match( source ) return source else return "/public/images/#{source}" end end # Creates a link tag of the given +name+ using +address+ has the href for # the link # # The +options+ will accept a hash of html attributes for the link tag. # It also accepts 2 modifiers that specialize the link behavior. # # * :confirm => 'question?': This will add a JavaScript confirm # prompt with the question specified. If the user accepts, the link is # processed normally, otherwise no action is taken. # * :popup => true || array of window options: This will force the # link to open in a popup window. By passing true, a default browser window # will be opened with the URL. You can also specify an array of options # that are passed-thru to JavaScripts window.open method. # # You can mix and match the +options+. # # link_to "Documentation", "http://camping.rubyforge.org/", :confirm => "Are you sure?" # link_to "Help", R( Help ), :popup => true # link_to image_tag( "thumb.png" ), "/public/images/fullsize.png", :popup => ['My house', 'height=300,width=600'] def link_to(name, address, options = {}) options = javascript_options( options ) options[:href] = address a( options ) do; name; end end private def javascript_options( options ) unless options.is_a?(Hash) raise ArgumentError, "options must be a Hash!" end confirm, popup = options.delete(:confirm), options.delete(:popup) options["onclick"] = case when confirm && popup "if (#{confirm_javascript_function(confirm)}) { #{popup_javascript_function(popup)} };return false;" when confirm "return #{confirm_javascript_function(confirm)};" when popup popup_javascript_function(popup) + 'return false;' else options["onclick"] end return options end # From Rails !!!! def confirm_javascript_function(confirm) "confirm('#{confirm}')" end # From Rails !!!! def popup_javascript_function(popup) popup.is_a?(Array) ? "window.open(this.href,'#{popup.first}','#{popup.last}');" : "window.open(this.href);" end end end