root/app/helpers/application_helper.rb @ 8ff59b90
b689e231 | Marc Dequènes (Duck) | # 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
|
|||
fcaa7349 | Marc Dequènes (Duck) | def display_standard_item_actions(obj = nil, action_list = nil)
|
|
b689e231 | Marc Dequènes (Duck) | obj = resource_object if obj.nil?
|
|
res = resource_family(obj)
|
|||
disp = ""
|
|||
fcaa7349 | Marc Dequènes (Duck) | 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
|
|||
b689e231 | Marc Dequènes (Duck) | 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
|
|||
676cb428 | Marc Dequènes (Duck) | def display_standard_form_buttons(submit_label)
|
|
b689e231 | Marc Dequènes (Duck) | res = resource_family(resource_model.new)
|
|
676cb428 | Marc Dequènes (Duck) | "<div class=\"actions\">" + display_image_submit_tag('actions/gtk-ok', :medium, :title => submit_label) +
|
|
b689e231 | Marc Dequènes (Duck) | "<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
|
|||
c7dd4b36 | Marc Dequènes (Duck) | ||
e7326ceb | Marc Dequènes (Duck) | def display_image_frame(image, params = {})
|
|
c7dd4b36 | Marc Dequènes (Duck) | 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
|
|||
e7326ceb | Marc Dequènes (Duck) | with_link = params.has_key?(:with_link) ? params[:with_link] : true
|
|
c7dd4b36 | Marc Dequènes (Duck) | ||
e7326ceb | Marc Dequènes (Duck) | thumb = image.artwork_step_thumbnails.find_by_thumbnail(size.to_s)
|
|
14a5963c | Marc Dequènes (Duck) | img_padding = 5
|
|
frame_margin = 3
|
|||
frame_width = thumb.width + img_padding * 2
|
|||
style = "text-align: center; padding: 0px; width: #{frame_width}px;"
|
|||
#style += " border: 1px solid red;"
|
|||
c7dd4b36 | Marc Dequènes (Duck) | case frame_type
|
|
when :classic
|
|||
14a5963c | Marc Dequènes (Duck) | style += " margin: #{frame_margin}px;"
|
|
c7dd4b36 | Marc Dequènes (Duck) | when :cell
|
|
14a5963c | Marc Dequènes (Duck) | style += " padding-left: 3px; padding-right: 3px; margin-top: #{frame_margin}px; margin-bottom: #{frame_margin}px; margin-left: auto; margin-right: auto;"
|
|
c7dd4b36 | Marc Dequènes (Duck) | when :float
|
|
14a5963c | Marc Dequènes (Duck) | style += " margin: #{frame_margin}px; float: left;"
|
|
e7326ceb | Marc Dequènes (Duck) | when :inline
|
|
style += " margin: #{frame_margin}px; display: inline-block;"
|
|||
c7dd4b36 | Marc Dequènes (Duck) | end
|
|
14a5963c | Marc Dequènes (Duck) | html = "<div class=\"artwork_frame\" style=\"" + style + "\">"
|
|
e7326ceb | Marc Dequènes (Duck) | html += "<div style=\"padding: #{img_padding}px #{img_padding}px 0; margin: 0px;\">" + display_thumbnail(image, size) + "</div>"
|
|
14a5963c | Marc Dequènes (Duck) | html += "<div>"
|
|
c7dd4b36 | Marc Dequènes (Duck) | infos = []
|
|
if display_title
|
|||
e7326ceb | Marc Dequènes (Duck) | title = params[:force_title] ? params[:force_title] : image.artwork.title
|
|
c7dd4b36 | Marc Dequènes (Duck) | infos << title
|
|
end
|
|||
if display_size
|
|||
e7326ceb | Marc Dequènes (Duck) | infos << "<span style=\"padding: 0; font-size: smaller;\">(" + image.artwork.artwork_size.human_size + ")</span>"
|
|
c7dd4b36 | Marc Dequènes (Duck) | end
|
|
html += infos.join("<br />")
|
|||
html += "</div>"
|
|||
14a5963c | Marc Dequènes (Duck) | html += "</div>"
|
|
e7326ceb | Marc Dequènes (Duck) | ||
with_link ? link_to(html, :controller => '/view', :action => 'artwork', :id => image.artwork.id) : html
|
|||
c7dd4b36 | Marc Dequènes (Duck) | end
|
|
b689e231 | Marc Dequènes (Duck) | end
|