gridstack 7.3.0 → 8.0.1

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