react-grid-layout 0.16.4 → 0.17.1

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/build/GridItem.js CHANGED
@@ -1,16 +1,15 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
4
7
 
5
- var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8
+ var _react = _interopRequireDefault(require("react"));
6
9
 
7
- var _react = require("react");
10
+ var _reactDom = _interopRequireDefault(require("react-dom"));
8
11
 
9
- var _react2 = _interopRequireDefault(_react);
10
-
11
- var _propTypes = require("prop-types");
12
-
13
- var _propTypes2 = _interopRequireDefault(_propTypes);
12
+ var _propTypes = _interopRequireDefault(require("prop-types"));
14
13
 
15
14
  var _reactDraggable = require("react-draggable");
16
15
 
@@ -18,489 +17,672 @@ var _reactResizable = require("react-resizable");
18
17
 
19
18
  var _utils = require("./utils");
20
19
 
21
- var _classnames = require("classnames");
22
-
23
- var _classnames2 = _interopRequireDefault(_classnames);
20
+ var _classnames = _interopRequireDefault(require("classnames"));
24
21
 
25
22
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
23
 
24
+ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
25
+
26
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
27
+
28
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
29
+
27
30
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
28
31
 
29
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
32
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
33
+
34
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
35
+
36
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
37
+
38
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
39
+
40
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
30
41
 
31
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
42
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
43
+
44
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
45
+
46
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
32
47
 
33
48
  /**
34
49
  * An individual item within a ReactGridLayout.
35
50
  */
36
- var GridItem = function (_React$Component) {
51
+ var GridItem =
52
+ /*#__PURE__*/
53
+ function (_React$Component) {
37
54
  _inherits(GridItem, _React$Component);
38
55
 
39
56
  function GridItem() {
40
- var _temp, _this, _ret;
57
+ var _getPrototypeOf2;
58
+
59
+ var _this;
41
60
 
42
61
  _classCallCheck(this, GridItem);
43
62
 
44
- for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
63
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
45
64
  args[_key] = arguments[_key];
46
65
  }
47
66
 
48
- return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {
67
+ _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(GridItem)).call.apply(_getPrototypeOf2, [this].concat(args)));
68
+
69
+ _defineProperty(_assertThisInitialized(_this), "state", {
49
70
  resizing: null,
50
71
  dragging: null,
51
72
  className: ""
52
- }, _temp), _possibleConstructorReturn(_this, _ret);
53
- }
54
-
55
- // Helper for generating column width
56
- GridItem.prototype.calcColWidth = function calcColWidth() {
57
- var _props = this.props,
58
- margin = _props.margin,
59
- containerPadding = _props.containerPadding,
60
- containerWidth = _props.containerWidth,
61
- cols = _props.cols;
62
-
63
- return (containerWidth - margin[0] * (cols - 1) - containerPadding[0] * 2) / cols;
64
- };
65
-
66
- /**
67
- * Return position on the page given an x, y, w, h.
68
- * left, top, width, height are all in pixels.
69
- * @param {Number} x X coordinate in grid units.
70
- * @param {Number} y Y coordinate in grid units.
71
- * @param {Number} w W coordinate in grid units.
72
- * @param {Number} h H coordinate in grid units.
73
- * @return {Object} Object containing coords.
74
- */
75
-
76
-
77
- GridItem.prototype.calcPosition = function calcPosition(x, y, w, h, state) {
78
- var _props2 = this.props,
79
- margin = _props2.margin,
80
- containerPadding = _props2.containerPadding,
81
- rowHeight = _props2.rowHeight;
82
-
83
- var colWidth = this.calcColWidth();
84
-
85
- var out = {
86
- left: Math.round((colWidth + margin[0]) * x + containerPadding[0]),
87
- top: Math.round((rowHeight + margin[1]) * y + containerPadding[1]),
88
- // 0 * Infinity === NaN, which causes problems with resize constraints;
89
- // Fix this if it occurs.
90
- // Note we do it here rather than later because Math.round(Infinity) causes deopt
91
- width: w === Infinity ? w : Math.round(colWidth * w + Math.max(0, w - 1) * margin[0]),
92
- height: h === Infinity ? h : Math.round(rowHeight * h + Math.max(0, h - 1) * margin[1])
93
- };
94
-
95
- if (state && state.resizing) {
96
- out.width = Math.round(state.resizing.width);
97
- out.height = Math.round(state.resizing.height);
98
- }
99
-
100
- if (state && state.dragging) {
101
- out.top = Math.round(state.dragging.top);
102
- out.left = Math.round(state.dragging.left);
103
- }
104
-
105
- return out;
106
- };
107
-
108
- /**
109
- * Translate x and y coordinates from pixels to grid units.
110
- * @param {Number} top Top position (relative to parent) in pixels.
111
- * @param {Number} left Left position (relative to parent) in pixels.
112
- * @return {Object} x and y in grid units.
113
- */
114
-
115
-
116
- GridItem.prototype.calcXY = function calcXY(top, left) {
117
- var _props3 = this.props,
118
- margin = _props3.margin,
119
- cols = _props3.cols,
120
- rowHeight = _props3.rowHeight,
121
- w = _props3.w,
122
- h = _props3.h,
123
- maxRows = _props3.maxRows;
124
-
125
- var colWidth = this.calcColWidth();
126
-
127
- // left = colWidth * x + margin * (x + 1)
128
- // l = cx + m(x+1)
129
- // l = cx + mx + m
130
- // l - m = cx + mx
131
- // l - m = x(c + m)
132
- // (l - m) / (c + m) = x
133
- // x = (left - margin) / (coldWidth + margin)
134
- var x = Math.round((left - margin[0]) / (colWidth + margin[0]));
135
- var y = Math.round((top - margin[1]) / (rowHeight + margin[1]));
136
-
137
- // Capping
138
- x = Math.max(Math.min(x, cols - w), 0);
139
- y = Math.max(Math.min(y, maxRows - h), 0);
140
-
141
- return { x: x, y: y };
142
- };
143
-
144
- /**
145
- * Given a height and width in pixel values, calculate grid units.
146
- * @param {Number} height Height in pixels.
147
- * @param {Number} width Width in pixels.
148
- * @return {Object} w, h as grid units.
149
- */
150
-
151
-
152
- GridItem.prototype.calcWH = function calcWH(_ref) {
153
- var height = _ref.height,
154
- width = _ref.width;
155
- var _props4 = this.props,
156
- margin = _props4.margin,
157
- maxRows = _props4.maxRows,
158
- cols = _props4.cols,
159
- rowHeight = _props4.rowHeight,
160
- x = _props4.x,
161
- y = _props4.y;
162
-
163
- var colWidth = this.calcColWidth();
164
-
165
- // width = colWidth * w - (margin * (w - 1))
166
- // ...
167
- // w = (width + margin) / (colWidth + margin)
168
- var w = Math.round((width + margin[0]) / (colWidth + margin[0]));
169
- var h = Math.round((height + margin[1]) / (rowHeight + margin[1]));
170
-
171
- // Capping
172
- w = Math.max(Math.min(w, cols - x), 0);
173
- h = Math.max(Math.min(h, maxRows - y), 0);
174
- return { w: w, h: h };
175
- };
176
-
177
- /**
178
- * This is where we set the grid item's absolute placement. It gets a little tricky because we want to do it
179
- * well when server rendering, and the only way to do that properly is to use percentage width/left because
180
- * we don't know exactly what the browser viewport is.
181
- * Unfortunately, CSS Transforms, which are great for performance, break in this instance because a percentage
182
- * left is relative to the item itself, not its container! So we cannot use them on the server rendering pass.
183
- *
184
- * @param {Object} pos Position object with width, height, left, top.
185
- * @return {Object} Style object.
186
- */
187
-
188
-
189
- GridItem.prototype.createStyle = function createStyle(pos) {
190
- var _props5 = this.props,
191
- usePercentages = _props5.usePercentages,
192
- containerWidth = _props5.containerWidth,
193
- useCSSTransforms = _props5.useCSSTransforms;
194
-
195
-
196
- var style = void 0;
197
- // CSS Transforms support (default)
198
- if (useCSSTransforms) {
199
- style = (0, _utils.setTransform)(pos);
200
- } else {
201
- // top,left (slow)
202
- style = (0, _utils.setTopLeft)(pos);
203
-
204
- // This is used for server rendering.
205
- if (usePercentages) {
206
- style.left = (0, _utils.perc)(pos.left / containerWidth);
207
- style.width = (0, _utils.perc)(pos.width / containerWidth);
208
- }
209
- }
210
-
211
- return style;
212
- };
213
-
214
- /**
215
- * Mix a Draggable instance into a child.
216
- * @param {Element} child Child element.
217
- * @return {Element} Child wrapped in Draggable.
218
- */
73
+ });
219
74
 
