-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathMosaicLayout.js
More file actions
79 lines (73 loc) · 2.23 KB
/
MosaicLayout.js
File metadata and controls
79 lines (73 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import {
createRemoveUpdate, updateTree,
getNodeAtPath, getOtherDirection, getPathToCorner, Corner,
} from '@lonli-lokli/react-mosaic-component';
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);
}
}