react-grid-layout 1.5.3 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1156 -442
- package/css/styles.css +5 -4
- package/dist/ResponsiveGridLayout-BkHF9YHa.d.mts +133 -0
- package/dist/ResponsiveGridLayout-CLn16-X3.d.ts +133 -0
- package/dist/calculate-DsVTldEE.d.ts +196 -0
- package/dist/calculate-DwbL1D06.d.mts +196 -0
- package/dist/chunk-2O3ZME4Q.mjs +703 -0
- package/dist/chunk-2O3ZME4Q.mjs.map +1 -0
- package/dist/chunk-526JND3R.mjs +3 -0
- package/dist/chunk-526JND3R.mjs.map +1 -0
- package/dist/chunk-7ELO5FRW.js +4 -0
- package/dist/chunk-7ELO5FRW.js.map +1 -0
- package/dist/chunk-AWM66AWF.mjs +422 -0
- package/dist/chunk-AWM66AWF.mjs.map +1 -0
- package/dist/chunk-BJFPTW5Q.js +449 -0
- package/dist/chunk-BJFPTW5Q.js.map +1 -0
- package/dist/chunk-FN6MIZ24.js +744 -0
- package/dist/chunk-FN6MIZ24.js.map +1 -0
- package/dist/chunk-GPZBANOK.js +435 -0
- package/dist/chunk-GPZBANOK.js.map +1 -0
- package/dist/chunk-LWF5EYMO.mjs +429 -0
- package/dist/chunk-LWF5EYMO.mjs.map +1 -0
- package/dist/chunk-MXXN7GUE.mjs +1408 -0
- package/dist/chunk-MXXN7GUE.mjs.map +1 -0
- package/dist/chunk-QRW3ND4U.js +1417 -0
- package/dist/chunk-QRW3ND4U.js.map +1 -0
- package/dist/core.d.mts +186 -0
- package/dist/core.d.ts +186 -0
- package/dist/core.js +274 -0
- package/dist/core.js.map +1 -0
- package/dist/core.mjs +5 -0
- package/dist/core.mjs.map +1 -0
- package/dist/extras.d.mts +208 -0
- package/dist/extras.d.ts +208 -0
- package/dist/extras.js +462 -0
- package/dist/extras.js.map +1 -0
- package/dist/extras.mjs +454 -0
- package/dist/extras.mjs.map +1 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +154 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +7 -0
- package/dist/index.mjs.map +1 -0
- package/dist/legacy.d.mts +163 -0
- package/dist/legacy.d.ts +163 -0
- package/dist/legacy.js +320 -0
- package/dist/legacy.js.map +1 -0
- package/dist/legacy.mjs +308 -0
- package/dist/legacy.mjs.map +1 -0
- package/dist/position-C8a5g6bb.d.mts +324 -0
- package/dist/position-H8tBL91b.d.ts +324 -0
- package/dist/react.d.mts +357 -0
- package/dist/react.d.ts +357 -0
- package/dist/react.js +96 -0
- package/dist/react.js.map +1 -0
- package/dist/react.mjs +7 -0
- package/dist/react.mjs.map +1 -0
- package/dist/responsive-BB5d98xz.d.mts +145 -0
- package/dist/responsive-BXI-GCXG.d.ts +145 -0
- package/dist/types-CokovIMH.d.mts +469 -0
- package/dist/types-CokovIMH.d.ts +469 -0
- package/index-dev.js +24 -0
- package/package.json +133 -40
- package/.babelrc.js +0 -22
- package/.browserslistrc +0 -3
- package/.eslintignore +0 -4
- package/.flowconfig +0 -20
- package/.prettierignore +0 -4
- package/.prettierrc +0 -17
- package/CHANGELOG.md +0 -985
- package/build/GridItem.js +0 -638
- package/build/ReactGridLayout.js +0 -741
- package/build/ReactGridLayoutPropTypes.js +0 -210
- package/build/ResponsiveReactGridLayout.js +0 -297
- package/build/calculateUtils.js +0 -165
- package/build/components/WidthProvider.js +0 -115
- package/build/fastRGLPropsEqual.js +0 -5
- package/build/responsiveUtils.js +0 -101
- package/build/utils.js +0 -842
- package/dist/react-grid-layout.min.js +0 -2
- package/dist/react-grid-layout.min.js.map +0 -1
- package/index.js.flow +0 -8
- package/ip_fetcher +0 -0
- package/ip_fetcher.c +0 -47
- package/lib/GridItem.jsx +0 -689
- package/lib/ReactGridLayout.jsx +0 -855
- package/lib/ReactGridLayoutPropTypes.js +0 -241
- package/lib/ResponsiveReactGridLayout.jsx +0 -349
- package/lib/calculateUtils.js +0 -169
- package/lib/components/WidthProvider.jsx +0 -110
- package/lib/fastRGLPropsEqual.js +0 -46
- package/lib/responsiveUtils.js +0 -118
- package/lib/utils.js +0 -995
- package/yarn-error.log +0 -7780
package/build/utils.js
DELETED
|
@@ -1,842 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.bottom = bottom;
|
|
7
|
-
exports.childrenEqual = childrenEqual;
|
|
8
|
-
exports.cloneLayout = cloneLayout;
|
|
9
|
-
exports.cloneLayoutItem = cloneLayoutItem;
|
|
10
|
-
exports.collides = collides;
|
|
11
|
-
exports.compact = compact;
|
|
12
|
-
exports.compactItem = compactItem;
|
|
13
|
-
exports.compactType = compactType;
|
|
14
|
-
exports.correctBounds = correctBounds;
|
|
15
|
-
exports.fastPositionEqual = fastPositionEqual;
|
|
16
|
-
exports.fastRGLPropsEqual = void 0;
|
|
17
|
-
exports.getAllCollisions = getAllCollisions;
|
|
18
|
-
exports.getFirstCollision = getFirstCollision;
|
|
19
|
-
exports.getLayoutItem = getLayoutItem;
|
|
20
|
-
exports.getStatics = getStatics;
|
|
21
|
-
exports.modifyLayout = modifyLayout;
|
|
22
|
-
exports.moveElement = moveElement;
|
|
23
|
-
exports.moveElementAwayFromCollision = moveElementAwayFromCollision;
|
|
24
|
-
exports.noop = void 0;
|
|
25
|
-
exports.perc = perc;
|
|
26
|
-
exports.resizeItemInDirection = resizeItemInDirection;
|
|
27
|
-
exports.setTopLeft = setTopLeft;
|
|
28
|
-
exports.setTransform = setTransform;
|
|
29
|
-
exports.sortLayoutItems = sortLayoutItems;
|
|
30
|
-
exports.sortLayoutItemsByColRow = sortLayoutItemsByColRow;
|
|
31
|
-
exports.sortLayoutItemsByRowCol = sortLayoutItemsByRowCol;
|
|
32
|
-
exports.synchronizeLayoutWithChildren = synchronizeLayoutWithChildren;
|
|
33
|
-
exports.validateLayout = validateLayout;
|
|
34
|
-
exports.withLayoutItem = withLayoutItem;
|
|
35
|
-
var _fastEquals = require("fast-equals");
|
|
36
|
-
var _react = _interopRequireDefault(require("react"));
|
|
37
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
38
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
39
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
40
|
-
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
41
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
42
|
-
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
43
|
-
/*:: import type {
|
|
44
|
-
ChildrenArray as ReactChildrenArray,
|
|
45
|
-
Element as ReactElement
|
|
46
|
-
} from "react";*/
|
|
47
|
-
/*:: export type ResizeHandleAxis =
|
|
48
|
-
| "s"
|
|
49
|
-
| "w"
|
|
50
|
-
| "e"
|
|
51
|
-
| "n"
|
|
52
|
-
| "sw"
|
|
53
|
-
| "nw"
|
|
54
|
-
| "se"
|
|
55
|
-
| "ne";*/
|
|
56
|
-
/*:: export type LayoutItem = {
|
|
57
|
-
w: number,
|
|
58
|
-
h: number,
|
|
59
|
-
x: number,
|
|
60
|
-
y: number,
|
|
61
|
-
i: string,
|
|
62
|
-
minW?: number,
|
|
63
|
-
minH?: number,
|
|
64
|
-
maxW?: number,
|
|
65
|
-
maxH?: number,
|
|
66
|
-
moved?: boolean,
|
|
67
|
-
static?: boolean,
|
|
68
|
-
isDraggable?: ?boolean,
|
|
69
|
-
isResizable?: ?boolean,
|
|
70
|
-
resizeHandles?: Array<ResizeHandleAxis>,
|
|
71
|
-
isBounded?: ?boolean
|
|
72
|
-
};*/
|
|
73
|
-
/*:: export type Layout = $ReadOnlyArray<LayoutItem>;*/
|
|
74
|
-
/*:: export type Position = {
|
|
75
|
-
left: number,
|
|
76
|
-
top: number,
|
|
77
|
-
width: number,
|
|
78
|
-
height: number
|
|
79
|
-
};*/
|
|
80
|
-
/*:: export type ReactDraggableCallbackData = {
|
|
81
|
-
node: HTMLElement,
|
|
82
|
-
x?: number,
|
|
83
|
-
y?: number,
|
|
84
|
-
deltaX: number,
|
|
85
|
-
deltaY: number,
|
|
86
|
-
lastX?: number,
|
|
87
|
-
lastY?: number
|
|
88
|
-
};*/
|
|
89
|
-
/*:: export type PartialPosition = { left: number, top: number };*/
|
|
90
|
-
/*:: export type DroppingPosition = { left: number, top: number, e: Event };*/
|
|
91
|
-
/*:: export type Size = { width: number, height: number };*/
|
|
92
|
-
/*:: export type GridDragEvent = {
|
|
93
|
-
e: Event,
|
|
94
|
-
node: HTMLElement,
|
|
95
|
-
newPosition: PartialPosition
|
|
96
|
-
};*/
|
|
97
|
-
/*:: export type GridResizeEvent = {
|
|
98
|
-
e: Event,
|
|
99
|
-
node: HTMLElement,
|
|
100
|
-
size: Size,
|
|
101
|
-
handle: string
|
|
102
|
-
};*/
|
|
103
|
-
/*:: export type DragOverEvent = MouseEvent & {
|
|
104
|
-
nativeEvent: {
|
|
105
|
-
layerX: number,
|
|
106
|
-
layerY: number,
|
|
107
|
-
...Event
|
|
108
|
-
}
|
|
109
|
-
};*/
|
|
110
|
-
/*:: export type Pick<FromType, Properties: { [string]: 0 }> = $Exact<
|
|
111
|
-
$ObjMapi<Properties, <K, V>(k: K, v: V) => $ElementType<FromType, K>>
|
|
112
|
-
>;*/
|
|
113
|
-
// Helpful port from TS
|
|
114
|
-
/*:: type REl = ReactElement<any>;*/
|
|
115
|
-
/*:: export type ReactChildren = ReactChildrenArray<REl>;*/
|
|
116
|
-
/*:: export type EventCallback = (
|
|
117
|
-
Layout,
|
|
118
|
-
oldItem: ?LayoutItem,
|
|
119
|
-
newItem: ?LayoutItem,
|
|
120
|
-
placeholder: ?LayoutItem,
|
|
121
|
-
Event,
|
|
122
|
-
?HTMLElement
|
|
123
|
-
) => void;*/
|
|
124
|
-
// All callbacks are of the signature (layout, oldItem, newItem, placeholder, e).
|
|
125
|
-
/*:: export type CompactType = ?("horizontal" | "vertical");*/
|
|
126
|
-
const isProduction = process.env.NODE_ENV === "production";
|
|
127
|
-
const DEBUG = false;
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Return the bottom coordinate of the layout.
|
|
131
|
-
*
|
|
132
|
-
* @param {Array} layout Layout array.
|
|
133
|
-
* @return {Number} Bottom coordinate.
|
|
134
|
-
*/
|
|
135
|
-
function bottom(layout /*: Layout*/) /*: number*/{
|
|
136
|
-
let max = 0,
|
|
137
|
-
bottomY;
|
|
138
|
-
for (let i = 0, len = layout.length; i < len; i++) {
|
|
139
|
-
bottomY = layout[i].y + layout[i].h;
|
|
140
|
-
if (bottomY > max) max = bottomY;
|
|
141
|
-
}
|
|
142
|
-
return max;
|
|
143
|
-
}
|
|
144
|
-
function cloneLayout(layout /*: Layout*/) /*: Layout*/{
|
|
145
|
-
const newLayout = Array(layout.length);
|
|
146
|
-
for (let i = 0, len = layout.length; i < len; i++) {
|
|
147
|
-
newLayout[i] = cloneLayoutItem(layout[i]);
|
|
148
|
-
}
|
|
149
|
-
return newLayout;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Modify a layoutItem inside a layout. Returns a new Layout,
|
|
153
|
-
// does not mutate. Carries over all other LayoutItems unmodified.
|
|
154
|
-
function modifyLayout(layout /*: Layout*/, layoutItem /*: LayoutItem*/) /*: Layout*/{
|
|
155
|
-
const newLayout = Array(layout.length);
|
|
156
|
-
for (let i = 0, len = layout.length; i < len; i++) {
|
|
157
|
-
if (layoutItem.i === layout[i].i) {
|
|
158
|
-
newLayout[i] = layoutItem;
|
|
159
|
-
} else {
|
|
160
|
-
newLayout[i] = layout[i];
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
return newLayout;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// Function to be called to modify a layout item.
|
|
167
|
-
// Does defensive clones to ensure the layout is not modified.
|
|
168
|
-
function withLayoutItem(layout /*: Layout*/, itemKey /*: string*/, cb /*: LayoutItem => LayoutItem*/) /*: [Layout, ?LayoutItem]*/{
|
|
169
|
-
let item = getLayoutItem(layout, itemKey);
|
|
170
|
-
if (!item) return [layout, null];
|
|
171
|
-
item = cb(cloneLayoutItem(item)); // defensive clone then modify
|
|
172
|
-
// FIXME could do this faster if we already knew the index
|
|
173
|
-
layout = modifyLayout(layout, item);
|
|
174
|
-
return [layout, item];
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Fast path to cloning, since this is monomorphic
|
|
178
|
-
function cloneLayoutItem(layoutItem /*: LayoutItem*/) /*: LayoutItem*/{
|
|
179
|
-
return {
|
|
180
|
-
w: layoutItem.w,
|
|
181
|
-
h: layoutItem.h,
|
|
182
|
-
x: layoutItem.x,
|
|
183
|
-
y: layoutItem.y,
|
|
184
|
-
i: layoutItem.i,
|
|
185
|
-
minW: layoutItem.minW,
|
|
186
|
-
maxW: layoutItem.maxW,
|
|
187
|
-
minH: layoutItem.minH,
|
|
188
|
-
maxH: layoutItem.maxH,
|
|
189
|
-
moved: Boolean(layoutItem.moved),
|
|
190
|
-
static: Boolean(layoutItem.static),
|
|
191
|
-
// These can be null/undefined
|
|
192
|
-
isDraggable: layoutItem.isDraggable,
|
|
193
|
-
isResizable: layoutItem.isResizable,
|
|
194
|
-
resizeHandles: layoutItem.resizeHandles,
|
|
195
|
-
isBounded: layoutItem.isBounded
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Comparing React `children` is a bit difficult. This is a good way to compare them.
|
|
201
|
-
* This will catch differences in keys, order, and length.
|
|
202
|
-
*/
|
|
203
|
-
function childrenEqual(a /*: ReactChildren*/, b /*: ReactChildren*/) /*: boolean*/{
|
|
204
|
-
return (0, _fastEquals.deepEqual)(_react.default.Children.map(a, c => c === null || c === void 0 ? void 0 : c.key), _react.default.Children.map(b, c => c === null || c === void 0 ? void 0 : c.key)) && (0, _fastEquals.deepEqual)(_react.default.Children.map(a, c => c === null || c === void 0 ? void 0 : c.props["data-grid"]), _react.default.Children.map(b, c => c === null || c === void 0 ? void 0 : c.props["data-grid"]));
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* See `fastRGLPropsEqual.js`.
|
|
209
|
-
* We want this to run as fast as possible - it is called often - and to be
|
|
210
|
-
* resilient to new props that we add. So rather than call lodash.isEqual,
|
|
211
|
-
* which isn't suited to comparing props very well, we use this specialized
|
|
212
|
-
* function in conjunction with preval to generate the fastest possible comparison
|
|
213
|
-
* function, tuned for exactly our props.
|
|
214
|
-
*/
|
|
215
|
-
/*:: type FastRGLPropsEqual = (Object, Object, Function) => boolean;*/
|
|
216
|
-
const fastRGLPropsEqual /*: FastRGLPropsEqual*/ = exports.fastRGLPropsEqual = require("./fastRGLPropsEqual");
|
|
217
|
-
|
|
218
|
-
// Like the above, but a lot simpler.
|
|
219
|
-
function fastPositionEqual(a /*: Position*/, b /*: Position*/) /*: boolean*/{
|
|
220
|
-
return a.left === b.left && a.top === b.top && a.width === b.width && a.height === b.height;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Given two layoutitems, check if they collide.
|
|
225
|
-
*/
|
|
226
|
-
function collides(l1 /*: LayoutItem*/, l2 /*: LayoutItem*/) /*: boolean*/{
|
|
227
|
-
if (l1.i === l2.i) return false; // same element
|
|
228
|
-
if (l1.x + l1.w <= l2.x) return false; // l1 is left of l2
|
|
229
|
-
if (l1.x >= l2.x + l2.w) return false; // l1 is right of l2
|
|
230
|
-
if (l1.y + l1.h <= l2.y) return false; // l1 is above l2
|
|
231
|
-
if (l1.y >= l2.y + l2.h) return false; // l1 is below l2
|
|
232
|
-
return true; // boxes overlap
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Given a layout, compact it. This involves going down each y coordinate and removing gaps
|
|
237
|
-
* between items.
|
|
238
|
-
*
|
|
239
|
-
* Does not modify layout items (clones). Creates a new layout array.
|
|
240
|
-
*
|
|
241
|
-
* @param {Array} layout Layout.
|
|
242
|
-
* @param {Boolean} verticalCompact Whether or not to compact the layout
|
|
243
|
-
* vertically.
|
|
244
|
-
* @param {Boolean} allowOverlap When `true`, allows overlapping grid items.
|
|
245
|
-
* @return {Array} Compacted Layout.
|
|
246
|
-
*/
|
|
247
|
-
function compact(layout /*: Layout*/, compactType /*: CompactType*/, cols /*: number*/, allowOverlap /*: ?boolean*/) /*: Layout*/{
|
|
248
|
-
// Statics go in the compareWith array right away so items flow around them.
|
|
249
|
-
const compareWith = getStatics(layout);
|
|
250
|
-
// We keep track of the bottom position.
|
|
251
|
-
let b = bottom(compareWith);
|
|
252
|
-
// We go through the items by row and column.
|
|
253
|
-
const sorted = sortLayoutItems(layout, compactType);
|
|
254
|
-
// Holding for new items.
|
|
255
|
-
const out = Array(layout.length);
|
|
256
|
-
for (let i = 0, len = sorted.length; i < len; i++) {
|
|
257
|
-
let l = cloneLayoutItem(sorted[i]);
|
|
258
|
-
|
|
259
|
-
// Don't move static elements
|
|
260
|
-
if (!l.static) {
|
|
261
|
-
l = compactItem(compareWith, l, compactType, cols, sorted, allowOverlap, b);
|
|
262
|
-
b = Math.max(b, l.y + l.h);
|
|
263
|
-
|
|
264
|
-
// Add to comparison array. We only collide with items before this one.
|
|
265
|
-
// Statics are already in this array.
|
|
266
|
-
compareWith.push(l);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
// Add to output array to make sure they still come out in the right order.
|
|
270
|
-
out[layout.indexOf(sorted[i])] = l;
|
|
271
|
-
|
|
272
|
-
// Clear moved flag, if it exists.
|
|
273
|
-
l.moved = false;
|
|
274
|
-
}
|
|
275
|
-
return out;
|
|
276
|
-
}
|
|
277
|
-
const heightWidth = {
|
|
278
|
-
x: "w",
|
|
279
|
-
y: "h"
|
|
280
|
-
};
|
|
281
|
-
/**
|
|
282
|
-
* Before moving item down, it will check if the movement will cause collisions and move those items down before.
|
|
283
|
-
*/
|
|
284
|
-
function resolveCompactionCollision(layout /*: Layout*/, item /*: LayoutItem*/, moveToCoord /*: number*/, axis /*: "x" | "y"*/) {
|
|
285
|
-
const sizeProp = heightWidth[axis];
|
|
286
|
-
item[axis] += 1;
|
|
287
|
-
const itemIndex = layout.map(layoutItem => {
|
|
288
|
-
return layoutItem.i;
|
|
289
|
-
}).indexOf(item.i);
|
|
290
|
-
|
|
291
|
-
// Go through each item we collide with.
|
|
292
|
-
for (let i = itemIndex + 1; i < layout.length; i++) {
|
|
293
|
-
const otherItem = layout[i];
|
|
294
|
-
// Ignore static items
|
|
295
|
-
if (otherItem.static) continue;
|
|
296
|
-
|
|
297
|
-
// Optimization: we can break early if we know we're past this el
|
|
298
|
-
// We can do this b/c it's a sorted layout
|
|
299
|
-
if (otherItem.y > item.y + item.h) break;
|
|
300
|
-
if (collides(item, otherItem)) {
|
|
301
|
-
resolveCompactionCollision(layout, otherItem, moveToCoord + item[sizeProp], axis);
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
item[axis] = moveToCoord;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* Compact an item in the layout.
|
|
309
|
-
*
|
|
310
|
-
* Modifies item.
|
|
311
|
-
*
|
|
312
|
-
*/
|
|
313
|
-
function compactItem(compareWith /*: Layout*/, l /*: LayoutItem*/, compactType /*: CompactType*/, cols /*: number*/, fullLayout /*: Layout*/, allowOverlap /*: ?boolean*/, b /*: ?number*/) /*: LayoutItem*/{
|
|
314
|
-
const compactV = compactType === "vertical";
|
|
315
|
-
const compactH = compactType === "horizontal";
|
|
316
|
-
if (compactV) {
|
|
317
|
-
// Bottom 'y' possible is the bottom of the layout.
|
|
318
|
-
// This allows you to do nice stuff like specify {y: Infinity}
|
|
319
|
-
// This is here because the layout must be sorted in order to get the correct bottom `y`.
|
|
320
|
-
if (typeof b === "number") {
|
|
321
|
-
l.y = Math.min(b, l.y);
|
|
322
|
-
} else {
|
|
323
|
-
l.y = Math.min(bottom(compareWith), l.y);
|
|
324
|
-
}
|
|
325
|
-
// Move the element up as far as it can go without colliding.
|
|
326
|
-
while (l.y > 0 && !getFirstCollision(compareWith, l)) {
|
|
327
|
-
l.y--;
|
|
328
|
-
}
|
|
329
|
-
} else if (compactH) {
|
|
330
|
-
// Move the element left as far as it can go without colliding.
|
|
331
|
-
while (l.x > 0 && !getFirstCollision(compareWith, l)) {
|
|
332
|
-
l.x--;
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
// Move it down, and keep moving it down if it's colliding.
|
|
337
|
-
let collides;
|
|
338
|
-
// Checking the compactType null value to avoid breaking the layout when overlapping is allowed.
|
|
339
|
-
while ((collides = getFirstCollision(compareWith, l)) && !(compactType === null && allowOverlap)) {
|
|
340
|
-
if (compactH) {
|
|
341
|
-
resolveCompactionCollision(fullLayout, l, collides.x + collides.w, "x");
|
|
342
|
-
} else {
|
|
343
|
-
resolveCompactionCollision(fullLayout, l, collides.y + collides.h, "y");
|
|
344
|
-
}
|
|
345
|
-
// Since we can't grow without bounds horizontally, if we've overflown, let's move it down and try again.
|
|
346
|
-
if (compactH && l.x + l.w > cols) {
|
|
347
|
-
l.x = cols - l.w;
|
|
348
|
-
l.y++;
|
|
349
|
-
// ALso move element as left as we can
|
|
350
|
-
while (l.x > 0 && !getFirstCollision(compareWith, l)) {
|
|
351
|
-
l.x--;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
// Ensure that there are no negative positions
|
|
357
|
-
l.y = Math.max(l.y, 0);
|
|
358
|
-
l.x = Math.max(l.x, 0);
|
|
359
|
-
return l;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* Given a layout, make sure all elements fit within its bounds.
|
|
364
|
-
*
|
|
365
|
-
* Modifies layout items.
|
|
366
|
-
*
|
|
367
|
-
* @param {Array} layout Layout array.
|
|
368
|
-
* @param {Number} bounds Number of columns.
|
|
369
|
-
*/
|
|
370
|
-
function correctBounds(layout /*: Layout*/, bounds /*: { cols: number }*/) /*: Layout*/{
|
|
371
|
-
const collidesWith = getStatics(layout);
|
|
372
|
-
for (let i = 0, len = layout.length; i < len; i++) {
|
|
373
|
-
const l = layout[i];
|
|
374
|
-
// Overflows right
|
|
375
|
-
if (l.x + l.w > bounds.cols) l.x = bounds.cols - l.w;
|
|
376
|
-
// Overflows left
|
|
377
|
-
if (l.x < 0) {
|
|
378
|
-
l.x = 0;
|
|
379
|
-
l.w = bounds.cols;
|
|
380
|
-
}
|
|
381
|
-
if (!l.static) collidesWith.push(l);else {
|
|
382
|
-
// If this is static and collides with other statics, we must move it down.
|
|
383
|
-
// We have to do something nicer than just letting them overlap.
|
|
384
|
-
while (getFirstCollision(collidesWith, l)) {
|
|
385
|
-
l.y++;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
return layout;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* Get a layout item by ID. Used so we can override later on if necessary.
|
|
394
|
-
*
|
|
395
|
-
* @param {Array} layout Layout array.
|
|
396
|
-
* @param {String} id ID
|
|
397
|
-
* @return {LayoutItem} Item at ID.
|
|
398
|
-
*/
|
|
399
|
-
function getLayoutItem(layout /*: Layout*/, id /*: string*/) /*: ?LayoutItem*/{
|
|
400
|
-
for (let i = 0, len = layout.length; i < len; i++) {
|
|
401
|
-
if (layout[i].i === id) return layout[i];
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
/**
|
|
406
|
-
* Returns the first item this layout collides with.
|
|
407
|
-
* It doesn't appear to matter which order we approach this from, although
|
|
408
|
-
* perhaps that is the wrong thing to do.
|
|
409
|
-
*
|
|
410
|
-
* @param {Object} layoutItem Layout item.
|
|
411
|
-
* @return {Object|undefined} A colliding layout item, or undefined.
|
|
412
|
-
*/
|
|
413
|
-
function getFirstCollision(layout /*: Layout*/, layoutItem /*: LayoutItem*/) /*: ?LayoutItem*/{
|
|
414
|
-
for (let i = 0, len = layout.length; i < len; i++) {
|
|
415
|
-
if (collides(layout[i], layoutItem)) return layout[i];
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
function getAllCollisions(layout /*: Layout*/, layoutItem /*: LayoutItem*/) /*: Array<LayoutItem>*/{
|
|
419
|
-
return layout.filter(l => collides(l, layoutItem));
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* Get all static elements.
|
|
424
|
-
* @param {Array} layout Array of layout objects.
|
|
425
|
-
* @return {Array} Array of static layout items..
|
|
426
|
-
*/
|
|
427
|
-
function getStatics(layout /*: Layout*/) /*: Array<LayoutItem>*/{
|
|
428
|
-
return layout.filter(l => l.static);
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* Move an element. Responsible for doing cascading movements of other elements.
|
|
433
|
-
*
|
|
434
|
-
* Modifies layout items.
|
|
435
|
-
*
|
|
436
|
-
* @param {Array} layout Full layout to modify.
|
|
437
|
-
* @param {LayoutItem} l element to move.
|
|
438
|
-
* @param {Number} [x] X position in grid units.
|
|
439
|
-
* @param {Number} [y] Y position in grid units.
|
|
440
|
-
*/
|
|
441
|
-
function moveElement(layout /*: Layout*/, l /*: LayoutItem*/, x /*: ?number*/, y /*: ?number*/, isUserAction /*: ?boolean*/, preventCollision /*: ?boolean*/, compactType /*: CompactType*/, cols /*: number*/, allowOverlap /*: ?boolean*/) /*: Layout*/{
|
|
442
|
-
// If this is static and not explicitly enabled as draggable,
|
|
443
|
-
// no move is possible, so we can short-circuit this immediately.
|
|
444
|
-
if (l.static && l.isDraggable !== true) return layout;
|
|
445
|
-
|
|
446
|
-
// Short-circuit if nothing to do.
|
|
447
|
-
if (l.y === y && l.x === x) return layout;
|
|
448
|
-
log("Moving element ".concat(l.i, " to [").concat(String(x), ",").concat(String(y), "] from [").concat(l.x, ",").concat(l.y, "]"));
|
|
449
|
-
const oldX = l.x;
|
|
450
|
-
const oldY = l.y;
|
|
451
|
-
|
|
452
|
-
// This is quite a bit faster than extending the object
|
|
453
|
-
if (typeof x === "number") l.x = x;
|
|
454
|
-
if (typeof y === "number") l.y = y;
|
|
455
|
-
l.moved = true;
|
|
456
|
-
|
|
457
|
-
// If this collides with anything, move it.
|
|
458
|
-
// When doing this comparison, we have to sort the items we compare with
|
|
459
|
-
// to ensure, in the case of multiple collisions, that we're getting the
|
|
460
|
-
// nearest collision.
|
|
461
|
-
let sorted = sortLayoutItems(layout, compactType);
|
|
462
|
-
const movingUp = compactType === "vertical" && typeof y === "number" ? oldY >= y : compactType === "horizontal" && typeof x === "number" ? oldX >= x : false;
|
|
463
|
-
// $FlowIgnore acceptable modification of read-only array as it was recently cloned
|
|
464
|
-
if (movingUp) sorted = sorted.reverse();
|
|
465
|
-
const collisions = getAllCollisions(sorted, l);
|
|
466
|
-
const hasCollisions = collisions.length > 0;
|
|
467
|
-
|
|
468
|
-
// We may have collisions. We can short-circuit if we've turned off collisions or
|
|
469
|
-
// allowed overlap.
|
|
470
|
-
if (hasCollisions && allowOverlap) {
|
|
471
|
-
// Easy, we don't need to resolve collisions. But we *did* change the layout,
|
|
472
|
-
// so clone it on the way out.
|
|
473
|
-
return cloneLayout(layout);
|
|
474
|
-
} else if (hasCollisions && preventCollision) {
|
|
475
|
-
// If we are preventing collision but not allowing overlap, we need to
|
|
476
|
-
// revert the position of this element so it goes to where it came from, rather
|
|
477
|
-
// than the user's desired location.
|
|
478
|
-
log("Collision prevented on ".concat(l.i, ", reverting."));
|
|
479
|
-
l.x = oldX;
|
|
480
|
-
l.y = oldY;
|
|
481
|
-
l.moved = false;
|
|
482
|
-
return layout; // did not change so don't clone
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
// Move each item that collides away from this element.
|
|
486
|
-
for (let i = 0, len = collisions.length; i < len; i++) {
|
|
487
|
-
const collision = collisions[i];
|
|
488
|
-
log("Resolving collision between ".concat(l.i, " at [").concat(l.x, ",").concat(l.y, "] and ").concat(collision.i, " at [").concat(collision.x, ",").concat(collision.y, "]"));
|
|
489
|
-
|
|
490
|
-
// Short circuit so we can't infinite loop
|
|
491
|
-
if (collision.moved) continue;
|
|
492
|
-
|
|
493
|
-
// Don't move static items - we have to move *this* element away
|
|
494
|
-
if (collision.static) {
|
|
495
|
-
layout = moveElementAwayFromCollision(layout, collision, l, isUserAction, compactType, cols);
|
|
496
|
-
} else {
|
|
497
|
-
layout = moveElementAwayFromCollision(layout, l, collision, isUserAction, compactType, cols);
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
return layout;
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
/**
|
|
504
|
-
* This is where the magic needs to happen - given a collision, move an element away from the collision.
|
|
505
|
-
* We attempt to move it up if there's room, otherwise it goes below.
|
|
506
|
-
*
|
|
507
|
-
* @param {Array} layout Full layout to modify.
|
|
508
|
-
* @param {LayoutItem} collidesWith Layout item we're colliding with.
|
|
509
|
-
* @param {LayoutItem} itemToMove Layout item we're moving.
|
|
510
|
-
*/
|
|
511
|
-
function moveElementAwayFromCollision(layout /*: Layout*/, collidesWith /*: LayoutItem*/, itemToMove /*: LayoutItem*/, isUserAction /*: ?boolean*/, compactType /*: CompactType*/, cols /*: number*/) /*: Layout*/{
|
|
512
|
-
const compactH = compactType === "horizontal";
|
|
513
|
-
// Compact vertically if not set to horizontal
|
|
514
|
-
const compactV = compactType === "vertical";
|
|
515
|
-
const preventCollision = collidesWith.static; // we're already colliding (not for static items)
|
|
516
|
-
|
|
517
|
-
// If there is enough space above the collision to put this element, move it there.
|
|
518
|
-
// We only do this on the main collision as this can get funky in cascades and cause
|
|
519
|
-
// unwanted swapping behavior.
|
|
520
|
-
if (isUserAction) {
|
|
521
|
-
// Reset isUserAction flag because we're not in the main collision anymore.
|
|
522
|
-
isUserAction = false;
|
|
523
|
-
|
|
524
|
-
// Make a mock item so we don't modify the item here, only modify in moveElement.
|
|
525
|
-
const fakeItem /*: LayoutItem*/ = {
|
|
526
|
-
x: compactH ? Math.max(collidesWith.x - itemToMove.w, 0) : itemToMove.x,
|
|
527
|
-
y: compactV ? Math.max(collidesWith.y - itemToMove.h, 0) : itemToMove.y,
|
|
528
|
-
w: itemToMove.w,
|
|
529
|
-
h: itemToMove.h,
|
|
530
|
-
i: "-1"
|
|
531
|
-
};
|
|
532
|
-
const firstCollision = getFirstCollision(layout, fakeItem);
|
|
533
|
-
const collisionNorth = firstCollision && firstCollision.y + firstCollision.h > collidesWith.y;
|
|
534
|
-
const collisionWest = firstCollision && collidesWith.x + collidesWith.w > firstCollision.x;
|
|
535
|
-
|
|
536
|
-
// No collision? If so, we can go up there; otherwise, we'll end up moving down as normal
|
|
537
|
-
if (!firstCollision) {
|
|
538
|
-
log("Doing reverse collision on ".concat(itemToMove.i, " up to [").concat(fakeItem.x, ",").concat(fakeItem.y, "]."));
|
|
539
|
-
return moveElement(layout, itemToMove, compactH ? fakeItem.x : undefined, compactV ? fakeItem.y : undefined, isUserAction, preventCollision, compactType, cols);
|
|
540
|
-
} else if (collisionNorth && compactV) {
|
|
541
|
-
return moveElement(layout, itemToMove, undefined, itemToMove.y + 1, isUserAction, preventCollision, compactType, cols);
|
|
542
|
-
} else if (collisionNorth && compactType == null) {
|
|
543
|
-
collidesWith.y = itemToMove.y;
|
|
544
|
-
itemToMove.y = itemToMove.y + itemToMove.h;
|
|
545
|
-
return layout;
|
|
546
|
-
} else if (collisionWest && compactH) {
|
|
547
|
-
return moveElement(layout, collidesWith, itemToMove.x, undefined, isUserAction, preventCollision, compactType, cols);
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
const newX = compactH ? itemToMove.x + 1 : undefined;
|
|
551
|
-
const newY = compactV ? itemToMove.y + 1 : undefined;
|
|
552
|
-
if (newX == null && newY == null) {
|
|
553
|
-
return layout;
|
|
554
|
-
}
|
|
555
|
-
return moveElement(layout, itemToMove, compactH ? itemToMove.x + 1 : undefined, compactV ? itemToMove.y + 1 : undefined, isUserAction, preventCollision, compactType, cols);
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
/**
|
|
559
|
-
* Helper to convert a number to a percentage string.
|
|
560
|
-
*
|
|
561
|
-
* @param {Number} num Any number
|
|
562
|
-
* @return {String} That number as a percentage.
|
|
563
|
-
*/
|
|
564
|
-
function perc(num /*: number*/) /*: string*/{
|
|
565
|
-
return num * 100 + "%";
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
/**
|
|
569
|
-
* Helper functions to constrain dimensions of a GridItem
|
|
570
|
-
*/
|
|
571
|
-
const constrainWidth = (left /*: number*/, currentWidth /*: number*/, newWidth /*: number*/, containerWidth /*: number*/) => {
|
|
572
|
-
return left + newWidth > containerWidth ? currentWidth : newWidth;
|
|
573
|
-
};
|
|
574
|
-
const constrainHeight = (top /*: number*/, currentHeight /*: number*/, newHeight /*: number*/) => {
|
|
575
|
-
return top < 0 ? currentHeight : newHeight;
|
|
576
|
-
};
|
|
577
|
-
const constrainLeft = (left /*: number*/) => Math.max(0, left);
|
|
578
|
-
const constrainTop = (top /*: number*/) => Math.max(0, top);
|
|
579
|
-
const resizeNorth = (currentSize, _ref, _containerWidth) => {
|
|
580
|
-
let {
|
|
581
|
-
left,
|
|
582
|
-
height,
|
|
583
|
-
width
|
|
584
|
-
} = _ref;
|
|
585
|
-
const top = currentSize.top - (height - currentSize.height);
|
|
586
|
-
return {
|
|
587
|
-
left,
|
|
588
|
-
width,
|
|
589
|
-
height: constrainHeight(top, currentSize.height, height),
|
|
590
|
-
top: constrainTop(top)
|
|
591
|
-
};
|
|
592
|
-
};
|
|
593
|
-
const resizeEast = (currentSize, _ref2, containerWidth) => {
|
|
594
|
-
let {
|
|
595
|
-
top,
|
|
596
|
-
left,
|
|
597
|
-
height,
|
|
598
|
-
width
|
|
599
|
-
} = _ref2;
|
|
600
|
-
return {
|
|
601
|
-
top,
|
|
602
|
-
height,
|
|
603
|
-
width: constrainWidth(currentSize.left, currentSize.width, width, containerWidth),
|
|
604
|
-
left: constrainLeft(left)
|
|
605
|
-
};
|
|
606
|
-
};
|
|
607
|
-
const resizeWest = (currentSize, _ref3, containerWidth) => {
|
|
608
|
-
let {
|
|
609
|
-
top,
|
|
610
|
-
height,
|
|
611
|
-
width
|
|
612
|
-
} = _ref3;
|
|
613
|
-
const left = currentSize.left - (width - currentSize.width);
|
|
614
|
-
return {
|
|
615
|
-
height,
|
|
616
|
-
width: left < 0 ? currentSize.width : constrainWidth(currentSize.left, currentSize.width, width, containerWidth),
|
|
617
|
-
top: constrainTop(top),
|
|
618
|
-
left: constrainLeft(left)
|
|
619
|
-
};
|
|
620
|
-
};
|
|
621
|
-
const resizeSouth = (currentSize, _ref4, containerWidth) => {
|
|
622
|
-
let {
|
|
623
|
-
top,
|
|
624
|
-
left,
|
|
625
|
-
height,
|
|
626
|
-
width
|
|
627
|
-
} = _ref4;
|
|
628
|
-
return {
|
|
629
|
-
width,
|
|
630
|
-
left,
|
|
631
|
-
height: constrainHeight(top, currentSize.height, height),
|
|
632
|
-
top: constrainTop(top)
|
|
633
|
-
};
|
|
634
|
-
};
|
|
635
|
-
const resizeNorthEast = function () {
|
|
636
|
-
return resizeNorth(arguments.length <= 0 ? undefined : arguments[0], resizeEast(...arguments), arguments.length <= 2 ? undefined : arguments[2]);
|
|
637
|
-
};
|
|
638
|
-
const resizeNorthWest = function () {
|
|
639
|
-
return resizeNorth(arguments.length <= 0 ? undefined : arguments[0], resizeWest(...arguments), arguments.length <= 2 ? undefined : arguments[2]);
|
|
640
|
-
};
|
|
641
|
-
const resizeSouthEast = function () {
|
|
642
|
-
return resizeSouth(arguments.length <= 0 ? undefined : arguments[0], resizeEast(...arguments), arguments.length <= 2 ? undefined : arguments[2]);
|
|
643
|
-
};
|
|
644
|
-
const resizeSouthWest = function () {
|
|
645
|
-
return resizeSouth(arguments.length <= 0 ? undefined : arguments[0], resizeWest(...arguments), arguments.length <= 2 ? undefined : arguments[2]);
|
|
646
|
-
};
|
|
647
|
-
const ordinalResizeHandlerMap = {
|
|
648
|
-
n: resizeNorth,
|
|
649
|
-
ne: resizeNorthEast,
|
|
650
|
-
e: resizeEast,
|
|
651
|
-
se: resizeSouthEast,
|
|
652
|
-
s: resizeSouth,
|
|
653
|
-
sw: resizeSouthWest,
|
|
654
|
-
w: resizeWest,
|
|
655
|
-
nw: resizeNorthWest
|
|
656
|
-
};
|
|
657
|
-
|
|
658
|
-
/**
|
|
659
|
-
* Helper for clamping width and position when resizing an item.
|
|
660
|
-
*/
|
|
661
|
-
function resizeItemInDirection(direction /*: ResizeHandleAxis*/, currentSize /*: Position*/, newSize /*: Position*/, containerWidth /*: number*/) /*: Position*/{
|
|
662
|
-
const ordinalHandler = ordinalResizeHandlerMap[direction];
|
|
663
|
-
// Shouldn't be possible given types; that said, don't fail hard
|
|
664
|
-
if (!ordinalHandler) return newSize;
|
|
665
|
-
return ordinalHandler(currentSize, _objectSpread(_objectSpread({}, currentSize), newSize), containerWidth);
|
|
666
|
-
}
|
|
667
|
-
function setTransform(_ref5 /*:: */) /*: Object*/{
|
|
668
|
-
let {
|
|
669
|
-
top,
|
|
670
|
-
left,
|
|
671
|
-
width,
|
|
672
|
-
height
|
|
673
|
-
} /*: Position*/ = _ref5 /*: Position*/;
|
|
674
|
-
// Replace unitless items with px
|
|
675
|
-
const translate = "translate(".concat(left, "px,").concat(top, "px)");
|
|
676
|
-
return {
|
|
677
|
-
transform: translate,
|
|
678
|
-
WebkitTransform: translate,
|
|
679
|
-
MozTransform: translate,
|
|
680
|
-
msTransform: translate,
|
|
681
|
-
OTransform: translate,
|
|
682
|
-
width: "".concat(width, "px"),
|
|
683
|
-
height: "".concat(height, "px"),
|
|
684
|
-
position: "absolute"
|
|
685
|
-
};
|
|
686
|
-
}
|
|
687
|
-
function setTopLeft(_ref6 /*:: */) /*: Object*/{
|
|
688
|
-
let {
|
|
689
|
-
top,
|
|
690
|
-
left,
|
|
691
|
-
width,
|
|
692
|
-
height
|
|
693
|
-
} /*: Position*/ = _ref6 /*: Position*/;
|
|
694
|
-
return {
|
|
695
|
-
top: "".concat(top, "px"),
|
|
696
|
-
left: "".concat(left, "px"),
|
|
697
|
-
width: "".concat(width, "px"),
|
|
698
|
-
height: "".concat(height, "px"),
|
|
699
|
-
position: "absolute"
|
|
700
|
-
};
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
/**
|
|
704
|
-
* Get layout items sorted from top left to right and down.
|
|
705
|
-
*
|
|
706
|
-
* @return {Array} Array of layout objects.
|
|
707
|
-
* @return {Array} Layout, sorted static items first.
|
|
708
|
-
*/
|
|
709
|
-
function sortLayoutItems(layout /*: Layout*/, compactType /*: CompactType*/) /*: Layout*/{
|
|
710
|
-
if (compactType === "horizontal") return sortLayoutItemsByColRow(layout);
|
|
711
|
-
if (compactType === "vertical") return sortLayoutItemsByRowCol(layout);else return layout;
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
/**
|
|
715
|
-
* Sort layout items by row ascending and column ascending.
|
|
716
|
-
*
|
|
717
|
-
* Does not modify Layout.
|
|
718
|
-
*/
|
|
719
|
-
function sortLayoutItemsByRowCol(layout /*: Layout*/) /*: Layout*/{
|
|
720
|
-
// Slice to clone array as sort modifies
|
|
721
|
-
return layout.slice(0).sort(function (a, b) {
|
|
722
|
-
if (a.y > b.y || a.y === b.y && a.x > b.x) {
|
|
723
|
-
return 1;
|
|
724
|
-
} else if (a.y === b.y && a.x === b.x) {
|
|
725
|
-
// Without this, we can get different sort results in IE vs. Chrome/FF
|
|
726
|
-
return 0;
|
|
727
|
-
}
|
|
728
|
-
return -1;
|
|
729
|
-
});
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
/**
|
|
733
|
-
* Sort layout items by column ascending then row ascending.
|
|
734
|
-
*
|
|
735
|
-
* Does not modify Layout.
|
|
736
|
-
*/
|
|
737
|
-
function sortLayoutItemsByColRow(layout /*: Layout*/) /*: Layout*/{
|
|
738
|
-
return layout.slice(0).sort(function (a, b) {
|
|
739
|
-
if (a.x > b.x || a.x === b.x && a.y > b.y) {
|
|
740
|
-
return 1;
|
|
741
|
-
}
|
|
742
|
-
return -1;
|
|
743
|
-
});
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
/**
|
|
747
|
-
* Generate a layout using the initialLayout and children as a template.
|
|
748
|
-
* Missing entries will be added, extraneous ones will be truncated.
|
|
749
|
-
*
|
|
750
|
-
* Does not modify initialLayout.
|
|
751
|
-
*
|
|
752
|
-
* @param {Array} initialLayout Layout passed in through props.
|
|
753
|
-
* @param {String} breakpoint Current responsive breakpoint.
|
|
754
|
-
* @param {?String} compact Compaction option.
|
|
755
|
-
* @return {Array} Working layout.
|
|
756
|
-
*/
|
|
757
|
-
function synchronizeLayoutWithChildren(initialLayout /*: Layout*/, children /*: ReactChildren*/, cols /*: number*/, compactType /*: CompactType*/, allowOverlap /*: ?boolean*/) /*: Layout*/{
|
|
758
|
-
initialLayout = initialLayout || [];
|
|
759
|
-
|
|
760
|
-
// Generate one layout item per child.
|
|
761
|
-
const layout /*: LayoutItem[]*/ = [];
|
|
762
|
-
_react.default.Children.forEach(children, (child /*: ReactElement<any>*/) => {
|
|
763
|
-
// Child may not exist
|
|
764
|
-
if ((child === null || child === void 0 ? void 0 : child.key) == null) return;
|
|
765
|
-
const exists = getLayoutItem(initialLayout, String(child.key));
|
|
766
|
-
const g = child.props["data-grid"];
|
|
767
|
-
// Don't overwrite the layout item if it's already in the initial layout.
|
|
768
|
-
// If it has a `data-grid` property, prefer that over what's in the layout.
|
|
769
|
-
if (exists && g == null) {
|
|
770
|
-
layout.push(cloneLayoutItem(exists));
|
|
771
|
-
} else {
|
|
772
|
-
// Hey, this item has a data-grid property, use it.
|
|
773
|
-
if (g) {
|
|
774
|
-
if (!isProduction) {
|
|
775
|
-
validateLayout([g], "ReactGridLayout.children");
|
|
776
|
-
}
|
|
777
|
-
// FIXME clone not really necessary here
|
|
778
|
-
layout.push(cloneLayoutItem(_objectSpread(_objectSpread({}, g), {}, {
|
|
779
|
-
i: child.key
|
|
780
|
-
})));
|
|
781
|
-
} else {
|
|
782
|
-
// Nothing provided: ensure this is added to the bottom
|
|
783
|
-
// FIXME clone not really necessary here
|
|
784
|
-
layout.push(cloneLayoutItem({
|
|
785
|
-
w: 1,
|
|
786
|
-
h: 1,
|
|
787
|
-
x: 0,
|
|
788
|
-
y: bottom(layout),
|
|
789
|
-
i: String(child.key)
|
|
790
|
-
}));
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
});
|
|
794
|
-
|
|
795
|
-
// Correct the layout.
|
|
796
|
-
const correctedLayout = correctBounds(layout, {
|
|
797
|
-
cols: cols
|
|
798
|
-
});
|
|
799
|
-
return allowOverlap ? correctedLayout : compact(correctedLayout, compactType, cols);
|
|
800
|
-
}
|
|
801
|
-
|
|
802
|
-
/**
|
|
803
|
-
* Validate a layout. Throws errors.
|
|
804
|
-
*
|
|
805
|
-
* @param {Array} layout Array of layout items.
|
|
806
|
-
* @param {String} [contextName] Context name for errors.
|
|
807
|
-
* @throw {Error} Validation error.
|
|
808
|
-
*/
|
|
809
|
-
function validateLayout(layout /*: Layout*/) /*: void*/{
|
|
810
|
-
let contextName /*: string*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "Layout";
|
|
811
|
-
const subProps = ["x", "y", "w", "h"];
|
|
812
|
-
if (!Array.isArray(layout)) throw new Error(contextName + " must be an array!");
|
|
813
|
-
for (let i = 0, len = layout.length; i < len; i++) {
|
|
814
|
-
const item = layout[i];
|
|
815
|
-
for (let j = 0; j < subProps.length; j++) {
|
|
816
|
-
const key = subProps[j];
|
|
817
|
-
const value = item[key];
|
|
818
|
-
if (typeof value !== "number" || Number.isNaN(value)) {
|
|
819
|
-
throw new Error("ReactGridLayout: ".concat(contextName, "[").concat(i, "].").concat(key, " must be a number! Received: ").concat(value, " (").concat(typeof value, ")"));
|
|
820
|
-
}
|
|
821
|
-
}
|
|
822
|
-
if (typeof item.i !== "undefined" && typeof item.i !== "string") {
|
|
823
|
-
throw new Error("ReactGridLayout: ".concat(contextName, "[").concat(i, "].i must be a string! Received: ").concat(item.i, " (").concat(typeof item.i, ")"));
|
|
824
|
-
}
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
|
|
828
|
-
// Legacy support for verticalCompact: false
|
|
829
|
-
function compactType(props /*: ?{ verticalCompact: boolean, compactType: CompactType }*/) /*: CompactType*/{
|
|
830
|
-
const {
|
|
831
|
-
verticalCompact,
|
|
832
|
-
compactType
|
|
833
|
-
} = props || {};
|
|
834
|
-
return verticalCompact === false ? null : compactType;
|
|
835
|
-
}
|
|
836
|
-
function log() {
|
|
837
|
-
if (!DEBUG) return;
|
|
838
|
-
// eslint-disable-next-line no-console
|
|
839
|
-
console.log(...arguments);
|
|
840
|
-
}
|
|
841
|
-
const noop = () => {};
|
|
842
|
-
exports.noop = noop;
|