gridstack 4.3.0 → 5.0.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.
Files changed (126) hide show
  1. package/README.md +22 -5
  2. package/dist/es5/gridstack-dd.d.ts +35 -0
  3. package/dist/es5/gridstack-dd.js +686 -0
  4. package/dist/es5/gridstack-dd.js.map +1 -0
  5. package/dist/es5/gridstack-ddi.d.ts +17 -0
  6. package/dist/es5/gridstack-ddi.js +31 -0
  7. package/dist/es5/gridstack-ddi.js.map +1 -0
  8. package/dist/es5/gridstack-engine.d.ts +93 -0
  9. package/dist/es5/gridstack-engine.js +955 -0
  10. package/dist/es5/gridstack-engine.js.map +1 -0
  11. package/dist/es5/gridstack-h5.d.ts +10 -0
  12. package/dist/es5/gridstack-h5.js +3 -0
  13. package/dist/es5/gridstack-h5.js.LICENSE.txt +7 -0
  14. package/dist/es5/gridstack-h5.js.map +1 -0
  15. package/dist/es5/gridstack-jq.d.ts +10 -0
  16. package/dist/es5/gridstack-jq.js +3 -0
  17. package/dist/es5/gridstack-jq.js.LICENSE.txt +99 -0
  18. package/dist/es5/gridstack-jq.js.map +1 -0
  19. package/dist/{gridstack-poly.js → es5/gridstack-poly.js} +154 -3
  20. package/dist/es5/gridstack-static.d.ts +9 -0
  21. package/dist/es5/gridstack-static.js +3 -0
  22. package/dist/es5/gridstack-static.js.LICENSE.txt +7 -0
  23. package/dist/es5/gridstack-static.js.map +1 -0
  24. package/dist/es5/gridstack.d.ts +346 -0
  25. package/dist/es5/gridstack.js +1517 -0
  26. package/dist/es5/gridstack.js.map +1 -0
  27. package/dist/es5/h5/dd-base-impl.d.ts +20 -0
  28. package/dist/es5/h5/dd-base-impl.js +43 -0
  29. package/dist/es5/h5/dd-base-impl.js.map +1 -0
  30. package/dist/es5/h5/dd-draggable.d.ts +29 -0
  31. package/dist/es5/h5/dd-draggable.js +329 -0
  32. package/dist/es5/h5/dd-draggable.js.map +1 -0
  33. package/dist/es5/h5/dd-droppable.d.ts +24 -0
  34. package/dist/es5/h5/dd-droppable.js +199 -0
  35. package/dist/es5/h5/dd-droppable.js.map +1 -0
  36. package/dist/es5/h5/dd-element.d.ts +27 -0
  37. package/dist/es5/h5/dd-element.js +96 -0
  38. package/dist/es5/h5/dd-element.js.map +1 -0
  39. package/dist/es5/h5/dd-manager.d.ts +8 -0
  40. package/dist/es5/h5/dd-manager.js +14 -0
  41. package/dist/es5/h5/dd-manager.js.map +1 -0
  42. package/dist/es5/h5/dd-resizable-handle.d.ts +14 -0
  43. package/dist/es5/h5/dd-resizable-handle.js +83 -0
  44. package/dist/es5/h5/dd-resizable-handle.js.map +1 -0
  45. package/dist/es5/h5/dd-resizable.d.ts +28 -0
  46. package/dist/es5/h5/dd-resizable.js +301 -0
  47. package/dist/es5/h5/dd-resizable.js.map +1 -0
  48. package/dist/es5/h5/dd-utils.d.ts +19 -0
  49. package/dist/es5/h5/dd-utils.js +111 -0
  50. package/dist/es5/h5/dd-utils.js.map +1 -0
  51. package/dist/es5/h5/gridstack-dd-native.d.ts +26 -0
  52. package/dist/es5/h5/gridstack-dd-native.js +175 -0
  53. package/dist/es5/h5/gridstack-dd-native.js.map +1 -0
  54. package/dist/es5/jq/gridstack-dd-jqueryui.d.ts +22 -0
  55. package/dist/es5/jq/gridstack-dd-jqueryui.js +162 -0
  56. package/dist/es5/jq/gridstack-dd-jqueryui.js.map +1 -0
  57. package/dist/es5/types.d.ts +263 -0
  58. package/dist/es5/types.js +7 -0
  59. package/dist/es5/types.js.map +1 -0
  60. package/dist/es5/utils.d.ts +73 -0
  61. package/dist/es5/utils.js +445 -0
  62. package/dist/es5/utils.js.map +1 -0
  63. package/dist/gridstack-dd.d.ts +1 -1
  64. package/dist/gridstack-dd.js +27 -16
  65. package/dist/gridstack-dd.js.map +1 -1
  66. package/dist/gridstack-ddi.d.ts +1 -1
  67. package/dist/gridstack-ddi.js +1 -1
  68. package/dist/gridstack-ddi.js.map +1 -1
  69. package/dist/gridstack-engine.d.ts +3 -2
  70. package/dist/gridstack-engine.js +83 -53
  71. package/dist/gridstack-engine.js.map +1 -1
  72. package/dist/gridstack-h5.d.ts +1 -1
  73. package/dist/gridstack-h5.js +1 -1
  74. package/dist/gridstack-h5.js.LICENSE.txt +1 -1
  75. package/dist/gridstack-h5.js.map +1 -1
  76. package/dist/gridstack-jq.d.ts +1 -1
  77. package/dist/gridstack-jq.js +1 -1
  78. package/dist/gridstack-jq.js.LICENSE.txt +1 -1
  79. package/dist/gridstack-jq.js.map +1 -1
  80. package/dist/gridstack-static.d.ts +1 -1
  81. package/dist/gridstack-static.js +1 -1
  82. package/dist/gridstack-static.js.LICENSE.txt +1 -1
  83. package/dist/gridstack-static.js.map +1 -1
  84. package/dist/gridstack.css +1 -5
  85. package/dist/gridstack.d.ts +2 -2
  86. package/dist/gridstack.js +81 -44
  87. package/dist/gridstack.js.map +1 -1
  88. package/dist/gridstack.min.css +1 -1
  89. package/dist/h5/dd-base-impl.d.ts +1 -1
  90. package/dist/h5/dd-base-impl.js +1 -1
  91. package/dist/h5/dd-base-impl.js.map +1 -1
  92. package/dist/h5/dd-draggable.d.ts +1 -2
  93. package/dist/h5/dd-draggable.js +26 -16
  94. package/dist/h5/dd-draggable.js.map +1 -1
  95. package/dist/h5/dd-droppable.d.ts +2 -1
  96. package/dist/h5/dd-droppable.js +42 -25
  97. package/dist/h5/dd-droppable.js.map +1 -1
  98. package/dist/h5/dd-element.d.ts +1 -1
  99. package/dist/h5/dd-element.js +1 -1
  100. package/dist/h5/dd-element.js.map +1 -1
  101. package/dist/h5/dd-manager.d.ts +1 -1
  102. package/dist/h5/dd-manager.js +1 -1
  103. package/dist/h5/dd-manager.js.map +1 -1
  104. package/dist/h5/dd-resizable-handle.d.ts +1 -1
  105. package/dist/h5/dd-resizable-handle.js +1 -1
  106. package/dist/h5/dd-resizable-handle.js.map +1 -1
  107. package/dist/h5/dd-resizable.d.ts +1 -2
  108. package/dist/h5/dd-resizable.js +3 -3
  109. package/dist/h5/dd-resizable.js.map +1 -1
  110. package/dist/h5/dd-utils.d.ts +3 -1
  111. package/dist/h5/dd-utils.js +13 -1
  112. package/dist/h5/dd-utils.js.map +1 -1
  113. package/dist/h5/gridstack-dd-native.d.ts +1 -1
  114. package/dist/h5/gridstack-dd-native.js +1 -1
  115. package/dist/h5/gridstack-dd-native.js.map +1 -1
  116. package/dist/jq/gridstack-dd-jqueryui.js.map +1 -1
  117. package/dist/src/gridstack.scss +6 -5
  118. package/dist/types.d.ts +12 -4
  119. package/dist/types.js +1 -1
  120. package/dist/types.js.map +1 -1
  121. package/dist/utils.d.ts +4 -2
  122. package/dist/utils.js +7 -6
  123. package/dist/utils.js.map +1 -1
  124. package/doc/CHANGES.md +24 -0
  125. package/doc/README.md +10 -5
  126. package/package.json +5 -3
