module IE6Fixer
module Helpers
# See the README.txt for an explanation for all helpers.
FIXED = "ie6_fixer/fixed.js"
FIXED_ACTIVE = "ie6_fixer/fixed_active.js"
FIXED_BACKGROUND = "body {background:url(/javascripts/ie6_fixer/foo) fixed;}\n"
WARNING = "ie6_fixer/warning/warning.js"
def ie_fixer *args, &block # => A block that is only visible to IE6, IE7 and IE8.
versions = "6-8" || args.map { |arg| arg.to_s.gsub("ie", "") }
if request.user_agent[/msie\s+[#{versions}]\.\d+/i].present?
set_fixes(&block)
end
nil
end
# USE:
# <% ie_fixer do %>
# Only IE6, IE7 and IE8 see this!
# <% end %>
def ie6_fixer &block # => A block that is only visibly to IE6.
if request.user_agent[/msie\s+6\.\d+/i].present?
set_fixes(&block)
end
nil
end
# USE:
# <% ie6_fixer do %>
# Only IE6 sees this!
# <% end %>
def png_fix *args # => Fixes transparent PNG's with CSS3 PIE.
args.each do |arg|
@styles.push("#{arg} {-pie-png-fix: true; behavior: url(/javascripts/ie6_fixer/PIE.htc);}")
end
nil
end
# USE:
# <% ie6_fixer do %>
# <%= png_fix "#id_name", ".class_name", "#content img" %>
# <% end %>
def css3_fix *args # => Fixes some of the CSS3 functions with PIE.htc.
args.each do |arg|
@styles.push("#{arg} {behavior: url(/javascripts/ie6_fixer/PIE.htc);}\n")
end
nil
end
# USE:
# <% ie6_fixer do %>
# <%= css3_fix "#content", ".footer" %>
# <% end %>
def fixed_fix *args # => Allows you to easily use 'position:fixed'. Won't work in combination with css3_fix!
options = args.extract_options!
@script_sources.push(FIXED) unless @script_sources.include?(FIXED)
@styles.push(FIXED_BACKGROUND) unless @styles.include?(FIXED_BACKGROUND)
options.each_key do |key|
top = ""
left = ""
top = "top:expression(fixedIE('Top',#{options[key][:top]},this));" if options[key][:top].present?
top = "top:expression(fixedIE('Bottom',#{options[key][:bottom]},this));" if options[key][:bottom].present?
left = "left:expression(fixedIE('Left',#{options[key][:left]},this));" if options[key][:left].present?
left = "left:expression(fixedIE('Right',#{options[key][:right]},this));" if options[key][:right].present?
@styles.push("#{key} {position:absolute; #{top} #{left} }\n")
end
nil
end
# USE:
# <% ie6_fixer do %>
# <%= fixed_fix "#header" => { :top => 10, :left => 20 }, ".footer" => { :bottom => 100, :right => 30 } %>
# <% end %>
def fixed_fix_active # => Another 'position:fixed' fix. Loads a Javascript file that runs the whole time.
@script_sources.push(FIXED_ACTIVE) unless @script_sources.include?(FIXED_ACTIVE)
nil
end
# USE:
# <% ie6_fixer do %>
# <%= fixed_fix_active %>
# <% end %>
def max_fix *args # => Allows you to easily use max-width and max-height.
options = args.extract_options!
options.each_key do |key|
width = ""
height = ""
width = "width: expression(document.body.clientWidth > #{options[key][:width].to_i} ? '#{options[key][:width]}px' : 'auto');" if options[key][:width].present?
height = "height: expression(this.scrollHeight > #{options[key][:height].to_i} ? '#{options[key][:height]}px' : 'auto')" if options[key][:height].present?
@styles.push("#{key} {#{width}#{height}}\n")
end
nil
end
# USE:
# <% ie6_fixer do %>
# <%= max_fix "#container" => { :width => 500, :height => 300 }, "#content" => { :height => 500 } %>
# <% end %>
def min_fix *args # => Allows you to easily use min-width and min-height.
options = args.extract_options!
options.each_key do |key|
width = ""
height = ""
width = "width: auto !important; width: #{options[key][:width]}px;" if options[key][:width].present?
height = "height: auto !important; height: #{options[key][:height]}px;" if options[key][:height].present?
@styles.push("#{key} {#{width}#{height}}\n")
end
nil
end
# USE:
# <% ie6_fixer do %>
# <%= min_fix "#container" => { :width => 500, :height => 300 }, "#content" => { :height => 500 } %>
# <% end %>
def warn_ie6 *args # => A helper that shows the user a notification that he/she is using an outdated browser.
options = args.extract_options!
@script_sources.push(WARNING) unless @script_sources.include?(WARNING)
options.each_key do |key|
@scripts.push("\t#{key} = '#{options[key]}';\n")
end
@scripts.push("\twarn_ie6('/javascripts/ie6_fixer/warning/');\n")
nil
end
# USE:
# <% ie6_fixer do %>
# <%= warn_ie6, :msg1 => "You're using a shitty browser. Please upgrade." %>
# <% end %>
def kill_ie6 # => A helper that's just for fun: sets 'display: none' on the body tag. ;)
@styles.push("body{display:none !important;}\n")
nil
end
# USE:
# <% ie6_fixer do %>
# <%= kill_ie6 %>
# <% end %>
private
def set_fixes &block
@styles = []
@script_sources = []
@scripts = []
# Set the variables
capture(&block)
if @script_sources.present?
concat javascript_include_tag(@script_sources, :cache => "ie6_fixer")
concat "\n"
end
if @scripts.present?
concat "\n".html_safe
end
if @styles.present?
concat "\n".html_safe
end
yield
end
end
end