gridstack 4.2.6 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/README.md +29 -19
  2. package/dist/es5/gridstack-dd.d.ts +35 -0
  3. package/dist/es5/gridstack-dd.js +683 -0
  4. package/dist/es5/gridstack-dd.js.map +1 -0
  5. package/dist/es5/gridstack-ddi.d.ts +17 -0
  6. package/dist/es5/gridstack-ddi.js +31 -0
  7. package/dist/es5/gridstack-ddi.js.map +1 -0
  8. package/dist/es5/gridstack-engine.d.ts +93 -0
  9. package/dist/es5/gridstack-engine.js +955 -0
  10. package/dist/es5/gridstack-engine.js.map +1 -0
  11. package/dist/es5/gridstack-h5.d.ts +10 -0
  12. package/dist/es5/gridstack-h5.js +3 -0
  13. package/dist/es5/gridstack-h5.js.LICENSE.txt +7 -0
  14. package/dist/es5/gridstack-h5.js.map +1 -0
  15. package/dist/es5/gridstack-jq.d.ts +10 -0
  16. package/dist/es5/gridstack-jq.js +3 -0
  17. package/dist/es5/gridstack-jq.js.LICENSE.txt +99 -0
  18. package/dist/es5/gridstack-jq.js.map +1 -0
  19. package/dist/{gridstack-poly.js → es5/gridstack-poly.js} +152 -1
  20. package/dist/es5/gridstack-static.d.ts +9 -0
  21. package/dist/es5/gridstack-static.js +3 -0
  22. package/dist/es5/gridstack-static.js.LICENSE.txt +7 -0
  23. package/dist/es5/gridstack-static.js.map +1 -0
  24. package/dist/es5/gridstack.d.ts +346 -0
  25. package/dist/es5/gridstack.js +1482 -0
  26. package/dist/es5/gridstack.js.map +1 -0
  27. package/dist/es5/h5/dd-base-impl.d.ts +20 -0
  28. package/dist/es5/h5/dd-base-impl.js +43 -0
  29. package/dist/es5/h5/dd-base-impl.js.map +1 -0
  30. package/dist/es5/h5/dd-draggable.d.ts +30 -0
  31. package/dist/es5/h5/dd-draggable.js +319 -0
  32. package/dist/es5/h5/dd-draggable.js.map +1 -0
  33. package/dist/es5/h5/dd-droppable.d.ts +23 -0
  34. package/dist/es5/h5/dd-droppable.js +182 -0
  35. package/dist/es5/h5/dd-droppable.js.map +1 -0
  36. package/dist/es5/h5/dd-element.d.ts +27 -0
  37. package/dist/es5/h5/dd-element.js +96 -0
  38. package/dist/es5/h5/dd-element.js.map +1 -0
  39. package/dist/es5/h5/dd-manager.d.ts +8 -0
  40. package/dist/es5/h5/dd-manager.js +14 -0
  41. package/dist/es5/h5/dd-manager.js.map +1 -0
  42. package/dist/es5/h5/dd-resizable-handle.d.ts +14 -0
  43. package/dist/es5/h5/dd-resizable-handle.js +83 -0
  44. package/dist/es5/h5/dd-resizable-handle.js.map +1 -0
  45. package/dist/es5/h5/dd-resizable.d.ts +29 -0
  46. package/dist/es5/h5/dd-resizable.js +302 -0
  47. package/dist/es5/h5/dd-resizable.js.map +1 -0
  48. package/dist/es5/h5/dd-utils.d.ts +17 -0
  49. package/dist/es5/h5/dd-utils.js +99 -0
  50. package/dist/es5/h5/dd-utils.js.map +1 -0
  51. package/dist/es5/h5/gridstack-dd-native.d.ts +26 -0
  52. package/dist/es5/h5/gridstack-dd-native.js +175 -0
  53. package/dist/es5/h5/gridstack-dd-native.js.map +1 -0
  54. package/dist/es5/jq/gridstack-dd-jqueryui.d.ts +22 -0
  55. package/dist/es5/jq/gridstack-dd-jqueryui.js +162 -0
  56. package/dist/es5/jq/gridstack-dd-jqueryui.js.map +1 -0
  57. package/dist/es5/types.d.ts +258 -0
  58. package/dist/es5/types.js +7 -0
  59. package/dist/es5/types.js.map +1 -0
  60. package/dist/es5/utils.d.ts +73 -0
  61. package/dist/es5/utils.js +444 -0
  62. package/dist/es5/utils.js.map +1 -0
  63. package/dist/gridstack-dd.d.ts +1 -1
  64. package/dist/gridstack-dd.js +16 -8
  65. package/dist/gridstack-dd.js.map +1 -1
  66. package/dist/gridstack-ddi.d.ts +1 -1
  67. package/dist/gridstack-ddi.js +1 -1
  68. package/dist/gridstack-ddi.js.map +1 -1
  69. package/dist/gridstack-engine.d.ts +3 -2
  70. package/dist/gridstack-engine.js +101 -61
  71. package/dist/gridstack-engine.js.map +1 -1
  72. package/dist/gridstack-extra.css +800 -530
  73. package/dist/gridstack-extra.min.css +1 -1
  74. package/dist/gridstack-h5.d.ts +1 -1
  75. package/dist/gridstack-h5.js +1 -1
  76. package/dist/gridstack-h5.js.LICENSE.txt +1 -1
  77. package/dist/gridstack-h5.js.map +1 -1
  78. package/dist/gridstack-jq.d.ts +1 -1
  79. package/dist/gridstack-jq.js +1 -1
  80. package/dist/gridstack-jq.js.LICENSE.txt +1 -1
  81. package/dist/gridstack-jq.js.map +1 -1
  82. package/dist/gridstack-static.d.ts +1 -1
  83. package/dist/gridstack-static.js +1 -1
  84. package/dist/gridstack-static.js.LICENSE.txt +1 -1
  85. package/dist/gridstack-static.js.map +1 -1
  86. package/dist/gridstack.css +305 -228
  87. package/dist/gridstack.d.ts +1 -1
  88. package/dist/gridstack.js +19 -12
  89. package/dist/gridstack.js.map +1 -1
  90. package/dist/gridstack.min.css +1 -1
  91. package/dist/h5/dd-base-impl.d.ts +1 -1
  92. package/dist/h5/dd-base-impl.js +1 -1
  93. package/dist/h5/dd-base-impl.js.map +1 -1
  94. package/dist/h5/dd-draggable.d.ts +1 -1
  95. package/dist/h5/dd-draggable.js +1 -1
  96. package/dist/h5/dd-draggable.js.map +1 -1
  97. package/dist/h5/dd-droppable.d.ts +1 -1
  98. package/dist/h5/dd-droppable.js +1 -1
  99. package/dist/h5/dd-droppable.js.map +1 -1
  100. package/dist/h5/dd-element.d.ts +1 -1
  101. package/dist/h5/dd-element.js +1 -1
  102. package/dist/h5/dd-element.js.map +1 -1
  103. package/dist/h5/dd-manager.d.ts +1 -1
  104. package/dist/h5/dd-manager.js +1 -1
  105. package/dist/h5/dd-manager.js.map +1 -1
  106. package/dist/h5/dd-resizable-handle.d.ts +1 -1
  107. package/dist/h5/dd-resizable-handle.js +1 -1
  108. package/dist/h5/dd-resizable-handle.js.map +1 -1
  109. package/dist/h5/dd-resizable.d.ts +1 -1
  110. package/dist/h5/dd-resizable.js +2 -1
  111. package/dist/h5/dd-resizable.js.map +1 -1
  112. package/dist/h5/dd-utils.d.ts +1 -1
  113. package/dist/h5/dd-utils.js +1 -1
  114. package/dist/h5/dd-utils.js.map +1 -1
  115. package/dist/h5/gridstack-dd-native.d.ts +1 -1
  116. package/dist/h5/gridstack-dd-native.js +1 -1
  117. package/dist/h5/gridstack-dd-native.js.map +1 -1
  118. package/dist/jq/gridstack-dd-jqueryui.js.map +1 -1
  119. package/dist/src/gridstack-extra.scss +7 -5
  120. package/dist/src/gridstack.scss +9 -7
  121. package/dist/types.d.ts +5 -2
  122. package/dist/types.js +1 -1
  123. package/dist/types.js.map +1 -1
  124. package/dist/utils.d.ts +3 -1
  125. package/dist/utils.js +7 -5
  126. package/dist/utils.js.map +1 -1
  127. package/doc/CHANGES.md +26 -0
  128. package/package.json +6 -5