75
+ _defineProperty(_assertThisInitialized(_this), "currentNode", void 0);
76
+
77
+ _defineProperty(_assertThisInitialized(_this), "onDragStart", function (e
78
+ /*: Event*/
79
+ , _ref) {
80
+ var node = _ref.node;
81
+ if (!_this.props.onDragStart) return;
82
+ var newPosition
83
+ /*: PartialPosition*/
84
+ = {
85
+ top: 0,
86
+ left: 0
87
+ }; // TODO: this wont work on nested parents
88
+
89
+ var offsetParent = node.offsetParent;
90
+ if (!offsetParent) return;
91
+ var parentRect = offsetParent.getBoundingClientRect();
92
+ var clientRect = node.getBoundingClientRect();
93
+ var cLeft = clientRect.left / _this.props.transformScale;
94
+ var pLeft = parentRect.left / _this.props.transformScale;
95
+ var cTop = clientRect.top / _this.props.transformScale;
96
+ var pTop = parentRect.top / _this.props.transformScale;
97
+ newPosition.left = cLeft - pLeft + offsetParent.scrollLeft;
98
+ newPosition.top = cTop - pTop + offsetParent.scrollTop;
99
+
100
+ _this.setState({
101
+ dragging: newPosition
102
+ });
103
+
104
+ var _this$calcXY = _this.calcXY(newPosition.top, newPosition.left),
105
+ x = _this$calcXY.x,
106
+ y = _this$calcXY.y;
107
+
108
+ return _this.props.onDragStart && _this.props.onDragStart.call(_assertThisInitialized(_this), _this.props.i, x, y, {
109
+ e: e,
110
+ node: node,
111
+ newPosition: newPosition
112
+ });
113
+ });
220
114
 
