Project

General

Profile

Download (8.92 KB) Statistics
| Branch: | Tag: | Revision:
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper

ICONS_FORMAT = :png
ICONS_SIZES = {
:small => "22x22",
:medium => "24x24",
:large => "48x48"
}
ICONS_SUBPATH = "icon-set"

def self.included(base)
Object.const_set(:SORTABLE_COLUMN_ASC, icons_subpath("actions/down", :small)) unless Object.const_defined? :SORTABLE_COLUMN_ASC
Object.const_set(:SORTABLE_COLUMN_DESC, icons_subpath("actions/up", :small)) unless Object.const_defined? :SORTABLE_COLUMN_DESC
end

def observe_field_with_reload(field_id, options = {})
observe_field(field_id, options) + "\n" +
update_page_tag do |page|
page << "Event.observe(window, 'load', function() {"
page << "element = document.getElementById('#{field_id}');"
page << "value = element.value;"
page << remote_function(options)
page << "});"
end
end

def observe_field_trigger(field_id, with_reload = true)
update_page_tag do |page|
page << "new Form.Element.EventObserver('#{field_id}', function() {"
page << "element = document.getElementById('#{field_id}');"
page << "value = element.value;"
yield(page)
page << "});"

if with_reload
page << "Event.observe(window, 'load', function() {"
page << "element = document.getElementById('#{field_id}');"
page << "value = element.value;"
yield(page)
page << "});"
end
end
end

def render_flash_messages
html = ""
["error", "warning", "notice"].each do |type|
ve = visual_effect(:highlight, type, :duration => 2.0) + "\\n"
unless flash[type.intern].nil?
html << content_tag("div", flash[type.intern].to_s,
:id => type,
:class => "flash")
end
end
content_tag("div", html, :id => "flash")
end

def icons_subpath(image_name, size_name)
image_file = image_name + "." + ICONS_FORMAT.to_s
if ICONS_FORMAT == :svg
File.join(ICONS_SUBPATH, "scalable", image_file)
else
File.join(ICONS_SUBPATH, ICONS_SIZES[size_name], image_file)
end
end
module_function :icons_subpath

def display_icon_tag(image_name, size_name, html_options = {})
if ICONS_FORMAT == :svg
width, height = ICONS_SIZES[size_name].split("x")
"<object type=\"image/svg+xml\" data=\"#{path_to_image(icons_subpath(image_name, size_name))}\" width=\"#{width}\" height=\"#{height}\"/>"
else
image_tag icons_subpath(image_name, size_name), html_options
end
end

def display_image_submit_tag(image_name, size_name, html_options = {})
image_submit_tag icons_subpath(image_name, size_name), html_options
end

def display_standard_item_actions(obj = nil, action_list = nil)
obj = resource_object if obj.nil?
res = resource_family(obj)
disp = ""
if action_list.nil? or action_list.include? :modify
disp += link_to display_icon_tag('actions/document-properties', :medium, :title => _("Modify")), edit_polymorphic_path(res.dup)
else
disp += display_action_placeholder
end
if action_list.nil? or action_list.include? :delete
disp += link_to display_icon_tag('actions/edit-delete', :medium, :title => _("Delete")), polymorphic_path(res.dup), :method => :delete, :confirm => _("Are you sure?")
else
disp += display_action_placeholder
end
return disp
end

def display_action_placeholder
"<div class='action-placeholder'> </div>"
end

# use with acts_as_lists
def display_list_item_actions(obj)
obj = resouce_object if obj.nil?
res = resource_family(obj)
path = polymorphic_url(res.dup, :action => "move", :routing_type => :path)

disp = ""
if obj.first?
2.times { disp += display_action_placeholder }
else
disp += link_to display_icon_tag('actions/go-top', :medium, :title => _("Move to top")), path + "?type=move_to_top", :method => :put
disp += link_to display_icon_tag('actions/go-up', :medium, :title => _("Move up")), path + "?type=move_higher", :method => :put
end
if obj.last?
2.times { disp += display_action_placeholder }
else
disp += link_to display_icon_tag('actions/go-down', :medium, :title => _("Move down")), path + "?type=move_lower", :method => :put
disp += link_to display_icon_tag('actions/go-bottom', :medium, :title => _("Move to bottom")), path + "?type=move_to_bottom", :method => :put
end
return disp
end

