gridstack 8.0.1 → 8.1.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 (193) hide show
  1. package/README.md +13 -11
  2. package/dist/dd-base-impl.d.ts +1 -1
  3. package/dist/dd-base-impl.js +1 -1
  4. package/dist/dd-base-impl.js.map +1 -1
  5. package/dist/dd-draggable.d.ts +3 -2
  6. package/dist/dd-draggable.js +9 -8
  7. package/dist/dd-draggable.js.map +1 -1
  8. package/dist/dd-droppable.d.ts +1 -1
  9. package/dist/dd-droppable.js +1 -1
  10. package/dist/dd-droppable.js.map +1 -1
  11. package/dist/dd-element.d.ts +1 -1
  12. package/dist/dd-element.js +1 -1
  13. package/dist/dd-element.js.map +1 -1
  14. package/dist/dd-gridstack.d.ts +1 -1
  15. package/dist/dd-gridstack.js +1 -1
  16. package/dist/dd-gridstack.js.map +1 -1
  17. package/dist/dd-manager.d.ts +1 -1
  18. package/dist/dd-manager.js +1 -1
  19. package/dist/dd-manager.js.map +1 -1
  20. package/dist/dd-resizable-handle.d.ts +1 -1
  21. package/dist/dd-resizable-handle.js +1 -1
  22. package/dist/dd-resizable-handle.js.map +1 -1
  23. package/dist/dd-resizable.d.ts +1 -1
  24. package/dist/dd-resizable.js +1 -1
  25. package/dist/dd-resizable.js.map +1 -1
  26. package/dist/dd-touch.d.ts +1 -1
  27. package/dist/dd-touch.js +1 -1
  28. package/dist/dd-touch.js.map +1 -1
  29. package/dist/es5/dd-base-impl.d.ts +1 -1
  30. package/dist/es5/dd-base-impl.js +1 -1
  31. package/dist/es5/dd-base-impl.js.map +1 -1
  32. package/dist/es5/dd-draggable.d.ts +3 -2
  33. package/dist/es5/dd-draggable.js +9 -8
  34. package/dist/es5/dd-draggable.js.map +1 -1
  35. package/dist/es5/dd-droppable.d.ts +1 -1
  36. package/dist/es5/dd-droppable.js +1 -1
  37. package/dist/es5/dd-droppable.js.map +1 -1
  38. package/dist/es5/dd-element.d.ts +1 -1
  39. package/dist/es5/dd-element.js +1 -1
  40. package/dist/es5/dd-element.js.map +1 -1
  41. package/dist/es5/dd-gridstack.d.ts +1 -1
  42. package/dist/es5/dd-gridstack.js +1 -1
  43. package/dist/es5/dd-gridstack.js.map +1 -1
  44. package/dist/es5/dd-manager.d.ts +1 -1
  45. package/dist/es5/dd-manager.js +1 -1
  46. package/dist/es5/dd-manager.js.map +1 -1
  47. package/dist/es5/dd-resizable-handle.d.ts +1 -1
  48. package/dist/es5/dd-resizable-handle.js +1 -1
  49. package/dist/es5/dd-resizable-handle.js.map +1 -1
  50. package/dist/es5/dd-resizable.d.ts +1 -1
  51. package/dist/es5/dd-resizable.js +1 -1
  52. package/dist/es5/dd-resizable.js.map +1 -1
  53. package/dist/es5/dd-touch.d.ts +1 -1
  54. package/dist/es5/dd-touch.js +1 -1
  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 +1 -1
  60. package/dist/es5/gridstack-engine.js +1 -1
  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 +3 -3
  64. package/dist/es5/gridstack.js +4 -13
  65. package/dist/es5/gridstack.js.map +1 -1
  66. package/dist/es5/types.d.ts +5 -3
  67. package/dist/es5/types.js +1 -1
  68. package/dist/es5/types.js.map +1 -1
  69. package/dist/es5/utils.d.ts +3 -3
  70. package/dist/es5/utils.js +12 -10
  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 +1 -1
  76. package/dist/gridstack-engine.js +1 -1
  77. package/dist/gridstack-engine.js.map +1 -1
  78. package/dist/gridstack-extra.css +180 -240
  79. package/dist/gridstack-extra.min.css +1 -1
  80. package/dist/gridstack.css +95 -118
  81. package/dist/gridstack.d.ts +3 -3
  82. package/dist/gridstack.js +4 -13
  83. package/dist/gridstack.js.map +1 -1
  84. package/dist/gridstack.min.css +1 -1
  85. package/dist/ng/gridstack-item.component.d.ts +1 -1
  86. package/dist/ng/gridstack-item.component.js +1 -1
  87. package/dist/ng/gridstack.component.d.ts +1 -1
  88. package/dist/ng/gridstack.component.js +1 -1
  89. package/dist/src/gridstack-extra.scss +9 -11
  90. package/dist/src/gridstack.scss +108 -97
  91. package/dist/types.d.ts +5 -3
  92. package/dist/types.js +1 -1
  93. package/dist/types.js.map +1 -1
  94. package/dist/utils.d.ts +3 -3
  95. package/dist/utils.js +12 -10
  96. package/dist/utils.js.map +1 -1
  97. package/doc/CHANGES.md +7 -0
  98. package/doc/README.md +5 -4
  99. package/package.json +1 -1
  100. package/dist_save/angular/README.md +0 -94
  101. package/dist_save/angular/gridstack-item.component.ts +0 -72
  102. package/dist_save/angular/gridstack.component.ts +0 -199
  103. package/dist_save/dd-base-impl.d.ts +0 -20
  104. package/dist_save/dd-base-impl.js +0 -36
  105. package/dist_save/dd-base-impl.js.map +0 -1
  106. package/dist_save/dd-draggable.d.ts +0 -28
  107. package/dist_save/dd-draggable.js +0 -343
  108. package/dist_save/dd-draggable.js.map +0 -1
  109. package/dist_save/dd-droppable.d.ts +0 -26
  110. package/dist_save/dd-droppable.js +0 -149
  111. package/dist_save/dd-droppable.js.map +0 -1
  112. package/dist_save/dd-element.d.ts +0 -27
  113. package/dist_save/dd-element.js +0 -95
  114. package/dist_save/dd-element.js.map +0 -1
  115. package/dist_save/dd-gridstack.d.ts +0 -34
  116. package/dist_save/dd-gridstack.js +0 -125
  117. package/dist_save/dd-gridstack.js.map +0 -1
  118. package/dist_save/dd-manager.d.ts +0 -22
  119. package/dist_save/dd-manager.js +0 -14
  120. package/dist_save/dd-manager.js.map +0 -1
  121. package/dist_save/dd-resizable-handle.d.ts +0 -14
  122. package/dist_save/dd-resizable-handle.js +0 -106
  123. package/dist_save/dd-resizable-handle.js.map +0 -1
  124. package/dist_save/dd-resizable.d.ts +0 -28
  125. package/dist_save/dd-resizable.js +0 -294
  126. package/dist_save/dd-resizable.js.map +0 -1
  127. package/dist_save/dd-touch.d.ts +0 -33
  128. package/dist_save/dd-touch.js +0 -183
  129. package/dist_save/dd-touch.js.map +0 -1
  130. package/dist_save/es5/dd-base-impl.d.ts +0 -20
  131. package/dist_save/es5/dd-base-impl.js +0 -41
  132. package/dist_save/es5/dd-base-impl.js.map +0 -1
  133. package/dist_save/es5/dd-draggable.d.ts +0 -28
  134. package/dist_save/es5/dd-draggable.js +0 -366
  135. package/dist_save/es5/dd-draggable.js.map +0 -1
  136. package/dist_save/es5/dd-droppable.d.ts +0 -26
  137. package/dist_save/es5/dd-droppable.js +0 -180
  138. package/dist_save/es5/dd-droppable.js.map +0 -1
  139. package/dist_save/es5/dd-element.d.ts +0 -27
  140. package/dist_save/es5/dd-element.js +0 -96
  141. package/dist_save/es5/dd-element.js.map +0 -1
  142. package/dist_save/es5/dd-gridstack.d.ts +0 -34
  143. package/dist_save/es5/dd-gridstack.js +0 -145
  144. package/dist_save/es5/dd-gridstack.js.map +0 -1
  145. package/dist_save/es5/dd-manager.d.ts +0 -22
  146. package/dist_save/es5/dd-manager.js +0 -17
  147. package/dist_save/es5/dd-manager.js.map +0 -1
  148. package/dist_save/es5/dd-resizable-handle.d.ts +0 -14
  149. package/dist_save/es5/dd-resizable-handle.js +0 -107
  150. package/dist_save/es5/dd-resizable-handle.js.map +0 -1
  151. package/dist_save/es5/dd-resizable.d.ts +0 -28
  152. package/dist_save/es5/dd-resizable.js +0 -316
  153. package/dist_save/es5/dd-resizable.js.map +0 -1
  154. package/dist_save/es5/dd-touch.d.ts +0 -33
  155. package/dist_save/es5/dd-touch.js +0 -186
  156. package/dist_save/es5/dd-touch.js.map +0 -1
  157. package/dist_save/es5/gridstack-all.js +0 -3
  158. package/dist_save/es5/gridstack-all.js.LICENSE.txt +0 -7
  159. package/dist_save/es5/gridstack-all.js.map +0 -1
  160. package/dist_save/es5/gridstack-engine.d.ts +0 -102
  161. package/dist_save/es5/gridstack-engine.js +0 -997
  162. package/dist_save/es5/gridstack-engine.js.map +0 -1
  163. package/dist_save/es5/gridstack-poly.js +0 -356
  164. package/dist_save/es5/gridstack.d.ts +0 -376
  165. package/dist_save/es5/gridstack.js +0 -2238
  166. package/dist_save/es5/gridstack.js.map +0 -1
  167. package/dist_save/es5/types.d.ts +0 -284
  168. package/dist_save/es5/types.js +0 -36
  169. package/dist_save/es5/types.js.map +0 -1
  170. package/dist_save/es5/utils.d.ts +0 -95
  171. package/dist_save/es5/utils.js +0 -590
  172. package/dist_save/es5/utils.js.map +0 -1
  173. package/dist_save/gridstack-all.js +0 -3
  174. package/dist_save/gridstack-all.js.LICENSE.txt +0 -7
  175. package/dist_save/gridstack-all.js.map +0 -1
  176. package/dist_save/gridstack-engine.d.ts +0 -102
  177. package/dist_save/gridstack-engine.js +0 -956
  178. package/dist_save/gridstack-engine.js.map +0 -1
  179. package/dist_save/gridstack-extra.css +0 -433
  180. package/dist_save/gridstack-extra.min.css +0 -1
  181. package/dist_save/gridstack.css +0 -226
  182. package/dist_save/gridstack.d.ts +0 -376
  183. package/dist_save/gridstack.js +0 -2162
  184. package/dist_save/gridstack.js.map +0 -1
  185. package/dist_save/gridstack.min.css +0 -1
  186. package/dist_save/src/gridstack-extra.scss +0 -27
  187. package/dist_save/src/gridstack.scss +0 -131
  188. package/dist_save/types.d.ts +0 -284
  189. package/dist_save/types.js +0 -36
  190. package/dist_save/types.js.map +0 -1
  191. package/dist_save/utils.d.ts +0 -95
  192. package/dist_save/utils.js +0 -548
  193. package/dist_save/utils.js.map +0 -1