221
- GridItem.prototype.mixinDraggable = function mixinDraggable(child) {
222
- return _react2.default.createElement(
223
- _reactDraggable.DraggableCore,
224
- {
225
- onStart: this.onDragHandler("onDragStart"),
226
- onDrag: this.onDragHandler("onDrag"),
227
- onStop: this.onDragHandler("onDragStop"),
228
- handle: this.props.handle,
229
- cancel: ".react-resizable-handle" + (this.props.cancel ? "," + this.props.cancel : "")
230
- },
231
- child
232
- );
233
- };
234
-
235
- /**
236
- * Mix a Resizable instance into a child.
237
- * @param {Element} child Child element.
238
- * @param {Object} position Position object (pixel values)
239
- * @return {Element} Child wrapped in Resizable.
240
- */
241
-
242
-
243
- GridItem.prototype.mixinResizable = function mixinResizable(child, position) {
244
- var _props6 = this.props,
245
- cols = _props6.cols,
246
- x = _props6.x,
247
- minW = _props6.minW,
248
- minH = _props6.minH,
249
- maxW = _props6.maxW,
250
- maxH = _props6.maxH;
251
-
252
- // This is the max possible width - doesn't go to infinity because of the width of the window
253
-
254
- var maxWidth = this.calcPosition(0, 0, cols - x, 0).width;
255
-
256
- // Calculate min/max constraints using our min & maxes
257
- var mins = this.calcPosition(0, 0, minW, minH);
258
- var maxes = this.calcPosition(0, 0, maxW, maxH);
259
- var minConstraints = [mins.width, mins.height];
260
- var maxConstraints = [Math.min(maxes.width, maxWidth), Math.min(maxes.height, Infinity)];
261
- return _react2.default.createElement(
262
- _reactResizable.Resizable,
263
- {
264
- width: position.width,
265
- height: position.height,
266
- minConstraints: minConstraints,
267
- maxConstraints: maxConstraints,
268
- onResizeStop: this.onResizeHandler("onResizeStop"),
269
- onResizeStart: this.onResizeHandler("onResizeStart"),
270
- onResize: this.onResizeHandler("onResize")
271
- },
272
- child
273
- );
274
- };
275
-
276
- /**
277
- * Wrapper around drag events to provide more useful data.
278
- * All drag events call the function with the given handler name,
279
- * with the signature (index, x, y).
280
- *
281
- * @param {String} handlerName Handler name to wrap.
282
- * @return {Function} Handler function.
283
- */
284
-
285
-
286
- GridItem.prototype.onDragHandler = function onDragHandler(handlerName) {
287
- var _this2 = this;
288
-
289
- return function (e, _ref2) {
115
+ _defineProperty(_assertThisInitialized(_this), "onDrag", function (e
116
+ /*: Event*/
117
+ , _ref2) {
290
118
  var node = _ref2.node,
291
119
  deltaX = _ref2.deltaX,
292
120
  deltaY = _ref2.deltaY;
121
+ if (!_this.props.onDrag) return;
122
+ var newPosition
123
+ /*: PartialPosition*/
124
+ = {
125
+ top: 0,
126
+ left: 0
127
+ };
128
+ if (!_this.state.dragging) throw new Error("onDrag called before onDragStart.");
129
+ newPosition.left = _this.state.dragging.left + deltaX;
130
+ newPosition.top = _this.state.dragging.top + deltaY;
131
+
132
+ _this.setState({
133
+ dragging: newPosition
134
+ });
135
+
136
+ var _this$calcXY2 = _this.calcXY(newPosition.top, newPosition.left),
137
+ x = _this$calcXY2.x,
138
+ y = _this$calcXY2.y;
139
+
140
+ return _this.props.onDrag && _this.props.onDrag.call(_assertThisInitialized(_this), _this.props.i, x, y, {
141
+ e: e,
142
+ node: node,
143
+ newPosition: newPosition
144
+ });
145
+ });
293
146
 
294
- var handler = _this2.props[handlerName];
295
- if (!handler) return;
147
+ _defineProperty(_assertThisInitialized(_this), "onDragStop", function (e
148
+ /*: Event*/
149
+ , _ref3) {
150
+ var node = _ref3.node;
151
+ if (!_this.props.onDragStop) return;
152
+ var newPosition
153
+ /*: PartialPosition*/
154
+ = {
155
+ top: 0,
156
+ left: 0
157
+ };
158
+ if (!_this.state.dragging) throw new Error("onDragEnd called before onDragStart.");
159
+ newPosition.left = _this.state.dragging.left;
160
+ newPosition.top = _this.state.dragging.top;
161
+
162
+ _this.setState({
163
+ dragging: null
164
+ });
165
+
166
+ var _this$calcXY3 = _this.calcXY(newPosition.top, newPosition.left),
167
+ x = _this$calcXY3.x,
168
+ y = _this$calcXY3.y;
169
+
170
+ return _this.props.onDragStop && _this.props.onDragStop.call(_assertThisInitialized(_this), _this.props.i, x, y, {
171
+ e: e,
172
+ node: node,
173
+ newPosition: newPosition
174
+ });
175
+ });
296
176
 
297
- var newPosition = { top: 0, left: 0 };
298
-
299
- // Get new XY
300
- switch (handlerName) {
301
- case "onDragStart":
302
- {
303
- // TODO: this wont work on nested parents
304
- var offsetParent = node.offsetParent;
305
-
306
- if (!offsetParent) return;
307
- var parentRect = offsetParent.getBoundingClientRect();
308
- var clientRect = node.getBoundingClientRect();
309
- newPosition.left = clientRect.left - parentRect.left + offsetParent.scrollLeft;
310
- newPosition.top = clientRect.top - parentRect.top + offsetParent.scrollTop;
311
- _this2.setState({ dragging: newPosition });
312
- break;
313
- }
314
- case "onDrag":
315
- if (!_this2.state.dragging) throw new Error("onDrag called before onDragStart.");
316
- newPosition.left = _this2.state.dragging.left + deltaX;
317
- newPosition.top = _this2.state.dragging.top + deltaY;
318
- _this2.setState({ dragging: newPosition });
319
- break;
320
- case "onDragStop":
321
- if (!_this2.state.dragging) throw new Error("onDragEnd called before onDragStart.");
322
- newPosition.left = _this2.state.dragging.left;
323
- newPosition.top = _this2.state.dragging.top;
324
- _this2.setState({ dragging: null });
325
- break;
326
- default:
327
- throw new Error("onDragHandler called with unrecognized handlerName: " + handlerName);
328
- }
177
+ _defineProperty(_assertThisInitialized(_this), "onResizeStop", function (e
178
+ /*: Event*/
179
+ , callbackData
180
+ /*: { node: HTMLElement, size: Position }*/
181
+ ) {
182
+ _this.onResizeHandler(e, callbackData, "onResizeStop");
183
+ });
329
184
 
330
- var _calcXY = _this2.calcXY(newPosition.top, newPosition.left),
331
- x = _calcXY.x,
332
- y = _calcXY.y;
185
+ _defineProperty(_assertThisInitialized(_this), "onResizeStart", function (e
186
+ /*: Event*/
187
+ , callbackData
188
+ /*: { node: HTMLElement, size: Position }*/
189
+ ) {
190
+ _this.onResizeHandler(e, callbackData, "onResizeStart");
191
+ });
333
192
 
334
- return handler.call(_this2, _this2.props.i, x, y, { e: e, node: node, newPosition: newPosition });
335
- };
336
- };
193
+ _defineProperty(_assertThisInitialized(_this), "onResize", function (e
194
+ /*: Event*/
195
+ , callbackData
196
+ /*: { node: HTMLElement, size: Position }*/
197
+ ) {
198
+ _this.onResizeHandler(e, callbackData, "onResize");
199
+ });
337
200
 
