|
| 1 | +class CroppInput < SimpleForm::Inputs::Base |
| 2 | + def input(wrapper_options = nil) |
| 3 | + @version = input_html_options.delete(:version) || :default |
| 4 | + @coord_attribute = input_html_options.delete(:coord_attribute) || object.send("#{@version}_#{attribute_name}_attr_coord") |
| 5 | + |
| 6 | + out = [] |
| 7 | + out << %{<div class="f-file">} |
| 8 | + out << %{ <label class="f-file__selection js-file">} |
| 9 | + out << %{ <span class="f-file__button">#{I18n.t('labels.admin.choose')}</span>} |
| 10 | + out << @builder.file_field(attribute_name, input_html_options) |
| 11 | + out << @builder.hidden_field(@coord_attribute) |
| 12 | + out << %{ <span class="f-file__selected"></span>} |
| 13 | + out << %{ </label>} |
| 14 | + out << %{</div>} |
| 15 | + out << preview |
| 16 | + |
| 17 | + out.join.html_safe |
| 18 | + end |
| 19 | + |
| 20 | + def preview |
| 21 | + return unless object.send(attribute_name).attached? |
| 22 | + out = [] |
| 23 | + |
| 24 | + aspect_ratio = input_html_options.delete(:aspect_ratio) || '16/9' |
| 25 | + aspect_ratio = aspect_ratio.split('/').map(&:to_f) |
| 26 | + aspect_ratio = aspect_ratio[0] / aspect_ratio[1] |
| 27 | + |
| 28 | + out << %{<div class="row"><div class="col-md-8"><div class="img-container">} |
| 29 | + |
| 30 | + out << template.image_tag(object.send(attribute_name), data: { aspect_ratio: aspect_ratio, preview: ".#{@version}_#{attribute_name}_preview", toggle: 'cropp', coord: @coord_attribute }) |
| 31 | + |
| 32 | + out << %{</div></div><div class="col-md-4">} |
| 33 | + out << %{<div class="cropper-dimensions">} |
| 34 | + out << %{<div class="field">} |
| 35 | + out << template.label_tag(:width, I18n.t('labels.admin.image.width'), class: 'control-label') |
| 36 | + out << template.number_field_tag(:width, nil, class: 'form-control') |
| 37 | + out << %{</div>} |
| 38 | + out << %{<div class="field">} |
| 39 | + |
| 40 | + out << template.label_tag(:height, I18n.t('labels.admin.image.height'), class: 'control-label') |
| 41 | + out << template.number_field_tag(:height, nil, class: 'form-control') |
| 42 | + out << %{</div>} |
| 43 | + |
| 44 | + out << %{</div>} |
| 45 | + out << %{<div class="docs-preview clearfix">} |
| 46 | + out << %{<p class="figure-caption">#{I18n.t('labels.admin.image.cropped_current')}</p>} |
| 47 | + out << %{<div class="img-preview preview-lg #{@version}_#{attribute_name}_preview"></div>} |
| 48 | + out << cropped |
| 49 | + |
| 50 | + out << %{</div></div></div>} |
| 51 | + out.join |
| 52 | + end |
| 53 | + |
| 54 | + def cropped |
| 55 | + return if @coord_attribute.blank? |
| 56 | + |
| 57 | + out = [] |
| 58 | + out << %{<div class="preview-cropped"><figure class="figure">} |
| 59 | + |
| 60 | + out << %{<figcaption class="figure-caption">#{I18n.t('labels.admin.image.cropped_saved')}</figcaption>} |
| 61 | + |
| 62 | + out << template.image_tag(object.send("#{@version}_#{attribute_name}_cropped")) |
| 63 | + |
| 64 | + out << %{</figure></div>} |
| 65 | + out.join |
| 66 | + end |
| 67 | +end |
0 commit comments