Skip to content

Commit

Permalink
Merge 6aa1659 into 7f35539
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Jan 31, 2017
2 parents 7f35539 + 6aa1659 commit 5afa57f
Show file tree
Hide file tree
Showing 19 changed files with 81 additions and 182 deletions.
12 changes: 7 additions & 5 deletions app/assets/javascripts/spotlight/crop.es6
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ export default class Crop {
constructor(cropArea) {
this.cropArea = cropArea;
this.cropArea.data('iiifCropper', this);
this.cropSelector = '[data-cropper="' + cropArea.data('cropper') + '"]';
this.iiifUrlField = $('#' + cropArea.data('iiif-url-field'));
this.iiifRegionField = $('#' + cropArea.data('iiif-region-field'))
this.cropSelector = '[data-cropper="' + cropArea.data('cropperKey') + '"]';
this.cropTool = $(this.cropSelector);
this.formPrefix = this.cropTool.data('form-prefix');
this.iiifUrlField = $('#' + this.formPrefix + '_iiif_tilesource');
this.iiifRegionField = $('#' + this.formPrefix + '_iiif_region');
this.form = cropArea.closest('form');
this.initialCropRegion = [0, 0, cropArea.data('crop-width'), cropArea.data('crop-height')];
this.tileSource = null;
Expand All @@ -26,12 +28,12 @@ export default class Crop {

// Setup autocomplete inputs to have the iiif_cropper context
setupAutoCompletes() {
var input = $('[data-behavior="autocomplete"]' + this.cropSelector, this.form);
var input = $('[data-behavior="autocomplete"]', this.cropTool);
input.data('iiifCropper', this);
}

setupAjaxFileUpload() {
this.fileInput = $('input[type="file"]' + this.cropSelector, this.form);
this.fileInput = $('input[type="file"]', this.cropTool);
this.fileInput.change(() => this.uploadFile());
}

Expand Down
6 changes: 1 addition & 5 deletions app/controllers/spotlight/featured_images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ def tilesource
# The create action can be called from a number of different forms, so
# we normalize all the parameters.
def create_params
parent_param = params.fetch(:featured_image)
image_params = parent_param[:thumbnail] ||
parent_param[:masthead] ||
parent_param[:avatar]
{ image: image_params || {} }
params.require(:featured_image).permit(:image)
end
end
end
33 changes: 28 additions & 5 deletions app/helpers/spotlight/crop_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@ module Spotlight
##
# iiif-crop options helpers
module CropHelper
def iiif_cropper(form, name, width, height)
IIIFCropper.new(form, name, width, height)
def iiif_cropper_tags(f, initial_crop_selection:)
content_tag(:div) do
concat f.hidden_field(:iiif_manifest_url)
concat f.hidden_field(:iiif_canvas_id)
concat f.hidden_field(:iiif_image_id)
concat f.hidden_field(:iiif_region)
concat f.hidden_field(:iiif_tilesource)
concat iiif_cropper_tag(f, initial_crop_selection: initial_crop_selection)
end
end

def contact_crop(form, name)
w, h = Spotlight::Engine.config.contact_square_size
iiif_cropper(form, name, w, h).draw
def iiif_cropper_tag(f, initial_crop_selection:)
content_tag :div, '', id: "#{form_prefix(f)}_iiif_cropper", data: {
behavior: 'iiif-cropper',
cropper_key: f.object.model_name.singular_route_key,
'crop-width': initial_crop_selection.first,
'crop-height': initial_crop_selection.last
}
end

def iiif_upload_tag(f)
f.file_field_without_bootstrap :file, name: 'featured_image[image]', data: { endpoint: polymorphic_path(f.object.model_name.route_key) }
end

def form_prefix(f)
if Rails::VERSION::MAJOR >= 5
f.object_name.parameterize(separator: '_')
else
f.object_name.parameterize('_')
end
end
end
end
105 changes: 0 additions & 105 deletions app/inputs/spotlight/iiif_cropper.rb

This file was deleted.

6 changes: 2 additions & 4 deletions app/views/spotlight/appearances/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="site-masthead">
<p class="instructions"><%= t(:'.site_masthead.help') %></p>
<% image_form = iiif_cropper(f, :masthead, *Spotlight::Engine.config.masthead_initial_crop_selection) %>
<%= f.fields_for(:masthead, current_exhibit.masthead || current_exhibit.build_masthead) do |m| %>
<%= render '/spotlight/featured_images/form', f: m, image_form: image_form %>
<%= render '/spotlight/featured_images/form', f: m, initial_crop_selection: Spotlight::Engine.config.masthead_initial_crop_selection %>
<% end %>
</div>

<div role="tabpanel" class="tab-pane" id="site-thumbnail">
<p class="instructions"><%= t(:'.site_thumbnail.help') %></p>
<% image_form = iiif_cropper(f, :thumbnail, *Spotlight::Engine.config.thumbnail_initial_crop_selection) %>
<%= f.fields_for(:thumbnail, current_exhibit.thumbnail || current_exhibit.build_thumbnail) do |m| %>
<%= render '/spotlight/featured_images/form', f: m, image_form: image_form %>
<%= render '/spotlight/featured_images/form', f: m, initial_crop_selection: Spotlight::Engine.config.thumbnail_initial_crop_selection %>
<% end %>
</div>

Expand Down
16 changes: 13 additions & 3 deletions app/views/spotlight/contacts/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@
<% end %>
<% end %>
<%= field_set_tag(t(:'.avatar.header')) do %>
<p class="instructions"><%= t(:'.avatar.help') %></p>
<%= contact_crop(f, :avatar) %>
<%= f.fields_for :avatar, (@contact.avatar || @contact.build_avatar) do |af| %>
<div data-cropper="<%= af.object.model_name.singular_route_key %>" data-form-prefix="<%= form_prefix(af) %>">
<%= field_set_tag(t(:'.avatar.header')) do %>
<p class="instructions"><%= t(:'.avatar.help') %></p>

<div>
<%= af.radio_button(:source, :remote, label: t(:'.source.remote.label')) %>
<%= iiif_upload_tag(af) %>
</div>

<%= iiif_cropper_tags af, initial_crop_selection: Spotlight::Engine.config.contact_square_size %>
<% end %>
</div>
<% end %>

<div class="form-group primary-actions">
Expand Down
16 changes: 6 additions & 10 deletions app/views/spotlight/featured_images/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
<% if Rails::VERSION::MAJOR >= 5 %>
<% form_prefix = f.object_name.parameterize(separator: '_') %>
<% else %>
<% form_prefix = f.object_name.parameterize('_') %>
<% end %>
<div data-cropper="<%= f.object.model_name.singular_route_key %>" data-form-prefix="<%= form_prefix(f) %>">
<%= field_set_tag do %>
<%= f.check_box(:display) %>
<% end if f.object.is_a? Spotlight::Masthead %>
Expand All @@ -12,8 +8,8 @@
<div>
<%= f.hidden_field(:document_global_id) %>
<%= f.hidden_field(:remote_image_url) %>
<%= text_field_tag(:document_title, (presenter(f.object.document).document_show_html_title if f.object.document), class: "form-control", data: { input_select_target: "##{form_prefix}_source_exhibit", 'behavior': 'autocomplete', masthead_typeahead: true, 'cropper': image_form.name, target_panel: "##{form_prefix}-target-panel", :'id-field' => "##{form_prefix}_document_global_id", remote_url_field: "##{form_prefix}_remote_image_url" }) %>
<div id="<%= form_prefix %>-target-panel" style="display:none">
<%= text_field_tag(:document_title, (presenter(f.object.document).document_show_html_title if f.object.document), class: "form-control", data: { input_select_target: "##{form_prefix(f)}_source_exhibit", 'behavior': 'autocomplete', masthead_typeahead: true, target_panel: "##{form_prefix(f)}-target-panel", :'id-field' => "##{form_prefix(f)}_document_global_id", remote_url_field: "##{form_prefix(f)}_remote_image_url" }) %>
<div id="<%= form_prefix(f) %>-target-panel" style="display:none">
<div class="panel-heading">
<div data-item-title="true"></div>
<div data-panel-image-pagination="true"></div>
Expand All @@ -24,11 +20,11 @@
</div>
<div>
<%= f.radio_button(:source, :remote, label: t(:'.source.remote.label')) %>
<%= image_form.upload(f) %>
<%= iiif_upload_tag(f) %>
</div>
<% end %>
<%= field_set_tag(t(:'.source.remote.header')) do %>
<p class="instructions"><%= t(:'.source.remote.help') %></p>
<%= image_form.iiif_hidden_fields(f) %>
<%= image_form.text_and_display(f) %>
<%= iiif_cropper_tags f, initial_crop_selection: initial_crop_selection %>
<% end %>
</div>
11 changes: 4 additions & 7 deletions app/views/spotlight/featured_images/_upload_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
<% if Rails::VERSION::MAJOR >= 5 %>
<% form_prefix = f.object_name.parameterize(separator: '_') %>
<% else %>
<% form_prefix = f.object_name.parameterize('_') %>
<% end %>
<div data-cropper="<%= f.object.model_name.singular_route_key %>" data-form-prefix="<%= form_prefix(f) %>">
<%= field_set_tag do %>
<%= f.check_box(:display) %>
<% end if f.object.is_a? Spotlight::Masthead %>
<%= field_set_tag(t(:'.source.header')) do %>
<div>
<%= f.hidden_field(:source, value: :remote, label: t(:'.source.remote.label')) %>
<%= image_form.upload(f) %>
<%= iiif_upload_tag(f) %>
</div>
<% end %>
<%= field_set_tag(t(:'.source.remote.header')) do %>
<p class="instructions"><%= t(:'.source.remote.help') %></p>
<%= image_form.text_and_display(f) %>
<%= iiif_cropper_tags f, initial_crop_selection: initial_crop_selection %>
<% end %>
</div>
3 changes: 1 addition & 2 deletions app/views/spotlight/pages/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@

<% if @page.respond_to? :thumbnail %>
<div role="tabpanel" class="tab-pane" id="page-thumbnail">
<% image_form = iiif_cropper(f, :thumbnail, *Spotlight::Engine.config.thumbnail_initial_crop_selection) %>
<%= f.fields_for :thumbnail, (@page.thumbnail || @page.build_thumbnail) do |m| %>
<p class="instructions"><%= t(:'.thumbnail.help') %></p>
<%= render '/spotlight/featured_images/form', f: m, image_form: image_form %>
<%= render '/spotlight/featured_images/form', f: m, initial_crop_selection: Spotlight::Engine.config.featured_image_thumb_size %>
<% end %>
</div>
<% end %>
Expand Down
6 changes: 2 additions & 4 deletions app/views/spotlight/searches/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,16 @@
<% end unless @search.query_params.blank? %>
</div>
<div role="tabpanel" class="tab-pane" id="search-masthead">
<% image_form = iiif_cropper(f, :masthead, *Spotlight::Engine.config.masthead_initial_crop_selection) %>
<%= f.fields_for :masthead, (@search.masthead || @search.build_masthead) do |m| %>
<p class="instructions"><%= t(:'.masthead.help') %></p>
<p class="instructions"><%= t(:'.masthead.help_secondary') %></p>
<%= render '/spotlight/featured_images/form', f: m, image_form: image_form %>
<%= render '/spotlight/featured_images/form', f: m, initial_crop_selection: Spotlight::Engine.config.masthead_initial_crop_selection %>
<% end %>
</div>
<div role="tabpanel" class="tab-pane" id="search-thumbnail">
<% image_form = iiif_cropper(f, :thumbnail, *Spotlight::Engine.config.featured_image_thumb_size) %>
<%= f.fields_for :thumbnail, (@search.thumbnail || @search.build_thumbnail) do |m| %>
<p class="instructions"><%= t(:'.thumbnail.help') %></p>
<%= render '/spotlight/featured_images/form', f: m, image_form: image_form %>
<%= render '/spotlight/featured_images/form', f: m, initial_crop_selection: Spotlight::Engine.config.featured_image_thumb_size %>
<% end %>
</div>

Expand Down
4 changes: 1 addition & 3 deletions app/views/spotlight/sites/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@

<div role="tabpanel" class="tab-pane" id="site-masthead">
<p class="instructions"><%= t(:'.site_masthead.help') %></p>
<% image_form = iiif_cropper(f, :masthead, *Spotlight::Engine.config.masthead_initial_crop_selection) %>
<%= f.fields_for(:masthead, @site.masthead || @site.build_masthead) do |m| %>
<%= image_form.iiif_region_field(m) %>
<%= render '/spotlight/featured_images/upload_form', f: m, image_form: image_form %>
<%= render '/spotlight/featured_images/upload_form', f: m, initial_crop_selection: Spotlight::Engine.config.masthead_initial_crop_selection %>
<% end %>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions config/locales/spotlight.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ en:
placeholder: Name of library or other physical location
telephone:
placeholder: Telephone number (optional)
source:
remote:
label: "Upload an image"
avatar:
header: Avatar
help: >
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/spotlight/featured_images_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
expect do
post :create, params: {
featured_image: {
thumbnail: fixture_file_upload('spec/fixtures/800x600.png', 'image/png')
image: fixture_file_upload('spec/fixtures/800x600.png', 'image/png')
}
}
end.not_to change { Spotlight::FeaturedImage.count }
Expand All @@ -31,7 +31,7 @@
expect do
post :create, params: {
featured_image: {
thumbnail: fixture_file_upload('spec/fixtures/800x600.png', 'image/png')
image: fixture_file_upload('spec/fixtures/800x600.png', 'image/png')
}
}
end.to change { Spotlight::FeaturedImage.count }.by(1)
Expand All @@ -46,7 +46,7 @@
expect do
post :create, params: {
featured_image: {
masthead: fixture_file_upload('spec/fixtures/800x600.png', 'image/png')
image: fixture_file_upload('spec/fixtures/800x600.png', 'image/png')
}
}
end.to change { Spotlight::FeaturedImage.count }.by(1)
Expand All @@ -61,7 +61,7 @@
expect do
post :create, params: {
featured_image: {
avatar: fixture_file_upload('spec/fixtures/800x600.png', 'image/png')
image: fixture_file_upload('spec/fixtures/800x600.png', 'image/png')
}
}
end.to change { Spotlight::FeaturedImage.count }.by(1)
Expand Down
11 changes: 5 additions & 6 deletions spec/helpers/spotlight/crop_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
describe Spotlight::CropHelper do
describe '.contact_crop' do
let(:template) { double }
let(:form) { double(draw: true) }
it 'has initial_set_select' do
expect(Spotlight::IIIFCropper).to receive(:new).with(template, :avatar, 70, 70).and_return(form)
helper.contact_crop(template, :avatar)
describe '#form_prefix' do
let(:form) { double(object_name: 'Spotlight::Exhibit') }

it 'parameterizes the form object' do
expect(helper.form_prefix(form)).to eq 'spotlight_exhibit'
end
end
end

0 comments on commit 5afa57f

Please sign in to comment.