Skip to content

Commit 03a9011

Browse files
[IIIF-148] add canvasIndex event, add test
1 parent a66d938 commit 03a9011

3 files changed

Lines changed: 65 additions & 14 deletions

File tree

src/app/item-page/mirador-viewer/mirador-viewer.component.spec.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
44
import { TranslateLoaderMock } from '../../shared/mocks/translate-loader.mock';
55
import { BitstreamDataService } from '../../core/data/bitstream-data.service';
66
import { createRelationshipsObservable } from '../simple/item-types/shared/item.component.spec';
7-
import { NO_ERRORS_SCHEMA } from '@angular/core';
7+
import { NO_ERRORS_SCHEMA, PLATFORM_ID } from '@angular/core';
88
import { MetadataMap } from '../../core/shared/metadata.models';
99
import { Item } from '../../core/shared/item.model';
1010
import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
@@ -207,7 +207,58 @@ describe('MiradorViewerComponent with a single image', () => {
207207
it('should call mirador service image count', () => {
208208
expect(viewerService.getImageCount).toHaveBeenCalled();
209209
});
210+
});
211+
212+
});
210213

214+
describe('MiradorViewerComponent on browser in prod mode', () => {
215+
let comp: MiradorViewerComponent;
216+
let fixture: ComponentFixture<MiradorViewerComponent>;
217+
const viewerService = jasmine.createSpyObj('MiradorViewerService', ['showEmbeddedViewer', 'getImageCount']);
218+
219+
beforeEach(waitForAsync(() => {
220+
viewerService.showEmbeddedViewer.and.returnValue(true);
221+
viewerService.getImageCount.and.returnValue(observableOf(1));
222+
TestBed.configureTestingModule({
223+
imports: [TranslateModule.forRoot({
224+
loader: {
225+
provide: TranslateLoader,
226+
useClass: TranslateLoaderMock
227+
}
228+
})],
229+
declarations: [MiradorViewerComponent],
230+
providers: [
231+
{ provide: BitstreamDataService, useValue: {} },
232+
{ provide: BundleDataService, useValue: {} },
233+
{ provide: HostWindowService, useValue: mockHostWindowService },
234+
{ provide: NativeWindowService, useValue: new NativeWindowRef() },
235+
{ provide: Location, useValue: {} },
236+
{ provide: PLATFORM_ID, useValue: 'browser' },
237+
],
238+
schemas: [NO_ERRORS_SCHEMA]
239+
}).overrideComponent(MiradorViewerComponent, {
240+
set: {
241+
providers: [
242+
{ provide: MiradorViewerService, useValue: viewerService }
243+
]
244+
}
245+
}).compileComponents();
246+
}));
247+
248+
describe('viewer init', () => {
249+
beforeEach(waitForAsync(() => {
250+
fixture = TestBed.createComponent(MiradorViewerComponent);
251+
comp = fixture.componentInstance;
252+
comp.object = getItem(noMetadata);
253+
fixture.detectChanges();
254+
}));
255+
256+
it('should set iframe listener', () => {
257+
const compAsAny = comp as any;
258+
spyOn(compAsAny._window.nativeWindow, 'addEventListener');
259+
compAsAny.ngOnInit();
260+
expect(compAsAny._window.nativeWindow.addEventListener).toHaveBeenCalled();
261+
});
211262
});
212263

213264
});

src/app/item-page/mirador-viewer/mirador-viewer.component.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import { NativeWindowRef, NativeWindowService } from '../../core/services/window
1414
const IFRAME_UPDATE_URL_MESSAGE = 'update-url';
1515

1616
interface IFrameMessageData {
17-
canvasId: string;
1817
type: string;
19-
param: string;
18+
canvasId: string;
19+
canvasIndex: string;
2020
}
2121

2222
@Component({
@@ -185,15 +185,14 @@ export class MiradorViewerComponent implements OnInit, OnDestroy {
185185
if (data.type === IFRAME_UPDATE_URL_MESSAGE) {
186186
const currentPath = this.location.path();
187187
const canvasId = data.canvasId;
188-
const param = data.param;
188+
const canvasIndex = data.canvasIndex;
189189
// Use URL API for easier query param manipulation
190190
const url = new URL(window.location.origin + currentPath);
191-
console.log(url);
192191
// Set or update the query param
193-
url.searchParams.set(param, canvasId);
192+
url.searchParams.set('canvasId', canvasId);
193+
url.searchParams.set('canvasIndex', canvasIndex);
194194
const newPathWithQuery = url.pathname + url.search;
195195
// Replace the current state (no reload, no new history entry)
196-
console.log('newPathWithQuery', newPathWithQuery);
197196
this.location.replaceState(newPathWithQuery);
198197
}
199198
};

src/mirador-viewer/locationPlugin.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
import { takeEvery } from 'redux-saga/effects';
1+
import { takeEvery, select } from 'redux-saga/effects';
22
import ActionTypes from 'mirador/dist/es/src/state/actions/action-types';
3+
import {getCanvasIndex} from "mirador/dist/es/src/state/selectors";
34

4-
export
55

66
/** This will be called every time the SET_CANVAS action is dispatched */
77
const onCanvasChange = function* (action) {
8-
console.log('SET_CANVAS action: ', action);
9-
if(action.canvasId) {
8+
const { windowId, canvasId } = action;
9+
if (windowId && canvasId) {
10+
const canvasIndex = yield select(getCanvasIndex, { windowId });
1011
// IDs are in the format of "https://dspaceglam7dev.4science.cloud/server/iiif/392363fe-015f-41e6-8cdd-b5c754605787/canvas/03c322b7-0182-44fa-8dc3-5d2efcece237"
1112
const id = action.canvasId.split('/').pop();
1213
const message = {
1314
type: 'update-url',
14-
param: 'canvasId',
15+
// index here starts from 0, whilst for setting the index it starts from 1.
16+
canvasIndex: canvasIndex + 1,
1517
canvasId: id
1618
};
17-
console.log('POSTING MESSAGE: ', message);
18-
if (id && id !== 'undefined') {
19+
if (id && id !== 'undefined' && typeof window !== 'undefined') {
1920
window.parent.postMessage(message, '*');
2021
}
2122
}

0 commit comments

Comments
 (0)