root/app/helpers/application_helper.rb @ bf5b5265
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?
|
|||
link_to(display_icon_tag('actions/document-new', :medium, :title => text) +
|
|||
400c9573 | Marc Dequènes (Duck) | " <span class=\"middle\">" + text + "</span>", new_resource_path)
|
|
b689e231 | Marc Dequènes (Duck) | end
|
|
676cb428 | Marc Dequènes (Duck) | def display_standard_form_buttons(submit_label)
|
|
"<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>" +
|
|
400c9573 | Marc Dequènes (Duck) | link_to(display_icon_tag('actions/gtk-cancel', :medium, :title => _("Cancel")), resource_index_path) +
|
|
b689e231 | Marc Dequènes (Duck) | "</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)
|
|||
2a4e5c89 | Marc Dequènes (Duck) | "<p>" + display_icon_tag('status/important', :large, :title => _("Alert!") ) +
|
|
b689e231 | Marc Dequènes (Duck) | "<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
|
|||
4ef5bacc | Marc Dequènes (Duck) | def display_thumbnail(img, size_name = nil)
|
|
b689e231 | Marc Dequènes (Duck) | return if img.nil?
|
|
37fbb0a9 | Marc Dequènes (Duck) | ||
if not img.exists?
|
|||
2a4e5c89 | Marc Dequènes (Duck) | return "<span class='error'>" + _("Missing Image!") + "</span>"
|
|
37fbb0a9 | Marc Dequènes (Duck) | end
|
|
4ef5bacc | Marc Dequènes (Duck) | thumb = img.thumbnail(size_name)
|
|
37fbb0a9 | Marc Dequènes (Duck) | if thumb.nil?
|
|
2a4e5c89 | Marc Dequènes (Duck) | "<span class='error'>" + _("Missing Thumbnail!") + "</span>"
|
|
37fbb0a9 | Marc Dequènes (Duck) | else
|
|
4ef5bacc | Marc Dequènes (Duck) | image_tag(img.public_filename(size_name))
|
|
b689e231 | Marc Dequènes (Duck) | end
|
|
end
|
|||
c7dd4b36 | Marc Dequènes (Duck) | ||
400c9573 | Marc Dequènes (Duck) | # MyCyma-specific, should be removed or adapted for Verard
|
|
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
|
|
2bb0d6d3 | Marc Dequènes (Duck) | image_padding = params.has_key?(:image_padding) ? params[:image_padding] : 5
|
|
frame_margin = params.has_key?(:frame_margin) ? params[:frame_margin] : 3
|
|||
33c93671 | Marc Dequènes (Duck) | classes = params.has_key?(:classes) ? params[:classes].to_a : []
|
|
c7dd4b36 | Marc Dequènes (Duck) | ||
37fbb0a9 | Marc Dequènes (Duck) | style = ""
|
|
5c004100 | Marc Dequènes (Duck) | thumb = image.thumbnail(size)
|
|
37fbb0a9 | Marc Dequènes (Duck) | unless thumb.nil?
|
|
frame_width = thumb.width + image_padding * 2
|
|||
style += "width: #{frame_width}px;"
|
|||
end
|
|||
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
|
|
33c93671 | Marc Dequènes (Duck) | classes << "image_frame" unless classes.member? "image_frame"
|
|
html = "<div class=\"#{classes.join(" ")}\" style=\"" + style + "\">"
|
|||
2bb0d6d3 | Marc Dequènes (Duck) | html += "<div style=\"padding: #{image_padding}px #{image_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
|
|
400c9573 | Marc Dequènes (Duck) | ||
def display_link_to_obj(obj, text_method)
|
|||
return if obj.nil?
|
|||
link_to_resource(obj, obj.send(text_method))
|
|||
end
|
|||
4ca83ea1 | Marc Dequènes (Duck) | ||
# see app/helpers/proto_menu_helper.rb for info about this method and its license
|
|||
def use_proto_menu
|
|||
@content_for_proto_menu_css = ""
|
|||
@content_for_proto_menu_js = ""
|
|||
content_for :proto_menu_css do
|
|||
stylesheet_link_tag "proto.menu.0.6"
|
|||
end
|
|||
content_for :proto_menu_js do
|
|||
javascript_include_tag "proto.menu.0.6"
|
|||
end
|
|||
end
|
|||
def gen_action_proto_menu(prefix_id, item_list)
|
|||
zone_id = prefix_id + "_zone"
|
|||
form_id = prefix_id + "_form"
|
|||
action_id = prefix_id + "_action"
|
|||
bf16d414 | Marc Dequènes (Duck) | items_id = prefix_id + "_ids[]"
|
|
empty_selection_msg = _("Empty selection")
|
|||
4ca83ea1 | Marc Dequènes (Duck) | ||
menu_items = []
|
|||
item_list.each do |item_data|
|
|||
menu_items << {
|
|||
:name => "'#{item_data[0]}'",
|
|||
:callback => "function() {
|
|||
bf16d414 | Marc Dequènes (Duck) | items = $A(document.getElementsByName('#{items_id}'));
|
|
one_checkbox_set = false;
|
|||
for (var i = 0; i < items.length; ++i)
|
|||
{
|
|||
if (items[i].checked)
|
|||
{
|
|||
one_checkbox_set = true;
|
|||
break;
|
|||
}
|
|||
}
|
|||
if (one_checkbox_set)
|
|||
{
|
|||
$('#{action_id}').value = '#{item_data[1]}';
|
|||
$('#{form_id}').submit();
|
|||
}
|
|||
else
|
|||
alert('#{empty_selection_msg}');
|
|||
4ca83ea1 | Marc Dequènes (Duck) | }
|
|
"
|
|||
}
|
|||
end
|
|||
add_proto_menu(zone_id, menu_items)
|
|||
end
|
|||
b689e231 | Marc Dequènes (Duck) | end
|