338
- /**
339
- * Wrapper around drag events to provide more useful data.
340
- * All drag events call the function with the given handler name,
341
- * with the signature (index, x, y).
342
- *
343
- * @param {String} handlerName Handler name to wrap.
344
- * @return {Function} Handler function.
345
- */
201
+ return _this;
202
+ }
346
203
 
204
+ _createClass(GridItem, [{
205
+ key: "componentDidUpdate",
206
+ value: function componentDidUpdate(prevProps
207
+ /*: Props*/
208
+ ) {
209
+ if (this.props.droppingPosition && prevProps.droppingPosition) {
210
+ this.moveDroppingItem(prevProps);
211
+ }
212
+ }
213
+ }, {
214
+ key: "moveDroppingItem",
215
+ value: function moveDroppingItem(prevProps
216
+ /*: Props*/
217
+ ) {
218
+ var droppingPosition = this.props.droppingPosition;
219
+ var dragging = this.state.dragging;
220
+
221
+ if (!droppingPosition || !prevProps.droppingPosition) {
222
+ return;
223
+ }
347
224
 
348
- GridItem.prototype.onResizeHandler = function onResizeHandler(handlerName) {
349
- var _this3 = this;
225
+ if (!this.currentNode) {
226
+ // eslint-disable-next-line react/no-find-dom-node
227
+ this.currentNode = ((_reactDom.default.findDOMNode(this)
228
+ /*: any*/
229
+ )
230
+ /*: HTMLElement*/
231
+ );
232
+ }
350
233
 
351
- return function (e, _ref3) {
352
- var node = _ref3.node,
353
- size = _ref3.size;
234
+ var shouldDrag = dragging && droppingPosition.x !== prevProps.droppingPosition.x || droppingPosition.y !== prevProps.droppingPosition.y;
235
+
236
+ if (!dragging) {
237
+ this.onDragStart(droppingPosition.e, {
238
+ node: this.currentNode,
239
+ deltaX: droppingPosition.x,
240
+ deltaY: droppingPosition.y
241
+ });
242
+ } else if (shouldDrag) {
243
+ var deltaX = droppingPosition.x - dragging.left;
244
+ var deltaY = droppingPosition.y - dragging.top;
245
+ this.onDrag(droppingPosition.e, {
246
+ node: this.currentNode,
247
+ deltaX: deltaX,
248
+ deltaY: deltaY
249
+ });
250
+ }
251
+ } // Helper for generating column width
252
+
253
+ }, {
254
+ key: "calcColWidth",
255
+ value: function calcColWidth()
256
+ /*: number*/
257
+ {
258
+ var _this$props = this.props,
259
+ margin = _this$props.margin,
260
+ containerPadding = _this$props.containerPadding,
261
+ containerWidth = _this$props.containerWidth,
262
+ cols = _this$props.cols;
263
+ return (containerWidth - margin[0] * (cols - 1) - containerPadding[0] * 2) / cols;
264
+ }
265
+ /**
266
+ * Return position on the page given an x, y, w, h.
267
+ * left, top, width, height are all in pixels.
268
+ * @param {Number} x X coordinate in grid units.
269
+ * @param {Number} y Y coordinate in grid units.
270
+ * @param {Number} w W coordinate in grid units.
271
+ * @param {Number} h H coordinate in grid units.
272
+ * @return {Object} Object containing coords.
273
+ */
274
+
275
+ }, {
276
+ key: "calcPosition",
277
+ value: function calcPosition(x
278
+ /*: number*/
279
+ , y
280
+ /*: number*/
281
+ , w
282
+ /*: number*/
283
+ , h
284
+ /*: number*/
285
+ , state
286
+ /*: ?Object*/
287
+ )
288
+ /*: Position*/
289
+ {
290
+ var _this$props2 = this.props,
291
+ margin = _this$props2.margin,
292
+ containerPadding = _this$props2.containerPadding,
293
+ rowHeight = _this$props2.rowHeight;
294
+ var colWidth = this.calcColWidth();
295
+ var out = {}; // If resizing, use the exact width and height as returned from resizing callbacks.
296
+
297
+ if (state && state.resizing) {
298
+ out.width = Math.round(state.resizing.width);
299
+ out.height = Math.round(state.resizing.height);
300
+ } // Otherwise, calculate from grid units.
301
+ else {
302
+ // 0 * Infinity === NaN, which causes problems with resize constraints;
303
+ // Fix this if it occurs.
304
+ // Note we do it here rather than later because Math.round(Infinity) causes deopt
305
+ out.width = w === Infinity ? w : Math.round(colWidth * w + Math.max(0, w - 1) * margin[0]);
306
+ out.height = h === Infinity ? h : Math.round(rowHeight * h + Math.max(0, h - 1) * margin[1]);
307
+ } // If dragging, use the exact width and height as returned from dragging callbacks.
308
+
309
+
310
+ if (state && state.dragging) {
311
+ out.top = Math.round(state.dragging.top);
312
+ out.left = Math.round(state.dragging.left);
313
+ } // Otherwise, calculate from grid units.
314
+ else {
315
+ out.top = Math.round((rowHeight + margin[1]) * y + containerPadding[1]);
316
+ out.left = Math.round((colWidth + margin[0]) * x + containerPadding[0]);
317
+ }
318
+
319
+ return out;
320
+ }
321
+ /**
322
+ * Translate x and y coordinates from pixels to grid units.
323
+ * @param {Number} top Top position (relative to parent) in pixels.
324
+ * @param {Number} left Left position (relative to parent) in pixels.
325
+ * @return {Object} x and y in grid units.
326
+ */
327
+
328
+ }, {
329
+ key: "calcXY",
330
+ value: function calcXY(top
331
+ /*: number*/
332
+ , left
333
+ /*: number*/
334
+ )
335
+ /*: { x: number, y: number }*/
336
+ {
337
+ var _this$props3 = this.props,
338
+ margin = _this$props3.margin,
339
+ cols = _this$props3.cols,
340
+ rowHeight = _this$props3.rowHeight,
341
+ w = _this$props3.w,
342
+ h = _this$props3.h,
343
+ maxRows = _this$props3.maxRows;
344
+ var colWidth = this.calcColWidth(); // left = colWidth * x + margin * (x + 1)
345
+ // l = cx + m(x+1)
346
+ // l = cx + mx + m
347
+ // l - m = cx + mx
348
+ // l - m = x(c + m)
349
+ // (l - m) / (c + m) = x
350
+ // x = (left - margin) / (coldWidth + margin)
351
+
352
+ var x = Math.round((left - margin[0]) / (colWidth + margin[0]));
353
+ var y = Math.round((top - margin[1]) / (rowHeight + margin[1])); // Capping
354
+
355
+ x = Math.max(Math.min(x, cols - w), 0);
356
+ y = Math.max(Math.min(y, maxRows - h), 0);
357
+ return {
358
+ x: x,
359
+ y: y
360
+ };
361
+ }
362
+ /**
363
+ * Given a height and width in pixel values, calculate grid units.
364
+ * @param {Number} height Height in pixels.
365
+ * @param {Number} width Width in pixels.
366
+ * @return {Object} w, h as grid units.
367
+ */
368
+
369
+ }, {
370
+ key: "calcWH",
371
+ value: function calcWH(_ref4)
372
+ /*: { w: number, h: number }*/
373
+ {
374
+ var height = _ref4.height,
375
+ width = _ref4.width;
376
+ var _this$props4 = this.props,
377
+ margin = _this$props4.margin,
378
+ maxRows = _this$props4.maxRows,
379
+ cols = _this$props4.cols,
380
+ rowHeight = _this$props4.rowHeight,
381
+ x = _this$props4.x,
382
+ y = _this$props4.y;
383
+ var colWidth = this.calcColWidth(); // width = colWidth * w - (margin * (w - 1))
384
+ // ...
385
+ // w = (width + margin) / (colWidth + margin)
386
+
387
+ var w = Math.round((width + margin[0]) / (colWidth + margin[0]));
388
+ var h = Math.round((height + margin[1]) / (rowHeight + margin[1])); // Capping
389
+
390
+ w = Math.max(Math.min(w, cols - x), 0);
391
+ h = Math.max(Math.min(h, maxRows - y), 0);
392
+ return {
393
+ w: w,
394
+ h: h
395
+ };
396
+ }
397
+ /**
398
+ * This is where we set the grid item's absolute placement. It gets a little tricky because we want to do it
399
+ * well when server rendering, and the only way to do that properly is to use percentage width/left because
400
+ * we don't know exactly what the browser viewport is.
401
+ * Unfortunately, CSS Transforms, which are great for performance, break in this instance because a percentage
402
+ * left is relative to the item itself, not its container! So we cannot use them on the server rendering pass.
403
+ *
404
+ * @param {Object} pos Position object with width, height, left, top.
405
+ * @return {Object} Style object.
406
+ */
407
+
408
+ }, {
409
+ key: "createStyle",
410
+ value: function createStyle(pos
411
+ /*: Position*/
412
+ )
413
+ /*: { [key: string]: ?string }*/
414
+ {
415
+ var _this$props5 = this.props,
416
+ usePercentages = _this$props5.usePercentages,
417
+ containerWidth = _this$props5.containerWidth,
418
+ useCSSTransforms = _this$props5.useCSSTransforms;
419
+ var style; // CSS Transforms support (default)
420
+
421
+ if (useCSSTransforms) {
422
+ style = (0, _utils.setTransform)(pos);
423
+ } else {
424
+ // top,left (slow)
425
+ style = (0, _utils.setTopLeft)(pos); // This is used for server rendering.
426
+
427
+ if (usePercentages) {
428
+ style.left = (0, _utils.perc)(pos.left / containerWidth);
429
+ style.width = (0, _utils.perc)(pos.width / containerWidth);
430
+ }
431
+ }
354
432
 
355
- var handler = _this3.props[handlerName];
433
+ return style;
434
+ }
435
+ /**
436
+ * Mix a Draggable instance into a child.
437
+ * @param {Element} child Child element.
438
+ * @return {Element} Child wrapped in Draggable.
439
+ */
440
+
441
+ }, {
442
+ key: "mixinDraggable",
443
+ value: function mixinDraggable(child
444
+ /*: ReactElement<any>*/
445
+ )
446
+ /*: ReactElement<any>*/
447
+ {
448
+ return _react.default.createElement(_reactDraggable.DraggableCore, {
449
+ onStart: this.onDragStart,
450
+ onDrag: this.onDrag,
451
+ onStop: this.onDragStop,
452
+ handle: this.props.handle,
453
+ cancel: ".react-resizable-handle" + (this.props.cancel ? "," + this.props.cancel : ""),
454
+ scale: this.props.transformScale
455
+ }, child);
456
+ }
457
+ /**
458
+ * Mix a Resizable instance into a child.
459
+ * @param {Element} child Child element.
460
+ * @param {Object} position Position object (pixel values)
461
+ * @return {Element} Child wrapped in Resizable.
462
+ */
463
+
464
+ }, {
465
+ key: "mixinResizable",
466
+ value: function mixinResizable(child
467
+ /*: ReactElement<any>*/
468
+ , position
469
+ /*: Position*/
470
+ )
471
+ /*: ReactElement<any>*/
472
+ {
473
+ var _this$props6 = this.props,
474
+ cols = _this$props6.cols,
475
+ x = _this$props6.x,
476
+ minW = _this$props6.minW,
477
+ minH = _this$props6.minH,
478
+ maxW = _this$props6.maxW,
479
+ maxH = _this$props6.maxH; // This is the max possible width - doesn't go to infinity because of the width of the window
480
+
481
+ var maxWidth = this.calcPosition(0, 0, cols - x, 0).width; // Calculate min/max constraints using our min & maxes
482
+
483
+ var mins = this.calcPosition(0, 0, minW, minH);
484
+ var maxes = this.calcPosition(0, 0, maxW, maxH);
485
+ var minConstraints = [mins.width, mins.height];
486
+ var maxConstraints = [Math.min(maxes.width, maxWidth), Math.min(maxes.height, Infinity)];
487
+ return _react.default.createElement(_reactResizable.Resizable, {
488
+ width: position.width,
489
+ height: position.height,
490
+ minConstraints: minConstraints,
491
+ maxConstraints: maxConstraints,
492
+ onResizeStop: this.onResizeStop,
493
+ onResizeStart: this.onResizeStart,
494
+ onResize: this.onResize
495
+ }, child);
496
+ }
497
+ /**
498
+ * onDragStart event handler
499
+ * @param {Event} e event data
500
+ * @param {Object} callbackData an object with node, delta and position information
501
+ */
502
+
503
+ }, {
504
+ key: "onResizeHandler",
505
+
506
+ /**
507
+ * Wrapper around drag events to provide more useful data.
508
+ * All drag events call the function with the given handler name,
509
+ * with the signature (index, x, y).
510
+ *
511
+ * @param {String} handlerName Handler name to wrap.
512
+ * @return {Function} Handler function.
513
+ */
514
+ value: function onResizeHandler(e
515
+ /*: Event*/
516
+ , _ref5, handlerName
517
+ /*: string*/
518
+ ) {
519
+ var node = _ref5.node,
520
+ size = _ref5.size;
521
+ var handler = this.props[handlerName];
356
522
  if (!handler) return;
357
- var _props7 = _this3.props,
358
- cols = _props7.cols,
359
- x = _props7.x,
360
- i = _props7.i,
361
- maxW = _props7.maxW,
362
- minW = _props7.minW,
363
- maxH = _props7.maxH,
364
- minH = _props7.minH;
365
-
366
- // Get new XY
523
+ var _this$props7 = this.props,
524
+ cols = _this$props7.cols,
525
+ x = _this$props7.x,
526
+ i = _this$props7.i,
527
+ maxW = _this$props7.maxW,
528
+ minW = _this$props7.minW,
529
+ maxH = _this$props7.maxH,
530
+ minH = _this$props7.minH; // Get new XY
367
531
 
368
- var _calcWH = _this3.calcWH(size),
369
- w = _calcWH.w,
370
- h = _calcWH.h;
532
+ var _this$calcWH = this.calcWH(size),
533
+ w = _this$calcWH.w,
534
+ h = _this$calcWH.h; // Cap w at numCols
371
535
 
372
- // Cap w at numCols
373
536
 
537
+ w = Math.min(w, cols - x); // Ensure w is at least 1
374
538
 
375
- w = Math.min(w, cols - x);
376
- // Ensure w is at least 1
377
- w = Math.max(w, 1);
539
+ w = Math.max(w, 1); // Min/max capping
378
540
 
379
- // Min/max capping
380
541
  w = Math.max(Math.min(w, maxW), minW);
381
542
  h = Math.max(Math.min(h, maxH), minH);
382
-
383
- _this3.setState({ resizing: handlerName === "onResizeStop" ? null : size });
384
-
385
- handler.call(_this3, i, w, h, { e: e, node: node, size: size });
386
- };
387
- };
388
-
389
- GridItem.prototype.render = function render() {
390
- var _props8 = this.props,
391
- x = _props8.x,
392
- y = _props8.y,
393
- w = _props8.w,
394
- h = _props8.h,
395
- isDraggable = _props8.isDraggable,
396
- isResizable = _props8.isResizable,
397
- useCSSTransforms = _props8.useCSSTransforms;
398
-
399
-
400
- var pos = this.calcPosition(x, y, w, h, this.state);
401
- var child = _react2.default.Children.only(this.props.children);
402
-
403
- // Create the child element. We clone the existing element but modify its className and style.
404
- var newChild = _react2.default.cloneElement(child, {
405
- className: (0, _classnames2.default)("react-grid-item", child.props.className, this.props.className, {
406
- static: this.props.static,
407
- resizing: Boolean(this.state.resizing),
408
- "react-draggable": isDraggable,
409
- "react-draggable-dragging": Boolean(this.state.dragging),
410
- cssTransforms: useCSSTransforms
411
- }),
412
- // We can set the width and height on the child, but unfortunately we can't set the position.
413
- style: _extends({}, this.props.style, child.props.style, this.createStyle(pos))
414
- });
415
-
416
- // Resizable support. This is usually on but the user can toggle it off.
417
- if (isResizable) newChild = this.mixinResizable(newChild, pos);
418
-
419
- // Draggable support. This is always on, except for with placeholders.
420
- if (isDraggable) newChild = this.mixinDraggable(newChild);
421
-
422
- return newChild;
423
- };
543
+ this.setState({
544
+ resizing: handlerName === "onResizeStop" ? null : size
545
+ });
546
+ handler.call(this, i, w, h, {
547
+ e: e,
548
+ node: node,
549
+ size: size
550
+ });
551
+ }
552
+ }, {
553
+ key: "render",
554
+ value: function render()
555
+ /*: ReactNode*/
556
+ {
557
+ var _this$props8 = this.props,
558
+ x = _this$props8.x,
559
+ y = _this$props8.y,
560
+ w = _this$props8.w,
561
+ h = _this$props8.h,
562
+ isDraggable = _this$props8.isDraggable,
563
+ isResizable = _this$props8.isResizable,
564
+ droppingPosition = _this$props8.droppingPosition,
565
+ useCSSTransforms = _this$props8.useCSSTransforms;
566
+ var pos = this.calcPosition(x, y, w, h, this.state);
567
+
568
+ var child = _react.default.Children.only(this.props.children); // Create the child element. We clone the existing element but modify its className and style.
569
+
570
+
571
+ var newChild = _react.default.cloneElement(child, {
572
+ className: (0, _classnames.default)("react-grid-item", child.props.className, this.props.className, {
573
+ static: this.props.static,
574
+ resizing: Boolean(this.state.resizing),
575
+ "react-draggable": isDraggable,
576
+ "react-draggable-dragging": Boolean(this.state.dragging),
577
+ dropping: Boolean(droppingPosition),
578
+ cssTransforms: useCSSTransforms
579
+ }),
580
+ // We can set the width and height on the child, but unfortunately we can't set the position.
581
+ style: _objectSpread({}, this.props.style, {}, child.props.style, {}, this.createStyle(pos))
582
+ }); // Resizable support. This is usually on but the user can toggle it off.
583
+
584
+
585
+ if (isResizable) newChild = this.mixinResizable(newChild, pos); // Draggable support. This is always on, except for with placeholders.
586
+
587
+ if (isDraggable) newChild = this.mixinDraggable(newChild);
588
+ return newChild;
589
+ }
590
+ }]);
424
591
 