@@ -0,0 +1,686 @@
1
+ "use strict";
2
+ /**
3
+ * gridstack-dd.ts 5.0
4
+ * Copyright (c) 2021 Alain Dumesny - see GridStack root license
5
+ */
6
+ var __extends = (this && this.__extends) || (function () {
7
+ var extendStatics = function (d, b) {
8
+ extendStatics = Object.setPrototypeOf ||
9
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
10
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
11
+ return extendStatics(d, b);
12
+ };
13
+ return function (d, b) {
14
+ extendStatics(d, b);
15
+ function __() { this.constructor = d; }
16
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
17
+ };
18
+ })();
19
+ var __assign = (this && this.__assign) || function () {
20
+ __assign = Object.assign || function(t) {
21
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
22
+ s = arguments[i];
23
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
24
+ t[p] = s[p];
25
+ }
26
+ return t;
27
+ };
28
+ return __assign.apply(this, arguments);
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.GridStackDD = void 0;
32
+ /* eslint-disable @typescript-eslint/no-unused-vars */
33
+ var gridstack_ddi_1 = require("./gridstack-ddi");
34
+ var gridstack_1 = require("./gridstack");
35
+ var utils_1 = require("./utils");
36
+ // TEST let count = 0;
37
+ /**
38
+ * Base class implementing common Grid drag'n'drop functionality, with domain specific subclass (h5 vs jq subclasses)
39
+ */
40
+ var GridStackDD = /** @class */ (function (_super) {
41
+ __extends(GridStackDD, _super);
42
+ function GridStackDD() {
43
+ return _super !== null && _super.apply(this, arguments) || this;
44
+ }
45
+ /** override to cast to correct type */
46
+ GridStackDD.get = function () {
47
+ return gridstack_ddi_1.GridStackDDI.get();
48
+ };
49
+ /** removes any drag&drop present (called during destroy) */
50
+ GridStackDD.prototype.remove = function (el) {
51
+ this.draggable(el, 'destroy').resizable(el, 'destroy');
52
+ if (el.gridstackNode) {
53
+ delete el.gridstackNode._initDD; // reset our DD init flag
54
+ }
55
+ return this;
56
+ };
57
+ return GridStackDD;
58
+ }(gridstack_ddi_1.GridStackDDI));
59
+ exports.GridStackDD = GridStackDD;
60
+ /********************************************************************************
61
+ * GridStack code that is doing drag&drop extracted here so main class is smaller
62
+ * for static grid that don't do any of this work anyway. Saves about 10k.
63
+ * https://www.typescriptlang.org/docs/handbook/declaration-merging.html
64
+ * https://www.typescriptlang.org/docs/handbook/mixins.html
65
+ ********************************************************************************/
66
+ /** @internal called to add drag over to support widgets being added externally */
67
+ gridstack_1.GridStack.prototype._setupAcceptWidget = function () {
68
+ var _this = this;
69
+ // check if we need to disable things
70
+ if (this.opts.staticGrid || (!this.opts.acceptWidgets && !this.opts.removable)) {
71
+ GridStackDD.get().droppable(this.el, 'destroy');
72
+ return this;
73
+ }
74
+ // vars shared across all methods
75
+ var cellHeight, cellWidth;
76
+ var onDrag = function (event, el, helper) {
77
+ var node = el.gridstackNode;
78
+ if (!node)
79
+ return;
80
+ helper = helper || el;
81
+ var parent = _this.el.getBoundingClientRect();
82
+ var _a = helper.getBoundingClientRect(), top = _a.top, left = _a.left;
83
+ left -= parent.left;
84
+ top -= parent.top;
85
+ var ui = { position: { top: top, left: left } };
86
+ if (node._temporaryRemoved) {
87
+ node.x = Math.max(0, Math.round(left / cellWidth));
88
+ node.y = Math.max(0, Math.round(top / cellHeight));
89
+ delete node.autoPosition;
90
+ _this.engine.nodeBoundFix(node);
91
+ // don't accept *initial* location if doesn't fit #1419 (locked drop region, or can't grow), but maybe try if it will go somewhere
92
+ if (!_this.engine.willItFit(node)) {
93
+ node.autoPosition = true; // ignore x,y and try for any slot...
94
+ if (!_this.engine.willItFit(node)) {
95
+ GridStackDD.get().off(el, 'drag'); // stop calling us
96
+ return; // full grid or can't grow
97
+ }
98
+ if (node._willFitPos) {
99
+ // use the auto position instead #1687
100
+ utils_1.Utils.copyPos(node, node._willFitPos);
101
+ delete node._willFitPos;
102
+ }
103
+ }
104
+ // re-use the existing node dragging method
105
+ _this._onStartMoving(helper, event, ui, node, cellWidth, cellHeight);
106
+ }
107
+ else {
108
+ // re-use the existing node dragging that does so much of the collision detection
109
+ _this._dragOrResize(helper, event, ui, node, cellWidth, cellHeight);
110
+ }
111
+ };
112
+ GridStackDD.get()
113
+ .droppable(this.el, {
114
+ accept: function (el) {
115
+ var node = el.gridstackNode;
116
+ // set accept drop to true on ourself (which we ignore) so we don't get "can't drop" icon in HTML5 mode while moving
117
+ if ((node === null || node === void 0 ? void 0 : node.grid) === _this)
118
+ return true;
119
+ if (!_this.opts.acceptWidgets)
120
+ return false;
121
+ // prevent deeper nesting until rest of 992 can be fixed
122
+ if (node === null || node === void 0 ? void 0 : node.subGrid)
123
+ return false;
124
+ // check for accept method or class matching
125
+ var canAccept = true;
126
+ if (typeof _this.opts.acceptWidgets === 'function') {
127
+ canAccept = _this.opts.acceptWidgets(el);
128
+ }
129
+ else {
130
+ var selector = (_this.opts.acceptWidgets === true ? '.grid-stack-item' : _this.opts.acceptWidgets);
131
+ canAccept = el.matches(selector);
132
+ }
133
+ // finally check to make sure we actually have space left #1571
134
+ if (canAccept && node && _this.opts.maxRow) {
135
+ var n = { w: node.w, h: node.h, minW: node.minW, minH: node.minH }; // only width/height matters and autoPosition
136
+ canAccept = _this.engine.willItFit(n);
137
+ }
138
+ return canAccept;
139
+ }
140
+ })
141
+ /**
142
+ * entering our grid area
143
+ */
144
+ .on(this.el, 'dropover', function (event, el, helper) {
145
+ // TEST console.log(`over ${this.el.gridstack.opts.id} ${count++}`);
146
+ var node = el.gridstackNode;
147
+ // ignore drop enter on ourself (unless we temporarily removed) which happens on a simple drag of our item
148
+ if ((node === null || node === void 0 ? void 0 : node.grid) === _this && !node._temporaryRemoved) {
149
+ // delete node._added; // reset this to track placeholder again in case we were over other grid #1484 (dropout doesn't always clear)
150
+ return false; // prevent parent from receiving msg (which may be a grid as well)
151
+ }
152
+ // fix #1578 when dragging fast, we may not get a leave on the previous grid so force one now
153
+ if ((node === null || node === void 0 ? void 0 : node.grid) && node.grid !== _this && !node._temporaryRemoved) {
154
+ // TEST console.log('dropover without leave');
155
+ var otherGrid = node.grid;
156
+ otherGrid._leave(el, helper);
157
+ }
158
+ // cache cell dimensions (which don't change), position can animate if we removed an item in otherGrid that affects us...
159
+ cellWidth = _this.cellWidth();
160
+ cellHeight = _this.getCellHeight(true);
161
+ // load any element attributes if we don't have a node
162
+ if (!node) { // @ts-ignore private read only on ourself
163
+ node = _this._readAttr(el);
164
+ }
165
+ if (!node.grid) {
166
+ node._isExternal = true;
167
+ el.gridstackNode = node;
168
+ }
169
+ // calculate the grid size based on element outer size
170
+ helper = helper || el;
171
+ var w = node.w || Math.round(helper.offsetWidth / cellWidth) || 1;
172
+ var h = node.h || Math.round(helper.offsetHeight / cellHeight) || 1;
173
+ // if the item came from another grid, make a copy and save the original info in case we go back there
174
+ if (node.grid && node.grid !== _this) {
175
+ // copy the node original values (min/max/id/etc...) but override width/height/other flags which are this grid specific
176
+ // TEST console.log('dropover cloning node');
177
+ if (!el._gridstackNodeOrig)
178
+ el._gridstackNodeOrig = node; // shouldn't have multiple nested!
179
+ el.gridstackNode = node = __assign(__assign({}, node), { w: w, h: h, grid: _this });
180
+ _this.engine.cleanupNode(node)
181
+ .nodeBoundFix(node);
182
+ // restore some internal fields we need after clearing them all
183
+ node._initDD =
184
+ node._isExternal = // DOM needs to be re-parented on a drop
185
+ node._temporaryRemoved = true; // so it can be inserted onDrag below
186
+ }
187
+ else {
188
+ node.w = w;
189
+ node.h = h;
190
+ node._temporaryRemoved = true; // so we can insert it
191
+ }
192
+ // clear any marked for complete removal (Note: don't check _isAboutToRemove as that is cleared above - just do it)
193
+ _itemRemoving(node.el, false);
194
+ GridStackDD.get().on(el, 'drag', onDrag);
195
+ // make sure this is called at least once when going fast #1578
196
+ onDrag(event, el, helper);
197
+ return false; // prevent parent from receiving msg (which may be a grid as well)
198
+ })
199
+ /**
200
+ * Leaving our grid area...
201
+ */
202
+ .on(this.el, 'dropout', function (event, el, helper) {
203
+ // TEST console.log(`out ${this.el.gridstack.opts.id} ${count++}`);
204
+ var node = el.gridstackNode;
205
+ if (!node)
206
+ return false;
207
+ // fix #1578 when dragging fast, we might get leave after other grid gets enter (which calls us to clean)
208
+ // so skip this one if we're not the active grid really..
209
+ if (!node.grid || node.grid === _this) {
210
+ _this._leave(el, helper);
211
+ }
212
+ return false; // prevent parent from receiving msg (which may be grid as well)
213
+ })
214
+ /**
215
+ * end - releasing the mouse
216
+ */
217
+ .on(this.el, 'drop', function (event, el, helper) {
218
+ var node = el.gridstackNode;
219
+ // ignore drop on ourself from ourself that didn't come from the outside - dragend will handle the simple move instead
220
+ if ((node === null || node === void 0 ? void 0 : node.grid) === _this && !node._isExternal)
221
+ return false;
222
+ var wasAdded = !!_this.placeholder.parentElement; // skip items not actually added to us because of constrains, but do cleanup #1419
223
+ _this.placeholder.remove();
224
+ // notify previous grid of removal
225
+ // TEST console.log('drop delete _gridstackNodeOrig')
226
+ var origNode = el._gridstackNodeOrig;
227
+ delete el._gridstackNodeOrig;
228
+ if (wasAdded && origNode && origNode.grid && origNode.grid !== _this) {
229
+ var oGrid = origNode.grid;
230
+ oGrid.engine.removedNodes.push(origNode);
231
+ oGrid._triggerRemoveEvent();
232
+ }
233
+ if (!node)
234
+ return false;
235
+ // use existing placeholder node as it's already in our list with drop location
236
+ if (wasAdded) {
237
+ _this.engine.cleanupNode(node); // removes all internal _xyz values
238
+ node.grid = _this;
239
+ }
240
+ GridStackDD.get().off(el, 'drag');
241
+ // if we made a copy ('helper' which is temp) of the original node then insert a copy, else we move the original node (#1102)
242
+ // as the helper will be nuked by jquery-ui otherwise
243
+ if (helper !== el) {
244
+ helper.remove();
245
+ el.gridstackNode = origNode; // original item (left behind) is re-stored to pre dragging as the node now has drop info
246
+ if (wasAdded) {
247
+ el = el.cloneNode(true);
248
+ }
249
+ }
250
+ else {
251
+ el.remove(); // reduce flicker as we change depth here, and size further down
252
+ GridStackDD.get().remove(el);
253
+ }
254
+ if (!wasAdded)
255
+ return false;
256
+ el.gridstackNode = node;
257
+ node.el = el;
258
+ // @ts-ignore
259
+ utils_1.Utils.copyPos(node, _this._readAttr(_this.placeholder)); // placeholder values as moving VERY fast can throw things off #1578
260
+ utils_1.Utils.removePositioningStyles(el); // @ts-ignore
261
+ _this._writeAttr(el, node);
262
+ _this.el.appendChild(el); // @ts-ignore
263
+ _this._updateContainerHeight();
264
+ _this.engine.addedNodes.push(node); // @ts-ignore
265
+ _this._triggerAddEvent(); // @ts-ignore
266
+ _this._triggerChangeEvent();
267
+ _this.engine.endUpdate();
268
+ if (_this._gsEventHandler['dropped']) {
269
+ _this._gsEventHandler['dropped'](__assign(__assign({}, event), { type: 'dropped' }), origNode && origNode.grid ? origNode : undefined, node);
270
+ }
271
+ // wait till we return out of the drag callback to set the new drag&resize handler or they may get messed up
272
+ window.setTimeout(function () {
273
+ // IFF we are still there (some application will use as placeholder and insert their real widget instead and better call makeWidget())
274
+ if (node.el && node.el.parentElement) {
275
+ _this._prepareDragDropByNode(node);
276
+ }
277
+ else {
278
+ _this.engine.removeNode(node);
279
+ }
280
+ });
281
+ return false; // prevent parent from receiving msg (which may be grid as well)
282
+ });
283
+ return this;
284
+ };
285
+ /** @internal mark item for removal */
286
+ function _itemRemoving(el, remove) {
287
+ var node = el ? el.gridstackNode : undefined;
288
+ if (!node || !node.grid)
289
+ return;
290
+ remove ? node._isAboutToRemove = true : delete node._isAboutToRemove;
291
+ remove ? el.classList.add('grid-stack-item-removing') : el.classList.remove('grid-stack-item-removing');
292
+ }
293
+ /** @internal called to setup a trash drop zone if the user specifies it */
294
+ gridstack_1.GridStack.prototype._setupRemoveDrop = function () {
295
+ if (!this.opts.staticGrid && typeof this.opts.removable === 'string') {
296
+ var trashEl = document.querySelector(this.opts.removable);
297
+ if (!trashEl)
298
+ return this;
299
+ // only register ONE drop-over/dropout callback for the 'trash', and it will
300
+ // update the passed in item and parent grid because the 'trash' is a shared resource anyway,
301
+ // and Native DD only has 1 event CB (having a list and technically a per grid removableOptions complicates things greatly)
302
+ if (!GridStackDD.get().isDroppable(trashEl)) {
303
+ GridStackDD.get().droppable(trashEl, this.opts.removableOptions)
304
+ .on(trashEl, 'dropover', function (event, el) { return _itemRemoving(el, true); })
305
+ .on(trashEl, 'dropout', function (event, el) { return _itemRemoving(el, false); });
306
+ }
307
+ }
308
+ return this;
309
+ };
310
+ /**
311
+ * call to setup dragging in from the outside (say toolbar), by specifying the class selection and options.
312
+ * Called during GridStack.init() as options, but can also be called directly (last param are cached) in case the toolbar
313
+ * is dynamically create and needs to change later.
314
+ **/
315
+ gridstack_1.GridStack.setupDragIn = function (_dragIn, _dragInOptions) {
316
+ var dragIn;
317
+ var dragInOptions;
318
+ var dragInDefaultOptions = {
319
+ revert: 'invalid',
320
+ handle: '.grid-stack-item-content',
321
+ scroll: false,
322
+ appendTo: 'body'
323
+ };
324
+ // cache in the passed in values (form grid init?) so they don't have to resend them each time
325
+ if (_dragIn) {
326
+ dragIn = _dragIn;
327
+ dragInOptions = __assign(__assign({}, dragInDefaultOptions), (_dragInOptions || {}));
328
+ }
329
+ if (typeof dragIn !== 'string')
330
+ return;
331
+ var dd = GridStackDD.get();
332
+ utils_1.Utils.getElements(dragIn).forEach(function (el) {
333
+ if (!dd.isDraggable(el))
334
+ dd.dragIn(el, dragInOptions);
335
+ });
336
+ };
337
+ /** @internal prepares the element for drag&drop **/
338
+ gridstack_1.GridStack.prototype._prepareDragDropByNode = function (node) {
339
+ var _this = this;
340
+ var el = node.el;
341
+ var dd = GridStackDD.get();
342
+ // check for disabled grid first
343
+ if (this.opts.staticGrid || ((node.noMove || this.opts.disableDrag) && (node.noResize || this.opts.disableResize))) {
344
+ if (node._initDD) {
345
+ dd.remove(el); // nukes everything instead of just disable, will add some styles back next
346
+ delete node._initDD;
347
+ }
348
+ el.classList.add('ui-draggable-disabled', 'ui-resizable-disabled'); // add styles one might depend on #1435
349
+ return this;
350
+ }
351
+ if (!node._initDD) {
352
+ // variables used/cashed between the 3 start/move/end methods, in addition to node passed above
353
+ var cellWidth_1;
354
+ var cellHeight_1;
355
+ /** called when item starts moving/resizing */
356
+ var onStartMoving = function (event, ui) {
357
+ // trigger any 'dragstart' / 'resizestart' manually
358
+ if (_this._gsEventHandler[event.type]) {
359
+ _this._gsEventHandler[event.type](event, event.target);
360
+ }
361
+ cellWidth_1 = _this.cellWidth();
362
+ cellHeight_1 = _this.getCellHeight(true); // force pixels for calculations
363
+ _this._onStartMoving(el, event, ui, node, cellWidth_1, cellHeight_1);
364
+ };
365
+ /** called when item is being dragged/resized */
366
+ var dragOrResize = function (event, ui) {
367
+ _this._dragOrResize(el, event, ui, node, cellWidth_1, cellHeight_1);
368
+ };
369
+ /** called when the item stops moving/resizing */
370
+ var onEndMoving = function (event) {
371
+ _this.placeholder.remove();
372
+ delete node._moving;
373
+ delete node._lastTried;
374
+ // if the item has moved to another grid, we're done here
375
+ var target = event.target;
376
+ if (!target.gridstackNode || target.gridstackNode.grid !== _this)
377
+ return;
378
+ node.el = target;
379
+ if (node._isAboutToRemove) {
380
+ var gridToNotify = el.gridstackNode.grid;
381
+ if (gridToNotify._gsEventHandler[event.type]) {
382
+ gridToNotify._gsEventHandler[event.type](event, target);
383
+ }
384
+ dd.remove(el);
385
+ gridToNotify.engine.removedNodes.push(node);
386
+ gridToNotify._triggerRemoveEvent();
387
+ // break circular links and remove DOM
388
+ delete el.gridstackNode;
389
+ delete node.el;
390
+ el.remove();
391
+ }
392
+ else {
393
+ if (!node._temporaryRemoved) {
394
+ // move to new placeholder location
395
+ utils_1.Utils.removePositioningStyles(target); // @ts-ignore
396
+ _this._writePosAttr(target, node);
397
+ }
398
+ else {
399
+ // got removed - restore item back to before dragging position
400
+ utils_1.Utils.removePositioningStyles(target);
401
+ utils_1.Utils.copyPos(node, node._orig); // @ts-ignore
402
+ _this._writePosAttr(target, node);
403
+ _this.engine.addNode(node);
404
+ }
405
+ if (_this._gsEventHandler[event.type]) {
406
+ _this._gsEventHandler[event.type](event, target);
407
+ }
408
+ }
409
+ // @ts-ignore
410
+ _this._extraDragRow = 0; // @ts-ignore
411
+ _this._updateContainerHeight(); // @ts-ignore
412
+ _this._triggerChangeEvent();
413
+ _this.engine.endUpdate();
414
+ };
415
+ dd.draggable(el, {
416
+ start: onStartMoving,
417
+ stop: onEndMoving,
418
+ drag: dragOrResize
419
+ }).resizable(el, {
420
+ start: onStartMoving,
421
+ stop: onEndMoving,
422
+ resize: dragOrResize
423
+ });
424
+ node._initDD = true; // we've set DD support now
425
+ }
426
+ // finally fine tune move vs resize by disabling any part...
427
+ if (node.noMove || this.opts.disableDrag) {
428
+ dd.draggable(el, 'disable');
429
+ el.classList.add('ui-draggable-disabled');
430
+ }
431
+ else {
432
+ dd.draggable(el, 'enable');
433
+ el.classList.remove('ui-draggable-disabled');
434
+ }
435
+ if (node.noResize || this.opts.disableResize) {
436
+ dd.resizable(el, 'disable');
437
+ el.classList.add('ui-resizable-disabled');
438
+ }
439
+ else {
440
+ dd.resizable(el, 'enable');
441
+ el.classList.remove('ui-resizable-disabled');
442
+ }
443
+ return this;
444
+ };
445
+ /** @internal called when item is starting a drag/resize */
446
+ gridstack_1.GridStack.prototype._onStartMoving = function (el, event, ui, node, cellWidth, cellHeight) {
447
+ this.engine.cleanNodes()
448
+ .beginUpdate(node);
449
+ // @ts-ignore
450
+ this._writePosAttr(this.placeholder, node);
451
+ this.el.appendChild(this.placeholder);
452
+ // TEST console.log('_onStartMoving placeholder')
453
+ node.el = this.placeholder;
454
+ node._lastUiPosition = ui.position;
455
+ node._prevYPix = ui.position.top;
456
+ node._moving = (event.type === 'dragstart'); // 'dropover' are not initially moving so they can go exactly where they enter (will push stuff out of the way)
457
+ delete node._lastTried;
458
+ if (event.type === 'dropover' && node._temporaryRemoved) {
459
+ // TEST console.log('engine.addNode x=' + node.x);
460
+ this.engine.addNode(node); // will add, fix collisions, update attr and clear _temporaryRemoved
461
+ node._moving = true; // AFTER, mark as moving object (wanted fix location before)
462
+ }
463
+ // set the min/max resize info
464
+ this.engine.cacheRects(cellWidth, cellHeight, this.opts.marginTop, this.opts.marginRight, this.opts.marginBottom, this.opts.marginLeft);
465
+ if (event.type === 'resizestart') {
466
+ var dd = GridStackDD.get()
467
+ .resizable(el, 'option', 'minWidth', cellWidth * (node.minW || 1))
468
+ .resizable(el, 'option', 'minHeight', cellHeight * (node.minH || 1));
469
+ if (node.maxW) {
470
+ dd.resizable(el, 'option', 'maxWidth', cellWidth * node.maxW);
471
+ }
472
+ if (node.maxH) {
473
+ dd.resizable(el, 'option', 'maxHeight', cellHeight * node.maxH);
474
+ }
475
+ }
476
+ };
477
+ /** @internal called when item leaving our area by either cursor dropout event
478
+ * or shape is outside our boundaries. remove it from us, and mark temporary if this was
479
+ * our item to start with else restore prev node values from prev grid it came from.
480
+ **/
481
+ gridstack_1.GridStack.prototype._leave = function (el, helper) {
482
+ var node = el.gridstackNode;
483
+ if (!node)
484
+ return;
485
+ GridStackDD.get().off(el, 'drag'); // no need to track while being outside
486
+ // this gets called when cursor leaves and shape is outside, so only do this once
487
+ if (node._temporaryRemoved)
488
+ return;
489
+ node._temporaryRemoved = true;
490
+ this.engine.removeNode(node); // remove placeholder as well, otherwise it's a sign node is not in our list, which is a bigger issue
491
+ node.el = node._isExternal && helper ? helper : el; // point back to real item being dragged
492
+ if (this.opts.removable === true) { // boolean vs a class string
493
+ // item leaving us and we are supposed to remove on leave (no need to drag onto trash) mark it so
494
+ _itemRemoving(el, true);
495
+ }
496
+ // finally if item originally came from another grid, but left us, restore things back to prev info
497
+ if (el._gridstackNodeOrig) {
498
+ // TEST console.log('leave delete _gridstackNodeOrig')
499
+ el.gridstackNode = el._gridstackNodeOrig;
500
+ delete el._gridstackNodeOrig;
501
+ }
502
+ else if (node._isExternal) {
503
+ // item came from outside (like a toolbar) so nuke any node info
504
+ delete node.el;
505
+ delete el.gridstackNode;
506
+ // and restore all nodes back to original
507
+ this.engine.restoreInitial();
508
+ }
509
+ };
510
+ /** @internal called when item is being dragged/resized */
511
+ gridstack_1.GridStack.prototype._dragOrResize = function (el, event, ui, node, cellWidth, cellHeight) {
512
+ var p = __assign({}, node._orig); // could be undefined (_isExternal) which is ok (drag only set x,y and w,h will default to node value)
513
+ var resizing;
514
+ var mLeft = this.opts.marginLeft, mRight = this.opts.marginRight, mTop = this.opts.marginTop, mBottom = this.opts.marginBottom;
515
+ // if margins (which are used to pass mid point by) are large relative to cell height/width, reduce them down #1855
516
+ var mHeight = Math.round(cellHeight * 0.1), mWidth = Math.round(cellWidth * 0.1);
517
+ mLeft = Math.min(mLeft, mWidth);
518
+ mRight = Math.min(mRight, mWidth);
519
+ mTop = Math.min(mTop, mHeight);
520
+ mBottom = Math.min(mBottom, mHeight);
521
+ if (event.type === 'drag') {
522
+ if (node._temporaryRemoved)
523
+ return; // handled by dropover
524
+ var distance = ui.position.top - node._prevYPix;
525
+ node._prevYPix = ui.position.top;
526
+ utils_1.Utils.updateScrollPosition(el, ui.position, distance);
527
+ // get new position taking into account the margin in the direction we are moving! (need to pass mid point by margin)
528
+ var left = ui.position.left + (ui.position.left > node._lastUiPosition.left ? -mRight : mLeft);
529
+ var top_1 = ui.position.top + (ui.position.top > node._lastUiPosition.top ? -mBottom : mTop);
530
+ p.x = Math.round(left / cellWidth);
531
+ p.y = Math.round(top_1 / cellHeight);
532
+ // @ts-ignore// if we're at the bottom hitting something else, grow the grid so cursor doesn't leave when trying to place below others
533
+ var prev = this._extraDragRow;
534
+ if (this.engine.collide(node, p)) {
535
+ var row = this.getRow();
536
+ var extra = Math.max(0, (p.y + node.h) - row);
537
+ if (this.opts.maxRow && row + extra > this.opts.maxRow) {
538
+ extra = Math.max(0, this.opts.maxRow - row);
539
+ } // @ts-ignore
540
+ this._extraDragRow = extra; // @ts-ignore
541
+ }
542
+ else
543
+ this._extraDragRow = 0; // @ts-ignore
544
+ if (this._extraDragRow !== prev)
545
+ this._updateContainerHeight();
546
+ if (node.x === p.x && node.y === p.y)
547
+ return; // skip same
548
+ // DON'T skip one we tried as we might have failed because of coverage <50% before
549
+ // if (node._lastTried && node._lastTried.x === x && node._lastTried.y === y) return;
550
+ }
551
+ else if (event.type === 'resize') {
552
+ if (p.x < 0)
553
+ return;
554
+ // Scrolling page if needed
555
+ utils_1.Utils.updateScrollResize(event, el, cellHeight);
556
+ // get new size
557
+ p.w = Math.round((ui.size.width - mLeft) / cellWidth);
558
+ p.h = Math.round((ui.size.height - mTop) / cellHeight);
559
+ if (node.w === p.w && node.h === p.h)
560
+ return;
561
+ if (node._lastTried && node._lastTried.w === p.w && node._lastTried.h === p.h)
562
+ return; // skip one we tried (but failed)
563
+ // if we size on left/top side this might move us, so get possible new position as well
564
+ var left = ui.position.left + mLeft;
565
+ var top_2 = ui.position.top + mTop;
566
+ p.x = Math.round(left / cellWidth);
567
+ p.y = Math.round(top_2 / cellHeight);
568
+ resizing = true;
569
+ }
570
+ node._lastTried = p; // set as last tried (will nuke if we go there)
571
+ var rect = {
572
+ x: ui.position.left + mLeft,
573
+ y: ui.position.top + mTop,
574
+ w: (ui.size ? ui.size.width : node.w * cellWidth) - mLeft - mRight,
575
+ h: (ui.size ? ui.size.height : node.h * cellHeight) - mTop - mBottom
576
+ };
577
+ if (this.engine.moveNodeCheck(node, __assign(__assign({}, p), { cellWidth: cellWidth, cellHeight: cellHeight, rect: rect, resizing: resizing }))) {
578
+ node._lastUiPosition = ui.position;
579
+ this.engine.cacheRects(cellWidth, cellHeight, mTop, mRight, mBottom, mLeft);
580
+ delete node._skipDown;
581
+ if (resizing && node.subGrid) {
582
+ node.subGrid.onParentResize();
583
+ } // @ts-ignore
584
+ this._extraDragRow = 0; // @ts-ignore
585
+ this._updateContainerHeight();
586
+ var target = event.target; // @ts-ignore
587
+ this._writePosAttr(target, node);
588
+ if (this._gsEventHandler[event.type]) {
589
+ this._gsEventHandler[event.type](event, target);
590
+ }
591
+ }
592
+ };
593
+ /**
594
+ * Enables/Disables moving.
595
+ * @param els widget or selector to modify.
596
+ * @param val if true widget will be draggable.
597
+ */
598
+ gridstack_1.GridStack.prototype.movable = function (els, val) {
599
+ var _this = this;
600
+ if (this.opts.staticGrid)
601
+ return this; // can't move a static grid!
602
+ gridstack_1.GridStack.getElements(els).forEach(function (el) {
603
+ var node = el.gridstackNode;
604
+ if (!node)
605
+ return;
606
+ if (val)
607
+ delete node.noMove;
608
+ else
609
+ node.noMove = true;
610
+ _this._prepareDragDropByNode(node); // init DD if need be, and adjust
611
+ });
612
+ return this;
613
+ };
614
+ /**
615
+ * Enables/Disables resizing.
616
+ * @param els widget or selector to modify
617
+ * @param val if true widget will be resizable.
618
+ */
619
+ gridstack_1.GridStack.prototype.resizable = function (els, val) {
620
+ var _this = this;
621
+ if (this.opts.staticGrid)
622
+ return this; // can't resize a static grid!
623
+ gridstack_1.GridStack.getElements(els).forEach(function (el) {
624
+ var node = el.gridstackNode;
625
+ if (!node)
626
+ return;
627
+ if (val)
628
+ delete node.noResize;
629
+ else
630
+ node.noResize = true;
631
+ _this._prepareDragDropByNode(node); // init DD if need be, and adjust
632
+ });
633
+ return this;
634
+ };
635
+ /**
636
+ * Temporarily disables widgets moving/resizing.
637
+ * If you want a more permanent way (which freezes up resources) use `setStatic(true)` instead.
638
+ * Note: no-op for static grid
639
+ * This is a shortcut for:
640
+ * @example
641
+ * grid.enableMove(false);
642
+ * grid.enableResize(false);
643
+ */
644
+ gridstack_1.GridStack.prototype.disable = function () {
645
+ if (this.opts.staticGrid)
646
+ return;
647
+ this.enableMove(false);
648
+ this.enableResize(false); // @ts-ignore
649
+ this._triggerEvent('disable');
650
+ return this;
651
+ };
652
+ /**
653
+ * Re-enables widgets moving/resizing - see disable().
654
+ * Note: no-op for static grid.
655
+ * This is a shortcut for:
656
+ * @example
657
+ * grid.enableMove(true);
658
+ * grid.enableResize(true);
659
+ */
660
+ gridstack_1.GridStack.prototype.enable = function () {
661
+ if (this.opts.staticGrid)
662
+ return;
663
+ this.enableMove(true);
664
+ this.enableResize(true); // @ts-ignore
665
+ this._triggerEvent('enable');
666
+ return this;
667
+ };
668
+ /** Enables/disables widget moving. No-op for static grids. */
669
+ gridstack_1.GridStack.prototype.enableMove = function (doEnable) {
670
+ var _this = this;
671
+ if (this.opts.staticGrid)
672
+ return this; // can't move a static grid!
673
+ this.opts.disableDrag = !doEnable; // FIRST before we update children as grid overrides #1658
674
+ this.engine.nodes.forEach(function (n) { return _this.movable(n.el, doEnable); });
675
+ return this;
676
+ };
677
+ /** Enables/disables widget resizing. No-op for static grids. */
678
+ gridstack_1.GridStack.prototype.enableResize = function (doEnable) {
679
+ var _this = this;
680
+ if (this.opts.staticGrid)
681
+ return this; // can't size a static grid!
682
+ this.opts.disableResize = !doEnable; // FIRST before we update children as grid overrides #1658
683
+ this.engine.nodes.forEach(function (n) { return _this.resizable(n.el, doEnable); });
684
+ return this;
685
+ };
686
+ //# sourceMappingURL=gridstack-dd.js.map