@@ -0,0 +1,1482 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
16
+ }) : (function(o, m, k, k2) {
17
+ if (k2 === undefined) k2 = k;
18
+ o[k2] = m[k];
19
+ }));
20
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
21
+ for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
22
+ };
23
+ var __spreadArrays = (this && this.__spreadArrays) || function () {
24
+ for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
25
+ for (var r = Array(s), k = 0, i = 0; i < il; i++)
26
+ for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
27
+ r[k] = a[j];
28
+ return r;
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.GridStack = void 0;
32
+ /*!
33
+ * GridStack 4.4.0
34
+ * https://gridstackjs.com/
35
+ *
36
+ * Copyright (c) 2021 Alain Dumesny
37
+ * see root license https://github.com/gridstack/gridstack.js/tree/master/LICENSE
38
+ */
39
+ var gridstack_engine_1 = require("./gridstack-engine");
40
+ var utils_1 = require("./utils");
41
+ var gridstack_ddi_1 = require("./gridstack-ddi");
42
+ // export all dependent file as well to make it easier for users to just import the main file
43
+ __exportStar(require("./types"), exports);
44
+ __exportStar(require("./utils"), exports);
45
+ __exportStar(require("./gridstack-engine"), exports);
46
+ __exportStar(require("./gridstack-ddi"), exports);
47
+ // default values for grid options - used during init and when saving out
48
+ var GridDefaults = {
49
+ column: 12,
50
+ minRow: 0,
51
+ maxRow: 0,
52
+ itemClass: 'grid-stack-item',
53
+ placeholderClass: 'grid-stack-placeholder',
54
+ placeholderText: '',
55
+ handle: '.grid-stack-item-content',
56
+ handleClass: null,
57
+ styleInHead: false,
58
+ cellHeight: 'auto',
59
+ cellHeightThrottle: 100,
60
+ margin: 10,
61
+ auto: true,
62
+ minWidth: 768,
63
+ float: false,
64
+ staticGrid: false,
65
+ animate: true,
66
+ alwaysShowResizeHandle: false,
67
+ resizable: {
68
+ autoHide: true,
69
+ handles: 'se'
70
+ },
71
+ draggable: {
72
+ handle: '.grid-stack-item-content',
73
+ scroll: false,
74
+ appendTo: 'body'
75
+ },
76
+ disableDrag: false,
77
+ disableResize: false,
78
+ rtl: 'auto',
79
+ removable: false,
80
+ removableOptions: {
81
+ accept: '.grid-stack-item'
82
+ },
83
+ marginUnit: 'px',
84
+ cellHeightUnit: 'px',
85
+ disableOneColumnMode: false,
86
+ oneColumnModeDomSort: false
87
+ };
88
+ /**
89
+ * Main gridstack class - you will need to call `GridStack.init()` first to initialize your grid.
90
+ * Note: your grid elements MUST have the following classes for the CSS layout to work:
91
+ * @example
92
+ * <div class="grid-stack">
93
+ * <div class="grid-stack-item">
94
+ * <div class="grid-stack-item-content">Item 1</div>
95
+ * </div>
96
+ * </div>
97
+ */
98
+ var GridStack = /** @class */ (function () {
99
+ /**
100
+ * Construct a grid item from the given element and options
101
+ * @param el
102
+ * @param opts
103
+ */
104
+ function GridStack(el, opts) {
105
+ var _this = this;
106
+ if (opts === void 0) { opts = {}; }
107
+ /** @internal */
108
+ this._gsEventHandler = {};
109
+ /** @internal extra row added when dragging at the bottom of the grid */
110
+ this._extraDragRow = 0;
111
+ this.el = el; // exposed HTML element to the user
112
+ opts = opts || {}; // handles null/undefined/0
113
+ // if row property exists, replace minRow and maxRow instead
114
+ if (opts.row) {
115
+ opts.minRow = opts.maxRow = opts.row;
116
+ delete opts.row;
117
+ }
118
+ var rowAttr = utils_1.Utils.toNumber(el.getAttribute('gs-row'));
119
+ // elements attributes override any passed options (like CSS style) - merge the two together
120
+ var defaults = __assign(__assign({}, utils_1.Utils.cloneDeep(GridDefaults)), { column: utils_1.Utils.toNumber(el.getAttribute('gs-column')) || 12, minRow: rowAttr ? rowAttr : utils_1.Utils.toNumber(el.getAttribute('gs-min-row')) || 0, maxRow: rowAttr ? rowAttr : utils_1.Utils.toNumber(el.getAttribute('gs-max-row')) || 0, staticGrid: utils_1.Utils.toBool(el.getAttribute('gs-static')) || false, _styleSheetClass: 'grid-stack-instance-' + (Math.random() * 10000).toFixed(0), alwaysShowResizeHandle: opts.alwaysShowResizeHandle || false, resizable: {
121
+ autoHide: !(opts.alwaysShowResizeHandle || false),
122
+ handles: 'se'
123
+ }, draggable: {
124
+ handle: (opts.handleClass ? '.' + opts.handleClass : (opts.handle ? opts.handle : '')) || '.grid-stack-item-content',
125
+ scroll: false,
126
+ appendTo: 'body'
127
+ }, removableOptions: {
128
+ accept: '.' + (opts.itemClass || 'grid-stack-item')
129
+ } });
130
+ if (el.getAttribute('gs-animate')) { // default to true, but if set to false use that instead
131
+ defaults.animate = utils_1.Utils.toBool(el.getAttribute('gs-animate'));
132
+ }
133
+ this.opts = utils_1.Utils.defaults(opts, defaults);
134
+ opts = null; // make sure we use this.opts instead
135
+ this.initMargin(); // part of settings defaults...
136
+ // Now check if we're loading into 1 column mode FIRST so we don't do un-necessary work (like cellHeight = width / 12 then go 1 column)
137
+ if (this.opts.column !== 1 && !this.opts.disableOneColumnMode && this._widthOrContainer() <= this.opts.minWidth) {
138
+ this._prevColumn = this.opts.column;
139
+ this.opts.column = 1;
140
+ }
141
+ if (this.opts.rtl === 'auto') {
142
+ this.opts.rtl = (el.style.direction === 'rtl');
143
+ }
144
+ if (this.opts.rtl) {
145
+ this.el.classList.add('grid-stack-rtl');
146
+ }
147
+ // check if we're been nested, and if so update our style and keep pointer around (used during save)
148
+ var parentGridItemEl = utils_1.Utils.closestByClass(this.el, GridDefaults.itemClass);
149
+ if (parentGridItemEl && parentGridItemEl.gridstackNode) {
150
+ this.opts._isNested = parentGridItemEl.gridstackNode;
151
+ this.opts._isNested.subGrid = this;
152
+ this.el.classList.add('grid-stack-nested');
153
+ }
154
+ this._isAutoCellHeight = (this.opts.cellHeight === 'auto');
155
+ if (this._isAutoCellHeight || this.opts.cellHeight === 'initial') {
156
+ // make the cell content square initially (will use resize/column event to keep it square)
157
+ this.cellHeight(undefined, false);
158
+ }
159
+ else {
160
+ // append unit if any are set
161
+ if (typeof this.opts.cellHeight == 'number' && this.opts.cellHeightUnit && this.opts.cellHeightUnit !== GridDefaults.cellHeightUnit) {
162
+ this.opts.cellHeight = this.opts.cellHeight + this.opts.cellHeightUnit;
163
+ delete this.opts.cellHeightUnit;
164
+ }
165
+ this.cellHeight(this.opts.cellHeight, false);
166
+ }
167
+ this.el.classList.add(this.opts._styleSheetClass);
168
+ this._setStaticClass();
169
+ this.engine = new gridstack_engine_1.GridStackEngine({
170
+ column: this.opts.column,
171
+ float: this.opts.float,
172
+ maxRow: this.opts.maxRow,
173
+ onChange: function (cbNodes) {
174
+ var maxH = 0;
175
+ _this.engine.nodes.forEach(function (n) { maxH = Math.max(maxH, n.y + n.h); });
176
+ cbNodes.forEach(function (n) {
177
+ var el = n.el;
178
+ if (!el)
179
+ return;
180
+ if (n._removeDOM) {
181
+ if (el)
182
+ el.remove();
183
+ delete n._removeDOM;
184
+ }
185
+ else {
186
+ _this._writePosAttr(el, n);
187
+ }
188
+ });
189
+ _this._updateStyles(false, maxH); // false = don't recreate, just append if need be
190
+ }
191
+ });
192
+ if (this.opts.auto) {
193
+ this.batchUpdate(); // prevent in between re-layout #1535 TODO: this only set float=true, need to prevent collision check...
194
+ var elements_1 = [];
195
+ this.getGridItems().forEach(function (el) {
196
+ var x = parseInt(el.getAttribute('gs-x'));
197
+ var y = parseInt(el.getAttribute('gs-y'));
198
+ elements_1.push({
199
+ el: el,
200
+ // if x,y are missing (autoPosition) add them to end of list - but keep their respective DOM order
201
+ i: (Number.isNaN(x) ? 1000 : x) + (Number.isNaN(y) ? 1000 : y) * _this.opts.column
202
+ });
203
+ });
204
+ elements_1.sort(function (a, b) { return a.i - b.i; }).forEach(function (e) { return _this._prepareElement(e.el); });
205
+ this.commit();
206
+ }
207
+ this.setAnimation(this.opts.animate);
208
+ this._updateStyles();
209
+ if (this.opts.column != 12) {
210
+ this.el.classList.add('grid-stack-' + this.opts.column);
211
+ }
212
+ // legacy support to appear 'per grid` options when really global.
213
+ if (this.opts.dragIn)
214
+ GridStack.setupDragIn(this.opts.dragIn, this.opts.dragInOptions);
215
+ delete this.opts.dragIn;
216
+ delete this.opts.dragInOptions;
217
+ this._setupRemoveDrop();
218
+ this._setupAcceptWidget();
219
+ this._updateWindowResizeEvent();
220
+ }
221
+ /**
222
+ * initializing the HTML element, or selector string, into a grid will return the grid. Calling it again will
223
+ * simply return the existing instance (ignore any passed options). There is also an initAll() version that support
224
+ * multiple grids initialization at once. Or you can use addGrid() to create the entire grid from JSON.
225
+ * @param options grid options (optional)
226
+ * @param elOrString element or CSS selector (first one used) to convert to a grid (default to '.grid-stack' class selector)
227
+ *
228
+ * @example
229
+ * let grid = GridStack.init();
230
+ *
231
+ * Note: the HTMLElement (of type GridHTMLElement) will store a `gridstack: GridStack` value that can be retrieve later
232
+ * let grid = document.querySelector('.grid-stack').gridstack;
233
+ */
234
+ GridStack.init = function (options, elOrString) {
235
+ if (options === void 0) { options = {}; }
236
+ if (elOrString === void 0) { elOrString = '.grid-stack'; }
237
+ var el = GridStack.getGridElement(elOrString);
238
+ if (!el) {
239
+ if (typeof elOrString === 'string') {
240
+ console.error('GridStack.initAll() no grid was found with selector "' + elOrString + '" - element missing or wrong selector ?' +
241
+ '\nNote: ".grid-stack" is required for proper CSS styling and drag/drop, and is the default selector.');
242
+ }
243
+ else {
244
+ console.error('GridStack.init() no grid element was passed.');
245
+ }
246
+ return null;
247
+ }
248
+ if (!el.gridstack) {
249
+ el.gridstack = new GridStack(el, utils_1.Utils.cloneDeep(options));
250
+ }
251
+ return el.gridstack;
252
+ };
253
+ /**
254
+ * Will initialize a list of elements (given a selector) and return an array of grids.
255
+ * @param options grid options (optional)
256
+ * @param selector elements selector to convert to grids (default to '.grid-stack' class selector)
257
+ *
258
+ * @example
259
+ * let grids = GridStack.initAll();
260
+ * grids.forEach(...)
261
+ */
262
+ GridStack.initAll = function (options, selector) {
263
+ if (options === void 0) { options = {}; }
264
+ if (selector === void 0) { selector = '.grid-stack'; }
265
+ var grids = [];
266
+ GridStack.getGridElements(selector).forEach(function (el) {
267
+ if (!el.gridstack) {
268
+ el.gridstack = new GridStack(el, utils_1.Utils.cloneDeep(options));
269
+ delete options.dragIn;
270
+ delete options.dragInOptions; // only need to be done once (really a static global thing, not per grid)
271
+ }
272
+ grids.push(el.gridstack);
273
+ });
274
+ if (grids.length === 0) {
275
+ console.error('GridStack.initAll() no grid was found with selector "' + selector + '" - element missing or wrong selector ?' +
276
+ '\nNote: ".grid-stack" is required for proper CSS styling and drag/drop, and is the default selector.');
277
+ }
278
+ return grids;
279
+ };
280
+ /**
281
+ * call to create a grid with the given options, including loading any children from JSON structure. This will call GridStack.init(), then
282
+ * grid.load() on any passed children (recursively). Great alternative to calling init() if you want entire grid to come from
283
+ * JSON serialized data, including options.
284
+ * @param parent HTML element parent to the grid
285
+ * @param opt grids options used to initialize the grid, and list of children
286
+ */
287
+ GridStack.addGrid = function (parent, opt) {
288
+ if (opt === void 0) { opt = {}; }
289
+ if (!parent)
290
+ return null;
291
+ // create the grid element, but check if the passed 'parent' already has grid styling and should be used instead
292
+ var el = parent;
293
+ if (!parent.classList.contains('grid-stack')) {
294
+ var doc = document.implementation.createHTMLDocument(''); // IE needs a param
295
+ doc.body.innerHTML = "<div class=\"grid-stack " + (opt.class || '') + "\"></div>";
296
+ el = doc.body.children[0];
297
+ parent.appendChild(el);
298
+ }
299
+ // create grid class and load any children
300
+ var grid = GridStack.init(opt, el);
301
+ if (grid.opts.children) {
302
+ var children = grid.opts.children;
303
+ delete grid.opts.children;
304
+ grid.load(children);
305
+ }
306
+ return grid;
307
+ };
308
+ Object.defineProperty(GridStack.prototype, "placeholder", {
309
+ /** @internal create placeholder DIV as needed */
310
+ get: function () {
311
+ if (!this._placeholder) {
312
+ var placeholderChild = document.createElement('div'); // child so padding match item-content
313
+ placeholderChild.className = 'placeholder-content';
314
+ if (this.opts.placeholderText) {
315
+ placeholderChild.innerHTML = this.opts.placeholderText;
316
+ }
317
+ this._placeholder = document.createElement('div');
318
+ this._placeholder.classList.add(this.opts.placeholderClass, GridDefaults.itemClass, this.opts.itemClass);
319
+ this.placeholder.appendChild(placeholderChild);
320
+ }
321
+ return this._placeholder;
322
+ },
323
+ enumerable: false,
324
+ configurable: true
325
+ });
326
+ /**
327
+ * add a new widget and returns it.
328
+ *
329
+ * Widget will be always placed even if result height is more than actual grid height.
330
+ * You need to use `willItFit()` before calling addWidget for additional check.
331
+ * See also `makeWidget()`.
332
+ *
333
+ * @example
334
+ * let grid = GridStack.init();
335
+ * grid.addWidget({w: 3, content: 'hello'});
336
+ * grid.addWidget('<div class="grid-stack-item"><div class="grid-stack-item-content">hello</div></div>', {w: 3});
337
+ *
338
+ * @param el GridStackWidget (which can have content string as well), html element, or string definition to add
339
+ * @param options widget position/size options (optional, and ignore if first param is already option) - see GridStackWidget
340
+ */
341
+ GridStack.prototype.addWidget = function (els, options) {
342
+ // support legacy call for now ?
343
+ if (arguments.length > 2) {
344
+ console.warn('gridstack.ts: `addWidget(el, x, y, width...)` is deprecated. Use `addWidget({x, y, w, content, ...})`. It will be removed soon');
345
+ // eslint-disable-next-line prefer-rest-params
346
+ var a = arguments, i = 1, opt = { x: a[i++], y: a[i++], w: a[i++], h: a[i++], autoPosition: a[i++],
347
+ minW: a[i++], maxW: a[i++], minH: a[i++], maxH: a[i++], id: a[i++] };
348
+ return this.addWidget(els, opt);
349
+ }
350
+ function isGridStackWidget(w) {
351
+ return w.x !== undefined || w.y !== undefined || w.w !== undefined || w.h !== undefined || w.content !== undefined ? true : false;
352
+ }
353
+ var el;
354
+ if (typeof els === 'string') {
355
+ var doc = document.implementation.createHTMLDocument(''); // IE needs a param
356
+ doc.body.innerHTML = els;
357
+ el = doc.body.children[0];
358
+ }
359
+ else if (arguments.length === 0 || arguments.length === 1 && isGridStackWidget(els)) {
360
+ var content = els ? els.content || '' : '';
361
+ options = els;
362
+ var doc = document.implementation.createHTMLDocument(''); // IE needs a param
363
+ doc.body.innerHTML = "<div class=\"grid-stack-item " + (this.opts.itemClass || '') + "\"><div class=\"grid-stack-item-content\">" + content + "</div></div>";
364
+ el = doc.body.children[0];
365
+ }
366
+ else {
367
+ el = els;
368
+ }
369
+ // Tempting to initialize the passed in opt with default and valid values, but this break knockout demos
370
+ // as the actual value are filled in when _prepareElement() calls el.getAttribute('gs-xyz) before adding the node.
371
+ // So make sure we load any DOM attributes that are not specified in passed in options (which override)
372
+ var domAttr = this._readAttr(el);
373
+ options = utils_1.Utils.cloneDeep(options) || {}; // make a copy before we modify in case caller re-uses it
374
+ utils_1.Utils.defaults(options, domAttr);
375
+ var node = this.engine.prepareNode(options);
376
+ this._writeAttr(el, options);
377
+ if (this._insertNotAppend) {
378
+ this.el.prepend(el);
379
+ }
380
+ else {
381
+ this.el.appendChild(el);
382
+ }
383
+ // similar to makeWidget() that doesn't read attr again and worse re-create a new node and loose any _id
384
+ this._prepareElement(el, true, options);
385
+ this._updateContainerHeight();
386
+ // check if nested grid definition is present
387
+ if (node.subGrid && !node.subGrid.el) { // see if there is a sub-grid to create too
388
+ var content = node.el.querySelector('.grid-stack-item-content');
389
+ node.subGrid = GridStack.addGrid(content, node.subGrid);
390
+ }
391
+ this._triggerAddEvent();
392
+ this._triggerChangeEvent();
393
+ return el;
394
+ };
395
+ /**
396
+ /**
397
+ * saves the current layout returning a list of widgets for serialization which might include any nested grids.
398
+ * @param saveContent if true (default) the latest html inside .grid-stack-content will be saved to GridStackWidget.content field, else it will
399
+ * be removed.
400
+ * @param saveGridOpt if true (default false), save the grid options itself, so you can call the new GridStack.addGrid()
401
+ * to recreate everything from scratch. GridStackOptions.children would then contain the widget list instead.
402
+ * @returns list of widgets or full grid option, including .children list of widgets
403
+ */
404
+ GridStack.prototype.save = function (saveContent, saveGridOpt) {
405
+ if (saveContent === void 0) { saveContent = true; }
406
+ if (saveGridOpt === void 0) { saveGridOpt = false; }
407
+ // return copied nodes we can modify at will...
408
+ var list = this.engine.save(saveContent);
409
+ // check for HTML content and nested grids
410
+ list.forEach(function (n) {
411
+ if (saveContent && n.el && !n.subGrid) { // sub-grid are saved differently, not plain content
412
+ var sub = n.el.querySelector('.grid-stack-item-content');
413
+ n.content = sub ? sub.innerHTML : undefined;
414
+ if (!n.content)
415
+ delete n.content;
416
+ }
417
+ else {
418
+ if (!saveContent) {
419
+ delete n.content;
420
+ }
421
+ // check for nested grid
422
+ if (n.subGrid) {
423
+ n.subGrid = n.subGrid.save(saveContent, true);
424
+ }
425
+ }
426
+ delete n.el;
427
+ });
428
+ // check if save entire grid options (needed for recursive) + children...
429
+ if (saveGridOpt) {
430
+ var o = utils_1.Utils.cloneDeep(this.opts);
431
+ // delete default values that will be recreated on launch
432
+ if (o.marginBottom === o.marginTop && o.marginRight === o.marginLeft && o.marginTop === o.marginRight) {
433
+ o.margin = o.marginTop;
434
+ delete o.marginTop;
435
+ delete o.marginRight;
436
+ delete o.marginBottom;
437
+ delete o.marginLeft;
438
+ }
439
+ if (o.rtl === (this.el.style.direction === 'rtl')) {
440
+ o.rtl = 'auto';
441
+ }
442
+ if (this._isAutoCellHeight) {
443
+ o.cellHeight = 'auto';
444
+ }
445
+ utils_1.Utils.removeInternalAndSame(o, GridDefaults);
446
+ o.children = list;
447
+ return o;
448
+ }
449
+ return list;
450
+ };
451
+ /**
452
+ * load the widgets from a list. This will call update() on each (matching by id) or add/remove widgets that are not there.
453
+ *
454
+ * @param layout list of widgets definition to update/create
455
+ * @param addAndRemove boolean (default true) or callback method can be passed to control if and how missing widgets can be added/removed, giving
456
+ * the user control of insertion.
457
+ *
458
+ * @example
459
+ * see http://gridstackjs.com/demo/serialization.html
460
+ **/
461
+ GridStack.prototype.load = function (layout, addAndRemove) {
462
+ var _this = this;
463
+ if (addAndRemove === void 0) { addAndRemove = true; }
464
+ var items = GridStack.Utils.sort(__spreadArrays(layout), -1, this._prevColumn || this.opts.column); // make copy before we mod/sort
465
+ this._insertNotAppend = true; // since create in reverse order...
466
+ // if we're loading a layout into 1 column (_prevColumn is set only when going to 1) and items don't fit, make sure to save
467
+ // the original wanted layout so we can scale back up correctly #1471
468
+ if (this._prevColumn && this._prevColumn !== this.opts.column && items.some(function (n) { return (n.x + n.w) > _this.opts.column; })) {
469
+ this._ignoreLayoutsNodeChange = true; // skip layout update
470
+ this.engine.cacheLayout(items, this._prevColumn, true);
471
+ }
472
+ var removed = [];
473
+ this.batchUpdate();
474
+ // see if any items are missing from new layout and need to be removed first
475
+ if (addAndRemove) {
476
+ var copyNodes = __spreadArrays(this.engine.nodes); // don't loop through array you modify
477
+ copyNodes.forEach(function (n) {
478
+ var item = items.find(function (w) { return n.id === w.id; });
479
+ if (!item) {
480
+ if (typeof (addAndRemove) === 'function') {
481
+ addAndRemove(_this, n, false);
482
+ }
483
+ else {
484
+ removed.push(n); // batch keep track
485
+ _this.removeWidget(n.el, true, false);
486
+ }
487
+ }
488
+ });
489
+ }
490
+ // now add/update the widgets
491
+ items.forEach(function (w) {
492
+ var item = (w.id || w.id === 0) ? _this.engine.nodes.find(function (n) { return n.id === w.id; }) : undefined;
493
+ if (item) {
494
+ _this.update(item.el, w);
495
+ if (w.subGrid && w.subGrid.children) { // update any sub grid as well
496
+ var sub = item.el.querySelector('.grid-stack');
497
+ if (sub && sub.gridstack) {
498
+ sub.gridstack.load(w.subGrid.children); // TODO: support updating grid options ?
499
+ _this._insertNotAppend = true; // got reset by above call
500
+ }
501
+ }
502
+ }
503
+ else if (addAndRemove) {
504
+ if (typeof (addAndRemove) === 'function') {
505
+ w = addAndRemove(_this, w, true).gridstackNode;
506
+ }
507
+ else {
508
+ w = _this.addWidget(w).gridstackNode;
509
+ }
510
+ }
511
+ });
512
+ this.engine.removedNodes = removed;
513
+ this.commit();
514
+ // after commit, clear that flag
515
+ delete this._ignoreLayoutsNodeChange;
516
+ delete this._insertNotAppend;
517
+ return this;
518
+ };
519
+ /**
520
+ * Initializes batch updates. You will see no changes until `commit()` method is called.
521
+ */
522
+ GridStack.prototype.batchUpdate = function () {
523
+ this.engine.batchUpdate();
524
+ return this;
525
+ };
526
+ /**
527
+ * Gets current cell height.
528
+ */
529
+ GridStack.prototype.getCellHeight = function (forcePixel) {
530
+ if (forcePixel === void 0) { forcePixel = false; }
531
+ if (this.opts.cellHeight && this.opts.cellHeight !== 'auto' &&
532
+ (!forcePixel || !this.opts.cellHeightUnit || this.opts.cellHeightUnit === 'px')) {
533
+ return this.opts.cellHeight;
534
+ }
535
+ // else do entire grid and # of rows
536
+ // or get first cell height ?
537
+ // let el = this.el.querySelector('.' + this.opts.itemClass) as HTMLElement;
538
+ // let height = Utils.toNumber(el.getAttribute('gs-h'));
539
+ // return Math.round(el.offsetHeight / height);
540
+ return Math.round(this.el.getBoundingClientRect().height) / parseInt(this.el.getAttribute('gs-current-row'));
541
+ };
542
+ /**
543
+ * Update current cell height - see `GridStackOptions.cellHeight` for format.
544
+ * This method rebuilds an internal CSS style sheet.
545
+ * Note: You can expect performance issues if call this method too often.
546
+ *
547
+ * @param val the cell height. If not passed (undefined), cells content will be made square (match width minus margin),
548
+ * if pass 0 the CSS will be generated by the application instead.
549
+ * @param update (Optional) if false, styles will not be updated
550
+ *
551
+ * @example
552
+ * grid.cellHeight(100); // same as 100px
553
+ * grid.cellHeight('70px');
554
+ * grid.cellHeight(grid.cellWidth() * 1.2);
555
+ */
556
+ GridStack.prototype.cellHeight = function (val, update) {
557
+ if (update === void 0) { update = true; }
558
+ // if not called internally, check if we're changing mode
559
+ if (update && val !== undefined) {
560
+ if (this._isAutoCellHeight !== (val === 'auto')) {
561
+ this._isAutoCellHeight = (val === 'auto');
562
+ this._updateWindowResizeEvent();
563
+ }
564
+ }
565
+ if (val === 'initial' || val === 'auto') {
566
+ val = undefined;
567
+ }
568
+ // make item content be square
569
+ if (val === undefined) {
570
+ var marginDiff = -this.opts.marginRight - this.opts.marginLeft
571
+ + this.opts.marginTop + this.opts.marginBottom;
572
+ val = this.cellWidth() + marginDiff;
573
+ }
574
+ var data = utils_1.Utils.parseHeight(val);
575
+ if (this.opts.cellHeightUnit === data.unit && this.opts.cellHeight === data.h) {
576
+ return this;
577
+ }
578
+ this.opts.cellHeightUnit = data.unit;
579
+ this.opts.cellHeight = data.h;
580
+ if (update) {
581
+ this._updateStyles(true, this.getRow()); // true = force re-create, for that # of rows
582
+ }
583
+ return this;
584
+ };
585
+ /** Gets current cell width. */
586
+ GridStack.prototype.cellWidth = function () {
587
+ return this._widthOrContainer() / this.opts.column;
588
+ };
589
+ /** return our expected width (or parent) for 1 column check */
590
+ GridStack.prototype._widthOrContainer = function () {
591
+ // use `offsetWidth` or `clientWidth` (no scrollbar) ?
592
+ // https://stackoverflow.com/questions/21064101/understanding-offsetwidth-clientwidth-scrollwidth-and-height-respectively
593
+ return (this.el.clientWidth || this.el.parentElement.clientWidth || window.innerWidth);
594
+ };
595
+ /**
596
+ * Finishes batch updates. Updates DOM nodes. You must call it after batchUpdate.
597
+ */
598
+ GridStack.prototype.commit = function () {
599
+ this.engine.commit();
600
+ this._triggerRemoveEvent();
601
+ this._triggerAddEvent();
602
+ this._triggerChangeEvent();
603
+ return this;
604
+ };
605
+ /** re-layout grid items to reclaim any empty space */
606
+ GridStack.prototype.compact = function () {
607
+ this.engine.compact();
608
+ this._triggerChangeEvent();
609
+ return this;
610
+ };
611
+ /**
612
+ * set the number of columns in the grid. Will update existing widgets to conform to new number of columns,
613
+ * as well as cache the original layout so you can revert back to previous positions without loss.
614
+ * Requires `gridstack-extra.css` or `gridstack-extra.min.css` for [2-11],
615
+ * else you will need to generate correct CSS (see https://github.com/gridstack/gridstack.js#change-grid-columns)
616
+ * @param column - Integer > 0 (default 12).
617
+ * @param layout specify the type of re-layout that will happen (position, size, etc...).
618
+ * Note: items will never be outside of the current column boundaries. default (moveScale). Ignored for 1 column
619
+ */
620
+ GridStack.prototype.column = function (column, layout) {
621
+ if (layout === void 0) { layout = 'moveScale'; }
622
+ if (column < 1 || this.opts.column === column)
623
+ return this;
624
+ var oldColumn = this.opts.column;
625
+ // if we go into 1 column mode (which happens if we're sized less than minW unless disableOneColumnMode is on)
626
+ // then remember the original columns so we can restore.
627
+ if (column === 1) {
628
+ this._prevColumn = oldColumn;
629
+ }
630
+ else {
631
+ delete this._prevColumn;
632
+ }
633
+ this.el.classList.remove('grid-stack-' + oldColumn);
634
+ this.el.classList.add('grid-stack-' + column);
635
+ this.opts.column = this.engine.column = column;
636
+ // update the items now - see if the dom order nodes should be passed instead (else default to current list)
637
+ var domNodes;
638
+ if (column === 1 && this.opts.oneColumnModeDomSort) {
639
+ domNodes = [];
640
+ this.getGridItems().forEach(function (el) {
641
+ if (el.gridstackNode) {
642
+ domNodes.push(el.gridstackNode);
643
+ }
644
+ });
645
+ if (!domNodes.length) {
646
+ domNodes = undefined;
647
+ }
648
+ }
649
+ this.engine.updateNodeWidths(oldColumn, column, domNodes, layout);
650
+ if (this._isAutoCellHeight)
651
+ this.cellHeight();
652
+ // and trigger our event last...
653
+ this._ignoreLayoutsNodeChange = true; // skip layout update
654
+ this._triggerChangeEvent();
655
+ delete this._ignoreLayoutsNodeChange;
656
+ return this;
657
+ };
658
+ /**
659
+ * get the number of columns in the grid (default 12)
660
+ */
661
+ GridStack.prototype.getColumn = function () {
662
+ return this.opts.column;
663
+ };
664
+ /** returns an array of grid HTML elements (no placeholder) - used to iterate through our children in DOM order */
665
+ GridStack.prototype.getGridItems = function () {
666
+ var _this = this;
667
+ return Array.from(this.el.children)
668
+ .filter(function (el) { return el.matches('.' + _this.opts.itemClass) && !el.matches('.' + _this.opts.placeholderClass); });
669
+ };
670
+ /**
671
+ * Destroys a grid instance. DO NOT CALL any methods or access any vars after this as it will free up members.
672
+ * @param removeDOM if `false` grid and items HTML elements will not be removed from the DOM (Optional. Default `true`).
673
+ */
674
+ GridStack.prototype.destroy = function (removeDOM) {
675
+ if (removeDOM === void 0) { removeDOM = true; }
676
+ if (!this.el)
677
+ return; // prevent multiple calls
678
+ this._updateWindowResizeEvent(true);
679
+ this.setStatic(true, false); // permanently removes DD but don't set CSS class (we're going away)
680
+ this.setAnimation(false);
681
+ if (!removeDOM) {
682
+ this.removeAll(removeDOM);
683
+ this.el.classList.remove(this.opts._styleSheetClass);
684
+ }
685
+ else {
686
+ this.el.parentNode.removeChild(this.el);
687
+ }
688
+ this._removeStylesheet();
689
+ this.el.removeAttribute('gs-current-row');
690
+ delete this.opts._isNested;
691
+ delete this.opts;
692
+ delete this._placeholder;
693
+ delete this.engine;
694
+ delete this.el.gridstack; // remove circular dependency that would prevent a freeing
695
+ delete this.el;
696
+ return this;
697
+ };
698
+ /**
699
+ * enable/disable floating widgets (default: `false`) See [example](http://gridstackjs.com/demo/float.html)
700
+ */
701
+ GridStack.prototype.float = function (val) {
702
+ this.engine.float = val;
703
+ this._triggerChangeEvent();
704
+ return this;
705
+ };
706
+ /**
707
+ * get the current float mode
708
+ */
709
+ GridStack.prototype.getFloat = function () {
710
+ return this.engine.float;
711
+ };
712
+ /**
713
+ * Get the position of the cell under a pixel on screen.
714
+ * @param position the position of the pixel to resolve in
715
+ * absolute coordinates, as an object with top and left properties
716
+ * @param useDocRelative if true, value will be based on document position vs parent position (Optional. Default false).
717
+ * Useful when grid is within `position: relative` element
718
+ *
719
+ * Returns an object with properties `x` and `y` i.e. the column and row in the grid.
720
+ */
721
+ GridStack.prototype.getCellFromPixel = function (position, useDocRelative) {
722
+ if (useDocRelative === void 0) { useDocRelative = false; }
723
+ var box = this.el.getBoundingClientRect();
724
+ // console.log(`getBoundingClientRect left: ${box.left} top: ${box.top} w: ${box.w} h: ${box.h}`)
725
+ var containerPos;
726
+ if (useDocRelative) {
727
+ containerPos = { top: box.top + document.documentElement.scrollTop, left: box.left };
728
+ // console.log(`getCellFromPixel scrollTop: ${document.documentElement.scrollTop}`)
729
+ }
730
+ else {
731
+ containerPos = { top: this.el.offsetTop, left: this.el.offsetLeft };
732
+ // console.log(`getCellFromPixel offsetTop: ${containerPos.left} offsetLeft: ${containerPos.top}`)
733
+ }
734
+ var relativeLeft = position.left - containerPos.left;
735
+ var relativeTop = position.top - containerPos.top;
736
+ var columnWidth = (box.width / this.opts.column);
737
+ var rowHeight = (box.height / parseInt(this.el.getAttribute('gs-current-row')));
738
+ return { x: Math.floor(relativeLeft / columnWidth), y: Math.floor(relativeTop / rowHeight) };
739
+ };
740
+ /** returns the current number of rows, which will be at least `minRow` if set */
741
+ GridStack.prototype.getRow = function () {
742
+ return Math.max(this.engine.getRow(), this.opts.minRow);
743
+ };
744
+ /**
745
+ * Checks if specified area is empty.
746
+ * @param x the position x.
747
+ * @param y the position y.
748
+ * @param w the width of to check
749
+ * @param h the height of to check
750
+ */
751
+ GridStack.prototype.isAreaEmpty = function (x, y, w, h) {
752
+ return this.engine.isAreaEmpty(x, y, w, h);
753
+ };
754
+ /**
755
+ * If you add elements to your grid by hand, you have to tell gridstack afterwards to make them widgets.
756
+ * If you want gridstack to add the elements for you, use `addWidget()` instead.
757
+ * Makes the given element a widget and returns it.
758
+ * @param els widget or single selector to convert.
759
+ *
760
+ * @example
761
+ * let grid = GridStack.init();
762
+ * grid.el.appendChild('<div id="gsi-1" gs-w="3"></div>');
763
+ * grid.makeWidget('#gsi-1');
764
+ */
765
+ GridStack.prototype.makeWidget = function (els) {
766
+ var el = GridStack.getElement(els);
767
+ this._prepareElement(el, true);
768
+ this._updateContainerHeight();
769
+ this._triggerAddEvent();
770
+ this._triggerChangeEvent();
771
+ return el;
772
+ };
773
+ /**
774
+ * Event handler that extracts our CustomEvent data out automatically for receiving custom
775
+ * notifications (see doc for supported events)
776
+ * @param name of the event (see possible values) or list of names space separated
777
+ * @param callback function called with event and optional second/third param
778
+ * (see README documentation for each signature).
779
+ *
780
+ * @example
781
+ * grid.on('added', function(e, items) { log('added ', items)} );
782
+ * or
783
+ * grid.on('added removed change', function(e, items) { log(e.type, items)} );
784
+ *
785
+ * Note: in some cases it is the same as calling native handler and parsing the event.
786
+ * grid.el.addEventListener('added', function(event) { log('added ', event.detail)} );
787
+ *
788
+ */
789
+ GridStack.prototype.on = function (name, callback) {
790
+ var _this = this;
791
+ // check for array of names being passed instead
792
+ if (name.indexOf(' ') !== -1) {
793
+ var names = name.split(' ');
794
+ names.forEach(function (name) { return _this.on(name, callback); });
795
+ return this;
796
+ }
797
+ if (name === 'change' || name === 'added' || name === 'removed' || name === 'enable' || name === 'disable') {
798
+ // native CustomEvent handlers - cash the generic handlers so we can easily remove
799
+ var noData = (name === 'enable' || name === 'disable');
800
+ if (noData) {
801
+ this._gsEventHandler[name] = function (event) { return callback(event); };
802
+ }
803
+ else {
804
+ this._gsEventHandler[name] = function (event) { return callback(event, event.detail); };
805
+ }
806
+ this.el.addEventListener(name, this._gsEventHandler[name]);
807
+ }
808
+ else if (name === 'drag' || name === 'dragstart' || name === 'dragstop' || name === 'resizestart' || name === 'resize' || name === 'resizestop' || name === 'dropped') {
809
+ // drag&drop stop events NEED to be call them AFTER we update node attributes so handle them ourself.
810
+ // do same for start event to make it easier...
811
+ this._gsEventHandler[name] = callback;
812
+ }
813
+ else {
814
+ console.log('GridStack.on(' + name + ') event not supported, but you can still use $(".grid-stack").on(...) while jquery-ui is still used internally.');
815
+ }
816
+ return this;
817
+ };
818
+ /**
819
+ * unsubscribe from the 'on' event below
820
+ * @param name of the event (see possible values)
821
+ */
822
+ GridStack.prototype.off = function (name) {
823
+ var _this = this;
824
+ // check for array of names being passed instead
825
+ if (name.indexOf(' ') !== -1) {
826
+ var names = name.split(' ');
827
+ names.forEach(function (name) { return _this.off(name); });
828
+ return this;
829
+ }
830
+ if (name === 'change' || name === 'added' || name === 'removed' || name === 'enable' || name === 'disable') {
831
+ // remove native CustomEvent handlers
832
+ if (this._gsEventHandler[name]) {
833
+ this.el.removeEventListener(name, this._gsEventHandler[name]);
834
+ }
835
+ }
836
+ delete this._gsEventHandler[name];
837
+ return this;
838
+ };
839
+ /**
840
+ * Removes widget from the grid.
841
+ * @param el widget or selector to modify
842
+ * @param removeDOM if `false` DOM element won't be removed from the tree (Default? true).
843
+ * @param triggerEvent if `false` (quiet mode) element will not be added to removed list and no 'removed' callbacks will be called (Default? true).
844
+ */
845
+ GridStack.prototype.removeWidget = function (els, removeDOM, triggerEvent) {
846
+ var _this = this;
847
+ if (removeDOM === void 0) { removeDOM = true; }
848
+ if (triggerEvent === void 0) { triggerEvent = true; }
849
+ GridStack.getElements(els).forEach(function (el) {
850
+ if (el.parentElement !== _this.el)
851
+ return; // not our child!
852
+ var node = el.gridstackNode;
853
+ // For Meteor support: https://github.com/gridstack/gridstack.js/pull/272
854
+ if (!node) {
855
+ node = _this.engine.nodes.find(function (n) { return el === n.el; });
856
+ }
857
+ if (!node)
858
+ return;
859
+ // remove our DOM data (circular link) and drag&drop permanently
860
+ delete el.gridstackNode;
861
+ gridstack_ddi_1.GridStackDDI.get().remove(el);
862
+ _this.engine.removeNode(node, removeDOM, triggerEvent);
863
+ if (removeDOM && el.parentElement) {
864
+ el.remove(); // in batch mode engine.removeNode doesn't call back to remove DOM
865
+ }
866
+ });
867
+ if (triggerEvent) {
868
+ this._triggerRemoveEvent();
869
+ this._triggerChangeEvent();
870
+ }
871
+ return this;
872
+ };
873
+ /**
874
+ * Removes all widgets from the grid.
875
+ * @param removeDOM if `false` DOM elements won't be removed from the tree (Default? `true`).
876
+ */
877
+ GridStack.prototype.removeAll = function (removeDOM) {
878
+ if (removeDOM === void 0) { removeDOM = true; }
879
+ // always remove our DOM data (circular link) before list gets emptied and drag&drop permanently
880
+ this.engine.nodes.forEach(function (n) {
881
+ delete n.el.gridstackNode;
882
+ gridstack_ddi_1.GridStackDDI.get().remove(n.el);
883
+ });
884
+ this.engine.removeAll(removeDOM);
885
+ this._triggerRemoveEvent();
886
+ return this;
887
+ };
888
+ /**
889
+ * Toggle the grid animation state. Toggles the `grid-stack-animate` class.
890
+ * @param doAnimate if true the grid will animate.
891
+ */
892
+ GridStack.prototype.setAnimation = function (doAnimate) {
893
+ if (doAnimate) {
894
+ this.el.classList.add('grid-stack-animate');
895
+ }
896
+ else {
897
+ this.el.classList.remove('grid-stack-animate');
898
+ }
899
+ return this;
900
+ };
901
+ /**
902
+ * Toggle the grid static state, which permanently removes/add Drag&Drop support, unlike disable()/enable() that just turns it off/on.
903
+ * Also toggle the grid-stack-static class.
904
+ * @param val if true the grid become static.
905
+ */
906
+ GridStack.prototype.setStatic = function (val, updateClass) {
907
+ var _this = this;
908
+ if (updateClass === void 0) { updateClass = true; }
909
+ if (this.opts.staticGrid === val)
910
+ return this;
911
+ this.opts.staticGrid = val;
912
+ this._setupRemoveDrop();
913
+ this._setupAcceptWidget();
914
+ this.engine.nodes.forEach(function (n) { return _this._prepareDragDropByNode(n); }); // either delete or init Drag&drop
915
+ if (updateClass) {
916
+ this._setStaticClass();
917
+ }
918
+ return this;
919
+ };
920
+ /**
921
+ * Updates widget position/size and other info. Note: if you need to call this on all nodes, use load() instead which will update what changed.
922
+ * @param els widget or selector of objects to modify (note: setting the same x,y for multiple items will be indeterministic and likely unwanted)
923
+ * @param opt new widget options (x,y,w,h, etc..). Only those set will be updated.
924
+ */
925
+ GridStack.prototype.update = function (els, opt) {
926
+ var _this = this;
927
+ // support legacy call for now ?
928
+ if (arguments.length > 2) {
929
+ console.warn('gridstack.ts: `update(el, x, y, w, h)` is deprecated. Use `update(el, {x, w, content, ...})`. It will be removed soon');
930
+ // eslint-disable-next-line prefer-rest-params
931
+ var a = arguments, i = 1;
932
+ opt = { x: a[i++], y: a[i++], w: a[i++], h: a[i++] };
933
+ return this.update(els, opt);
934
+ }
935
+ GridStack.getElements(els).forEach(function (el) {
936
+ if (!el || !el.gridstackNode)
937
+ return;
938
+ var n = el.gridstackNode;
939
+ var w = utils_1.Utils.cloneDeep(opt); // make a copy we can modify in case they re-use it or multiple items
940
+ delete w.autoPosition;
941
+ // move/resize widget if anything changed
942
+ var keys = ['x', 'y', 'w', 'h'];
943
+ var m;
944
+ if (keys.some(function (k) { return w[k] !== undefined && w[k] !== n[k]; })) {
945
+ m = {};
946
+ keys.forEach(function (k) {
947
+ m[k] = (w[k] !== undefined) ? w[k] : n[k];
948
+ delete w[k];
949
+ });
950
+ }
951
+ // for a move as well IFF there is any min/max fields set
952
+ if (!m && (w.minW || w.minH || w.maxW || w.maxH)) {
953
+ m = {}; // will use node position but validate values
954
+ }
955
+ // check for content changing
956
+ if (w.content) {
957
+ var sub = el.querySelector('.grid-stack-item-content');
958
+ if (sub && sub.innerHTML !== w.content) {
959
+ sub.innerHTML = w.content;
960
+ }
961
+ delete w.content;
962
+ }
963
+ // any remaining fields are assigned, but check for dragging changes, resize constrain
964
+ var changed = false;
965
+ var ddChanged = false;
966
+ for (var key in w) {
967
+ if (key[0] !== '_' && n[key] !== w[key]) {
968
+ n[key] = w[key];
969
+ changed = true;
970
+ ddChanged = ddChanged || (!_this.opts.staticGrid && (key === 'noResize' || key === 'noMove' || key === 'locked'));
971
+ }
972
+ }
973
+ // finally move the widget
974
+ if (m) {
975
+ _this.engine.cleanNodes()
976
+ .beginUpdate(n)
977
+ .moveNode(n, m);
978
+ _this._updateContainerHeight();
979
+ _this._triggerChangeEvent();
980
+ _this.engine.endUpdate();
981
+ }
982
+ if (changed) { // move will only update x,y,w,h so update the rest too
983
+ _this._writeAttr(el, n);
984
+ }
985
+ if (ddChanged) {
986
+ _this._prepareDragDropByNode(n);
987
+ }
988
+ });
989
+ return this;
990
+ };
991
+ /**
992
+ * Updates the margins which will set all 4 sides at once - see `GridStackOptions.margin` for format options (CSS string format of 1,2,4 values or single number).
993
+ * @param value margin value
994
+ */
995
+ GridStack.prototype.margin = function (value) {
996
+ var isMultiValue = (typeof value === 'string' && value.split(' ').length > 1);
997
+ // check if we can skip re-creating our CSS file... won't check if multi values (too much hassle)
998
+ if (!isMultiValue) {
999
+ var data = utils_1.Utils.parseHeight(value);
1000
+ if (this.opts.marginUnit === data.unit && this.opts.margin === data.h)
1001
+ return;
1002
+ }
1003
+ // re-use existing margin handling
1004
+ this.opts.margin = value;
1005
+ this.opts.marginTop = this.opts.marginBottom = this.opts.marginLeft = this.opts.marginRight = undefined;
1006
+ this.initMargin();
1007
+ this._updateStyles(true); // true = force re-create
1008
+ return this;
1009
+ };
1010
+ /** returns current margin number value (undefined if 4 sides don't match) */
1011
+ GridStack.prototype.getMargin = function () { return this.opts.margin; };
1012
+ /**
1013
+ * Returns true if the height of the grid will be less than the vertical
1014
+ * constraint. Always returns true if grid doesn't have height constraint.
1015
+ * @param node contains x,y,w,h,auto-position options
1016
+ *
1017
+ * @example
1018
+ * if (grid.willItFit(newWidget)) {
1019
+ * grid.addWidget(newWidget);
1020
+ * } else {
1021
+ * alert('Not enough free space to place the widget');
1022
+ * }
1023
+ */
1024
+ GridStack.prototype.willItFit = function (node) {
1025
+ // support legacy call for now
1026
+ if (arguments.length > 1) {
1027
+ console.warn('gridstack.ts: `willItFit(x,y,w,h,autoPosition)` is deprecated. Use `willItFit({x, y,...})`. It will be removed soon');
1028
+ // eslint-disable-next-line prefer-rest-params
1029
+ var a = arguments, i = 0, w = { x: a[i++], y: a[i++], w: a[i++], h: a[i++], autoPosition: a[i++] };
1030
+ return this.willItFit(w);
1031
+ }
1032
+ return this.engine.willItFit(node);
1033
+ };
1034
+ /** @internal */
1035
+ GridStack.prototype._triggerChangeEvent = function () {
1036
+ if (this.engine.batchMode)
1037
+ return this;
1038
+ var elements = this.engine.getDirtyNodes(true); // verify they really changed
1039
+ if (elements && elements.length) {
1040
+ if (!this._ignoreLayoutsNodeChange) {
1041
+ this.engine.layoutsNodesChange(elements);
1042
+ }
1043
+ this._triggerEvent('change', elements);
1044
+ }
1045
+ this.engine.saveInitial(); // we called, now reset initial values & dirty flags
1046
+ return this;
1047
+ };
1048
+ /** @internal */
1049
+ GridStack.prototype._triggerAddEvent = function () {
1050
+ if (this.engine.batchMode)
1051
+ return this;
1052
+ if (this.engine.addedNodes && this.engine.addedNodes.length > 0) {
1053
+ if (!this._ignoreLayoutsNodeChange) {
1054
+ this.engine.layoutsNodesChange(this.engine.addedNodes);
1055
+ }
1056
+ // prevent added nodes from also triggering 'change' event (which is called next)
1057
+ this.engine.addedNodes.forEach(function (n) { delete n._dirty; });
1058
+ this._triggerEvent('added', this.engine.addedNodes);
1059
+ this.engine.addedNodes = [];
1060
+ }
1061
+ return this;
1062
+ };
1063
+ /** @internal */
1064
+ GridStack.prototype._triggerRemoveEvent = function () {
1065
+ if (this.engine.batchMode)
1066
+ return this;
1067
+ if (this.engine.removedNodes && this.engine.removedNodes.length > 0) {
1068
+ this._triggerEvent('removed', this.engine.removedNodes);
1069
+ this.engine.removedNodes = [];
1070
+ }
1071
+ return this;
1072
+ };
1073
+ /** @internal */
1074
+ GridStack.prototype._triggerEvent = function (name, data) {
1075
+ var event = data ? new CustomEvent(name, { bubbles: false, detail: data }) : new Event(name);
1076
+ this.el.dispatchEvent(event);
1077
+ return this;
1078
+ };
1079
+ /** @internal called to delete the current dynamic style sheet used for our layout */
1080
+ GridStack.prototype._removeStylesheet = function () {
1081
+ if (this._styles) {
1082
+ utils_1.Utils.removeStylesheet(this._styles._id);
1083
+ delete this._styles;
1084
+ }
1085
+ return this;
1086
+ };
1087
+ /** @internal updated/create the CSS styles for row based layout and initial margin setting */
1088
+ GridStack.prototype._updateStyles = function (forceUpdate, maxH) {
1089
+ if (forceUpdate === void 0) { forceUpdate = false; }
1090
+ // call to delete existing one if we change cellHeight / margin
1091
+ if (forceUpdate) {
1092
+ this._removeStylesheet();
1093
+ }
1094
+ this._updateContainerHeight();
1095
+ // if user is telling us they will handle the CSS themselves by setting heights to 0. Do we need this opts really ??
1096
+ if (this.opts.cellHeight === 0) {
1097
+ return this;
1098
+ }
1099
+ var cellHeight = this.opts.cellHeight;
1100
+ var cellHeightUnit = this.opts.cellHeightUnit;
1101
+ var prefix = "." + this.opts._styleSheetClass + " > ." + this.opts.itemClass;
1102
+ // create one as needed
1103
+ if (!this._styles) {
1104
+ var id = 'gridstack-style-' + (Math.random() * 100000).toFixed();
1105
+ // insert style to parent (instead of 'head' by default) to support WebComponent
1106
+ var styleLocation = this.opts.styleInHead ? undefined : this.el.parentNode;
1107
+ this._styles = utils_1.Utils.createStylesheet(id, styleLocation);
1108
+ if (!this._styles)
1109
+ return this;
1110
+ this._styles._id = id;
1111
+ this._styles._max = 0;
1112
+ // these are done once only
1113
+ utils_1.Utils.addCSSRule(this._styles, prefix, "min-height: " + cellHeight + cellHeightUnit);
1114
+ // content margins
1115
+ var top_1 = this.opts.marginTop + this.opts.marginUnit;
1116
+ var bottom = this.opts.marginBottom + this.opts.marginUnit;
1117
+ var right = this.opts.marginRight + this.opts.marginUnit;
1118
+ var left = this.opts.marginLeft + this.opts.marginUnit;
1119
+ var content = prefix + " > .grid-stack-item-content";
1120
+ var placeholder = "." + this.opts._styleSheetClass + " > .grid-stack-placeholder > .placeholder-content";
1121
+ utils_1.Utils.addCSSRule(this._styles, content, "top: " + top_1 + "; right: " + right + "; bottom: " + bottom + "; left: " + left + ";");
1122
+ utils_1.Utils.addCSSRule(this._styles, placeholder, "top: " + top_1 + "; right: " + right + "; bottom: " + bottom + "; left: " + left + ";");
1123
+ // resize handles offset (to match margin)
1124
+ utils_1.Utils.addCSSRule(this._styles, prefix + " > .ui-resizable-ne", "right: " + right);
1125
+ utils_1.Utils.addCSSRule(this._styles, prefix + " > .ui-resizable-e", "right: " + right);
1126
+ utils_1.Utils.addCSSRule(this._styles, prefix + " > .ui-resizable-se", "right: " + right + "; bottom: " + bottom);
1127
+ utils_1.Utils.addCSSRule(this._styles, prefix + " > .ui-resizable-nw", "left: " + left);
1128
+ utils_1.Utils.addCSSRule(this._styles, prefix + " > .ui-resizable-w", "left: " + left);
1129
+ utils_1.Utils.addCSSRule(this._styles, prefix + " > .ui-resizable-sw", "left: " + left + "; bottom: " + bottom);
1130
+ }
1131
+ // now update the height specific fields
1132
+ maxH = maxH || this._styles._max;
1133
+ if (maxH > this._styles._max) {
1134
+ var getHeight = function (rows) { return (cellHeight * rows) + cellHeightUnit; };
1135
+ for (var i = this._styles._max + 1; i <= maxH; i++) { // start at 1
1136
+ var h = getHeight(i);
1137
+ utils_1.Utils.addCSSRule(this._styles, prefix + "[gs-y=\"" + (i - 1) + "\"]", "top: " + getHeight(i - 1)); // start at 0
1138
+ utils_1.Utils.addCSSRule(this._styles, prefix + "[gs-h=\"" + i + "\"]", "height: " + h);
1139
+ utils_1.Utils.addCSSRule(this._styles, prefix + "[gs-min-h=\"" + i + "\"]", "min-height: " + h);
1140
+ utils_1.Utils.addCSSRule(this._styles, prefix + "[gs-max-h=\"" + i + "\"]", "max-height: " + h);
1141
+ }
1142
+ this._styles._max = maxH;
1143
+ }
1144
+ return this;
1145
+ };
1146
+ /** @internal */
1147
+ GridStack.prototype._updateContainerHeight = function () {
1148
+ if (!this.engine || this.engine.batchMode)
1149
+ return this;
1150
+ var row = this.getRow() + this._extraDragRow; // checks for minRow already
1151
+ // check for css min height
1152
+ var cssMinHeight = parseInt(getComputedStyle(this.el)['min-height']);
1153
+ if (cssMinHeight > 0) {
1154
+ var minRow = Math.round(cssMinHeight / this.getCellHeight(true));
1155
+ if (row < minRow) {
1156
+ row = minRow;
1157
+ }
1158
+ }
1159
+ this.el.setAttribute('gs-current-row', String(row));
1160
+ if (row === 0) {
1161
+ this.el.style.removeProperty('height');
1162
+ return this;
1163
+ }
1164
+ var cellHeight = this.opts.cellHeight;
1165
+ var unit = this.opts.cellHeightUnit;
1166
+ if (!cellHeight)
1167
+ return this;
1168
+ this.el.style.height = row * cellHeight + unit;
1169
+ return this;
1170
+ };
1171
+ /** @internal */
1172
+ GridStack.prototype._prepareElement = function (el, triggerAddEvent, node) {
1173
+ if (triggerAddEvent === void 0) { triggerAddEvent = false; }
1174
+ if (!node) {
1175
+ el.classList.add(this.opts.itemClass);
1176
+ node = this._readAttr(el);
1177
+ }
1178
+ el.gridstackNode = node;
1179
+ node.el = el;
1180
+ node.grid = this;
1181
+ var copy = __assign({}, node);
1182
+ node = this.engine.addNode(node, triggerAddEvent);
1183
+ // write node attr back in case there was collision or we have to fix bad values during addNode()
1184
+ if (!utils_1.Utils.same(node, copy)) {
1185
+ this._writeAttr(el, node);
1186
+ }
1187
+ this._prepareDragDropByNode(node);
1188
+ return this;
1189
+ };
1190
+ /** @internal call to write position x,y,w,h attributes back to element */
1191
+ GridStack.prototype._writePosAttr = function (el, n) {
1192
+ if (n.x !== undefined && n.x !== null) {
1193
+ el.setAttribute('gs-x', String(n.x));
1194
+ }
1195
+ if (n.y !== undefined && n.y !== null) {
1196
+ el.setAttribute('gs-y', String(n.y));
1197
+ }
1198
+ if (n.w) {
1199
+ el.setAttribute('gs-w', String(n.w));
1200
+ }
1201
+ if (n.h) {
1202
+ el.setAttribute('gs-h', String(n.h));
1203
+ }
1204
+ return this;
1205
+ };
1206
+ /** @internal call to write any default attributes back to element */
1207
+ GridStack.prototype._writeAttr = function (el, node) {
1208
+ if (!node)
1209
+ return this;
1210
+ this._writePosAttr(el, node);
1211
+ var attrs /*: GridStackWidget but strings */ = {
1212
+ autoPosition: 'gs-auto-position',
1213
+ minW: 'gs-min-w',
1214
+ minH: 'gs-min-h',
1215
+ maxW: 'gs-max-w',
1216
+ maxH: 'gs-max-h',
1217
+ noResize: 'gs-no-resize',
1218
+ noMove: 'gs-no-move',
1219
+ locked: 'gs-locked',
1220
+ id: 'gs-id',
1221
+ resizeHandles: 'gs-resize-handles'
1222
+ };
1223
+ for (var key in attrs) {
1224
+ if (node[key]) { // 0 is valid for x,y only but done above already and not in list anyway
1225
+ el.setAttribute(attrs[key], String(node[key]));
1226
+ }
1227
+ else {
1228
+ el.removeAttribute(attrs[key]);
1229
+ }
1230
+ }
1231
+ return this;
1232
+ };
1233
+ /** @internal call to read any default attributes from element */
1234
+ GridStack.prototype._readAttr = function (el) {
1235
+ var node = {};
1236
+ node.x = utils_1.Utils.toNumber(el.getAttribute('gs-x'));
1237
+ node.y = utils_1.Utils.toNumber(el.getAttribute('gs-y'));
1238
+ node.w = utils_1.Utils.toNumber(el.getAttribute('gs-w'));
1239
+ node.h = utils_1.Utils.toNumber(el.getAttribute('gs-h'));
1240
+ node.maxW = utils_1.Utils.toNumber(el.getAttribute('gs-max-w'));
1241
+ node.minW = utils_1.Utils.toNumber(el.getAttribute('gs-min-w'));
1242
+ node.maxH = utils_1.Utils.toNumber(el.getAttribute('gs-max-h'));
1243
+ node.minH = utils_1.Utils.toNumber(el.getAttribute('gs-min-h'));
1244
+ node.autoPosition = utils_1.Utils.toBool(el.getAttribute('gs-auto-position'));
1245
+ node.noResize = utils_1.Utils.toBool(el.getAttribute('gs-no-resize'));
1246
+ node.noMove = utils_1.Utils.toBool(el.getAttribute('gs-no-move'));
1247
+ node.locked = utils_1.Utils.toBool(el.getAttribute('gs-locked'));
1248
+ node.resizeHandles = el.getAttribute('gs-resize-handles');
1249
+ node.id = el.getAttribute('gs-id');
1250
+ // remove any key not found (null or false which is default)
1251
+ for (var key in node) {
1252
+ if (!node.hasOwnProperty(key))
1253
+ return;
1254
+ if (!node[key] && node[key] !== 0) { // 0 can be valid value (x,y only really)
1255
+ delete node[key];
1256
+ }
1257
+ }
1258
+ return node;
1259
+ };
1260
+ /** @internal */
1261
+ GridStack.prototype._setStaticClass = function () {
1262
+ var _a, _b;
1263
+ var classes = ['grid-stack-static'];
1264
+ if (this.opts.staticGrid) {
1265
+ (_a = this.el.classList).add.apply(_a, classes);
1266
+ this.el.setAttribute('gs-static', 'true');
1267
+ }
1268
+ else {
1269
+ (_b = this.el.classList).remove.apply(_b, classes);
1270
+ this.el.removeAttribute('gs-static');
1271
+ }
1272
+ return this;
1273
+ };
1274
+ /**
1275
+ * called when we are being resized by the window - check if the one Column Mode needs to be turned on/off
1276
+ * and remember the prev columns we used, as well as check for auto cell height (square)
1277
+ */
1278
+ GridStack.prototype.onParentResize = function () {
1279
+ var _this = this;
1280
+ if (!this.el || !this.el.clientWidth)
1281
+ return; // return if we're gone or no size yet (will get called again)
1282
+ var oneColumn = !this.opts.disableOneColumnMode && this.el.clientWidth <= this.opts.minWidth;
1283
+ var changedOneColumn = false;
1284
+ if ((this.opts.column === 1) !== oneColumn) {
1285
+ changedOneColumn = true;
1286
+ if (this.opts.animate) {
1287
+ this.setAnimation(false);
1288
+ } // 1 <-> 12 is too radical, turn off animation
1289
+ this.column(oneColumn ? 1 : this._prevColumn);
1290
+ if (this.opts.animate) {
1291
+ this.setAnimation(true);
1292
+ }
1293
+ }
1294
+ // make the cells content square again
1295
+ if (this._isAutoCellHeight) {
1296
+ if (!changedOneColumn && this.opts.cellHeightThrottle) {
1297
+ if (!this._cellHeightThrottle) {
1298
+ this._cellHeightThrottle = utils_1.Utils.throttle(function () { return _this.cellHeight(); }, this.opts.cellHeightThrottle);
1299
+ }
1300
+ this._cellHeightThrottle();
1301
+ }
1302
+ else {
1303
+ // immediate update if we've changed to/from oneColumn or have no threshold
1304
+ this.cellHeight();
1305
+ }
1306
+ }
1307
+ // finally update any nested grids
1308
+ this.engine.nodes.forEach(function (n) {
1309
+ if (n.subGrid) {
1310
+ n.subGrid.onParentResize();
1311
+ }
1312
+ });
1313
+ return this;
1314
+ };
1315
+ /** add or remove the window size event handler */
1316
+ GridStack.prototype._updateWindowResizeEvent = function (forceRemove) {
1317
+ if (forceRemove === void 0) { forceRemove = false; }
1318
+ // only add event if we're not nested (parent will call us) and we're auto sizing cells or supporting oneColumn (i.e. doing work)
1319
+ var workTodo = (this._isAutoCellHeight || !this.opts.disableOneColumnMode) && !this.opts._isNested;
1320
+ if (!forceRemove && workTodo && !this._windowResizeBind) {
1321
+ this._windowResizeBind = this.onParentResize.bind(this); // so we can properly remove later
1322
+ window.addEventListener('resize', this._windowResizeBind);
1323
+ }
1324
+ else if ((forceRemove || !workTodo) && this._windowResizeBind) {
1325
+ window.removeEventListener('resize', this._windowResizeBind);
1326
+ delete this._windowResizeBind; // remove link to us so we can free
1327
+ }
1328
+ return this;
1329
+ };
1330
+ /** @internal convert a potential selector into actual element */
1331
+ GridStack.getElement = function (els) {
1332
+ if (els === void 0) { els = '.grid-stack-item'; }
1333
+ return utils_1.Utils.getElement(els);
1334
+ };
1335
+ /** @internal */
1336
+ GridStack.getElements = function (els) {
1337
+ if (els === void 0) { els = '.grid-stack-item'; }
1338
+ return utils_1.Utils.getElements(els);
1339
+ };
1340
+ /** @internal */
1341
+ GridStack.getGridElement = function (els) { return GridStack.getElement(els); };
1342
+ /** @internal */
1343
+ GridStack.getGridElements = function (els) { return utils_1.Utils.getElements(els); };
1344
+ /** @internal initialize margin top/bottom/left/right and units */
1345
+ GridStack.prototype.initMargin = function () {
1346
+ var data;
1347
+ var margin = 0;
1348
+ // support passing multiple values like CSS (ex: '5px 10px 0 20px')
1349
+ var margins = [];
1350
+ if (typeof this.opts.margin === 'string') {
1351
+ margins = this.opts.margin.split(' ');
1352
+ }
1353
+ if (margins.length === 2) { // top/bot, left/right like CSS
1354
+ this.opts.marginTop = this.opts.marginBottom = margins[0];
1355
+ this.opts.marginLeft = this.opts.marginRight = margins[1];
1356
+ }
1357
+ else if (margins.length === 4) { // Clockwise like CSS
1358
+ this.opts.marginTop = margins[0];
1359
+ this.opts.marginRight = margins[1];
1360
+ this.opts.marginBottom = margins[2];
1361
+ this.opts.marginLeft = margins[3];
1362
+ }
1363
+ else {
1364
+ data = utils_1.Utils.parseHeight(this.opts.margin);
1365
+ this.opts.marginUnit = data.unit;
1366
+ margin = this.opts.margin = data.h;
1367
+ }
1368
+ // see if top/bottom/left/right need to be set as well
1369
+ if (this.opts.marginTop === undefined) {
1370
+ this.opts.marginTop = margin;
1371
+ }
1372
+ else {
1373
+ data = utils_1.Utils.parseHeight(this.opts.marginTop);
1374
+ this.opts.marginTop = data.h;
1375
+ delete this.opts.margin;
1376
+ }
1377
+ if (this.opts.marginBottom === undefined) {
1378
+ this.opts.marginBottom = margin;
1379
+ }
1380
+ else {
1381
+ data = utils_1.Utils.parseHeight(this.opts.marginBottom);
1382
+ this.opts.marginBottom = data.h;
1383
+ delete this.opts.margin;
1384
+ }
1385
+ if (this.opts.marginRight === undefined) {
1386
+ this.opts.marginRight = margin;
1387
+ }
1388
+ else {
1389
+ data = utils_1.Utils.parseHeight(this.opts.marginRight);
1390
+ this.opts.marginRight = data.h;
1391
+ delete this.opts.margin;
1392
+ }
1393
+ if (this.opts.marginLeft === undefined) {
1394
+ this.opts.marginLeft = margin;
1395
+ }
1396
+ else {
1397
+ data = utils_1.Utils.parseHeight(this.opts.marginLeft);
1398
+ this.opts.marginLeft = data.h;
1399
+ delete this.opts.margin;
1400
+ }
1401
+ this.opts.marginUnit = data.unit; // in case side were spelled out, use those units instead...
1402
+ if (this.opts.marginTop === this.opts.marginBottom && this.opts.marginLeft === this.opts.marginRight && this.opts.marginTop === this.opts.marginRight) {
1403
+ this.opts.margin = this.opts.marginTop; // makes it easier to check for no-ops in setMargin()
1404
+ }
1405
+ return this;
1406
+ };
1407
+ /*
1408
+ * drag&drop empty stubs that will be implemented in gridstack-dd.ts for non static grid
1409
+ * so we don't incur the load unless needed.
1410
+ * NOTE: had to make those methods public in order to define them else as
1411
+ * GridStack.prototype._setupAcceptWidget = function()
1412
+ * maybe there is a better way ????
1413
+ */
1414
+ /* eslint-disable @typescript-eslint/no-unused-vars */
1415
+ /**
1416
+ * call to setup dragging in from the outside (say toolbar), by specifying the class selection and options.
1417
+ * Called during GridStack.init() as options, but can also be called directly (last param are cached) in case the toolbar
1418
+ * is dynamically create and needs to change later.
1419
+ * @param dragIn string selector (ex: '.sidebar .grid-stack-item')
1420
+ * @param dragInOptions options - see DDDragInOpt. (default: {revert: 'invalid', handle: '.grid-stack-item-content', scroll: false, appendTo: 'body'}
1421
+ **/
1422
+ GridStack.setupDragIn = function (dragIn, dragInOptions) { };
1423
+ /**
1424
+ * Enables/Disables dragging by the user of specific grid element. If you want all items, and have it affect future items, use enableMove() instead. No-op for static grids.
1425
+ * IF you are looking to prevent an item from moving (due to being pushed around by another during collision) use locked property instead.
1426
+ * @param els widget or selector to modify.
1427
+ * @param val if true widget will be draggable.
1428
+ */
1429
+ GridStack.prototype.movable = function (els, val) { return this; };
1430
+ /**
1431
+ * Enables/Disables user resizing of specific grid element. If you want all items, and have it affect future items, use enableResize() instead. No-op for static grids.
1432
+ * @param els widget or selector to modify
1433
+ * @param val if true widget will be resizable.
1434
+ */
1435
+ GridStack.prototype.resizable = function (els, val) { return this; };
1436
+ /**
1437
+ * Temporarily disables widgets moving/resizing.
1438
+ * If you want a more permanent way (which freezes up resources) use `setStatic(true)` instead.
1439
+ * Note: no-op for static grid
1440
+ * This is a shortcut for:
1441
+ * @example
1442
+ * grid.enableMove(false);
1443
+ * grid.enableResize(false);
1444
+ */
1445
+ GridStack.prototype.disable = function () { return this; };
1446
+ /**
1447
+ * Re-enables widgets moving/resizing - see disable().
1448
+ * Note: no-op for static grid.
1449
+ * This is a shortcut for:
1450
+ * @example
1451
+ * grid.enableMove(true);
1452
+ * grid.enableResize(true);
1453
+ */
1454
+ GridStack.prototype.enable = function () { return this; };
1455
+ /**
1456
+ * Enables/disables widget moving. No-op for static grids.
1457
+ */
1458
+ GridStack.prototype.enableMove = function (doEnable) { return this; };
1459
+ /**
1460
+ * Enables/disables widget resizing. No-op for static grids.
1461
+ */
1462
+ GridStack.prototype.enableResize = function (doEnable) { return this; };
1463
+ /** @internal called to add drag over support to support widgets */
1464
+ GridStack.prototype._setupAcceptWidget = function () { return this; };
1465
+ /** @internal called to setup a trash drop zone if the user specifies it */
1466
+ GridStack.prototype._setupRemoveDrop = function () { return this; };
1467
+ /** @internal prepares the element for drag&drop **/
1468
+ GridStack.prototype._prepareDragDropByNode = function (node) { return this; };
1469
+ /** @internal handles actual drag/resize start **/
1470
+ GridStack.prototype._onStartMoving = function (el, event, ui, node, cellWidth, cellHeight) { return; };
1471
+ /** @internal handles actual drag/resize **/
1472
+ GridStack.prototype._dragOrResize = function (el, event, ui, node, cellWidth, cellHeight) { return; };
1473
+ /** @internal called when a node leaves our area (mouse out or shape outside) **/
1474
+ GridStack.prototype._leave = function (el, helper) { return; };
1475
+ /** scoping so users can call GridStack.Utils.sort() for example */
1476
+ GridStack.Utils = utils_1.Utils;
1477
+ /** scoping so users can call new GridStack.Engine(12) for example */
1478
+ GridStack.Engine = gridstack_engine_1.GridStackEngine;
1479
+ return GridStack;
1480
+ }());
1481
+ exports.GridStack = GridStack;
1482
+ //# sourceMappingURL=gridstack.js.map