def display_new_action(text = nil)
text = sprintf(_("New %s"), _(resource_model.human_name)) if text.nil?
res = resource_family(resource_model.new)
link_to(display_icon_tag('actions/document-new', :medium, :title => text) +
" <span class=\"middle\">" + text + "</span>", new_polymorphic_path(res.dup))
end

def display_standard_form_buttons(submit_label)
res = resource_family(resource_model.new)
"<div class=\"actions\">" + display_image_submit_tag('actions/gtk-ok', :medium, :title => submit_label) +
"<div class=\"big-action-placeholder\"></div>" +
link_to(display_icon_tag('actions/gtk-cancel', :medium, :title => _("Cancel")), polymorphic_path(res.dup)) +
"</div>"
end

def link_to_resource(obj, text)
link_to text, resource_path(obj)
end

def link_to_resource_index(text)
link_to(display_icon_tag('actions/document-open', :medium, :title => text) +
" <span class=\"middle\">" + text + "</span>", resource_index_path)
end

def display_alert(text)
"<p>" + display_icon_tag('status/important', :large, :title => _("Alert !") ) +
"<span class=\"alert\">" + text + "</span></p>"
end

def link_to_parent_resource_index(text)
link_to(display_icon_tag('actions/back', :medium, :title => text) +
" <span class=\"middle\">" + text + "</span>", parent_resource_index_path)
end

def blank_option
"<option value=\"\"></option>"
end

def title_option(text)
"<option value=\"\" disabled=\"disabled\">#{text}</option>"
end

def conditional_hidden_style(obj, field)
" style=\"display: none\";" unless obj and obj.send(field)
end

def link_to_child(obj, child, text)
link_to text, child_polymorphic_path(obj, child)
end

def link_to_children_index(obj, children_model, title, with_count = true, text = nil)
method = discover_associated_model_method(obj.class, children_model)
in_link = display_icon_tag('mimetypes/package', :medium, :title => title)
in_link += " <span class=\"middle\">#{text}</span>" if text
html = link_to(in_link, child_index_polymorphic_path(obj, children_model))
html += " (" + obj.send(method).size.to_s + ")" if with_count
return html
end

def display_boolean(bool, colors = true)
text = bool ? _("Yes") : _("No")
if colors
css_class = bool ? "boolean-true" : "boolean-false"
"<span class=\"#{css_class}\">#{text}</span>"
else
text
end
end

def display_timestamp(timestamp)
timestamp.strftime("%Y-%m-%d %H:%M") if timestamp
end

def display_list(list, ordered = false)
tag = (ordered) ? "ol" : "ul"
html = "<#{tag}>"
list.each {|el| html += "<li>#{el}</li>" }
html += "</#{tag}>"
end

def form_comment(cat)
cat.field :comment, _("Comment:") + sprintf(_("<br/>(with %s<br/>formating)"), link_to("Textile", "http://hobix.com/textile/", :popup => true)), :text_area, :cols => 60, :rows => 10
end

def table_comment(table, comment)
table.content [_("Comment:"), textilize(comment)]
end

def table_timestamp_info(table, obj)
table.content [_("Created at:"), obj.created_at]
table.content [_("Updated at:"), obj.updated_at]
end

def display_thumbnail(img, thumb_name = nil)
return if img.nil?
unless File.exists? img.full_filename(thumb_name)
img.saved_attachment = true
img.after_process_attachment
end
image_tag(img.public_filename(thumb_name))
end

def display_artwork_frame(artwork, params = {})
frame_type = params.has_key?(:frame_type) ? params[:frame_type] : :classic
size = params[:size] || :thumb_big
display_title = params.has_key?(:display_title) ? params[:display_title] : true
display_size = params.has_key?(:display_size) ? params[:display_size] : true

style = "text-align: center; padding: 0px;"
case frame_type
when :classic
style += " margin: 7px;"
when :cell
style += " padding-left: 3px; padding-right: 3px; margin-top: 7px; margin-bottom: 7px; margin-left: auto; margin-right: auto;"
when :float
style += " margin: 7px; float: left;"
end

html = "<div style=\"" + style + "\">"
html += "<div style=\padding: 0px; margin: 0px;\">" + display_thumbnail(artwork.image, size) + "</div>"
infos = []
if display_title
title = params[:force_title] ? params[:force_title] : artwork.title
infos << title
end
if display_size
infos << "<span style=\"font-size: smaller;\">(" + artwork.artwork_size.human_size + ")</span>"
end
html += infos.join("<br />")
html += "</div>"
end
end
(1-1/2)