-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2397 from ProjectMirador/2380-add-window
Don't reshuffle when a window is added, and add them in a specific order
- Loading branch information
Showing
6 changed files
with
157 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import MosaicLayout from '../../../src/lib/MosaicLayout'; | ||
|
||
describe('MosaicLayout', () => { | ||
describe('constructor', () => { | ||
it('sets layout', () => { | ||
expect(new MosaicLayout('foo').layout).toEqual('foo'); | ||
}); | ||
}); | ||
describe('addWindows', () => { | ||
let instance; | ||
beforeEach(() => { | ||
instance = new MosaicLayout('foo'); | ||
}); | ||
it('case 1 window: adds to the top right', () => { | ||
expect(instance.layout).toEqual('foo'); | ||
instance.addWindows(['bar']); | ||
expect(instance.layout).toEqual({ | ||
direction: 'row', | ||
first: 'foo', | ||
second: 'bar', | ||
}); | ||
}); | ||
it('case 3 windows: adds to the top right', () => { | ||
expect(instance.layout).toEqual('foo'); | ||
instance.addWindows(['bar', 'bat', 'bark']); | ||
expect(instance.layout).toEqual({ | ||
direction: 'row', | ||
first: 'foo', | ||
second: { | ||
direction: 'column', | ||
first: { | ||
direction: 'row', | ||
first: 'bat', | ||
second: 'bark', | ||
}, | ||
second: 'bar', | ||
}, | ||
}); | ||
}); | ||
}); | ||
describe('removeWindows', () => { | ||
let instance; | ||
beforeEach(() => { | ||
instance = new MosaicLayout({ first: 'foo', second: 'bar' }); | ||
}); | ||
it('case 1 window: returns a single window', () => { | ||
instance.removeWindows(['bar'], { bar: ['second'] }); | ||
expect(instance.layout).toEqual('foo'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { createRemoveUpdate, updateTree } from 'react-mosaic-component/lib/util/mosaicUpdates'; | ||
import { | ||
getNodeAtPath, getOtherDirection, getPathToCorner, Corner, | ||
} from 'react-mosaic-component/lib/util/mosaicUtilities'; | ||
import dropRight from 'lodash/dropRight'; | ||
|
||
/** */ | ||
export default class MosaicLayout { | ||
/** */ | ||
constructor(layout) { | ||
this.layout = layout; | ||
} | ||
|
||
/** */ | ||
pathToCorner(corner = Corner.TOP_RIGHT) { | ||
return getPathToCorner(this.layout, corner); | ||
} | ||
|
||
/** */ | ||
pathToParent(path) { | ||
return getNodeAtPath(this.layout, dropRight(path)); | ||
} | ||
|
||
/** */ | ||
nodeAtPath(path) { | ||
return getNodeAtPath(this.layout, path); | ||
} | ||
|
||
/** | ||
* addWindows - updates the layout with new windows using an algorithm ported | ||
* from the react-mosaic-components examples. Will always add to the Top Right | ||
* https://github.com/nomcopter/react-mosaic/blob/5081df8d1528d4c3b83a72763a46a30b3048fe95/demo/ExampleApp.tsx#L119-L154 | ||
* @param {Array} addedWindowIds [description] | ||
*/ | ||
addWindows(addedWindowIds) { | ||
addedWindowIds.forEach((windowId, i) => { | ||
const path = this.pathToCorner(); | ||
const parent = this.pathToParent(path); | ||
const destination = this.nodeAtPath(path); | ||
const direction = parent ? getOtherDirection(parent.direction) : 'row'; | ||
let first; | ||
let second; | ||
if (direction === 'row') { | ||
first = destination; | ||
second = addedWindowIds[i]; | ||
} else { | ||
first = addedWindowIds[i]; | ||
second = destination; | ||
} | ||
const update = { | ||
path, | ||
spec: { | ||
$set: { | ||
direction, | ||
first, | ||
second, | ||
}, | ||
}, | ||
}; | ||
// We cannot batch the updates together because we need to recalculate | ||
// the new location for each new window | ||
this.layout = updateTree(this.layout, [update]); | ||
}); | ||
} | ||
|
||
/** | ||
* removeWindows - Generate a set of "removeUpdates" to update layout binary | ||
* tree. Then update the layout. | ||
* @param {Array} removedWindowIds | ||
* @param {Object} windowPaths - a lookup table for window paths | ||
*/ | ||
removeWindows(removedWindowIds, windowPaths) { | ||
const removeUpdates = removedWindowIds | ||
.map(windowId => ( | ||
createRemoveUpdate(this.layout, windowPaths[windowId]) | ||
)); | ||
this.layout = updateTree(this.layout, removeUpdates); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters