Module: IconLinks::ViewHelpers

Included in:
MethodMissing
Defined in:
lib/icon_links/view_helpers.rb

Constant Summary collapse

@@icon_url_cache =
{}

Instance Method Summary collapse

Instance Method Details

#collapsible_tag(type, options) ⇒ Object

Creates a collapsible div



126
127
128
# File 'lib/icon_links/view_helpers.rb', line 126

def collapsible_tag(type, options)
  toggle_tag(type, options, :collapse, :expand)
end

#desc_of(class_symbol, field_val) ⇒ Object

This is similar to help_for(), but it instead looks up the description for a field on a per-table basis. For example, desc_of :life_cycle, ‘QA’ would look up life_cycles where life_cycle = ‘QA’ and return life_cycle.description



92
93
94
95
96
97
98
# File 'lib/icon_links/view_helpers.rb', line 92

def desc_of(class_symbol, field_val)
  class_name = class_symbol.to_s.camelize
  row = nil
  eval "row = #{class_name}.find(:first, :conditions => ['name = ?', field_val])"
  return '' unless row
  help_icon row.description
end

#expandable_tag(type, options) ⇒ Object

Creates an expandable toggleable div



121
122
123
# File 'lib/icon_links/view_helpers.rb', line 121

def expandable_tag(type, options)
  toggle_tag(type, options, :expand, :collapse)
end

#function_icon(text, function) ⇒ Object

This is similar to icon_to, only it is designed to call a JS function



25
26
27
# File 'lib/icon_links/view_helpers.rb', line 25

def function_icon(text, function)
  link_to(icon_for(text), '#', :onclick => function)
end

#help_for(help_topic, icon = :help) ⇒ Object

This returns a help link similar to icon_to, only using the help from the system_help table. If no help is found, then an empty string is returned (allowing you to randomly call this on any object, without having to check if help exists first).



68
69
70
71
# File 'lib/icon_links/view_helpers.rb', line 68

def help_for(help_topic, icon=:help)
  help = help_text(help_topic)
  help == '' ? '' : help_icon(help, icon)
end

#help_icon(help_text, icon = :help) ⇒ Object

Creates the icon to pop up the help bubble



84
85
86
87
# File 'lib/icon_links/view_helpers.rb', line 84

def help_icon(help_text, icon=:help)
  return '' unless help_text
  return %Q(<a href="#" title="#{help_text}" onclick="return false">#{icon_tag(icon, :class => 'help')}</a>)
end

#help_text(help_topic) ⇒ Object

Return help text, suitable for use as a help tooltip box thingy Note: Comma escaping is a workaround for a workaround, in the hacks to dhtmlXGrid to support “img:[/path Plain Text],b” <– comma



76
77
78
79
80
81
# File 'lib/icon_links/view_helpers.rb', line 76

def help_text(help_topic)
  help = SystemHelp.topic(help_topic)
  help ? help.help_text.to_s.gsub('&','&amp;').
          gsub('"','&quot;').gsub('<','&lt;').
          gsub('>','&gt;').gsub(',','&#044;') : ''
end

#icon_for(text, excess = nil) ⇒ Object

This expands the text and returns the icon path. It is used internally by icon_to and icon_to_remote to render the link text.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/icon_links/view_helpers.rb', line 31

def icon_for(text, excess=nil)
  link = nil
  if text.is_a? Array
    # array
    icon = icon_tag(text.first)
    link = cat(icon, text.last)
  else
    link = icon_tag(text)
  end
  link += excess if excess
  link
end

#icon_tag(type, options = {}) ⇒ Object

This retrieves the assigned icon from the System object, then returns suitably-formatted HTML. It does some simple caching for each icon URL as well.



56
57
58
59
60
61
62
# File 'lib/icon_links/view_helpers.rb', line 56

def icon_tag(type, options={})
  name = type.to_s
  png_image_tag icon_url(type), {
    :class => 'icon', :border => 0, :alt => name.humanize,
    :width => 16, :height => 16
  }.update(options)
end

#icon_to(text, *args) ⇒ Object

This creates a link using the specified type of icon. The args accepted are the exact same as the args for link_to.



8
9
10
11
12
13
# File 'lib/icon_links/view_helpers.rb', line 8

def icon_to(text, *args)
  if args.first.is_a?(Hash)
    return icon_tag(:clear) unless args[0].delete(:if)
  end
  link_to(icon_for(text), *args)
end

#icon_to_remote(text, *args) ⇒ Object

This creates a link using the specified type of icon. The args accepted are the exact same as the args for link_to_remote.



17
18
19
20
21
22
# File 'lib/icon_links/view_helpers.rb', line 17

def icon_to_remote(text, *args)
  if args.first.is_a?(Hash)
    return icon_tag(:clear) unless args[0].delete(:if)
  end
  link_to_remote(icon_for(text), *args)
end

#icon_url(type) ⇒ Object

Can’t normalize this with above because calls to this want to exclude help



45
46
47
48
49
50
51
# File 'lib/icon_links/view_helpers.rb', line 45

def icon_url(type)
  name = type.to_s
  return @@icon_url_cache[name] if @@icon_url_cache[name]
  # Can't normalize this with above because calls to this want to exclude help
  @@icon_url_cache[name] ||= IconLinks.custom_icon_images[name] ||
           File.join(IconLinks.icon_image_url, name + IconLinks.icon_image_suffix)
end

#png_image_tag(image, options = {}) ⇒ Object

This is a hack needed for IE, adapted from koivi.com/ie-png-transparency/



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/icon_links/view_helpers.rb', line 101

def png_image_tag(image, options={})
  image = icon_url image if image.is_a?(Symbol)
  begin
    # When called from table_form_builder, request does not exist and raises exception
    if image =~ /\.png$/i && request.user_agent.downcase =~ /msie\s+(5\.[5-9]|[6]\.[0-9]*).*(win)/
      options[:style] ||= ''
      options[:style] += "filter: progid:DXImageTransform.Microsoft.AlphaImageLoader" +
                         "(src=&quot;#{image}&quot;,sizingMethod=&quot;scale&quot;);"
      image = System.clear_png_url    # reset image to clear png
    end
  rescue
    # In FormBuilder, so write it to the builder
    @template.image_tag image, options
  else
    # Return a string
    image_tag image, options
  end
end

#toggle_tag(type, options, first_icon, alt_icon) ⇒ Object

“Raw” function that can be used to create an expand/collapse tag



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/icon_links/view_helpers.rb', line 131

def toggle_tag(type, options, first_icon, alt_icon)

  target = options[:id]
  if !target
    if options.has_key?(:show)
      show = options[:show]
      target = show.is_a?(ActiveRecord::Base) ? "show_#{show.class.name.to_s.underscore}_#{show.to_param}" \
                                              : "show_#{show.to_param}"
    else
      target = 'toggle'
    end
  end

  options[:style] = 'display:none;' + (options[:style]||'')
  var = "toggle_#{target}"
  img = "icon_for_#{var}"
  %(<script type="text/javascript">#{var} = true;</script>) +
  %(<a href="#" onclick="#{var} = !#{var}; $('#{target}').toggle();) +
  %( if (#{var}) { $('#{img}').src = '#{icon_url(first_icon)}'; })+
  %( else { $('#{img}').src = '#{icon_url(alt_icon)}'; }) +
  %(">#{icon_tag(first_icon, :id => img)}#{options[:label]||''}</a>)
end