Project

General

Profile

Download (9.95 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?
link_to(display_icon_tag('actions/document-new', :medium, :title => text) +
" <span class=\"middle\">" + text + "</span>", new_resource_path)
end

def display_standard_form_buttons(submit_label)
"<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")), resource_index_path) +
"</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?

if not img.exists?
return "<span class='error'>" + _("Missing Image !") + "</span>"
end

thumb = img.thumbnail(thumb_name)
if thumb.nil?
"<span class='error'>" + _("Missing Thumbnail !") + "</span>"
else
image_tag(img.public_filename(thumb_name))
end
end

# MyCyma-specific, should be removed or adapted for Verard
def display_image_frame(image, 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
with_link = params.has_key?(:with_link) ? params[:with_link] : true
image_padding = params.has_key?(:image_padding) ? params[:image_padding] : 5
frame_margin = params.has_key?(:frame_margin) ? params[:frame_margin] : 3
classes = params.has_key?(:classes) ? params[:classes].to_a : []

style = ""

thumb = image.thumbnail(size)
unless thumb.nil?
frame_width = thumb.width + image_padding * 2
style += "width: #{frame_width}px;"
end

case frame_type
when :classic
style += " margin: #{frame_margin}px;"
when :cell
style += " padding-left: 3px; padding-right: 3px; margin-top: #{frame_margin}px; margin-bottom: #{frame_margin}px; margin-left: auto; margin-right: auto;"
when :float
style += " margin: #{frame_margin}px; float: left;"
when :inline
style += " margin: #{frame_margin}px; display: inline-block;"
end

classes << "image_frame" unless classes.member? "image_frame"

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

with_link ? link_to(html, :controller => '/view', :action => 'artwork', :id => image.artwork.id) : html
end

def display_link_to_obj(obj, text_method)
return if obj.nil?
link_to_resource(obj, obj.send(text_method))
end
end
(1-1/2)