gridstack 7.2.3 → 8.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 (196) hide show
  1. package/README.md +9 -3
  2. package/dist/dd-base-impl.d.ts +20 -20
  3. package/dist/dd-base-impl.js +31 -35
  4. package/dist/dd-base-impl.js.map +1 -1
  5. package/dist/dd-draggable.d.ts +28 -28
  6. package/dist/dd-draggable.js +336 -345
  7. package/dist/dd-draggable.js.map +1 -1
  8. package/dist/dd-droppable.d.ts +26 -26
  9. package/dist/dd-droppable.js +146 -148
  10. package/dist/dd-droppable.js.map +1 -1
  11. package/dist/dd-element.d.ts +27 -27
  12. package/dist/dd-element.js +90 -94
  13. package/dist/dd-element.js.map +1 -1
  14. package/dist/dd-gridstack.d.ts +34 -34
  15. package/dist/dd-gridstack.js +127 -124
  16. package/dist/dd-gridstack.js.map +1 -1
  17. package/dist/dd-manager.d.ts +22 -22
  18. package/dist/dd-manager.js +9 -13
  19. package/dist/dd-manager.js.map +1 -1
  20. package/dist/dd-resizable-handle.d.ts +14 -14
  21. package/dist/dd-resizable-handle.js +102 -105
  22. package/dist/dd-resizable-handle.js.map +1 -1
  23. package/dist/dd-resizable.d.ts +28 -28
  24. package/dist/dd-resizable.js +290 -296
  25. package/dist/dd-resizable.js.map +1 -1
  26. package/dist/dd-touch.d.ts +33 -33
  27. package/dist/dd-touch.js +173 -181
  28. package/dist/dd-touch.js.map +1 -1
  29. package/dist/es5/dd-base-impl.d.ts +20 -20
  30. package/dist/es5/dd-base-impl.js +40 -40
  31. package/dist/es5/dd-base-impl.js.map +1 -1
  32. package/dist/es5/dd-draggable.d.ts +28 -28
  33. package/dist/es5/dd-draggable.js +366 -368
  34. package/dist/es5/dd-draggable.js.map +1 -1
  35. package/dist/es5/dd-droppable.d.ts +26 -26
  36. package/dist/es5/dd-droppable.js +181 -179
  37. package/dist/es5/dd-droppable.js.map +1 -1
  38. package/dist/es5/dd-element.d.ts +27 -27
  39. package/dist/es5/dd-element.js +95 -95
  40. package/dist/es5/dd-element.js.map +1 -1
  41. package/dist/es5/dd-gridstack.d.ts +34 -34
  42. package/dist/es5/dd-gridstack.js +144 -144
  43. package/dist/es5/dd-gridstack.js.map +1 -1
  44. package/dist/es5/dd-manager.d.ts +22 -22
  45. package/dist/es5/dd-manager.js +16 -16
  46. package/dist/es5/dd-manager.js.map +1 -1
  47. package/dist/es5/dd-resizable-handle.d.ts +14 -14
  48. package/dist/es5/dd-resizable-handle.js +105 -106
  49. package/dist/es5/dd-resizable-handle.js.map +1 -1
  50. package/dist/es5/dd-resizable.d.ts +28 -28
  51. package/dist/es5/dd-resizable.js +317 -318
  52. package/dist/es5/dd-resizable.js.map +1 -1
  53. package/dist/es5/dd-touch.d.ts +33 -33
  54. package/dist/es5/dd-touch.js +185 -184
  55. package/dist/es5/dd-touch.js.map +1 -1
  56. package/dist/es5/gridstack-all.js +1 -1
  57. package/dist/es5/gridstack-all.js.LICENSE.txt +1 -1
  58. package/dist/es5/gridstack-all.js.map +1 -1
  59. package/dist/es5/gridstack-engine.d.ts +102 -100
  60. package/dist/es5/gridstack-engine.js +1000 -976
  61. package/dist/es5/gridstack-engine.js.map +1 -1
  62. package/dist/es5/gridstack-poly.js +1 -1
  63. package/dist/es5/gridstack.d.ts +389 -376
  64. package/dist/es5/gridstack.js +2252 -2227
  65. package/dist/es5/gridstack.js.map +1 -1
  66. package/dist/es5/types.d.ts +279 -279
  67. package/dist/es5/types.js +47 -35
  68. package/dist/es5/types.js.map +1 -1
  69. package/dist/es5/utils.d.ts +95 -91
  70. package/dist/es5/utils.js +591 -566
  71. package/dist/es5/utils.js.map +1 -1
  72. package/dist/gridstack-all.js +1 -1
  73. package/dist/gridstack-all.js.LICENSE.txt +1 -1
  74. package/dist/gridstack-all.js.map +1 -1
  75. package/dist/gridstack-engine.d.ts +102 -100
  76. package/dist/gridstack-engine.js +950 -936
  77. package/dist/gridstack-engine.js.map +1 -1
  78. package/dist/gridstack-extra.css +0 -390
  79. package/dist/gridstack-extra.min.css +1 -1
  80. package/dist/gridstack.css +3 -97
  81. package/dist/gridstack.d.ts +389 -376
  82. package/dist/gridstack.js +2155 -2151
  83. package/dist/gridstack.js.map +1 -1
  84. package/dist/gridstack.min.css +1 -1
  85. package/dist/ng/README.md +154 -0
  86. package/dist/ng/gridstack-item.component.d.ts +29 -0
  87. package/dist/ng/gridstack-item.component.js +65 -0
  88. package/dist/ng/gridstack-item.component.js.map +1 -0
  89. package/dist/ng/gridstack.component.d.ts +118 -0
  90. package/dist/ng/gridstack.component.js +245 -0
  91. package/dist/ng/gridstack.component.js.map +1 -0
  92. package/dist/src/gridstack-extra.scss +0 -2
  93. package/dist/src/gridstack.scss +6 -9
  94. package/dist/types.d.ts +279 -279
  95. package/dist/types.js +44 -35
  96. package/dist/types.js.map +1 -1
  97. package/dist/utils.d.ts +95 -91
  98. package/dist/utils.js +539 -524
  99. package/dist/utils.js.map +1 -1
  100. package/{dist → dist_save}/angular/gridstack-item.component.ts +22 -4
  101. package/{dist → dist_save}/angular/gridstack.component.ts +57 -30
  102. package/dist_save/dd-base-impl.d.ts +20 -0
  103. package/dist_save/dd-base-impl.js +36 -0
  104. package/dist_save/dd-base-impl.js.map +1 -0
  105. package/dist_save/dd-draggable.d.ts +28 -0
  106. package/dist_save/dd-draggable.js +343 -0
  107. package/dist_save/dd-draggable.js.map +1 -0
  108. package/dist_save/dd-droppable.d.ts +26 -0
  109. package/dist_save/dd-droppable.js +149 -0
  110. package/dist_save/dd-droppable.js.map +1 -0
  111. package/dist_save/dd-element.d.ts +27 -0
  112. package/dist_save/dd-element.js +95 -0
  113. package/dist_save/dd-element.js.map +1 -0
  114. package/dist_save/dd-gridstack.d.ts +34 -0
  115. package/dist_save/dd-gridstack.js +125 -0
  116. package/dist_save/dd-gridstack.js.map +1 -0
  117. package/dist_save/dd-manager.d.ts +22 -0
  118. package/dist_save/dd-manager.js +14 -0
  119. package/dist_save/dd-manager.js.map +1 -0
  120. package/dist_save/dd-resizable-handle.d.ts +14 -0
  121. package/dist_save/dd-resizable-handle.js +106 -0
  122. package/dist_save/dd-resizable-handle.js.map +1 -0
  123. package/dist_save/dd-resizable.d.ts +28 -0
  124. package/dist_save/dd-resizable.js +294 -0
  125. package/dist_save/dd-resizable.js.map +1 -0
  126. package/dist_save/dd-touch.d.ts +33 -0
  127. package/dist_save/dd-touch.js +183 -0
  128. package/dist_save/dd-touch.js.map +1 -0
  129. package/dist_save/es5/dd-base-impl.d.ts +20 -0
  130. package/dist_save/es5/dd-base-impl.js +41 -0
  131. package/dist_save/es5/dd-base-impl.js.map +1 -0
  132. package/dist_save/es5/dd-draggable.d.ts +28 -0
  133. package/dist_save/es5/dd-draggable.js +366 -0
  134. package/dist_save/es5/dd-draggable.js.map +1 -0
  135. package/dist_save/es5/dd-droppable.d.ts +26 -0
  136. package/dist_save/es5/dd-droppable.js +180 -0
  137. package/dist_save/es5/dd-droppable.js.map +1 -0
  138. package/dist_save/es5/dd-element.d.ts +27 -0
  139. package/dist_save/es5/dd-element.js +96 -0
  140. package/dist_save/es5/dd-element.js.map +1 -0
  141. package/dist_save/es5/dd-gridstack.d.ts +34 -0
  142. package/dist_save/es5/dd-gridstack.js +145 -0
  143. package/dist_save/es5/dd-gridstack.js.map +1 -0
  144. package/dist_save/es5/dd-manager.d.ts +22 -0
  145. package/dist_save/es5/dd-manager.js +17 -0
  146. package/dist_save/es5/dd-manager.js.map +1 -0
  147. package/dist_save/es5/dd-resizable-handle.d.ts +14 -0
  148. package/dist_save/es5/dd-resizable-handle.js +107 -0
  149. package/dist_save/es5/dd-resizable-handle.js.map +1 -0
  150. package/dist_save/es5/dd-resizable.d.ts +28 -0
  151. package/dist_save/es5/dd-resizable.js +316 -0
  152. package/dist_save/es5/dd-resizable.js.map +1 -0
  153. package/dist_save/es5/dd-touch.d.ts +33 -0
  154. package/dist_save/es5/dd-touch.js +186 -0
  155. package/dist_save/es5/dd-touch.js.map +1 -0
  156. package/dist_save/es5/gridstack-all.js +3 -0
  157. package/dist_save/es5/gridstack-all.js.LICENSE.txt +7 -0
  158. package/dist_save/es5/gridstack-all.js.map +1 -0
  159. package/dist_save/es5/gridstack-engine.d.ts +102 -0
  160. package/dist_save/es5/gridstack-engine.js +997 -0
  161. package/dist_save/es5/gridstack-engine.js.map +1 -0
  162. package/dist_save/es5/gridstack-poly.js +356 -0
  163. package/dist_save/es5/gridstack.d.ts +376 -0
  164. package/dist_save/es5/gridstack.js +2238 -0
  165. package/dist_save/es5/gridstack.js.map +1 -0
  166. package/dist_save/es5/types.d.ts +284 -0
  167. package/dist_save/es5/types.js +36 -0
  168. package/dist_save/es5/types.js.map +1 -0
  169. package/dist_save/es5/utils.d.ts +95 -0
  170. package/dist_save/es5/utils.js +590 -0
  171. package/dist_save/es5/utils.js.map +1 -0
  172. package/dist_save/gridstack-all.js +3 -0
  173. package/dist_save/gridstack-all.js.LICENSE.txt +7 -0
  174. package/dist_save/gridstack-all.js.map +1 -0
  175. package/dist_save/gridstack-engine.d.ts +102 -0
  176. package/dist_save/gridstack-engine.js +956 -0
  177. package/dist_save/gridstack-engine.js.map +1 -0
  178. package/dist_save/gridstack-extra.css +433 -0
  179. package/dist_save/gridstack-extra.min.css +1 -0
  180. package/dist_save/gridstack.css +226 -0
  181. package/dist_save/gridstack.d.ts +376 -0
  182. package/dist_save/gridstack.js +2162 -0
  183. package/dist_save/gridstack.js.map +1 -0
  184. package/dist_save/gridstack.min.css +1 -0
  185. package/dist_save/src/gridstack-extra.scss +27 -0
  186. package/dist_save/src/gridstack.scss +131 -0
  187. package/dist_save/types.d.ts +284 -0
  188. package/dist_save/types.js +36 -0
  189. package/dist_save/types.js.map +1 -0
  190. package/dist_save/utils.d.ts +95 -0
  191. package/dist_save/utils.js +548 -0
  192. package/dist_save/utils.js.map +1 -0
  193. package/doc/CHANGES.md +19 -0
  194. package/doc/README.md +17 -1
  195. package/package.json +25 -24
  196. /package/{dist → dist_save}/angular/README.md +0 -0