@@ -1,997 +0,0 @@
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
- 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.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
- 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
- utils_1.Utils.sanitizeMinMax(node);
359
- // check for NaN (in case messed up strings were passed. can't do parseInt() || defaults.x above as 0 is valid #)
360
- if (typeof node.x == 'string') {
361
- node.x = Number(node.x);
362
- }
363
- if (typeof node.y == 'string') {
364
- node.y = Number(node.y);
365
- }
366
- if (typeof node.w == 'string') {
367
- node.w = Number(node.w);
368
- }
369
- if (typeof node.h == 'string') {
370
- node.h = Number(node.h);
371
- }
372
- if (isNaN(node.x)) {
373
- node.x = defaults.x;
374
- node.autoPosition = true;
375
- }
376
- if (isNaN(node.y)) {
377
- node.y = defaults.y;
378
- node.autoPosition = true;
379
- }
380
- if (isNaN(node.w)) {
381
- node.w = defaults.w;
382
- }
383
- if (isNaN(node.h)) {
384
- node.h = defaults.h;
385
- }
386
- return this.nodeBoundFix(node, resizing);
387
- };
388
- /** part2 of preparing a node to fit inside our grid - checks for x,y,w from grid dimensions */
389
- GridStackEngine.prototype.nodeBoundFix = function (node, resizing) {
390
- var before = node._orig || utils_1.Utils.copyPos({}, node);
391
- if (node.maxW) {
392
- node.w = Math.min(node.w, node.maxW);
393
- }
394
- if (node.maxH) {
395
- node.h = Math.min(node.h, node.maxH);
396
- }
397
- if (node.minW && node.minW <= this.column) {
398
- node.w = Math.max(node.w, node.minW);
399
- }
400
- if (node.minH) {
401
- node.h = Math.max(node.h, node.minH);
402
- }
403
- // if user loaded a larger than allowed widget for current # of columns (or force 1 column mode),
404
- // remember it's position & width so we can restore back (1 -> 12 column) #1655 #1985
405
- // IFF we're not in the middle of column resizing!
406
- var saveOrig = this.column === 1 || node.x + node.w > this.column;
407
- if (saveOrig && this.column < 12 && !this._inColumnResize && node._id && this.findCacheLayout(node, 12) === -1) {
408
- var copy = __assign({}, node); // need _id + positions
409
- if (copy.autoPosition) {
410
- delete copy.x;
411
- delete copy.y;
412
- }
413
- else
414
- copy.x = Math.min(11, copy.x);
415
- copy.w = Math.min(12, copy.w);
416
- this.cacheOneLayout(copy, 12);
417
- }
418
- if (node.w > this.column) {
419
- node.w = this.column;
420
- }
421
- else if (node.w < 1) {
422
- node.w = 1;
423
- }
424
- if (this.maxRow && node.h > this.maxRow) {
425
- node.h = this.maxRow;
426
- }
427
- else if (node.h < 1) {
428
- node.h = 1;
429
- }
430
- if (node.x < 0) {
431
- node.x = 0;
432
- }
433
- if (node.y < 0) {
434
- node.y = 0;
435
- }
436
- if (node.x + node.w > this.column) {
437
- if (resizing) {
438
- node.w = this.column - node.x;
439
- }
440
- else {
441
- node.x = this.column - node.w;
442
- }
443
- }
444
- if (this.maxRow && node.y + node.h > this.maxRow) {
445
- if (resizing) {
446
- node.h = this.maxRow - node.y;
447
- }
448
- else {
449
- node.y = this.maxRow - node.h;
450
- }
451
- }
452
- if (!utils_1.Utils.samePos(node, before)) {
453
- node._dirty = true;
454
- }
455
- return node;
456
- };
457
- /** returns a list of modified nodes from their original values */
458
- GridStackEngine.prototype.getDirtyNodes = function (verify) {
459
- // compare original x,y,w,h instead as _dirty can be a temporary state
460
- if (verify) {
461
- return this.nodes.filter(function (n) { return n._dirty && !utils_1.Utils.samePos(n, n._orig); });
462
- }
463
- return this.nodes.filter(function (n) { return n._dirty; });
464
- };
465
- /** @internal call this to call onChange callback with dirty nodes so DOM can be updated */
466
- GridStackEngine.prototype._notify = function (removedNodes) {
467
- if (this.batchMode || !this.onChange)
468
- return this;
469
- var dirtyNodes = (removedNodes || []).concat(this.getDirtyNodes());
470
- this.onChange(dirtyNodes);
471
- return this;
472
- };
473
- /** @internal remove dirty and last tried info */
474
- GridStackEngine.prototype.cleanNodes = function () {
475
- if (this.batchMode)
476
- return this;
477
- this.nodes.forEach(function (n) {
478
- delete n._dirty;
479
- delete n._lastTried;
480
- });
481
- return this;
482
- };
483
- /** @internal called to save initial position/size to track real dirty state.
484
- * Note: should be called right after we call change event (so next API is can detect changes)
485
- * as well as right before we start move/resize/enter (so we can restore items to prev values) */
486
- GridStackEngine.prototype.saveInitial = function () {
487
- this.nodes.forEach(function (n) {
488
- n._orig = utils_1.Utils.copyPos({}, n);
489
- delete n._dirty;
490
- });
491
- this._hasLocked = this.nodes.some(function (n) { return n.locked; });
492
- return this;
493
- };
494
- /** @internal restore all the nodes back to initial values (called when we leave) */
495
- GridStackEngine.prototype.restoreInitial = function () {
496
- this.nodes.forEach(function (n) {
497
- if (utils_1.Utils.samePos(n, n._orig))
498
- return;
499
- utils_1.Utils.copyPos(n, n._orig);
500
- n._dirty = true;
501
- });
502
- this._notify();
503
- return this;
504
- };
505
- /** find the first available empty spot for the given node width/height, updating the x,y attributes. return true if found.
506
- * optionally you can pass your own existing node list and column count, otherwise defaults to that engine data.
507
- */
508
- GridStackEngine.prototype.findEmptyPosition = function (node, nodeList, column) {
509
- if (nodeList === void 0) { nodeList = this.nodes; }
510
- if (column === void 0) { column = this.column; }
511
- nodeList = utils_1.Utils.sort(nodeList, -1, column);
512
- var found = false;
513
- var _loop_1 = function (i) {
514
- var x = i % column;
515
- var y = Math.floor(i / column);
516
- if (x + node.w > column) {
517
- return "continue";
518
- }
519
- var box = { x: x, y: y, w: node.w, h: node.h };
520
- if (!nodeList.find(function (n) { return utils_1.Utils.isIntercepted(box, n); })) {
521
- node.x = x;
522
- node.y = y;
523
- delete node.autoPosition;
524
- found = true;
525
- }
526
- };
527
- for (var i = 0; !found; ++i) {
528
- _loop_1(i);
529
- }
530
- return found;
531
- };
532
- /** call to add the given node to our list, fixing collision and re-packing */
533
- GridStackEngine.prototype.addNode = function (node, triggerAddEvent) {
534
- if (triggerAddEvent === void 0) { triggerAddEvent = false; }
535
- var dup = this.nodes.find(function (n) { return n._id === node._id; });
536
- if (dup)
537
- return dup; // prevent inserting twice! return it instead.
538
- // skip prepareNode if we're in middle of column resize (not new) but do check for bounds!
539
- node = this._inColumnResize ? this.nodeBoundFix(node) : this.prepareNode(node);
540
- delete node._temporaryRemoved;
541
- delete node._removeDOM;
542
- if (node.autoPosition && this.findEmptyPosition(node)) {
543
- delete node.autoPosition; // found our slot
544
- }
545
- this.nodes.push(node);
546
- if (triggerAddEvent) {
547
- this.addedNodes.push(node);
548
- }
549
- this._fixCollisions(node);
550
- if (!this.batchMode) {
551
- this._packNodes()._notify();
552
- }
553
- return node;
554
- };
555
- GridStackEngine.prototype.removeNode = function (node, removeDOM, triggerEvent) {
556
- if (removeDOM === void 0) { removeDOM = true; }
557
- if (triggerEvent === void 0) { triggerEvent = false; }
558
- if (!this.nodes.find(function (n) { return n === node; })) {
559
- // TEST console.log(`Error: GridStackEngine.removeNode() node._id=${node._id} not found!`)
560
- return this;
561
- }
562
- if (triggerEvent) { // we wait until final drop to manually track removed items (rather than during drag)
563
- this.removedNodes.push(node);
564
- }
565
- if (removeDOM)
566
- node._removeDOM = true; // let CB remove actual HTML (used to set _id to null, but then we loose layout info)
567
- // don't use 'faster' .splice(findIndex(),1) in case node isn't in our list, or in multiple times.
568
- this.nodes = this.nodes.filter(function (n) { return n !== node; });
569
- return this._packNodes()
570
- ._notify([node]);
571
- };
572
- GridStackEngine.prototype.removeAll = function (removeDOM) {
573
- if (removeDOM === void 0) { removeDOM = true; }
574
- delete this._layouts;
575
- if (this.nodes.length === 0)
576
- return this;
577
- 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)
578
- this.removedNodes = this.nodes;
579
- this.nodes = [];
580
- return this._notify(this.removedNodes);
581
- };
582
- /** checks if item can be moved (layout constrain) vs moveNode(), returning true if was able to move.
583
- * In more complicated cases (maxRow) it will attempt at moving the item and fixing
584
- * others in a clone first, then apply those changes if still within specs. */
585
- GridStackEngine.prototype.moveNodeCheck = function (node, o) {
586
- var _this = this;
587
- // if (node.locked) return false;
588
- if (!this.changedPosConstrain(node, o))
589
- return false;
590
- o.pack = true;
591
- // simpler case: move item directly...
592
- if (!this.maxRow) {
593
- return this.moveNode(node, o);
594
- }
595
- // complex case: create a clone with NO maxRow (will check for out of bounds at the end)
596
- var clonedNode;
597
- var clone = new GridStackEngine({
598
- column: this.column,
599
- float: this.float,
600
- nodes: this.nodes.map(function (n) {
601
- if (n === node) {
602
- clonedNode = __assign({}, n);
603
- return clonedNode;
604
- }
605
- return __assign({}, n);
606
- })
607
- });
608
- if (!clonedNode)
609
- return false;
610
- // check if we're covering 50% collision and could move
611
- var canMove = clone.moveNode(clonedNode, o) && clone.getRow() <= this.maxRow;
612
- // else check if we can force a swap (float=true, or different shapes) on non-resize
613
- if (!canMove && !o.resizing && o.collide) {
614
- var collide = o.collide.el.gridstackNode; // find the source node the clone collided with at 50%
615
- if (this.swap(node, collide)) { // swaps and mark dirty
616
- this._notify();
617
- return true;
618
- }
619
- }
620
- if (!canMove)
621
- return false;
622
- // if clone was able to move, copy those mods over to us now instead of caller trying to do this all over!
623
- // Note: we can't use the list directly as elements and other parts point to actual node, so copy content
624
- clone.nodes.filter(function (n) { return n._dirty; }).forEach(function (c) {
625
- var n = _this.nodes.find(function (a) { return a._id === c._id; });
626
- if (!n)
627
- return;
628
- utils_1.Utils.copyPos(n, c);
629
- n._dirty = true;
630
- });
631
- this._notify();
632
- return true;
633
- };
634
- /** return true if can fit in grid height constrain only (always true if no maxRow) */
635
- GridStackEngine.prototype.willItFit = function (node) {
636
- delete node._willFitPos;
637
- if (!this.maxRow)
638
- return true;
639
- // create a clone with NO maxRow and check if still within size
640
- var clone = new GridStackEngine({
641
- column: this.column,
642
- float: this.float,
643
- nodes: this.nodes.map(function (n) { return __assign({}, n); })
644
- });
645
- 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
646
- this.cleanupNode(n);
647
- delete n.el;
648
- delete n._id;
649
- delete n.content;
650
- delete n.grid;
651
- clone.addNode(n);
652
- if (clone.getRow() <= this.maxRow) {
653
- node._willFitPos = utils_1.Utils.copyPos({}, n);
654
- return true;
655
- }
656
- return false;
657
- };
658
- /** true if x,y or w,h are different after clamping to min/max */
659
- GridStackEngine.prototype.changedPosConstrain = function (node, p) {
660
- // first make sure w,h are set for caller
661
- p.w = p.w || node.w;
662
- p.h = p.h || node.h;
663
- if (node.x !== p.x || node.y !== p.y)
664
- return true;
665
- // check constrained w,h
666
- if (node.maxW) {
667
- p.w = Math.min(p.w, node.maxW);
668
- }
669
- if (node.maxH) {
670
- p.h = Math.min(p.h, node.maxH);
671
- }
672
- if (node.minW) {
673
- p.w = Math.max(p.w, node.minW);
674
- }
675
- if (node.minH) {
676
- p.h = Math.max(p.h, node.minH);
677
- }
678
- return (node.w !== p.w || node.h !== p.h);
679
- };
680
- /** return true if the passed in node was actually moved (checks for no-op and locked) */
681
- GridStackEngine.prototype.moveNode = function (node, o) {
682
- var _a, _b;
683
- if (!node || /*node.locked ||*/ !o)
684
- return false;
685
- var wasUndefinedPack;
686
- if (o.pack === undefined) {
687
- wasUndefinedPack = o.pack = true;
688
- }
689
- // constrain the passed in values and check if we're still changing our node
690
- if (typeof o.x !== 'number') {
691
- o.x = node.x;
692
- }
693
- if (typeof o.y !== 'number') {
694
- o.y = node.y;
695
- }
696
- if (typeof o.w !== 'number') {
697
- o.w = node.w;
698
- }
699
- if (typeof o.h !== 'number') {
700
- o.h = node.h;
701
- }
702
- var resizing = (node.w !== o.w || node.h !== o.h);
703
- var nn = utils_1.Utils.copyPos({}, node, true); // get min/max out first, then opt positions next
704
- utils_1.Utils.copyPos(nn, o);
705
- nn = this.nodeBoundFix(nn, resizing);
706
- utils_1.Utils.copyPos(o, nn);
707
- if (utils_1.Utils.samePos(node, o))
708
- return false;
709
- var prevPos = utils_1.Utils.copyPos({}, node);
710
- // check if we will need to fix collision at our new location
711
- var collides = this.collideAll(node, nn, o.skip);
712
- var needToMove = true;
713
- if (collides.length) {
714
- var activeDrag = node._moving && !o.nested;
715
- // check to make sure we actually collided over 50% surface area while dragging
716
- var collide = activeDrag ? this.directionCollideCoverage(node, o, collides) : collides[0];
717
- // 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
718
- 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) {
719
- var over = utils_1.Utils.areaIntercept(o.rect, collide._rect);
720
- var a1 = utils_1.Utils.area(o.rect);
721
- var a2 = utils_1.Utils.area(collide._rect);
722
- var perc = over / (a1 < a2 ? a1 : a2);
723
- if (perc > .8) {
724
- collide.grid.makeSubGrid(collide.el, undefined, node);
725
- collide = undefined;
726
- }
727
- }
728
- if (collide) {
729
- needToMove = !this._fixCollisions(node, nn, collide, o); // check if already moved...
730
- }
731
- else {
732
- needToMove = false; // we didn't cover >50% for a move, skip...
733
- if (wasUndefinedPack)
734
- delete o.pack;
735
- }
736
- }
737
- // now move (to the original ask vs the collision version which might differ) and repack things
738
- if (needToMove) {
739
- node._dirty = true;
740
- utils_1.Utils.copyPos(node, nn);
741
- }
742
- if (o.pack) {
743
- this._packNodes()
744
- ._notify();
745
- }
746
- return !utils_1.Utils.samePos(node, prevPos); // pack might have moved things back
747
- };
748
- GridStackEngine.prototype.getRow = function () {
749
- return this.nodes.reduce(function (row, n) { return Math.max(row, n.y + n.h); }, 0);
750
- };
751
- GridStackEngine.prototype.beginUpdate = function (node) {
752
- if (!node._updating) {
753
- node._updating = true;
754
- delete node._skipDown;
755
- if (!this.batchMode)
756
- this.saveInitial();
757
- }
758
- return this;
759
- };
760
- GridStackEngine.prototype.endUpdate = function () {
761
- var n = this.nodes.find(function (n) { return n._updating; });
762
- if (n) {
763
- delete n._updating;
764
- delete n._skipDown;
765
- }
766
- return this;
767
- };
768
- /** saves a copy of the largest column layout (eg 12 even when rendering oneColumnMode) so we don't loose orig layout,
769
- * returning a list of widgets for serialization */
770
- GridStackEngine.prototype.save = function (saveElement) {
771
- var _a;
772
- if (saveElement === void 0) { saveElement = true; }
773
- // use the highest layout for any saved info so we can have full detail on reload #1849
774
- var len = (_a = this._layouts) === null || _a === void 0 ? void 0 : _a.length;
775
- var layout = len && this.column !== (len - 1) ? this._layouts[len - 1] : null;
776
- var list = [];
777
- this.sortNodes();
778
- this.nodes.forEach(function (n) {
779
- var wl = layout === null || layout === void 0 ? void 0 : layout.find(function (l) { return l._id === n._id; });
780
- var w = __assign({}, n);
781
- // use layout info instead if set
782
- if (wl) {
783
- w.x = wl.x;
784
- w.y = wl.y;
785
- w.w = wl.w;
786
- }
787
- utils_1.Utils.removeInternalForSave(w, !saveElement);
788
- list.push(w);
789
- });
790
- return list;
791
- };
792
- /** @internal called whenever a node is added or moved - updates the cached layouts */
793
- GridStackEngine.prototype.layoutsNodesChange = function (nodes) {
794
- var _this = this;
795
- if (!this._layouts || this._inColumnResize)
796
- return this;
797
- // remove smaller layouts - we will re-generate those on the fly... larger ones need to update
798
- this._layouts.forEach(function (layout, column) {
799
- if (!layout || column === _this.column)
800
- return _this;
801
- if (column < _this.column) {
802
- _this._layouts[column] = undefined;
803
- }
804
- else {
805
- // we save the original x,y,w (h isn't cached) to see what actually changed to propagate better.
806
- // NOTE: we don't need to check against out of bound scaling/moving as that will be done when using those cache values. #1785
807
- var ratio_1 = column / _this.column;
808
- nodes.forEach(function (node) {
809
- if (!node._orig)
810
- return; // didn't change (newly added ?)
811
- var n = layout.find(function (l) { return l._id === node._id; });
812
- if (!n)
813
- return; // no cache for new nodes. Will use those values.
814
- // Y changed, push down same amount
815
- // TODO: detect doing item 'swaps' will help instead of move (especially in 1 column mode)
816
- if (node.y !== node._orig.y) {
817
- n.y += (node.y - node._orig.y);
818
- }
819
- // X changed, scale from new position
820
- if (node.x !== node._orig.x) {
821
- n.x = Math.round(node.x * ratio_1);
822
- }
823
- // width changed, scale from new width
824
- if (node.w !== node._orig.w) {
825
- n.w = Math.round(node.w * ratio_1);
826
- }
827
- // ...height always carries over from cache
828
- });
829
- }
830
- });
831
- return this;
832
- };
833
- /**
834
- * @internal Called to scale the widget width & position up/down based on the column change.
835
- * Note we store previous layouts (especially original ones) to make it possible to go
836
- * from say 12 -> 1 -> 12 and get back to where we were.
837
- *
838
- * @param prevColumn previous number of columns
839
- * @param column new column number
840
- * @param nodes different sorted list (ex: DOM order) instead of current list
841
- * @param layout specify the type of re-layout that will happen (position, size, etc...).
842
- * Note: items will never be outside of the current column boundaries. default (moveScale). Ignored for 1 column
843
- */
844
- GridStackEngine.prototype.updateNodeWidths = function (prevColumn, column, nodes, layout) {
845
- var _this = this;
846
- var _a;
847
- if (layout === void 0) { layout = 'moveScale'; }
848
- if (!this.nodes.length || !column || prevColumn === column)
849
- return this;
850
- // cache the current layout in case they want to go back (like 12 -> 1 -> 12) as it requires original data
851
- this.cacheLayout(this.nodes, prevColumn);
852
- this.batchUpdate(); // do this EARLY as it will call saveInitial() so we can detect where we started for _dirty and collision
853
- var newNodes = [];
854
- // if we're going to 1 column and using DOM order rather than default sorting, then generate that layout
855
- var domOrder = false;
856
- if (column === 1 && (nodes === null || nodes === void 0 ? void 0 : nodes.length)) {
857
- domOrder = true;
858
- var top_1 = 0;
859
- nodes.forEach(function (n) {
860
- n.x = 0;
861
- n.w = 1;
862
- n.y = Math.max(n.y, top_1);
863
- top_1 = n.y + n.h;
864
- });
865
- newNodes = nodes;
866
- nodes = [];
867
- }
868
- else {
869
- nodes = utils_1.Utils.sort(this.nodes, -1, prevColumn); // current column reverse sorting so we can insert last to front (limit collision)
870
- }
871
- // see if we have cached previous layout IFF we are going up in size (restore) otherwise always
872
- // generate next size down from where we are (looks more natural as you gradually size down).
873
- var cacheNodes = [];
874
- if (column > prevColumn) {
875
- cacheNodes = this._layouts[column] || [];
876
- // ...if not, start with the largest layout (if not already there) as down-scaling is more accurate
877
- // by pretending we came from that larger column by assigning those values as starting point
878
- var lastIndex = this._layouts.length - 1;
879
- if (!cacheNodes.length && prevColumn !== lastIndex && ((_a = this._layouts[lastIndex]) === null || _a === void 0 ? void 0 : _a.length)) {
880
- prevColumn = lastIndex;
881
- this._layouts[lastIndex].forEach(function (cacheNode) {
882
- var n = nodes.find(function (n) { return n._id === cacheNode._id; });
883
- if (n) {
884
- // still current, use cache info positions
885
- n.x = cacheNode.x;
886
- n.y = cacheNode.y;
887
- n.w = cacheNode.w;
888
- }
889
- });
890
- }
891
- }
892
- // if we found cache re-use those nodes that are still current
893
- cacheNodes.forEach(function (cacheNode) {
894
- var j = nodes.findIndex(function (n) { return n._id === cacheNode._id; });
895
- if (j !== -1) {
896
- // still current, use cache info positions
897
- if (cacheNode.autoPosition || isNaN(cacheNode.x) || isNaN(cacheNode.y)) {
898
- _this.findEmptyPosition(cacheNode, newNodes);
899
- }
900
- if (!cacheNode.autoPosition) {
901
- nodes[j].x = cacheNode.x;
902
- nodes[j].y = cacheNode.y;
903
- nodes[j].w = cacheNode.w;
904
- newNodes.push(nodes[j]);
905
- }
906
- nodes.splice(j, 1);
907
- }
908
- });
909
- // ...and add any extra non-cached ones
910
- if (nodes.length) {
911
- if (typeof layout === 'function') {
912
- layout(column, prevColumn, newNodes, nodes);
913
- }
914
- else if (!domOrder) {
915
- var ratio_2 = column / prevColumn;
916
- var move_1 = (layout === 'move' || layout === 'moveScale');
917
- var scale_1 = (layout === 'scale' || layout === 'moveScale');
918
- nodes.forEach(function (node) {
919
- // NOTE: x + w could be outside of the grid, but addNode() below will handle that
920
- node.x = (column === 1 ? 0 : (move_1 ? Math.round(node.x * ratio_2) : Math.min(node.x, column - 1)));
921
- node.w = ((column === 1 || prevColumn === 1) ? 1 :
922
- scale_1 ? (Math.round(node.w * ratio_2) || 1) : (Math.min(node.w, column)));
923
- newNodes.push(node);
924
- });
925
- nodes = [];
926
- }
927
- }
928
- // finally re-layout them in reverse order (to get correct placement)
929
- if (!domOrder)
930
- newNodes = utils_1.Utils.sort(newNodes, -1, column);
931
- this._inColumnResize = true; // prevent cache update
932
- this.nodes = []; // pretend we have no nodes to start with (add() will use same structures) to simplify layout
933
- newNodes.forEach(function (node) {
934
- _this.addNode(node, false); // 'false' for add event trigger
935
- delete node._orig; // make sure the commit doesn't try to restore things back to original
936
- });
937
- this.batchUpdate(false);
938
- delete this._inColumnResize;
939
- return this;
940
- };
941
- /**
942
- * call to cache the given layout internally to the given location so we can restore back when column changes size
943
- * @param nodes list of nodes
944
- * @param column corresponding column index to save it under
945
- * @param clear if true, will force other caches to be removed (default false)
946
- */
947
- GridStackEngine.prototype.cacheLayout = function (nodes, column, clear) {
948
- if (clear === void 0) { clear = false; }
949
- var copy = [];
950
- nodes.forEach(function (n, i) {
951
- n._id = n._id || GridStackEngine._idSeq++; // make sure we have an id in case this is new layout, else re-use id already set
952
- 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
953
- });
954
- this._layouts = clear ? [] : this._layouts || []; // use array to find larger quick
955
- this._layouts[column] = copy;
956
- return this;
957
- };
958
- /**
959
- * call to cache the given node layout internally to the given location so we can restore back when column changes size
960
- * @param node single node to cache
961
- * @param column corresponding column index to save it under
962
- */
963
- GridStackEngine.prototype.cacheOneLayout = function (n, column) {
964
- n._id = n._id || GridStackEngine._idSeq++;
965
- var l = { x: n.x, y: n.y, w: n.w, _id: n._id };
966
- if (n.autoPosition) {
967
- delete l.x;
968
- delete l.y;
969
- l.autoPosition = true;
970
- }
971
- this._layouts = this._layouts || [];
972
- this._layouts[column] = this._layouts[column] || [];
973
- var index = this.findCacheLayout(n, column);
974
- if (index === -1)
975
- this._layouts[column].push(l);
976
- else
977
- this._layouts[column][index] = l;
978
- return this;
979
- };
980
- GridStackEngine.prototype.findCacheLayout = function (n, column) {
981
- var _a, _b, _c;
982
- 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;
983
- };
984
- /** called to remove all internal values but the _id */
985
- GridStackEngine.prototype.cleanupNode = function (node) {
986
- for (var prop in node) {
987
- if (prop[0] === '_' && prop !== '_id')
988
- delete node[prop];
989
- }
990
- return this;
991
- };
992
- /** @internal unique global internal _id counter NOT starting at 0 */
993
- GridStackEngine._idSeq = 1;
994
- return GridStackEngine;
995
- }());
996
- exports.GridStackEngine = GridStackEngine;
997
- //# sourceMappingURL=gridstack-engine.js.map