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