Skip to content

Commit

Permalink
make sul-embed work with exhibits select image section
Browse files Browse the repository at this point in the history
  • Loading branch information
dnoneill committed Dec 13, 2024
1 parent 4343055 commit 5733c78
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/components/embed/m3_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
id='sul-embed-m3'
class='m3'
data-m3-uri='<%= viewer.manifest_json_url %>'
data-viewer-config='<%= viewer.iiif_initial_viewer_config %>'
data-canvas-id='<%= viewer.canvas_id %>'
data-canvas-index='<%= viewer.canvas_index %>'
data-hide-title='<%= viewer.embed_request.hide_title? %>'
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/embed_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def linted_params
raise ActionController::BadRequest
end
params.permit(:url, :maxwidth, :maxheight, :format, :fullheight, :new_component,
:hide_title, :hide_embed, :hide_download, :hide_search, :min_files_to_search,
:hide_title, :hide_embed, :hide_download, :hide_search, :min_files_to_search, :iiif_initial_viewer_config,
:canvas_id, :canvas_index, :search, :suggested_search, :image_tools, :cdl_hold_record_id)
end

Expand Down
40 changes: 39 additions & 1 deletion app/javascript/src/modules/m3_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import embedModePlugin from '../plugins/embedModePlugin';
import analyticsPlugin from '../plugins/analyticsPlugin';
import cdlAuthPlugin from '../plugins/cdlAuthPlugin';
import xywhPlugin from '../plugins/xywhPlugin';
import { getExportableState } from 'mirador/dist/es/src/state/selectors';

export default {
init: function() {
Expand All @@ -31,7 +32,10 @@ export default {
sideBarPanel = 'attribution';
}

Mirador.viewer({
let viewerConfig;
if (data.viewerConfig) { viewerConfig = JSON.parse(data.viewerConfig) }

const viewerInstance = Mirador.viewer({
id: 'sul-embed-m3',
miradorDownloadPlugin: {
restrictDownloadOnSizeDefinition: true,
Expand Down Expand Up @@ -81,6 +85,7 @@ export default {
suggestedSearches: data.suggestedSearch.length > 0 ? [data.suggestedSearch] : null,
loadedManifest: data.m3Uri,
canvasIndex: Number(data.canvasIndex),
initialViewerConfig: viewerConfig,
canvasId: data.canvasId,
...(cdl && {
cdl: {
Expand Down Expand Up @@ -136,5 +141,38 @@ export default {
analyticsPlugin,
xywhPlugin,
].filter(Boolean));

window.addEventListener('message', (event) => {
const regex = /^(stanford\.edu|[\w-]+\.stanford\.edu)$/;
if (regex.exec(event.origin) === null) {
return;
}

if (event && event.data) {
let parsedData;
try {
parsedData = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
} catch (error) {
console.error('Failed to parse event data:', error);
return; // Exit if parsing fails
}

if (parsedData.type === "requestState") {
const currentState = viewerInstance.store.getState();
const exportableState = getExportableState(currentState);
const xywhCoords = viewerInstance.container.querySelector('[data-xywh-coords]').dataset.xywhCoords;

// Send the state back to the parent window
window.parent.postMessage(
JSON.stringify({
type: 'stateResponse',
data: {...exportableState, ...{'xywh': xywhCoords}},
source: 'sul-embed-m3',
}),
event.origin
);
}
}
});
}
};
2 changes: 1 addition & 1 deletion app/viewers/embed/viewer/m3_viewer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Embed
module Viewer
class M3Viewer < CommonViewer
delegate :search, :suggested_search, :canvas_id, :cdl_hold_record_id, to: :embed_request
delegate :search, :suggested_search, :canvas_id, :cdl_hold_record_id, :iiif_initial_viewer_config, to: :embed_request

def component
M3Component
Expand Down
1 change: 1 addition & 0 deletions app/views/embed/iiif.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
data-search='<%= @embed_request.search %>'
data-suggested-search='<%= @embed_request.suggested_search %>'
data-image-tools='<%= @embed_request.image_tools %>'
data-viewer-config='<%= @embed_request.iiif_initial_viewer_config %>'
style='height:100%; width:100%'>
</div>

Expand Down
7 changes: 6 additions & 1 deletion lib/embed/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def as_url_params
:min_files_to_search,
:canvas_id, :canvas_index,
:search, :suggested_search,
:image_tools
:image_tools,
:iiif_initial_viewer_config
)

if p.respond_to? :permit!
Expand Down Expand Up @@ -107,6 +108,10 @@ def image_tools
params[:image_tools]
end

def iiif_initial_viewer_config
params[:iiif_initial_viewer_config]
end

def validate!(url_parameter: true, url_scheme: true, format: true)
require_url_parameter if url_parameter
validate_url_scheme if url_scheme
Expand Down

0 comments on commit 5733c78

Please sign in to comment.