@@ -1,977 +1,1001 @@
1
- "use strict";
2
- /**
3
- * gridstack-engine.ts 7.2.3
4
- * Copyright (c) 2021-2022 Alain Dumesny - see GridStack root license
5
- */
6
- var __assign = (this && this.__assign) || function () {
7
- __assign = Object.assign || function(t) {
8
- for (var s, i = 1, n = arguments.length; i < n; i++) {
9
- s = arguments[i];
10
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
11
- t[p] = s[p];
12
- }
13
- return t;
14
- };
15
- return __assign.apply(this, arguments);
16
- };
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.GridStackEngine = void 0;
19
- var utils_1 = require("./utils");
20
- /**
21
- * Defines the GridStack engine that does most no DOM grid manipulation.
22
- * See GridStack methods and vars for descriptions.
23
- *
24
- * NOTE: values should not be modified directly - call the main GridStack API instead
25
- */
26
- var GridStackEngine = /** @class */ (function () {
27
- function GridStackEngine(opts) {
28
- if (opts === void 0) { opts = {}; }
29
- this.addedNodes = [];
30
- this.removedNodes = [];
31
- this.column = opts.column || 12;
32
- this.maxRow = opts.maxRow;
33
- this._float = opts.float;
34
- this.nodes = opts.nodes || [];
35
- this.onChange = opts.onChange;
36
- }
37
- GridStackEngine.prototype.batchUpdate = function (flag) {
38
- if (flag === void 0) { flag = true; }
39
- if (!!this.batchMode === flag)
40
- return this;
41
- this.batchMode = flag;
42
- if (flag) {
43
- this._prevFloat = this._float;
44
- this._float = true; // let things go anywhere for now... will restore and possibly reposition later
45
- this.saveInitial(); // since begin update (which is called multiple times) won't do this
46
- }
47
- else {
48
- this._float = this._prevFloat;
49
- delete this._prevFloat;
50
- this._packNodes()._notify();
51
- }
52
- return this;
53
- };
54
- // use entire row for hitting area (will use bottom reverse sorted first) if we not actively moving DOWN and didn't already skip
55
- GridStackEngine.prototype._useEntireRowArea = function (node, nn) {
56
- return !this.float && !this._hasLocked && (!node._moving || node._skipDown || nn.y <= node.y);
57
- };
58
- /** @internal fix collision on given 'node', going to given new location 'nn', with optional 'collide' node already found.
59
- * return true if we moved. */
60
- GridStackEngine.prototype._fixCollisions = function (node, nn, collide, opt) {
61
- if (nn === void 0) { nn = node; }
62
- if (opt === void 0) { opt = {}; }
63
- this.sortNodes(-1); // from last to first, so recursive collision move items in the right order
64
- collide = collide || this.collide(node, nn); // REAL area collide for swap and skip if none...
65
- if (!collide)
66
- return false;
67
- // swap check: if we're actively moving in gravity mode, see if we collide with an object the same size
68
- if (node._moving && !opt.nested && !this.float) {
69
- if (this.swap(node, collide))
70
- return true;
71
- }
72
- // during while() collisions MAKE SURE to check entire row so larger items don't leap frog small ones (push them all down starting last in grid)
73
- var area = nn;
74
- if (this._useEntireRowArea(node, nn)) {
75
- area = { x: 0, w: this.column, y: nn.y, h: nn.h };
76
- collide = this.collide(node, area, opt.skip); // force new hit
77
- }
78
- var didMove = false;
79
- var newOpt = { nested: true, pack: false };
80
- while (collide = collide || this.collide(node, area, opt.skip)) { // could collide with more than 1 item... so repeat for each
81
- var moved = void 0;
82
- // if colliding with a locked item OR moving down with top gravity (and collide could move up) -> skip past the collide,
83
- // but remember that skip down so we only do this once (and push others otherwise).
84
- if (collide.locked || node._moving && !node._skipDown && nn.y > node.y && !this.float &&
85
- // can take space we had, or before where we're going
86
- (!this.collide(collide, __assign(__assign({}, collide), { y: node.y }), node) || !this.collide(collide, __assign(__assign({}, collide), { y: nn.y - collide.h }), node))) {
87
- node._skipDown = (node._skipDown || nn.y > node.y);
88
- moved = this.moveNode(node, __assign(__assign(__assign({}, nn), { y: collide.y + collide.h }), newOpt));
89
- if (collide.locked && moved) {
90
- utils_1.Utils.copyPos(nn, node); // moving after lock become our new desired location
91
- }
92
- else if (!collide.locked && moved && opt.pack) {
93
- // we moved after and will pack: do it now and keep the original drop location, but past the old collide to see what else we might push way
94
- this._packNodes();
95
- nn.y = collide.y + collide.h;
96
- utils_1.Utils.copyPos(node, nn);
97
- }
98
- didMove = didMove || moved;
99
- }
100
- else {
101
- // move collide down *after* where we will be, ignoring where we are now (don't collide with us)
102
- moved = this.moveNode(collide, __assign(__assign(__assign({}, collide), { y: nn.y + nn.h, skip: node }), newOpt));
103
- }
104
- if (!moved) {
105
- return didMove;
106
- } // break inf loop if we couldn't move after all (ex: maxRow, fixed)
107
- collide = undefined;
108
- }
109
- return didMove;
110
- };
111
- /** return the nodes that intercept the given node. Optionally a different area can be used, as well as a second node to skip */
112
- GridStackEngine.prototype.collide = function (skip, area, skip2) {
113
- if (area === void 0) { area = skip; }
114
- return this.nodes.find(function (n) { return n !== skip && n !== skip2 && utils_1.Utils.isIntercepted(n, area); });
115
- };
116
- GridStackEngine.prototype.collideAll = function (skip, area, skip2) {
117
- if (area === void 0) { area = skip; }
118
- return this.nodes.filter(function (n) { return n !== skip && n !== skip2 && utils_1.Utils.isIntercepted(n, area); });
119
- };
120
- /** does a pixel coverage collision based on where we started, returning the node that has the most coverage that is >50% mid line */
121
- GridStackEngine.prototype.directionCollideCoverage = function (node, o, collides) {
122
- if (!o.rect || !node._rect)
123
- return;
124
- var r0 = node._rect; // where started
125
- var r = __assign({}, o.rect); // where we are
126
- // update dragged rect to show where it's coming from (above or below, etc...)
127
- if (r.y > r0.y) {
128
- r.h += r.y - r0.y;
129
- r.y = r0.y;
130
- }
131
- else {
132
- r.h += r0.y - r.y;
133
- }
134
- if (r.x > r0.x) {
135
- r.w += r.x - r0.x;
136
- r.x = r0.x;
137
- }
138
- else {
139
- r.w += r0.x - r.x;
140
- }
141
- var collide;
142
- collides.forEach(function (n) {
143
- if (n.locked || !n._rect)
144
- return;
145
- var r2 = n._rect; // overlapping target
146
- var yOver = Number.MAX_VALUE, xOver = Number.MAX_VALUE, overMax = 0.5; // need >50%
147
- // depending on which side we started from, compute the overlap % of coverage
148
- // (ex: from above/below we only compute the max horizontal line coverage)
149
- if (r0.y < r2.y) { // from above
150
- yOver = ((r.y + r.h) - r2.y) / r2.h;
151
- }
152
- else if (r0.y + r0.h > r2.y + r2.h) { // from below
153
- yOver = ((r2.y + r2.h) - r.y) / r2.h;
154
- }
155
- if (r0.x < r2.x) { // from the left
156
- xOver = ((r.x + r.w) - r2.x) / r2.w;
157
- }
158
- else if (r0.x + r0.w > r2.x + r2.w) { // from the right
159
- xOver = ((r2.x + r2.w) - r.x) / r2.w;
160
- }
161
- var over = Math.min(xOver, yOver);
162
- if (over > overMax) {
163
- overMax = over;
164
- collide = n;
165
- }
166
- });
167
- o.collide = collide; // save it so we don't have to find it again
168
- return collide;
169
- };
170
- /** does a pixel coverage returning the node that has the most coverage by area */
171
- /*
172
- protected collideCoverage(r: GridStackPosition, collides: GridStackNode[]): {collide: GridStackNode, over: number} {
173
- let collide: GridStackNode;
174
- let overMax = 0;
175
- collides.forEach(n => {
176
- if (n.locked || !n._rect) return;
177
- let over = Utils.areaIntercept(r, n._rect);
178
- if (over > overMax) {
179
- overMax = over;
180
- collide = n;
181
- }
182
- });
183
- return {collide, over: overMax};
184
- }
185
- */
186
- /** called to cache the nodes pixel rectangles used for collision detection during drag */
187
- GridStackEngine.prototype.cacheRects = function (w, h, top, right, bottom, left) {
188
- this.nodes.forEach(function (n) {
189
- return n._rect = {
190
- y: n.y * h + top,
191
- x: n.x * w + left,
192
- w: n.w * w - left - right,
193
- h: n.h * h - top - bottom
194
- };
195
- });
196
- return this;
197
- };
198
- /** called to possibly swap between 2 nodes (same size or column, not locked, touching), returning true if successful */
199
- GridStackEngine.prototype.swap = function (a, b) {
200
- if (!b || b.locked || !a || a.locked)
201
- return false;
202
- function _doSwap() {
203
- var x = b.x, y = b.y;
204
- b.x = a.x;
205
- b.y = a.y; // b -> a position
206
- if (a.h != b.h) {
207
- a.x = x;
208
- a.y = b.y + b.h; // a -> goes after b
209
- }
210
- else if (a.w != b.w) {
211
- a.x = b.x + b.w;
212
- a.y = y; // a -> goes after b
213
- }
214
- else {
215
- a.x = x;
216
- a.y = y; // a -> old b position
217
- }
218
- a._dirty = b._dirty = true;
219
- return true;
220
- }
221
- var touching; // remember if we called it (vs undefined)
222
- // same size and same row or column, and touching
223
- if (a.w === b.w && a.h === b.h && (a.x === b.x || a.y === b.y) && (touching = utils_1.Utils.isTouching(a, b)))
224
- return _doSwap();
225
- if (touching === false)
226
- return; // IFF ran test and fail, bail out
227
- // check for taking same columns (but different height) and touching
228
- if (a.w === b.w && a.x === b.x && (touching || (touching = utils_1.Utils.isTouching(a, b)))) {
229
- if (b.y < a.y) {
230
- var t = a;
231
- a = b;
232
- b = t;
233
- } // swap a <-> b vars so a is first
234
- return _doSwap();
235
- }
236
- if (touching === false)
237
- return;
238
- // check if taking same row (but different width) and touching
239
- if (a.h === b.h && a.y === b.y && (touching || (touching = utils_1.Utils.isTouching(a, b)))) {
240
- if (b.x < a.x) {
241
- var t = a;
242
- a = b;
243
- b = t;
244
- } // swap a <-> b vars so a is first
245
- return _doSwap();
246
- }
247
- return false;
248
- };
249
- GridStackEngine.prototype.isAreaEmpty = function (x, y, w, h) {
250
- var nn = { x: x || 0, y: y || 0, w: w || 1, h: h || 1 };
251
- return !this.collide(nn);
252
- };
253
- /** re-layout grid items to reclaim any empty space */
254
- GridStackEngine.prototype.compact = function () {
255
- var _this = this;
256
- if (this.nodes.length === 0)
257
- return this;
258
- this.batchUpdate()
259
- .sortNodes();
260
- var copyNodes = this.nodes;
261
- this.nodes = []; // pretend we have no nodes to conflict layout to start with...
262
- copyNodes.forEach(function (node) {
263
- if (!node.locked) {
264
- node.autoPosition = true;
265
- }
266
- _this.addNode(node, false); // 'false' for add event trigger
267
- node._dirty = true; // will force attr update
268
- });
269
- return this.batchUpdate(false);
270
- };
271
- Object.defineProperty(GridStackEngine.prototype, "float", {
272
- /** float getter method */
273
- get: function () { return this._float || false; },
274
- /** enable/disable floating widgets (default: `false`) See [example](http://gridstackjs.com/demo/float.html) */
275
- set: function (val) {
276
- if (this._float === val)
277
- return;
278
- this._float = val || false;
279
- if (!val) {
280
- this._packNodes()._notify();
281
- }
282
- },
283
- enumerable: false,
284
- configurable: true
285
- });
286
- /** sort the nodes array from first to last, or reverse. Called during collision/placement to force an order */
287
- GridStackEngine.prototype.sortNodes = function (dir) {
288
- this.nodes = utils_1.Utils.sort(this.nodes, dir, this.column);
289
- return this;
290
- };
291
- /** @internal called to top gravity pack the items back OR revert back to original Y positions when floating */
292
- GridStackEngine.prototype._packNodes = function () {
293
- var _this = this;
294
- if (this.batchMode) {
295
- return this;
296
- }
297
- this.sortNodes(); // first to last
298
- if (this.float) {
299
- // restore original Y pos
300
- this.nodes.forEach(function (n) {
301
- if (n._updating || n._orig === undefined || n.y === n._orig.y)
302
- return;
303
- var newY = n.y;
304
- while (newY > n._orig.y) {
305
- --newY;
306
- var collide = _this.collide(n, { x: n.x, y: newY, w: n.w, h: n.h });
307
- if (!collide) {
308
- n._dirty = true;
309
- n.y = newY;
310
- }
311
- }
312
- });
313
- }
314
- else {
315
- // top gravity pack
316
- this.nodes.forEach(function (n, i) {
317
- if (n.locked)
318
- return;
319
- while (n.y > 0) {
320
- var newY = i === 0 ? 0 : n.y - 1;
321
- var canBeMoved = i === 0 || !_this.collide(n, { x: n.x, y: newY, w: n.w, h: n.h });
322
- if (!canBeMoved)
323
- break;
324
- // Note: must be dirty (from last position) for GridStack::OnChange CB to update positions
325
- // and move items back. The user 'change' CB should detect changes from the original
326
- // starting position instead.
327
- n._dirty = (n.y !== newY);
328
- n.y = newY;
329
- }
330
- });
331
- }
332
- return this;
333
- };
334
- /**
335
- * given a random node, makes sure it's coordinates/values are valid in the current grid
336
- * @param node to adjust
337
- * @param resizing if out of bound, resize down or move into the grid to fit ?
338
- */
339
- GridStackEngine.prototype.prepareNode = function (node, resizing) {
340
- node = node || {};
341
- node._id = node._id || GridStackEngine._idSeq++;
342
- // if we're missing position, have the grid position us automatically (before we set them to 0,0)
343
- if (node.x === undefined || node.y === undefined || node.x === null || node.y === null) {
344
- node.autoPosition = true;
345
- }
346
- // assign defaults for missing required fields
347
- var defaults = { x: 0, y: 0, w: 1, h: 1 };
348
- utils_1.Utils.defaults(node, defaults);
349
- if (!node.autoPosition) {
350
- delete node.autoPosition;
351
- }
352
- if (!node.noResize) {
353
- delete node.noResize;
354
- }
355
- if (!node.noMove) {
356
- delete node.noMove;
357
- }
358
- // check for NaN (in case messed up strings were passed. can't do parseInt() || defaults.x above as 0 is valid #)
359
- if (typeof node.x == 'string') {
360
- node.x = Number(node.x);
361
- }
362
- if (typeof node.y == 'string') {
363
- node.y = Number(node.y);
364
- }
365
- if (typeof node.w == 'string') {
366
- node.w = Number(node.w);
367
- }
368
- if (typeof node.h == 'string') {
369
- node.h = Number(node.h);
370
- }
371
- if (isNaN(node.x)) {
372
- node.x = defaults.x;
373
- node.autoPosition = true;
374
- }
375
- if (isNaN(node.y)) {
376
- node.y = defaults.y;
377
- node.autoPosition = true;
378
- }
379
- if (isNaN(node.w)) {
380
- node.w = defaults.w;
381
- }
382
- if (isNaN(node.h)) {
383
- node.h = defaults.h;
384
- }
385
- return this.nodeBoundFix(node, resizing);
386
- };
387
- /** part2 of preparing a node to fit inside our grid - checks for x,y,w from grid dimensions */
388
- GridStackEngine.prototype.nodeBoundFix = function (node, resizing) {
389
- var before = node._orig || utils_1.Utils.copyPos({}, node);
390
- if (node.maxW) {
391
- node.w = Math.min(node.w, node.maxW);
392
- }
393
- if (node.maxH) {
394
- node.h = Math.min(node.h, node.maxH);
395
- }
396
- if (node.minW && node.minW <= this.column) {
397
- node.w = Math.max(node.w, node.minW);
398
- }
399
- if (node.minH) {
400
- node.h = Math.max(node.h, node.minH);
401
- }
402
- // if user loaded a larger than allowed widget for current # of columns (or force 1 column mode),
403
- // remember it's position & width so we can restore back (1 -> 12 column) #1655 #1985
404
- // IFF we're not in the middle of column resizing!
405
- var saveOrig = this.column === 1 || node.x + node.w > this.column;
406
- if (saveOrig && this.column < 12 && !this._inColumnResize && !node.autoPosition && node._id && this.findCacheLayout(node, 12) === -1) {
407
- var copy = __assign({}, node); // need _id + positions
408
- copy.x = Math.min(11, copy.x);
409
- copy.w = Math.min(12, copy.w);
410
- this.cacheOneLayout(copy, 12);
411
- }
412
- if (node.w > this.column) {
413
- node.w = this.column;
414
- }
415
- else if (node.w < 1) {
416
- node.w = 1;
417
- }
418
- if (this.maxRow && node.h > this.maxRow) {
419
- node.h = this.maxRow;
420
- }
421
- else if (node.h < 1) {
422
- node.h = 1;
423
- }
424
- if (node.x < 0) {
425
- node.x = 0;
426
- }
427
- if (node.y < 0) {
428
- node.y = 0;
429
- }
430
- if (node.x + node.w > this.column) {
431
- if (resizing) {
432
- node.w = this.column - node.x;
433
- }
434
- else {
435
- node.x = this.column - node.w;
436
- }
437
- }
438
- if (this.maxRow && node.y + node.h > this.maxRow) {
439
- if (resizing) {
440
- node.h = this.maxRow - node.y;
441
- }
442
- else {
443
- node.y = this.maxRow - node.h;
444
- }
445
- }
446
- if (!utils_1.Utils.samePos(node, before)) {
447
- node._dirty = true;
448
- }
449
- return node;
450
- };
451
- /** returns a list of modified nodes from their original values */
452
- GridStackEngine.prototype.getDirtyNodes = function (verify) {
453
- // compare original x,y,w,h instead as _dirty can be a temporary state
454
- if (verify) {
455
- return this.nodes.filter(function (n) { return n._dirty && !utils_1.Utils.samePos(n, n._orig); });
456
- }
457
- return this.nodes.filter(function (n) { return n._dirty; });
458
- };
459
- /** @internal call this to call onChange callback with dirty nodes so DOM can be updated */
460
- GridStackEngine.prototype._notify = function (removedNodes) {
461
- if (this.batchMode || !this.onChange)
462
- return this;
463
- var dirtyNodes = (removedNodes || []).concat(this.getDirtyNodes());
464
- this.onChange(dirtyNodes);
465
- return this;
466
- };
467
- /** @internal remove dirty and last tried info */
468
- GridStackEngine.prototype.cleanNodes = function () {
469
- if (this.batchMode)
470
- return this;
471
- this.nodes.forEach(function (n) {
472
- delete n._dirty;
473
- delete n._lastTried;
474
- });
475
- return this;
476
- };
477
- /** @internal called to save initial position/size to track real dirty state.
478
- * Note: should be called right after we call change event (so next API is can detect changes)
479
- * as well as right before we start move/resize/enter (so we can restore items to prev values) */
480
- GridStackEngine.prototype.saveInitial = function () {
481
- this.nodes.forEach(function (n) {
482
- n._orig = utils_1.Utils.copyPos({}, n);
483
- delete n._dirty;
484
- });
485
- this._hasLocked = this.nodes.some(function (n) { return n.locked; });
486
- return this;
487
- };
488
- /** @internal restore all the nodes back to initial values (called when we leave) */
489
- GridStackEngine.prototype.restoreInitial = function () {
490
- this.nodes.forEach(function (n) {
491
- if (utils_1.Utils.samePos(n, n._orig))
492
- return;
493
- utils_1.Utils.copyPos(n, n._orig);
494
- n._dirty = true;
495
- });
496
- this._notify();
497
- return this;
498
- };
499
- /** find the first available empty spot for the given node width/height, updating the x,y attributes. return true if found */
500
- GridStackEngine.prototype.findEmptyPosition = function (node) {
501
- this.sortNodes();
502
- var found = false;
503
- var _loop_1 = function (i) {
504
- var x = i % this_1.column;
505
- var y = Math.floor(i / this_1.column);
506
- if (x + node.w > this_1.column) {
507
- return "continue";
508
- }
509
- var box = { x: x, y: y, w: node.w, h: node.h };
510
- if (!this_1.nodes.find(function (n) { return utils_1.Utils.isIntercepted(box, n); })) {
511
- node.x = x;
512
- node.y = y;
513
- found = true;
514
- }
515
- };
516
- var this_1 = this;
517
- for (var i = 0; !found; ++i) {
518
- _loop_1(i);
519
- }
520
- return found;
521
- };
522
- /** call to add the given node to our list, fixing collision and re-packing */
523
- GridStackEngine.prototype.addNode = function (node, triggerAddEvent) {
524
- if (triggerAddEvent === void 0) { triggerAddEvent = false; }
525
- var dup = this.nodes.find(function (n) { return n._id === node._id; });
526
- if (dup)
527
- return dup; // prevent inserting twice! return it instead.
528
- // skip prepareNode if we're in middle of column resize (not new) but do check for bounds!
529
- node = this._inColumnResize ? this.nodeBoundFix(node) : this.prepareNode(node);
530
- delete node._temporaryRemoved;
531
- delete node._removeDOM;
532
- if (node.autoPosition && this.findEmptyPosition(node)) {
533
- delete node.autoPosition; // found our slot
534
- }
535
- this.nodes.push(node);
536
- if (triggerAddEvent) {
537
- this.addedNodes.push(node);
538
- }
539
- this._fixCollisions(node);
540
- if (!this.batchMode) {
541
- this._packNodes()._notify();
542
- }
543
- return node;
544
- };
545
- GridStackEngine.prototype.removeNode = function (node, removeDOM, triggerEvent) {
546
- if (removeDOM === void 0) { removeDOM = true; }
547
- if (triggerEvent === void 0) { triggerEvent = false; }
548
- if (!this.nodes.find(function (n) { return n === node; })) {
549
- // TEST console.log(`Error: GridStackEngine.removeNode() node._id=${node._id} not found!`)
550
- return this;
551
- }
552
- if (triggerEvent) { // we wait until final drop to manually track removed items (rather than during drag)
553
- this.removedNodes.push(node);
554
- }
555
- if (removeDOM)
556
- node._removeDOM = true; // let CB remove actual HTML (used to set _id to null, but then we loose layout info)
557
- // don't use 'faster' .splice(findIndex(),1) in case node isn't in our list, or in multiple times.
558
- this.nodes = this.nodes.filter(function (n) { return n !== node; });
559
- return this._packNodes()
560
- ._notify([node]);
561
- };
562
- GridStackEngine.prototype.removeAll = function (removeDOM) {
563
- if (removeDOM === void 0) { removeDOM = true; }
564
- delete this._layouts;
565
- if (this.nodes.length === 0)
566
- return this;
567
- removeDOM && this.nodes.forEach(function (n) { return n._removeDOM = true; }); // let CB remove actual HTML (used to set _id to null, but then we loose layout info)
568
- this.removedNodes = this.nodes;
569
- this.nodes = [];
570
- return this._notify(this.removedNodes);
571
- };
572
- /** checks if item can be moved (layout constrain) vs moveNode(), returning true if was able to move.
573
- * In more complicated cases (maxRow) it will attempt at moving the item and fixing
574
- * others in a clone first, then apply those changes if still within specs. */
575
- GridStackEngine.prototype.moveNodeCheck = function (node, o) {
576
- var _this = this;
577
- // if (node.locked) return false;
578
- if (!this.changedPosConstrain(node, o))
579
- return false;
580
- o.pack = true;
581
- // simpler case: move item directly...
582
- if (!this.maxRow) {
583
- return this.moveNode(node, o);
584
- }
585
- // complex case: create a clone with NO maxRow (will check for out of bounds at the end)
586
- var clonedNode;
587
- var clone = new GridStackEngine({
588
- column: this.column,
589
- float: this.float,
590
- nodes: this.nodes.map(function (n) {
591
- if (n === node) {
592
- clonedNode = __assign({}, n);
593
- return clonedNode;
594
- }
595
- return __assign({}, n);
596
- })
597
- });
598
- if (!clonedNode)
599
- return false;
600
- // check if we're covering 50% collision and could move
601
- var canMove = clone.moveNode(clonedNode, o) && clone.getRow() <= this.maxRow;
602
- // else check if we can force a swap (float=true, or different shapes) on non-resize
603
- if (!canMove && !o.resizing && o.collide) {
604
- var collide = o.collide.el.gridstackNode; // find the source node the clone collided with at 50%
605
- if (this.swap(node, collide)) { // swaps and mark dirty
606
- this._notify();
607
- return true;
608
- }
609
- }
610
- if (!canMove)
611
- return false;
612
- // if clone was able to move, copy those mods over to us now instead of caller trying to do this all over!
613
- // Note: we can't use the list directly as elements and other parts point to actual node, so copy content
614
- clone.nodes.filter(function (n) { return n._dirty; }).forEach(function (c) {
615
- var n = _this.nodes.find(function (a) { return a._id === c._id; });
616
- if (!n)
617
- return;
618
- utils_1.Utils.copyPos(n, c);
619
- n._dirty = true;
620
- });
621
- this._notify();
622
- return true;
623
- };
624
- /** return true if can fit in grid height constrain only (always true if no maxRow) */
625
- GridStackEngine.prototype.willItFit = function (node) {
626
- delete node._willFitPos;
627
- if (!this.maxRow)
628
- return true;
629
- // create a clone with NO maxRow and check if still within size
630
- var clone = new GridStackEngine({
631
- column: this.column,
632
- float: this.float,
633
- nodes: this.nodes.map(function (n) { return __assign({}, n); })
634
- });
635
- var n = __assign({}, node); // clone node so we don't mod any settings on it but have full autoPosition and min/max as well! #1687
636
- this.cleanupNode(n);
637
- delete n.el;
638
- delete n._id;
639
- delete n.content;
640
- delete n.grid;
641
- clone.addNode(n);
642
- if (clone.getRow() <= this.maxRow) {
643
- node._willFitPos = utils_1.Utils.copyPos({}, n);
644
- return true;
645
- }
646
- return false;
647
- };
648
- /** true if x,y or w,h are different after clamping to min/max */
649
- GridStackEngine.prototype.changedPosConstrain = function (node, p) {
650
- // first make sure w,h are set for caller
651
- p.w = p.w || node.w;
652
- p.h = p.h || node.h;
653
- if (node.x !== p.x || node.y !== p.y)
654
- return true;
655
- // check constrained w,h
656
- if (node.maxW) {
657
- p.w = Math.min(p.w, node.maxW);
658
- }
659
- if (node.maxH) {
660
- p.h = Math.min(p.h, node.maxH);
661
- }
662
- if (node.minW) {
663
- p.w = Math.max(p.w, node.minW);
664
- }
665
- if (node.minH) {
666
- p.h = Math.max(p.h, node.minH);
667
- }
668
- return (node.w !== p.w || node.h !== p.h);
669
- };
670
- /** return true if the passed in node was actually moved (checks for no-op and locked) */
671
- GridStackEngine.prototype.moveNode = function (node, o) {
672
- var _a, _b;
673
- if (!node || /*node.locked ||*/ !o)
674
- return false;
675
- var wasUndefinedPack;
676
- if (o.pack === undefined) {
677
- wasUndefinedPack = o.pack = true;
678
- }
679
- // constrain the passed in values and check if we're still changing our node
680
- if (typeof o.x !== 'number') {
681
- o.x = node.x;
682
- }
683
- if (typeof o.y !== 'number') {
684
- o.y = node.y;
685
- }
686
- if (typeof o.w !== 'number') {
687
- o.w = node.w;
688
- }
689
- if (typeof o.h !== 'number') {
690
- o.h = node.h;
691
- }
692
- var resizing = (node.w !== o.w || node.h !== o.h);
693
- var nn = utils_1.Utils.copyPos({}, node, true); // get min/max out first, then opt positions next
694
- utils_1.Utils.copyPos(nn, o);
695
- nn = this.nodeBoundFix(nn, resizing);
696
- utils_1.Utils.copyPos(o, nn);
697
- if (utils_1.Utils.samePos(node, o))
698
- return false;
699
- var prevPos = utils_1.Utils.copyPos({}, node);
700
- // check if we will need to fix collision at our new location
701
- var collides = this.collideAll(node, nn, o.skip);
702
- var needToMove = true;
703
- if (collides.length) {
704
- var activeDrag = node._moving && !o.nested;
705
- // check to make sure we actually collided over 50% surface area while dragging
706
- var collide = activeDrag ? this.directionCollideCoverage(node, o, collides) : collides[0];
707
- // if we're enabling creation of sub-grids on the fly, see if we're covering 80% of either one, if we didn't already do that
708
- if (activeDrag && collide && ((_b = (_a = node.grid) === null || _a === void 0 ? void 0 : _a.opts) === null || _b === void 0 ? void 0 : _b.subGridDynamic) && !node.grid._isTemp) {
709
- var over = utils_1.Utils.areaIntercept(o.rect, collide._rect);
710
- var a1 = utils_1.Utils.area(o.rect);
711
- var a2 = utils_1.Utils.area(collide._rect);
712
- var perc = over / (a1 < a2 ? a1 : a2);
713
- if (perc > .8) {
714
- collide.grid.makeSubGrid(collide.el, undefined, node);
715
- collide = undefined;
716
- }
717
- }
718
- if (collide) {
719
- needToMove = !this._fixCollisions(node, nn, collide, o); // check if already moved...
720
- }
721
- else {
722
- needToMove = false; // we didn't cover >50% for a move, skip...
723
- if (wasUndefinedPack)
724
- delete o.pack;
725
- }
726
- }
727
- // now move (to the original ask vs the collision version which might differ) and repack things
728
- if (needToMove) {
729
- node._dirty = true;
730
- utils_1.Utils.copyPos(node, nn);
731
- }
732
- if (o.pack) {
733
- this._packNodes()
734
- ._notify();
735
- }
736
- return !utils_1.Utils.samePos(node, prevPos); // pack might have moved things back
737
- };
738
- GridStackEngine.prototype.getRow = function () {
739
- return this.nodes.reduce(function (row, n) { return Math.max(row, n.y + n.h); }, 0);
740
- };
741
- GridStackEngine.prototype.beginUpdate = function (node) {
742
- if (!node._updating) {
743
- node._updating = true;
744
- delete node._skipDown;
745
- if (!this.batchMode)
746
- this.saveInitial();
747
- }
748
- return this;
749
- };
750
- GridStackEngine.prototype.endUpdate = function () {
751
- var n = this.nodes.find(function (n) { return n._updating; });
752
- if (n) {
753
- delete n._updating;
754
- delete n._skipDown;
755
- }
756
- return this;
757
- };
758
- /** saves a copy of the largest column layout (eg 12 even when rendering oneColumnMode) so we don't loose orig layout,
759
- * returning a list of widgets for serialization */
760
- GridStackEngine.prototype.save = function (saveElement) {
761
- var _a;
762
- if (saveElement === void 0) { saveElement = true; }
763
- // use the highest layout for any saved info so we can have full detail on reload #1849
764
- var len = (_a = this._layouts) === null || _a === void 0 ? void 0 : _a.length;
765
- var layout = len && this.column !== (len - 1) ? this._layouts[len - 1] : null;
766
- var list = [];
767
- this.sortNodes();
768
- this.nodes.forEach(function (n) {
769
- var wl = layout === null || layout === void 0 ? void 0 : layout.find(function (l) { return l._id === n._id; });
770
- var w = __assign({}, n);
771
- // use layout info instead if set
772
- if (wl) {
773
- w.x = wl.x;
774
- w.y = wl.y;
775
- w.w = wl.w;
776
- }
777
- utils_1.Utils.removeInternalForSave(w, !saveElement);
778
- list.push(w);
779
- });
780
- return list;
781
- };
782
- /** @internal called whenever a node is added or moved - updates the cached layouts */
783
- GridStackEngine.prototype.layoutsNodesChange = function (nodes) {
784
- var _this = this;
785
- if (!this._layouts || this._inColumnResize)
786
- return this;
787
- // remove smaller layouts - we will re-generate those on the fly... larger ones need to update
788
- this._layouts.forEach(function (layout, column) {
789
- if (!layout || column === _this.column)
790
- return _this;
791
- if (column < _this.column) {
792
- _this._layouts[column] = undefined;
793
- }
794
- else {
795
- // we save the original x,y,w (h isn't cached) to see what actually changed to propagate better.
796
- // NOTE: we don't need to check against out of bound scaling/moving as that will be done when using those cache values. #1785
797
- var ratio_1 = column / _this.column;
798
- nodes.forEach(function (node) {
799
- if (!node._orig)
800
- return; // didn't change (newly added ?)
801
- var n = layout.find(function (l) { return l._id === node._id; });
802
- if (!n)
803
- return; // no cache for new nodes. Will use those values.
804
- // Y changed, push down same amount
805
- // TODO: detect doing item 'swaps' will help instead of move (especially in 1 column mode)
806
- if (node.y !== node._orig.y) {
807
- n.y += (node.y - node._orig.y);
808
- }
809
- // X changed, scale from new position
810
- if (node.x !== node._orig.x) {
811
- n.x = Math.round(node.x * ratio_1);
812
- }
813
- // width changed, scale from new width
814
- if (node.w !== node._orig.w) {
815
- n.w = Math.round(node.w * ratio_1);
816
- }
817
- // ...height always carries over from cache
818
- });
819
- }
820
- });
821
- return this;
822
- };
823
- /**
824
- * @internal Called to scale the widget width & position up/down based on the column change.
825
- * Note we store previous layouts (especially original ones) to make it possible to go
826
- * from say 12 -> 1 -> 12 and get back to where we were.
827
- *
828
- * @param prevColumn previous number of columns
829
- * @param column new column number
830
- * @param nodes different sorted list (ex: DOM order) instead of current list
831
- * @param layout specify the type of re-layout that will happen (position, size, etc...).
832
- * Note: items will never be outside of the current column boundaries. default (moveScale). Ignored for 1 column
833
- */
834
- GridStackEngine.prototype.updateNodeWidths = function (prevColumn, column, nodes, layout) {
835
- var _this = this;
836
- var _a;
837
- if (layout === void 0) { layout = 'moveScale'; }
838
- if (!this.nodes.length || !column || prevColumn === column)
839
- return this;
840
- // cache the current layout in case they want to go back (like 12 -> 1 -> 12) as it requires original data
841
- this.cacheLayout(this.nodes, prevColumn);
842
- this.batchUpdate(); // do this EARLY as it will call saveInitial() so we can detect where we started for _dirty and collision
843
- var newNodes = [];
844
- // if we're going to 1 column and using DOM order rather than default sorting, then generate that layout
845
- var domOrder = false;
846
- if (column === 1 && (nodes === null || nodes === void 0 ? void 0 : nodes.length)) {
847
- domOrder = true;
848
- var top_1 = 0;
849
- nodes.forEach(function (n) {
850
- n.x = 0;
851
- n.w = 1;
852
- n.y = Math.max(n.y, top_1);
853
- top_1 = n.y + n.h;
854
- });
855
- newNodes = nodes;
856
- nodes = [];
857
- }
858
- else {
859
- nodes = utils_1.Utils.sort(this.nodes, -1, prevColumn); // current column reverse sorting so we can insert last to front (limit collision)
860
- }
861
- // see if we have cached previous layout IFF we are going up in size (restore) otherwise always
862
- // generate next size down from where we are (looks more natural as you gradually size down).
863
- var cacheNodes = [];
864
- if (column > prevColumn) {
865
- cacheNodes = this._layouts[column] || [];
866
- // ...if not, start with the largest layout (if not already there) as down-scaling is more accurate
867
- // by pretending we came from that larger column by assigning those values as starting point
868
- var lastIndex = this._layouts.length - 1;
869
- if (!cacheNodes.length && prevColumn !== lastIndex && ((_a = this._layouts[lastIndex]) === null || _a === void 0 ? void 0 : _a.length)) {
870
- prevColumn = lastIndex;
871
- this._layouts[lastIndex].forEach(function (cacheNode) {
872
- var n = nodes.find(function (n) { return n._id === cacheNode._id; });
873
- if (n) {
874
- // still current, use cache info positions
875
- n.x = cacheNode.x;
876
- n.y = cacheNode.y;
877
- n.w = cacheNode.w;
878
- }
879
- });
880
- }
881
- }
882
- // if we found cache re-use those nodes that are still current
883
- cacheNodes.forEach(function (cacheNode) {
884
- var j = nodes.findIndex(function (n) { return n._id === cacheNode._id; });
885
- if (j !== -1) {
886
- // still current, use cache info positions
887
- nodes[j].x = cacheNode.x;
888
- nodes[j].y = cacheNode.y;
889
- nodes[j].w = cacheNode.w;
890
- newNodes.push(nodes[j]);
891
- nodes.splice(j, 1);
892
- }
893
- });
894
- // ...and add any extra non-cached ones
895
- if (nodes.length) {
896
- if (typeof layout === 'function') {
897
- layout(column, prevColumn, newNodes, nodes);
898
- }
899
- else if (!domOrder) {
900
- var ratio_2 = column / prevColumn;
901
- var move_1 = (layout === 'move' || layout === 'moveScale');
902
- var scale_1 = (layout === 'scale' || layout === 'moveScale');
903
- nodes.forEach(function (node) {
904
- // NOTE: x + w could be outside of the grid, but addNode() below will handle that
905
- node.x = (column === 1 ? 0 : (move_1 ? Math.round(node.x * ratio_2) : Math.min(node.x, column - 1)));
906
- node.w = ((column === 1 || prevColumn === 1) ? 1 :
907
- scale_1 ? (Math.round(node.w * ratio_2) || 1) : (Math.min(node.w, column)));
908
- newNodes.push(node);
909
- });
910
- nodes = [];
911
- }
912
- }
913
- // finally re-layout them in reverse order (to get correct placement)
914
- if (!domOrder)
915
- newNodes = utils_1.Utils.sort(newNodes, -1, column);
916
- this._inColumnResize = true; // prevent cache update
917
- this.nodes = []; // pretend we have no nodes to start with (add() will use same structures) to simplify layout
918
- newNodes.forEach(function (node) {
919
- _this.addNode(node, false); // 'false' for add event trigger
920
- delete node._orig; // make sure the commit doesn't try to restore things back to original
921
- });
922
- this.batchUpdate(false);
923
- delete this._inColumnResize;
924
- return this;
925
- };
926
- /**
927
- * call to cache the given layout internally to the given location so we can restore back when column changes size
928
- * @param nodes list of nodes
929
- * @param column corresponding column index to save it under
930
- * @param clear if true, will force other caches to be removed (default false)
931
- */
932
- GridStackEngine.prototype.cacheLayout = function (nodes, column, clear) {
933
- if (clear === void 0) { clear = false; }
934
- var copy = [];
935
- nodes.forEach(function (n, i) {
936
- n._id = n._id || GridStackEngine._idSeq++; // make sure we have an id in case this is new layout, else re-use id already set
937
- copy[i] = { x: n.x, y: n.y, w: n.w, _id: n._id }; // only thing we change is x,y,w and id to find it back
938
- });
939
- this._layouts = clear ? [] : this._layouts || []; // use array to find larger quick
940
- this._layouts[column] = copy;
941
- return this;
942
- };
943
- /**
944
- * call to cache the given node layout internally to the given location so we can restore back when column changes size
945
- * @param node single node to cache
946
- * @param column corresponding column index to save it under
947
- */
948
- GridStackEngine.prototype.cacheOneLayout = function (n, column) {
949
- n._id = n._id || GridStackEngine._idSeq++;
950
- var layout = { x: n.x, y: n.y, w: n.w, _id: n._id };
951
- this._layouts = this._layouts || [];
952
- this._layouts[column] = this._layouts[column] || [];
953
- var index = this.findCacheLayout(n, column);
954
- if (index === -1)
955
- this._layouts[column].push(layout);
956
- else
957
- this._layouts[column][index] = layout;
958
- return this;
959
- };
960
- GridStackEngine.prototype.findCacheLayout = function (n, column) {
961
- var _a, _b, _c;
962
- return (_c = (_b = (_a = this._layouts) === null || _a === void 0 ? void 0 : _a[column]) === null || _b === void 0 ? void 0 : _b.findIndex(function (l) { return l._id === n._id; })) !== null && _c !== void 0 ? _c : -1;
963
- };
964
- /** called to remove all internal values but the _id */
965
- GridStackEngine.prototype.cleanupNode = function (node) {
966
- for (var prop in node) {
967
- if (prop[0] === '_' && prop !== '_id')
968
- delete node[prop];
969
- }
970
- return this;
971
- };
972
- /** @internal unique global internal _id counter NOT starting at 0 */
973
- GridStackEngine._idSeq = 1;
974
- return GridStackEngine;
975
- }());
976
- exports.GridStackEngine = GridStackEngine;
1
+ "use strict";
2
+ /**
3
+ * gridstack-engine.ts 8.0.0
4
+ * Copyright (c) 2021-2022 Alain Dumesny - see GridStack root license
5
+ */
6
+ var __assign = (this && this.__assign) || function () {
7
+ __assign = Object.assign || function(t) {
8
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
9
+ s = arguments[i];
10
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
11
+ t[p] = s[p];
12
+ }
13
+ return t;
14
+ };
15
+ return __assign.apply(this, arguments);
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.GridStackEngine = void 0;
19
+ var utils_1 = require("./utils");
20
+ /**
21
+ * Defines the GridStack engine that does most no DOM grid manipulation.
22
+ * See GridStack methods and vars for descriptions.
23
+ *
24
+ * NOTE: values should not be modified directly - call the main GridStack API instead
25
+ */
26
+ var GridStackEngine = exports.GridStackEngine = /** @class */ (function () {
27
+ function GridStackEngine(opts) {
28
+ if (opts === void 0) { opts = {}; }
29
+ this.addedNodes = [];
30
+ this.removedNodes = [];
31
+ this.column = opts.column || 12;
32
+ this.maxRow = opts.maxRow;
33
+ this._float = opts.float;
34
+ this.nodes = opts.nodes || [];
35
+ this.onChange = opts.onChange;
36
+ }
37
+ GridStackEngine.prototype.batchUpdate = function (flag) {
38
+ if (flag === void 0) { flag = true; }
39
+ if (!!this.batchMode === flag)
40
+ return this;
41
+ this.batchMode = flag;
42
+ if (flag) {
43
+ this._prevFloat = this._float;
44
+ this._float = true; // let things go anywhere for now... will restore and possibly reposition later
45
+ this.saveInitial(); // since begin update (which is called multiple times) won't do this
46
+ }
47
+ else {
48
+ this._float = this._prevFloat;
49
+ delete this._prevFloat;
50
+ this._packNodes()._notify();
51
+ }
52
+ return this;
53
+ };
54
+ // use entire row for hitting area (will use bottom reverse sorted first) if we not actively moving DOWN and didn't already skip
55
+ GridStackEngine.prototype._useEntireRowArea = function (node, nn) {
56
+ return (!this.float || this.batchMode && !this._prevFloat) && !this._hasLocked && (!node._moving || node._skipDown || nn.y <= node.y);
57
+ };
58
+ /** @internal fix collision on given 'node', going to given new location 'nn', with optional 'collide' node already found.
59
+ * return true if we moved. */
60
+ GridStackEngine.prototype._fixCollisions = function (node, nn, collide, opt) {
61
+ if (nn === void 0) { nn = node; }
62
+ if (opt === void 0) { opt = {}; }
63
+ this.sortNodes(-1); // from last to first, so recursive collision move items in the right order
64
+ collide = collide || this.collide(node, nn); // REAL area collide for swap and skip if none...
65
+ if (!collide)
66
+ return false;
67
+ // swap check: if we're actively moving in gravity mode, see if we collide with an object the same size
68
+ if (node._moving && !opt.nested && !this.float) {
69
+ if (this.swap(node, collide))
70
+ return true;
71
+ }
72
+ // during while() collisions MAKE SURE to check entire row so larger items don't leap frog small ones (push them all down starting last in grid)
73
+ var area = nn;
74
+ if (this._useEntireRowArea(node, nn)) {
75
+ area = { x: 0, w: this.column, y: nn.y, h: nn.h };
76
+ collide = this.collide(node, area, opt.skip); // force new hit
77
+ }
78
+ var didMove = false;
79
+ var newOpt = { nested: true, pack: false };
80
+ while (collide = collide || this.collide(node, area, opt.skip)) { // could collide with more than 1 item... so repeat for each
81
+ var moved = void 0;
82
+ // if colliding with a locked item OR moving down with top gravity (and collide could move up) -> skip past the collide,
83
+ // but remember that skip down so we only do this once (and push others otherwise).
84
+ if (collide.locked || node._moving && !node._skipDown && nn.y > node.y && !this.float &&
85
+ // can take space we had, or before where we're going
86
+ (!this.collide(collide, __assign(__assign({}, collide), { y: node.y }), node) || !this.collide(collide, __assign(__assign({}, collide), { y: nn.y - collide.h }), node))) {
87
+ node._skipDown = (node._skipDown || nn.y > node.y);
88
+ moved = this.moveNode(node, __assign(__assign(__assign({}, nn), { y: collide.y + collide.h }), newOpt));
89
+ if (collide.locked && moved) {
90
+ utils_1.Utils.copyPos(nn, node); // moving after lock become our new desired location
91
+ }
92
+ else if (!collide.locked && moved && opt.pack) {
93
+ // we moved after and will pack: do it now and keep the original drop location, but past the old collide to see what else we might push way
94
+ this._packNodes();
95
+ nn.y = collide.y + collide.h;
96
+ utils_1.Utils.copyPos(node, nn);
97
+ }
98
+ didMove = didMove || moved;
99
+ }
100
+ else {
101
+ // move collide down *after* where we will be, ignoring where we are now (don't collide with us)
102
+ moved = this.moveNode(collide, __assign(__assign(__assign({}, collide), { y: nn.y + nn.h, skip: node }), newOpt));
103
+ }
104
+ if (!moved) {
105
+ return didMove;
106
+ } // break inf loop if we couldn't move after all (ex: maxRow, fixed)
107
+ collide = undefined;
108
+ }
109
+ return didMove;
110
+ };
111
+ /** return the nodes that intercept the given node. Optionally a different area can be used, as well as a second node to skip */
112
+ GridStackEngine.prototype.collide = function (skip, area, skip2) {
113
+ if (area === void 0) { area = skip; }
114
+ return this.nodes.find(function (n) { return n !== skip && n !== skip2 && utils_1.Utils.isIntercepted(n, area); });
115
+ };
116
+ GridStackEngine.prototype.collideAll = function (skip, area, skip2) {
117
+ if (area === void 0) { area = skip; }
118
+ return this.nodes.filter(function (n) { return n !== skip && n !== skip2 && utils_1.Utils.isIntercepted(n, area); });
119
+ };
120
+ /** does a pixel coverage collision based on where we started, returning the node that has the most coverage that is >50% mid line */
121
+ GridStackEngine.prototype.directionCollideCoverage = function (node, o, collides) {
122
+ if (!o.rect || !node._rect)
123
+ return;
124
+ var r0 = node._rect; // where started
125
+ var r = __assign({}, o.rect); // where we are
126
+ // update dragged rect to show where it's coming from (above or below, etc...)
127
+ if (r.y > r0.y) {
128
+ r.h += r.y - r0.y;
129
+ r.y = r0.y;
130
+ }
131
+ else {
132
+ r.h += r0.y - r.y;
133
+ }
134
+ if (r.x > r0.x) {
135
+ r.w += r.x - r0.x;
136
+ r.x = r0.x;
137
+ }
138
+ else {
139
+ r.w += r0.x - r.x;
140
+ }
141
+ var collide;
142
+ collides.forEach(function (n) {
143
+ if (n.locked || !n._rect)
144
+ return;
145
+ var r2 = n._rect; // overlapping target
146
+ var yOver = Number.MAX_VALUE, xOver = Number.MAX_VALUE, overMax = 0.5; // need >50%
147
+ // depending on which side we started from, compute the overlap % of coverage
148
+ // (ex: from above/below we only compute the max horizontal line coverage)
149
+ if (r0.y < r2.y) { // from above
150
+ yOver = ((r.y + r.h) - r2.y) / r2.h;
151
+ }
152
+ else if (r0.y + r0.h > r2.y + r2.h) { // from below
153
+ yOver = ((r2.y + r2.h) - r.y) / r2.h;
154
+ }
155
+ if (r0.x < r2.x) { // from the left
156
+ xOver = ((r.x + r.w) - r2.x) / r2.w;
157
+ }
158
+ else if (r0.x + r0.w > r2.x + r2.w) { // from the right
159
+ xOver = ((r2.x + r2.w) - r.x) / r2.w;
160
+ }
161
+ var over = Math.min(xOver, yOver);
162
+ if (over > overMax) {
163
+ overMax = over;
164
+ collide = n;
165
+ }
166
+ });
167
+ o.collide = collide; // save it so we don't have to find it again
168
+ return collide;
169
+ };
170
+ /** does a pixel coverage returning the node that has the most coverage by area */
171
+ /*
172
+ protected collideCoverage(r: GridStackPosition, collides: GridStackNode[]): {collide: GridStackNode, over: number} {
173
+ let collide: GridStackNode;
174
+ let overMax = 0;
175
+ collides.forEach(n => {
176
+ if (n.locked || !n._rect) return;
177
+ let over = Utils.areaIntercept(r, n._rect);
178
+ if (over > overMax) {
179
+ overMax = over;
180
+ collide = n;
181
+ }
182
+ });
183
+ return {collide, over: overMax};
184
+ }
185
+ */
186
+ /** called to cache the nodes pixel rectangles used for collision detection during drag */
187
+ GridStackEngine.prototype.cacheRects = function (w, h, top, right, bottom, left) {
188
+ this.nodes.forEach(function (n) {
189
+ return n._rect = {
190
+ y: n.y * h + top,
191
+ x: n.x * w + left,
192
+ w: n.w * w - left - right,
193
+ h: n.h * h - top - bottom
194
+ };
195
+ });
196
+ return this;
197
+ };
198
+ /** called to possibly swap between 2 nodes (same size or column, not locked, touching), returning true if successful */
199
+ GridStackEngine.prototype.swap = function (a, b) {
200
+ if (!b || b.locked || !a || a.locked)
201
+ return false;
202
+ function _doSwap() {
203
+ var x = b.x, y = b.y;
204
+ b.x = a.x;
205
+ b.y = a.y; // b -> a position
206
+ if (a.h != b.h) {
207
+ a.x = x;
208
+ a.y = b.y + b.h; // a -> goes after b
209
+ }
210
+ else if (a.w != b.w) {
211
+ a.x = b.x + b.w;
212
+ a.y = y; // a -> goes after b
213
+ }
214
+ else {
215
+ a.x = x;
216
+ a.y = y; // a -> old b position
217
+ }
218
+ a._dirty = b._dirty = true;
219
+ return true;
220
+ }
221
+ var touching; // remember if we called it (vs undefined)
222
+ // same size and same row or column, and touching
223
+ if (a.w === b.w && a.h === b.h && (a.x === b.x || a.y === b.y) && (touching = utils_1.Utils.isTouching(a, b)))
224
+ return _doSwap();
225
+ if (touching === false)
226
+ return; // IFF ran test and fail, bail out
227
+ // check for taking same columns (but different height) and touching
228
+ if (a.w === b.w && a.x === b.x && (touching || (touching = utils_1.Utils.isTouching(a, b)))) {
229
+ if (b.y < a.y) {
230
+ var t = a;
231
+ a = b;
232
+ b = t;
233
+ } // swap a <-> b vars so a is first
234
+ return _doSwap();
235
+ }
236
+ if (touching === false)
237
+ return;
238
+ // check if taking same row (but different width) and touching
239
+ if (a.h === b.h && a.y === b.y && (touching || (touching = utils_1.Utils.isTouching(a, b)))) {
240
+ if (b.x < a.x) {
241
+ var t = a;
242
+ a = b;
243
+ b = t;
244
+ } // swap a <-> b vars so a is first
245
+ return _doSwap();
246
+ }
247
+ return false;
248
+ };
249
+ GridStackEngine.prototype.isAreaEmpty = function (x, y, w, h) {
250
+ var nn = { x: x || 0, y: y || 0, w: w || 1, h: h || 1 };
251
+ return !this.collide(nn);
252
+ };
253
+ /** re-layout grid items to reclaim any empty space */
254
+ GridStackEngine.prototype.compact = function () {
255
+ var _this = this;
256
+ if (this.nodes.length === 0)
257
+ return this;
258
+ this.batchUpdate()
259
+ .sortNodes();
260
+ var copyNodes = this.nodes;
261
+ this.nodes = []; // pretend we have no nodes to conflict layout to start with...
262
+ copyNodes.forEach(function (node) {
263
+ if (!node.locked) {
264
+ node.autoPosition = true;
265
+ }
266
+ _this.addNode(node, false); // 'false' for add event trigger
267
+ node._dirty = true; // will force attr update
268
+ });
269
+ return this.batchUpdate(false);
270
+ };
271
+ Object.defineProperty(GridStackEngine.prototype, "float", {
272
+ /** float getter method */
273
+ get: function () { return this._float || false; },
274
+ /** enable/disable floating widgets (default: `false`) See [example](http://gridstackjs.com/demo/float.html) */
275
+ set: function (val) {
276
+ if (this._float === val)
277
+ return;
278
+ this._float = val || false;
279
+ if (!val) {
280
+ this._packNodes()._notify();
281
+ }
282
+ },
283
+ enumerable: false,
284
+ configurable: true
285
+ });
286
+ /** sort the nodes array from first to last, or reverse. Called during collision/placement to force an order */
287
+ GridStackEngine.prototype.sortNodes = function (dir) {
288
+ this.nodes = utils_1.Utils.sort(this.nodes, dir, this.column);
289
+ return this;
290
+ };
291
+ /** @internal called to top gravity pack the items back OR revert back to original Y positions when floating */
292
+ GridStackEngine.prototype._packNodes = function () {
293
+ var _this = this;
294
+ if (this.batchMode) {
295
+ return this;
296
+ }
297
+ this.sortNodes(); // first to last
298
+ if (this.float) {
299
+ // restore original Y pos
300
+ this.nodes.forEach(function (n) {
301
+ if (n._updating || n._orig === undefined || n.y === n._orig.y)
302
+ return;
303
+ var newY = n.y;
304
+ while (newY > n._orig.y) {
305
+ --newY;
306
+ var collide = _this.collide(n, { x: n.x, y: newY, w: n.w, h: n.h });
307
+ if (!collide) {
308
+ n._dirty = true;
309
+ n.y = newY;
310
+ }
311
+ }
312
+ });
313
+ }
314
+ else {
315
+ // top gravity pack
316
+ this.nodes.forEach(function (n, i) {
317
+ if (n.locked)
318
+ return;
319
+ while (n.y > 0) {
320
+ var newY = i === 0 ? 0 : n.y - 1;
321
+ var canBeMoved = i === 0 || !_this.collide(n, { x: n.x, y: newY, w: n.w, h: n.h });
322
+ if (!canBeMoved)
323
+ break;
324
+ // Note: must be dirty (from last position) for GridStack::OnChange CB to update positions
325
+ // and move items back. The user 'change' CB should detect changes from the original
326
+ // starting position instead.
327
+ n._dirty = (n.y !== newY);
328
+ n.y = newY;
329
+ }
330
+ });
331
+ }
332
+ return this;
333
+ };
334
+ /**
335
+ * given a random node, makes sure it's coordinates/values are valid in the current grid
336
+ * @param node to adjust
337
+ * @param resizing if out of bound, resize down or move into the grid to fit ?
338
+ */
339
+ GridStackEngine.prototype.prepareNode = function (node, resizing) {
340
+ var _a;
341
+ node = node || {};
342
+ node._id = (_a = node._id) !== null && _a !== void 0 ? _a : GridStackEngine._idSeq++;
343
+ // if we're missing position, have the grid position us automatically (before we set them to 0,0)
344
+ if (node.x === undefined || node.y === undefined || node.x === null || node.y === null) {
345
+ node.autoPosition = true;
346
+ }
347
+ // assign defaults for missing required fields
348
+ var defaults = { x: 0, y: 0, w: 1, h: 1 };
349
+ utils_1.Utils.defaults(node, defaults);
350
+ if (!node.autoPosition) {
351
+ delete node.autoPosition;
352
+ }
353
+ if (!node.noResize) {
354
+ delete node.noResize;
355
+ }
356
+ if (!node.noMove) {
357
+ delete node.noMove;
358
+ }
359
+ utils_1.Utils.sanitizeMinMax(node);
360
+ // check for NaN (in case messed up strings were passed. can't do parseInt() || defaults.x above as 0 is valid #)
361
+ if (typeof node.x == 'string') {
362
+ node.x = Number(node.x);
363
+ }
364
+ if (typeof node.y == 'string') {
365
+ node.y = Number(node.y);
366
+ }
367
+ if (typeof node.w == 'string') {
368
+ node.w = Number(node.w);
369
+ }
370
+ if (typeof node.h == 'string') {
371
+ node.h = Number(node.h);
372
+ }
373
+ if (isNaN(node.x)) {
374
+ node.x = defaults.x;
375
+ node.autoPosition = true;
376
+ }
377
+ if (isNaN(node.y)) {
378
+ node.y = defaults.y;
379
+ node.autoPosition = true;
380
+ }
381
+ if (isNaN(node.w)) {
382
+ node.w = defaults.w;
383
+ }
384
+ if (isNaN(node.h)) {
385
+ node.h = defaults.h;
386
+ }
387
+ return this.nodeBoundFix(node, resizing);
388
+ };
389
+ /** part2 of preparing a node to fit inside our grid - checks for x,y,w from grid dimensions */
390
+ GridStackEngine.prototype.nodeBoundFix = function (node, resizing) {
391
+ var before = node._orig || utils_1.Utils.copyPos({}, node);
392
+ if (node.maxW) {
393
+ node.w = Math.min(node.w, node.maxW);
394
+ }
395
+ if (node.maxH) {
396
+ node.h = Math.min(node.h, node.maxH);
397
+ }
398
+ if (node.minW && node.minW <= this.column) {
399
+ node.w = Math.max(node.w, node.minW);
400
+ }
401
+ if (node.minH) {
402
+ node.h = Math.max(node.h, node.minH);
403
+ }
404
+ // if user loaded a larger than allowed widget for current # of columns (or force 1 column mode),
405
+ // remember it's position & width so we can restore back (1 -> 12 column) #1655 #1985
406
+ // IFF we're not in the middle of column resizing!
407
+ var saveOrig = this.column === 1 || node.x + node.w > this.column;
408
+ if (saveOrig && this.column < 12 && !this._inColumnResize && node._id && this.findCacheLayout(node, 12) === -1) {
409
+ var copy = __assign({}, node); // need _id + positions
410
+ if (copy.autoPosition) {
411
+ delete copy.x;
412
+ delete copy.y;
413
+ }
414
+ else
415
+ copy.x = Math.min(11, copy.x);
416
+ copy.w = Math.min(12, copy.w);
417
+ this.cacheOneLayout(copy, 12);
418
+ }
419
+ if (node.w > this.column) {
420
+ node.w = this.column;
421
+ }
422
+ else if (node.w < 1) {
423
+ node.w = 1;
424
+ }
425
+ if (this.maxRow && node.h > this.maxRow) {
426
+ node.h = this.maxRow;
427
+ }
428
+ else if (node.h < 1) {
429
+ node.h = 1;
430
+ }
431
+ if (node.x < 0) {
432
+ node.x = 0;
433
+ }
434
+ if (node.y < 0) {
435
+ node.y = 0;
436
+ }
437
+ if (node.x + node.w > this.column) {
438
+ if (resizing) {
439
+ node.w = this.column - node.x;
440
+ }
441
+ else {
442
+ node.x = this.column - node.w;
443
+ }
444
+ }
445
+ if (this.maxRow && node.y + node.h > this.maxRow) {
446
+ if (resizing) {
447
+ node.h = this.maxRow - node.y;
448
+ }
449
+ else {
450
+ node.y = this.maxRow - node.h;
451
+ }
452
+ }
453
+ if (!utils_1.Utils.samePos(node, before)) {
454
+ node._dirty = true;
455
+ }
456
+ return node;
457
+ };
458
+ /** returns a list of modified nodes from their original values */
459
+ GridStackEngine.prototype.getDirtyNodes = function (verify) {
460
+ // compare original x,y,w,h instead as _dirty can be a temporary state
461
+ if (verify) {
462
+ return this.nodes.filter(function (n) { return n._dirty && !utils_1.Utils.samePos(n, n._orig); });
463
+ }
464
+ return this.nodes.filter(function (n) { return n._dirty; });
465
+ };
466
+ /** @internal call this to call onChange callback with dirty nodes so DOM can be updated */
467
+ GridStackEngine.prototype._notify = function (removedNodes) {
468
+ if (this.batchMode || !this.onChange)
469
+ return this;
470
+ var dirtyNodes = (removedNodes || []).concat(this.getDirtyNodes());
471
+ this.onChange(dirtyNodes);
472
+ return this;
473
+ };
474
+ /** @internal remove dirty and last tried info */
475
+ GridStackEngine.prototype.cleanNodes = function () {
476
+ if (this.batchMode)
477
+ return this;
478
+ this.nodes.forEach(function (n) {
479
+ delete n._dirty;
480
+ delete n._lastTried;
481
+ });
482
+ return this;
483
+ };
484
+ /** @internal called to save initial position/size to track real dirty state.
485
+ * Note: should be called right after we call change event (so next API is can detect changes)
486
+ * as well as right before we start move/resize/enter (so we can restore items to prev values) */
487
+ GridStackEngine.prototype.saveInitial = function () {
488
+ this.nodes.forEach(function (n) {
489
+ n._orig = utils_1.Utils.copyPos({}, n);
490
+ delete n._dirty;
491
+ });
492
+ this._hasLocked = this.nodes.some(function (n) { return n.locked; });
493
+ return this;
494
+ };
495
+ /** @internal restore all the nodes back to initial values (called when we leave) */
496
+ GridStackEngine.prototype.restoreInitial = function () {
497
+ this.nodes.forEach(function (n) {
498
+ if (utils_1.Utils.samePos(n, n._orig))
499
+ return;
500
+ utils_1.Utils.copyPos(n, n._orig);
501
+ n._dirty = true;
502
+ });
503
+ this._notify();
504
+ return this;
505
+ };
506
+ /** find the first available empty spot for the given node width/height, updating the x,y attributes. return true if found.
507
+ * optionally you can pass your own existing node list and column count, otherwise defaults to that engine data.
508
+ */
509
+ GridStackEngine.prototype.findEmptyPosition = function (node, nodeList, column) {
510
+ if (nodeList === void 0) { nodeList = this.nodes; }
511
+ if (column === void 0) { column = this.column; }
512
+ nodeList = utils_1.Utils.sort(nodeList, -1, column);
513
+ var found = false;
514
+ var _loop_1 = function (i) {
515
+ var x = i % column;
516
+ var y = Math.floor(i / column);
517
+ if (x + node.w > column) {
518
+ return "continue";
519
+ }
520
+ var box = { x: x, y: y, w: node.w, h: node.h };
521
+ if (!nodeList.find(function (n) { return utils_1.Utils.isIntercepted(box, n); })) {
522
+ node.x = x;
523
+ node.y = y;
524
+ delete node.autoPosition;
525
+ found = true;
526
+ }
527
+ };
528
+ for (var i = 0; !found; ++i) {
529
+ _loop_1(i);
530
+ }
531
+ return found;
532
+ };
533
+ /** call to add the given node to our list, fixing collision and re-packing */
534
+ GridStackEngine.prototype.addNode = function (node, triggerAddEvent) {
535
+ if (triggerAddEvent === void 0) { triggerAddEvent = false; }
536
+ var dup = this.nodes.find(function (n) { return n._id === node._id; });
537
+ if (dup)
538
+ return dup; // prevent inserting twice! return it instead.
539
+ // skip prepareNode if we're in middle of column resize (not new) but do check for bounds!
540
+ node = this._inColumnResize ? this.nodeBoundFix(node) : this.prepareNode(node);
541
+ delete node._temporaryRemoved;
542
+ delete node._removeDOM;
543
+ if (node.autoPosition && this.findEmptyPosition(node)) {
544
+ delete node.autoPosition; // found our slot
545
+ }
546
+ this.nodes.push(node);
547
+ if (triggerAddEvent) {
548
+ this.addedNodes.push(node);
549
+ }
550
+ this._fixCollisions(node);
551
+ if (!this.batchMode) {
552
+ this._packNodes()._notify();
553
+ }
554
+ return node;
555
+ };
556
+ GridStackEngine.prototype.removeNode = function (node, removeDOM, triggerEvent) {
557
+ if (removeDOM === void 0) { removeDOM = true; }
558
+ if (triggerEvent === void 0) { triggerEvent = false; }
559
+ if (!this.nodes.find(function (n) { return n === node; })) {
560
+ // TEST console.log(`Error: GridStackEngine.removeNode() node._id=${node._id} not found!`)
561
+ return this;
562
+ }
563
+ if (triggerEvent) { // we wait until final drop to manually track removed items (rather than during drag)
564
+ this.removedNodes.push(node);
565
+ }
566
+ if (removeDOM)
567
+ node._removeDOM = true; // let CB remove actual HTML (used to set _id to null, but then we loose layout info)
568
+ // don't use 'faster' .splice(findIndex(),1) in case node isn't in our list, or in multiple times.
569
+ this.nodes = this.nodes.filter(function (n) { return n !== node; });
570
+ return this._packNodes()
571
+ ._notify([node]);
572
+ };
573
+ GridStackEngine.prototype.removeAll = function (removeDOM) {
574
+ if (removeDOM === void 0) { removeDOM = true; }
575
+ delete this._layouts;
576
+ if (!this.nodes.length)
577
+ return this;
578
+ removeDOM && this.nodes.forEach(function (n) { return n._removeDOM = true; }); // let CB remove actual HTML (used to set _id to null, but then we loose layout info)
579
+ this.removedNodes = this.nodes;
580
+ this.nodes = [];
581
+ return this._notify(this.removedNodes);
582
+ };
583
+ /** checks if item can be moved (layout constrain) vs moveNode(), returning true if was able to move.
584
+ * In more complicated cases (maxRow) it will attempt at moving the item and fixing
585
+ * others in a clone first, then apply those changes if still within specs. */
586
+ GridStackEngine.prototype.moveNodeCheck = function (node, o) {
587
+ var _this = this;
588
+ // if (node.locked) return false;
589
+ if (!this.changedPosConstrain(node, o))
590
+ return false;
591
+ o.pack = true;
592
+ // simpler case: move item directly...
593
+ if (!this.maxRow) {
594
+ return this.moveNode(node, o);
595
+ }
596
+ // complex case: create a clone with NO maxRow (will check for out of bounds at the end)
597
+ var clonedNode;
598
+ var clone = new GridStackEngine({
599
+ column: this.column,
600
+ float: this.float,
601
+ nodes: this.nodes.map(function (n) {
602
+ if (n === node) {
603
+ clonedNode = __assign({}, n);
604
+ return clonedNode;
605
+ }
606
+ return __assign({}, n);
607
+ })
608
+ });
609
+ if (!clonedNode)
610
+ return false;
611
+ // check if we're covering 50% collision and could move
612
+ var canMove = clone.moveNode(clonedNode, o) && clone.getRow() <= this.maxRow;
613
+ // else check if we can force a swap (float=true, or different shapes) on non-resize
614
+ if (!canMove && !o.resizing && o.collide) {
615
+ var collide = o.collide.el.gridstackNode; // find the source node the clone collided with at 50%
616
+ if (this.swap(node, collide)) { // swaps and mark dirty
617
+ this._notify();
618
+ return true;
619
+ }
620
+ }
621
+ if (!canMove)
622
+ return false;
623
+ // if clone was able to move, copy those mods over to us now instead of caller trying to do this all over!
624
+ // Note: we can't use the list directly as elements and other parts point to actual node, so copy content
625
+ clone.nodes.filter(function (n) { return n._dirty; }).forEach(function (c) {
626
+ var n = _this.nodes.find(function (a) { return a._id === c._id; });
627
+ if (!n)
628
+ return;
629
+ utils_1.Utils.copyPos(n, c);
630
+ n._dirty = true;
631
+ });
632
+ this._notify();
633
+ return true;
634
+ };
635
+ /** return true if can fit in grid height constrain only (always true if no maxRow) */
636
+ GridStackEngine.prototype.willItFit = function (node) {
637
+ delete node._willFitPos;
638
+ if (!this.maxRow)
639
+ return true;
640
+ // create a clone with NO maxRow and check if still within size
641
+ var clone = new GridStackEngine({
642
+ column: this.column,
643
+ float: this.float,
644
+ nodes: this.nodes.map(function (n) { return __assign({}, n); })
645
+ });
646
+ var n = __assign({}, node); // clone node so we don't mod any settings on it but have full autoPosition and min/max as well! #1687
647
+ this.cleanupNode(n);
648
+ delete n.el;
649
+ delete n._id;
650
+ delete n.content;
651
+ delete n.grid;
652
+ clone.addNode(n);
653
+ if (clone.getRow() <= this.maxRow) {
654
+ node._willFitPos = utils_1.Utils.copyPos({}, n);
655
+ return true;
656
+ }
657
+ return false;
658
+ };
659
+ /** true if x,y or w,h are different after clamping to min/max */
660
+ GridStackEngine.prototype.changedPosConstrain = function (node, p) {
661
+ // first make sure w,h are set for caller
662
+ p.w = p.w || node.w;
663
+ p.h = p.h || node.h;
664
+ if (node.x !== p.x || node.y !== p.y)
665
+ return true;
666
+ // check constrained w,h
667
+ if (node.maxW) {
668
+ p.w = Math.min(p.w, node.maxW);
669
+ }
670
+ if (node.maxH) {
671
+ p.h = Math.min(p.h, node.maxH);
672
+ }
673
+ if (node.minW) {
674
+ p.w = Math.max(p.w, node.minW);
675
+ }
676
+ if (node.minH) {
677
+ p.h = Math.max(p.h, node.minH);
678
+ }
679
+ return (node.w !== p.w || node.h !== p.h);
680
+ };
681
+ /** return true if the passed in node was actually moved (checks for no-op and locked) */
682
+ GridStackEngine.prototype.moveNode = function (node, o) {
683
+ var _a, _b;
684
+ if (!node || /*node.locked ||*/ !o)
685
+ return false;
686
+ var wasUndefinedPack;
687
+ if (o.pack === undefined) {
688
+ wasUndefinedPack = o.pack = true;
689
+ }
690
+ // constrain the passed in values and check if we're still changing our node
691
+ if (typeof o.x !== 'number') {
692
+ o.x = node.x;
693
+ }
694
+ if (typeof o.y !== 'number') {
695
+ o.y = node.y;
696
+ }
697
+ if (typeof o.w !== 'number') {
698
+ o.w = node.w;
699
+ }
700
+ if (typeof o.h !== 'number') {
701
+ o.h = node.h;
702
+ }
703
+ var resizing = (node.w !== o.w || node.h !== o.h);
704
+ var nn = utils_1.Utils.copyPos({}, node, true); // get min/max out first, then opt positions next
705
+ utils_1.Utils.copyPos(nn, o);
706
+ nn = this.nodeBoundFix(nn, resizing);
707
+ utils_1.Utils.copyPos(o, nn);
708
+ if (utils_1.Utils.samePos(node, o))
709
+ return false;
710
+ var prevPos = utils_1.Utils.copyPos({}, node);
711
+ // check if we will need to fix collision at our new location
712
+ var collides = this.collideAll(node, nn, o.skip);
713
+ var needToMove = true;
714
+ if (collides.length) {
715
+ var activeDrag = node._moving && !o.nested;
716
+ // check to make sure we actually collided over 50% surface area while dragging
717
+ var collide = activeDrag ? this.directionCollideCoverage(node, o, collides) : collides[0];
718
+ // if we're enabling creation of sub-grids on the fly, see if we're covering 80% of either one, if we didn't already do that
719
+ if (activeDrag && collide && ((_b = (_a = node.grid) === null || _a === void 0 ? void 0 : _a.opts) === null || _b === void 0 ? void 0 : _b.subGridDynamic) && !node.grid._isTemp) {
720
+ var over = utils_1.Utils.areaIntercept(o.rect, collide._rect);
721
+ var a1 = utils_1.Utils.area(o.rect);
722
+ var a2 = utils_1.Utils.area(collide._rect);
723
+ var perc = over / (a1 < a2 ? a1 : a2);
724
+ if (perc > .8) {
725
+ collide.grid.makeSubGrid(collide.el, undefined, node);
726
+ collide = undefined;
727
+ }
728
+ }
729
+ if (collide) {
730
+ needToMove = !this._fixCollisions(node, nn, collide, o); // check if already moved...
731
+ }
732
+ else {
733
+ needToMove = false; // we didn't cover >50% for a move, skip...
734
+ if (wasUndefinedPack)
735
+ delete o.pack;
736
+ }
737
+ }
738
+ // now move (to the original ask vs the collision version which might differ) and repack things
739
+ if (needToMove) {
740
+ node._dirty = true;
741
+ utils_1.Utils.copyPos(node, nn);
742
+ }
743
+ if (o.pack) {
744
+ this._packNodes()
745
+ ._notify();
746
+ }
747
+ return !utils_1.Utils.samePos(node, prevPos); // pack might have moved things back
748
+ };
749
+ GridStackEngine.prototype.getRow = function () {
750
+ return this.nodes.reduce(function (row, n) { return Math.max(row, n.y + n.h); }, 0);
751
+ };
752
+ GridStackEngine.prototype.beginUpdate = function (node) {
753
+ if (!node._updating) {
754
+ node._updating = true;
755
+ delete node._skipDown;
756
+ if (!this.batchMode)
757
+ this.saveInitial();
758
+ }
759
+ return this;
760
+ };
761
+ GridStackEngine.prototype.endUpdate = function () {
762
+ var n = this.nodes.find(function (n) { return n._updating; });
763
+ if (n) {
764
+ delete n._updating;
765
+ delete n._skipDown;
766
+ }
767
+ return this;
768
+ };
769
+ /** saves a copy of the largest column layout (eg 12 even when rendering oneColumnMode) so we don't loose orig layout,
770
+ * returning a list of widgets for serialization */
771
+ GridStackEngine.prototype.save = function (saveElement, saveCB) {
772
+ var _a;
773
+ if (saveElement === void 0) { saveElement = true; }
774
+ // use the highest layout for any saved info so we can have full detail on reload #1849
775
+ var len = (_a = this._layouts) === null || _a === void 0 ? void 0 : _a.length;
776
+ var layout = len && this.column !== (len - 1) ? this._layouts[len - 1] : null;
777
+ var list = [];
778
+ this.sortNodes();
779
+ this.nodes.forEach(function (n) {
780
+ var wl = layout === null || layout === void 0 ? void 0 : layout.find(function (l) { return l._id === n._id; });
781
+ var w = __assign({}, n);
782
+ // use layout info instead if set
783
+ if (wl) {
784
+ w.x = wl.x;
785
+ w.y = wl.y;
786
+ w.w = wl.w;
787
+ }
788
+ utils_1.Utils.removeInternalForSave(w, !saveElement);
789
+ if (saveCB)
790
+ saveCB(n, w);
791
+ list.push(w);
792
+ });
793
+ return list;
794
+ };
795
+ /** @internal called whenever a node is added or moved - updates the cached layouts */
796
+ GridStackEngine.prototype.layoutsNodesChange = function (nodes) {
797
+ var _this = this;
798
+ if (!this._layouts || this._inColumnResize)
799
+ return this;
800
+ // remove smaller layouts - we will re-generate those on the fly... larger ones need to update
801
+ this._layouts.forEach(function (layout, column) {
802
+ if (!layout || column === _this.column)
803
+ return _this;
804
+ if (column < _this.column) {
805
+ _this._layouts[column] = undefined;
806
+ }
807
+ else {
808
+ // we save the original x,y,w (h isn't cached) to see what actually changed to propagate better.
809
+ // NOTE: we don't need to check against out of bound scaling/moving as that will be done when using those cache values. #1785
810
+ var ratio_1 = column / _this.column;
811
+ nodes.forEach(function (node) {
812
+ if (!node._orig)
813
+ return; // didn't change (newly added ?)
814
+ var n = layout.find(function (l) { return l._id === node._id; });
815
+ if (!n)
816
+ return; // no cache for new nodes. Will use those values.
817
+ // Y changed, push down same amount
818
+ // TODO: detect doing item 'swaps' will help instead of move (especially in 1 column mode)
819
+ if (node.y !== node._orig.y) {
820
+ n.y += (node.y - node._orig.y);
821
+ }
822
+ // X changed, scale from new position
823
+ if (node.x !== node._orig.x) {
824
+ n.x = Math.round(node.x * ratio_1);
825
+ }
826
+ // width changed, scale from new width
827
+ if (node.w !== node._orig.w) {
828
+ n.w = Math.round(node.w * ratio_1);
829
+ }
830
+ // ...height always carries over from cache
831
+ });
832
+ }
833
+ });
834
+ return this;
835
+ };
836
+ /**
837
+ * @internal Called to scale the widget width & position up/down based on the column change.
838
+ * Note we store previous layouts (especially original ones) to make it possible to go
839
+ * from say 12 -> 1 -> 12 and get back to where we were.
840
+ *
841
+ * @param prevColumn previous number of columns
842
+ * @param column new column number
843
+ * @param nodes different sorted list (ex: DOM order) instead of current list
844
+ * @param layout specify the type of re-layout that will happen (position, size, etc...).
845
+ * Note: items will never be outside of the current column boundaries. default (moveScale). Ignored for 1 column
846
+ */
847
+ GridStackEngine.prototype.updateNodeWidths = function (prevColumn, column, nodes, layout) {
848
+ var _this = this;
849
+ var _a;
850
+ if (layout === void 0) { layout = 'moveScale'; }
851
+ if (!this.nodes.length || !column || prevColumn === column)
852
+ return this;
853
+ // cache the current layout in case they want to go back (like 12 -> 1 -> 12) as it requires original data
854
+ this.cacheLayout(this.nodes, prevColumn);
855
+ this.batchUpdate(); // do this EARLY as it will call saveInitial() so we can detect where we started for _dirty and collision
856
+ var newNodes = [];
857
+ // if we're going to 1 column and using DOM order rather than default sorting, then generate that layout
858
+ var domOrder = false;
859
+ if (column === 1 && (nodes === null || nodes === void 0 ? void 0 : nodes.length)) {
860
+ domOrder = true;
861
+ var top_1 = 0;
862
+ nodes.forEach(function (n) {
863
+ n.x = 0;
864
+ n.w = 1;
865
+ n.y = Math.max(n.y, top_1);
866
+ top_1 = n.y + n.h;
867
+ });
868
+ newNodes = nodes;
869
+ nodes = [];
870
+ }
871
+ else {
872
+ nodes = utils_1.Utils.sort(this.nodes, -1, prevColumn); // current column reverse sorting so we can insert last to front (limit collision)
873
+ }
874
+ // see if we have cached previous layout IFF we are going up in size (restore) otherwise always
875
+ // generate next size down from where we are (looks more natural as you gradually size down).
876
+ var cacheNodes = [];
877
+ if (column > prevColumn) {
878
+ cacheNodes = this._layouts[column] || [];
879
+ // ...if not, start with the largest layout (if not already there) as down-scaling is more accurate
880
+ // by pretending we came from that larger column by assigning those values as starting point
881
+ var lastIndex = this._layouts.length - 1;
882
+ if (!cacheNodes.length && prevColumn !== lastIndex && ((_a = this._layouts[lastIndex]) === null || _a === void 0 ? void 0 : _a.length)) {
883
+ prevColumn = lastIndex;
884
+ this._layouts[lastIndex].forEach(function (cacheNode) {
885
+ var n = nodes.find(function (n) { return n._id === cacheNode._id; });
886
+ if (n) {
887
+ // still current, use cache info positions
888
+ n.x = cacheNode.x;
889
+ n.y = cacheNode.y;
890
+ n.w = cacheNode.w;
891
+ }
892
+ });
893
+ }
894
+ }
895
+ // if we found cache re-use those nodes that are still current
896
+ cacheNodes.forEach(function (cacheNode) {
897
+ var j = nodes.findIndex(function (n) { return n._id === cacheNode._id; });
898
+ if (j !== -1) {
899
+ // still current, use cache info positions
900
+ if (cacheNode.autoPosition || isNaN(cacheNode.x) || isNaN(cacheNode.y)) {
901
+ _this.findEmptyPosition(cacheNode, newNodes);
902
+ }
903
+ if (!cacheNode.autoPosition) {
904
+ nodes[j].x = cacheNode.x;
905
+ nodes[j].y = cacheNode.y;
906
+ nodes[j].w = cacheNode.w;
907
+ newNodes.push(nodes[j]);
908
+ }
909
+ nodes.splice(j, 1);
910
+ }
911
+ });
912
+ // ...and add any extra non-cached ones
913
+ if (nodes.length) {
914
+ if (typeof layout === 'function') {
915
+ layout(column, prevColumn, newNodes, nodes);
916
+ }
917
+ else if (!domOrder) {
918
+ var ratio_2 = column / prevColumn;
919
+ var move_1 = (layout === 'move' || layout === 'moveScale');
920
+ var scale_1 = (layout === 'scale' || layout === 'moveScale');
921
+ nodes.forEach(function (node) {
922
+ // NOTE: x + w could be outside of the grid, but addNode() below will handle that
923
+ node.x = (column === 1 ? 0 : (move_1 ? Math.round(node.x * ratio_2) : Math.min(node.x, column - 1)));
924
+ node.w = ((column === 1 || prevColumn === 1) ? 1 :
925
+ scale_1 ? (Math.round(node.w * ratio_2) || 1) : (Math.min(node.w, column)));
926
+ newNodes.push(node);
927
+ });
928
+ nodes = [];
929
+ }
930
+ }
931
+ // finally re-layout them in reverse order (to get correct placement)
932
+ if (!domOrder)
933
+ newNodes = utils_1.Utils.sort(newNodes, -1, column);
934
+ this._inColumnResize = true; // prevent cache update
935
+ this.nodes = []; // pretend we have no nodes to start with (add() will use same structures) to simplify layout
936
+ newNodes.forEach(function (node) {
937
+ _this.addNode(node, false); // 'false' for add event trigger
938
+ delete node._orig; // make sure the commit doesn't try to restore things back to original
939
+ });
940
+ this.batchUpdate(false);
941
+ delete this._inColumnResize;
942
+ return this;
943
+ };
944
+ /**
945
+ * call to cache the given layout internally to the given location so we can restore back when column changes size
946
+ * @param nodes list of nodes
947
+ * @param column corresponding column index to save it under
948
+ * @param clear if true, will force other caches to be removed (default false)
949
+ */
950
+ GridStackEngine.prototype.cacheLayout = function (nodes, column, clear) {
951
+ if (clear === void 0) { clear = false; }
952
+ var copy = [];
953
+ nodes.forEach(function (n, i) {
954
+ var _a;
955
+ n._id = (_a = n._id) !== null && _a !== void 0 ? _a : GridStackEngine._idSeq++; // make sure we have an id in case this is new layout, else re-use id already set
956
+ copy[i] = { x: n.x, y: n.y, w: n.w, _id: n._id }; // only thing we change is x,y,w and id to find it back
957
+ });
958
+ this._layouts = clear ? [] : this._layouts || []; // use array to find larger quick
959
+ this._layouts[column] = copy;
960
+ return this;
961
+ };
962
+ /**
963
+ * call to cache the given node layout internally to the given location so we can restore back when column changes size
964
+ * @param node single node to cache
965
+ * @param column corresponding column index to save it under
966
+ */
967
+ GridStackEngine.prototype.cacheOneLayout = function (n, column) {
968
+ var _a;
969
+ n._id = (_a = n._id) !== null && _a !== void 0 ? _a : GridStackEngine._idSeq++;
970
+ var l = { x: n.x, y: n.y, w: n.w, _id: n._id };
971
+ if (n.autoPosition) {
972
+ delete l.x;
973
+ delete l.y;
974
+ l.autoPosition = true;
975
+ }
976
+ this._layouts = this._layouts || [];
977
+ this._layouts[column] = this._layouts[column] || [];
978
+ var index = this.findCacheLayout(n, column);
979
+ if (index === -1)
980
+ this._layouts[column].push(l);
981
+ else
982
+ this._layouts[column][index] = l;
983
+ return this;
984
+ };
985
+ GridStackEngine.prototype.findCacheLayout = function (n, column) {
986
+ var _a, _b, _c;
987
+ return (_c = (_b = (_a = this._layouts) === null || _a === void 0 ? void 0 : _a[column]) === null || _b === void 0 ? void 0 : _b.findIndex(function (l) { return l._id === n._id; })) !== null && _c !== void 0 ? _c : -1;
988
+ };
989
+ /** called to remove all internal values but the _id */
990
+ GridStackEngine.prototype.cleanupNode = function (node) {
991
+ for (var prop in node) {
992
+ if (prop[0] === '_' && prop !== '_id')
993
+ delete node[prop];
994
+ }
995
+ return this;
996
+ };
997
+ /** @internal unique global internal _id counter */
998
+ GridStackEngine._idSeq = 0;
999
+ return GridStackEngine;
1000
+ }());
977
1001
  //# sourceMappingURL=gridstack-engine.js.map