|
| 1 | +require 'action_view/helpers/tag_helper' |
| 2 | +require 'action_view/helpers/url_helper' |
| 3 | + |
| 4 | +module Refinery |
| 5 | + module Admin |
| 6 | + class ImagePresenter < Refinery::BasePresenter |
| 7 | + include ActionView::Helpers::TagHelper |
| 8 | + include ActionView::Helpers::UrlHelper |
| 9 | + include Refinery::ImageHelper |
| 10 | + include Refinery::TagHelper |
| 11 | + include Refinery::TranslationHelper |
| 12 | + |
| 13 | + attr_accessor :image, :created_at, :context, :urls |
| 14 | + delegate :refinery, :params, :output_buffer, :output_buffer=, :t, to: :context |
| 15 | + delegate_missing_to :image |
| 16 | + |
| 17 | + def initialize(image, context) |
| 18 | + @context = context |
| 19 | + @created_at = image.created_at |
| 20 | + @image = image |
| 21 | + @urls = { |
| 22 | + edit: refinery.edit_admin_image_path(image), |
| 23 | + delete: refinery.admin_image_path(image), |
| 24 | + preview: image.url |
| 25 | + } |
| 26 | + end |
| 27 | + |
| 28 | + class << self |
| 29 | + attr_accessor :context, :images, :view, :pagination_class |
| 30 | + delegate :tag, :group_by_date, to: :context |
| 31 | + |
| 32 | + def collection(images, view_format, context) |
| 33 | + @view = "#{view_format}_view" |
| 34 | + @context = context |
| 35 | + @pagination_class = context.pagination_css_class |
| 36 | + @collection_id = "image_#{view_format}" |
| 37 | + @images = images.map { |image| self.new(image, context) } |
| 38 | + self |
| 39 | + end |
| 40 | + |
| 41 | + def to_html |
| 42 | + tag.ul id: @collection_id, class: ['clearfix', 'pagination_frame', pagination_class] do |
| 43 | + self.send(@view) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + def grid_view |
| 48 | + images.each.reduce(ActiveSupport::SafeBuffer.new) do |buffer, image| |
| 49 | + buffer << image.to_html { image.grid_entry } |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + def list_view |
| 54 | + group_by_date(images) |
| 55 | + .each |
| 56 | + .reduce(ActiveSupport::SafeBuffer.new) do |groups_buffer, (_container, image_group)| |
| 57 | + date_time = image_group.first.created_at |
| 58 | + date_header = tag.h3 context.l(Date.parse(date_time.to_s), format: :long) |
| 59 | + # darn zebra striping |
| 60 | + images = image_group.each_with_index.reduce(ActiveSupport::SafeBuffer.new) do |items_buffer, (image, index)| |
| 61 | + items_buffer << image.to_html(index.odd?) { image.list_entry } |
| 62 | + end |
| 63 | + groups_buffer << [date_header, images].join(' ').html_safe |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def view_classes |
| 68 | + ['clearfix', 'pagination_frame', 'images_list', pagination_class] |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + def to_html(stripe = true) |
| 73 | + stripe_class = stripe ? 'on-hover' : 'on' |
| 74 | + tag.li id: "image_#{image.id}", class: stripe_class do |
| 75 | + entry = tag.span class: :item do |
| 76 | + [yield, *translations, image_settings].join.html_safe |
| 77 | + end |
| 78 | + entry << actions |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + def list_entry |
| 83 | + edit_link { image.title } |
| 84 | + end |
| 85 | + |
| 86 | + def grid_entry |
| 87 | + edit_link { context.image_fu image, '149x149#c', title: image_title, alt: image.alt } |
| 88 | + end |
| 89 | + |
| 90 | + private |
| 91 | + |
| 92 | + def edit_link |
| 93 | + link_to urls[:edit], class: [:edit, :title], |
| 94 | + tooltip: t('edit', scope: 'refinery.admin.images') do |
| 95 | + yield |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + def image_settings |
| 100 | + tag.span class: [:preview] do |
| 101 | + image_information |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + def actions |
| 106 | + tag.span class: :actions do |
| 107 | + [edit_icon, info_icon, delete_icon, preview_icon].join(' ').html_safe |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + # Actions |
| 112 | + def preview_icon |
| 113 | + action_icon :preview, urls[:preview], t('view_live_html', scope: 'refinery.admin.images') |
| 114 | + end |
| 115 | + |
| 116 | + def edit_icon |
| 117 | + action_icon :edit, urls[:edit], t('edit', scope: 'refinery.admin.images', title: image_title) |
| 118 | + end |
| 119 | + |
| 120 | + def delete_icon |
| 121 | + delete_options = { |
| 122 | + class: %w[cancel confirm-delete], |
| 123 | + data: { confirm: ::I18n.t('message', scope: 'refinery.admin.delete', title: image_title) } |
| 124 | + } |
| 125 | + action_icon :delete, urls[:delete], t('delete', scope: 'refinery.admin.images'), delete_options |
| 126 | + end |
| 127 | + |
| 128 | + def info_icon |
| 129 | + action_icon :info, '', image_information |
| 130 | + end |
| 131 | + |
| 132 | + def image_information |
| 133 | + # give as much info as we have, but without duplication |
| 134 | + # title, alt and filename may all be different, or any two may be the same. Only show what is different |
| 135 | + # Title is already shown in the edit_link |
| 136 | + # |
| 137 | + filename = image.image_name.split('.').first |
| 138 | + |
| 139 | + settings = ["Title: #{title}"] |
| 140 | + settings << "File: #{filename}" unless filename == title |
| 141 | + settings << "Alt: #{image_alt}" unless image_alt.nil? || image_alt == filename || image_alt == title |
| 142 | + settings << "Type: #{image.image.mime_type}" |
| 143 | + settings << "Size: #{context.number_to_human_size(image.size)}" |
| 144 | + settings.join(', ').html_safe |
| 145 | + end |
| 146 | + |
| 147 | + def translations |
| 148 | + if Refinery::I18n.frontend_locales.many? |
| 149 | + tag.span class: :locales do |
| 150 | + switch_locale(image, urls[:edit], :image_title).html_safe |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + end |
| 156 | + end |
| 157 | +end |
| 158 | + |
| 159 | + |
0 commit comments