425
592
  return GridItem;
426
- }(_react2.default.Component);
593
+ }(_react.default.Component);
427
594
 
428
- GridItem.propTypes = {
429
- // Children must be only a single element
430
- children: _propTypes2.default.element,
595
+ exports.default = GridItem;
431
596
 
597
+ _defineProperty(GridItem, "propTypes", {
598
+ // Children must be only a single element
599
+ children: _propTypes.default.element,
432
600
  // General grid attributes
433
- cols: _propTypes2.default.number.isRequired,
434
- containerWidth: _propTypes2.default.number.isRequired,
435
- rowHeight: _propTypes2.default.number.isRequired,
436
- margin: _propTypes2.default.array.isRequired,
437
- maxRows: _propTypes2.default.number.isRequired,
438
- containerPadding: _propTypes2.default.array.isRequired,
439
-
601
+ cols: _propTypes.default.number.isRequired,
602
+ containerWidth: _propTypes.default.number.isRequired,
603
+ rowHeight: _propTypes.default.number.isRequired,
604
+ margin: _propTypes.default.array.isRequired,
605
+ maxRows: _propTypes.default.number.isRequired,
606
+ containerPadding: _propTypes.default.array.isRequired,
440
607
  // These are all in grid units
441
- x: _propTypes2.default.number.isRequired,
442
- y: _propTypes2.default.number.isRequired,
443
- w: _propTypes2.default.number.isRequired,
444
- h: _propTypes2.default.number.isRequired,
445
-
608
+ x: _propTypes.default.number.isRequired,
609
+ y: _propTypes.default.number.isRequired,
610
+ w: _propTypes.default.number.isRequired,
611
+ h: _propTypes.default.number.isRequired,
446
612
  // All optional
447
- minW: function minW(props, propName) {
613
+ minW: function minW(props
614
+ /*: Props*/
615
+ , propName
616
+ /*: string*/
617
+ ) {
448
618
  var value = props[propName];
449
619
  if (typeof value !== "number") return new Error("minWidth not Number");
450
620
  if (value > props.w || value > props.maxW) return new Error("minWidth larger than item width/maxWidth");
451
621
  },
452
-
453
- maxW: function maxW(props, propName) {
622
+ maxW: function maxW(props
623
+ /*: Props*/
624
+ , propName
625
+ /*: string*/
626
+ ) {
454
627
  var value = props[propName];
455
628
  if (typeof value !== "number") return new Error("maxWidth not Number");
456
629
  if (value < props.w || value < props.minW) return new Error("maxWidth smaller than item width/minWidth");
457
630
  },
458
-
459
- minH: function minH(props, propName) {
631
+ minH: function minH(props
632
+ /*: Props*/
633
+ , propName
634
+ /*: string*/
635
+ ) {
460
636
  var value = props[propName];
461
637
  if (typeof value !== "number") return new Error("minHeight not Number");
462
638
  if (value > props.h || value > props.maxH) return new Error("minHeight larger than item height/maxHeight");
463
639
  },
464
-
465
- maxH: function maxH(props, propName) {
640
+ maxH: function maxH(props
641
+ /*: Props*/
642
+ , propName
643
+ /*: string*/
644
+ ) {
466
645
  var value = props[propName];
467
646
  if (typeof value !== "number") return new Error("maxHeight not Number");
468
647
  if (value < props.h || value < props.minH) return new Error("maxHeight smaller than item height/minHeight");
469
648
  },
470
-
471
649
  // ID is nice to have for callbacks
472
- i: _propTypes2.default.string.isRequired,
473
-
650
+ i: _propTypes.default.string.isRequired,
474
651
  // Functions
475
- onDragStop: _propTypes2.default.func,
476
- onDragStart: _propTypes2.default.func,
477
- onDrag: _propTypes2.default.func,
478
- onResizeStop: _propTypes2.default.func,
479
- onResizeStart: _propTypes2.default.func,
480
- onResize: _propTypes2.default.func,
481
-
652
+ onDragStop: _propTypes.default.func,
653
+ onDragStart: _propTypes.default.func,
654
+ onDrag: _propTypes.default.func,
655
+ onResizeStop: _propTypes.default.func,
656
+ onResizeStart: _propTypes.default.func,
657
+ onResize: _propTypes.default.func,
482
658
  // Flags
483
- isDraggable: _propTypes2.default.bool.isRequired,
484
- isResizable: _propTypes2.default.bool.isRequired,
485
- static: _propTypes2.default.bool,
486
-
659
+ isDraggable: _propTypes.default.bool.isRequired,
660
+ isResizable: _propTypes.default.bool.isRequired,
661
+ static: _propTypes.default.bool,
487
662
  // Use CSS transforms instead of top/left
488
- useCSSTransforms: _propTypes2.default.bool.isRequired,
489
-
663
+ useCSSTransforms: _propTypes.default.bool.isRequired,
664
+ transformScale: _propTypes.default.number,
490
665
  // Others
491
- className: _propTypes2.default.string,
666
+ className: _propTypes.default.string,
492
667
  // Selector for draggable handle
493
- handle: _propTypes2.default.string,
668
+ handle: _propTypes.default.string,
494
669
  // Selector for draggable cancel (see react-draggable)
495
- cancel: _propTypes2.default.string
496
- };
497
- GridItem.defaultProps = {
670
+ cancel: _propTypes.default.string,
671
+ // Current position of a dropping element
672
+ droppingPosition: _propTypes.default.shape({
673
+ e: _propTypes.default.object.isRequired,
674
+ x: _propTypes.default.number.isRequired,
675
+ y: _propTypes.default.number.isRequired
676
+ })
677
+ });
678
+
679
+ _defineProperty(GridItem, "defaultProps", {
498
680
  className: "",
499
681
  cancel: "",
500
682
  handle: "",
501
683
  minH: 1,
502
684
  minW: 1,
503
685
  maxH: Infinity,
504
- maxW: Infinity
505
- };
506
- exports.default = GridItem;
686
+ maxW: Infinity,
687
+ transformScale: 1
688
+ });