diff --git a/__tests__/src/actions/window.test.js b/__tests__/src/actions/window.test.js index 89889961ebd05eb9c0ae7bfe04ea6d0e371d6f33..2a819fe326cef8ba2af116450aff9140d8688d56 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: 200, - y: 200, + x: 260, + y: 300, 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 2aaf858d9bb97d77c8d3100a89d385fc88955a93..4faa9a962bdaa62486c550ae77efba5c9438c52c 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: 200, - y: 200, - 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: 200 + (Math.floor(numWindows / 10) * 50 + (numWindows * 30) % 300), + y: 200 + ((numWindows * 50) % 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' }, + ], + }); }; }