diff --git a/__tests__/src/actions/window.test.js b/__tests__/src/actions/window.test.js index e56f8a5dd6507a1bd05e5245e8ac22f2dd6b8fb3..dd02d2a0af08e7c55c138c917c7b98c4378bf96e 100644 --- a/__tests__/src/actions/window.test.js +++ b/__tests__/src/actions/window.test.js @@ -18,8 +18,8 @@ describe('window actions', () => { manifestId: null, maximized: false, rangeId: null, - x: 2700, - y: 2700, + x: 2760, + y: 2760, sideBarPanel: 'info', width: 400, height: 400, @@ -31,7 +31,19 @@ describe('window actions', () => { { position: 'far-bottom', content: 'thumbnail_navigation' }, ], }; - const action = actions.addWindow(options); + + const mockState = { + windows: { a: {}, b: {} }, + }; + + const mockDispatch = jest.fn(() => ({})); + const mockGetState = jest.fn(() => mockState); + const thunk = actions.addWindow(options); + + thunk(mockDispatch, mockGetState); + + const action = mockDispatch.mock.calls[0][0]; + expect(action).toMatchObject(expectedAction); expect(action.window.companionWindowIds.length).toEqual(2); expect(action.window.companionWindowIds[0]).toEqual(action.companionWindows[0].id); diff --git a/src/state/actions/window.js b/src/state/actions/window.js index 770cc4643f423d5c5d16f6942aaad9dcba655ebb..197b117434651bb4de0d6e09fc7d244945c80fcb 100644 --- a/src/state/actions/window.js +++ b/src/state/actions/window.js @@ -18,32 +18,38 @@ export function focusWindow(windowId) { * @memberof ActionCreators */ export function addWindow(options) { - const cwDefault = `cw-${uuid()}`; - const cwThumbs = `cw-${uuid()}`; - const defaultOptions = { - id: `window-${uuid()}`, - canvasIndex: 0, - collectionIndex: 0, - manifestId: null, - rangeId: null, - thumbnailNavigationId: cwThumbs, - width: 400, - height: 400, - x: 2700, - y: 2700, - companionWindowIds: [cwDefault, cwThumbs], - sideBarPanel: 'info', - rotation: null, - view: 'single', - maximized: false, - }; - return { - type: ActionTypes.ADD_WINDOW, - window: { ...defaultOptions, ...options }, - companionWindows: [ - { id: cwDefault, position: 'left', content: 'info' }, - { id: cwThumbs, position: options.thumbnailNavigationPosition || 'far-bottom', content: 'thumbnail_navigation' }, - ], + return (dispatch, getState) => { + const { windows } = getState(); + const numWindows = Object.keys(windows).length; + + const cwDefault = `cw-${uuid()}`; + const cwThumbs = `cw-${uuid()}`; + const defaultOptions = { + id: `window-${uuid()}`, + canvasIndex: 0, + collectionIndex: 0, + manifestId: null, + rangeId: null, + thumbnailNavigationId: cwThumbs, + width: 400, + height: 400, + x: 2700 + (Math.floor(numWindows / 10) * 50 + (numWindows * 30) % 300), + y: 2700 + ((numWindows * 30) % 300), + companionWindowIds: [cwDefault, cwThumbs], + sideBarPanel: 'info', + rotation: null, + view: 'single', + maximized: false, + }; + + dispatch({ + type: ActionTypes.ADD_WINDOW, + window: { ...defaultOptions, ...options }, + companionWindows: [ + { id: cwDefault, position: 'left', content: 'info' }, + { id: cwThumbs, position: options.thumbnailNavigationPosition || 'far-bottom', content: 'thumbnail_navigation' }, + ], + }); }; }