gridstack 7.3.0 → 8.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (196) hide show
  1. package/README.md +9 -2
  2. package/dist/dd-base-impl.d.ts +20 -20
  3. package/dist/dd-base-impl.js +31 -35
  4. package/dist/dd-base-impl.js.map +1 -1
  5. package/dist/dd-draggable.d.ts +28 -28
  6. package/dist/dd-draggable.js +336 -344
  7. package/dist/dd-draggable.js.map +1 -1
  8. package/dist/dd-droppable.d.ts +26 -26
  9. package/dist/dd-droppable.js +146 -148
  10. package/dist/dd-droppable.js.map +1 -1
  11. package/dist/dd-element.d.ts +27 -27
  12. package/dist/dd-element.js +90 -94
  13. package/dist/dd-element.js.map +1 -1
  14. package/dist/dd-gridstack.d.ts +34 -34
  15. package/dist/dd-gridstack.js +127 -124
  16. package/dist/dd-gridstack.js.map +1 -1
  17. package/dist/dd-manager.d.ts +22 -22
  18. package/dist/dd-manager.js +9 -13
  19. package/dist/dd-manager.js.map +1 -1
  20. package/dist/dd-resizable-handle.d.ts +14 -14
  21. package/dist/dd-resizable-handle.js +102 -105
  22. package/dist/dd-resizable-handle.js.map +1 -1
  23. package/dist/dd-resizable.d.ts +28 -28
  24. package/dist/dd-resizable.js +290 -296
  25. package/dist/dd-resizable.js.map +1 -1
  26. package/dist/dd-touch.d.ts +33 -33
  27. package/dist/dd-touch.js +173 -182
  28. package/dist/dd-touch.js.map +1 -1
  29. package/dist/es5/dd-base-impl.d.ts +20 -20
  30. package/dist/es5/dd-base-impl.js +40 -40
  31. package/dist/es5/dd-base-impl.js.map +1 -1
  32. package/dist/es5/dd-draggable.d.ts +28 -28
  33. package/dist/es5/dd-draggable.js +366 -367
  34. package/dist/es5/dd-draggable.js.map +1 -1
  35. package/dist/es5/dd-droppable.d.ts +26 -26
  36. package/dist/es5/dd-droppable.js +181 -179
  37. package/dist/es5/dd-droppable.js.map +1 -1
  38. package/dist/es5/dd-element.d.ts +27 -27
  39. package/dist/es5/dd-element.js +95 -95
  40. package/dist/es5/dd-element.js.map +1 -1
  41. package/dist/es5/dd-gridstack.d.ts +34 -34
  42. package/dist/es5/dd-gridstack.js +144 -144
  43. package/dist/es5/dd-gridstack.js.map +1 -1
  44. package/dist/es5/dd-manager.d.ts +22 -22
  45. package/dist/es5/dd-manager.js +16 -16
  46. package/dist/es5/dd-manager.js.map +1 -1
  47. package/dist/es5/dd-resizable-handle.d.ts +14 -14
  48. package/dist/es5/dd-resizable-handle.js +105 -106
  49. package/dist/es5/dd-resizable-handle.js.map +1 -1
  50. package/dist/es5/dd-resizable.d.ts +28 -28
  51. package/dist/es5/dd-resizable.js +317 -318
  52. package/dist/es5/dd-resizable.js.map +1 -1
  53. package/dist/es5/dd-touch.d.ts +33 -33
  54. package/dist/es5/dd-touch.js +185 -185
  55. package/dist/es5/dd-touch.js.map +1 -1
  56. package/dist/es5/gridstack-all.js +1 -1
  57. package/dist/es5/gridstack-all.js.LICENSE.txt +1 -1
  58. package/dist/es5/gridstack-all.js.map +1 -1
  59. package/dist/es5/gridstack-engine.d.ts +102 -102
  60. package/dist/es5/gridstack-engine.js +1000 -995
  61. package/dist/es5/gridstack-engine.js.map +1 -1
  62. package/dist/es5/gridstack-poly.js +1 -1
  63. package/dist/es5/gridstack.d.ts +392 -376
  64. package/dist/es5/gridstack.js +2254 -2233
  65. package/dist/es5/gridstack.js.map +1 -1
  66. package/dist/es5/types.d.ts +279 -284
  67. package/dist/es5/types.js +47 -35
  68. package/dist/es5/types.js.map +1 -1
  69. package/dist/es5/utils.d.ts +95 -93
  70. package/dist/es5/utils.js +600 -569
  71. package/dist/es5/utils.js.map +1 -1
  72. package/dist/gridstack-all.js +1 -1
  73. package/dist/gridstack-all.js.LICENSE.txt +1 -1
  74. package/dist/gridstack-all.js.map +1 -1
  75. package/dist/gridstack-engine.d.ts +102 -102
  76. package/dist/gridstack-engine.js +950 -954
  77. package/dist/gridstack-engine.js.map +1 -1
  78. package/dist/gridstack-extra.css +0 -390
  79. package/dist/gridstack-extra.min.css +1 -1
  80. package/dist/gridstack.css +19 -97
  81. package/dist/gridstack.d.ts +392 -376
  82. package/dist/gridstack.js +2155 -2157
  83. package/dist/gridstack.js.map +1 -1
  84. package/dist/gridstack.min.css +1 -1
  85. package/dist/ng/README.md +154 -0
  86. package/dist/ng/gridstack-item.component.d.ts +29 -0
  87. package/dist/ng/gridstack-item.component.js +65 -0
  88. package/dist/ng/gridstack-item.component.js.map +1 -0
  89. package/dist/ng/gridstack.component.d.ts +118 -0
  90. package/dist/ng/gridstack.component.js +245 -0
  91. package/dist/ng/gridstack.component.js.map +1 -0
  92. package/dist/src/gridstack-extra.scss +0 -2
  93. package/dist/src/gridstack.scss +14 -9
  94. package/dist/types.d.ts +279 -284
  95. package/dist/types.js +44 -35
  96. package/dist/types.js.map +1 -1
  97. package/dist/utils.d.ts +95 -93
  98. package/dist/utils.js +546 -527
  99. package/dist/utils.js.map +1 -1
  100. package/{dist → dist_save}/angular/gridstack-item.component.ts +5 -3
  101. package/{dist → dist_save}/angular/gridstack.component.ts +32 -18
  102. package/dist_save/dd-base-impl.d.ts +20 -0
  103. package/dist_save/dd-base-impl.js +36 -0
  104. package/dist_save/dd-base-impl.js.map +1 -0
  105. package/dist_save/dd-draggable.d.ts +28 -0
  106. package/dist_save/dd-draggable.js +343 -0
  107. package/dist_save/dd-draggable.js.map +1 -0
  108. package/dist_save/dd-droppable.d.ts +26 -0
  109. package/dist_save/dd-droppable.js +149 -0
  110. package/dist_save/dd-droppable.js.map +1 -0
  111. package/dist_save/dd-element.d.ts +27 -0
  112. package/dist_save/dd-element.js +95 -0
  113. package/dist_save/dd-element.js.map +1 -0
  114. package/dist_save/dd-gridstack.d.ts +34 -0
  115. package/dist_save/dd-gridstack.js +125 -0
  116. package/dist_save/dd-gridstack.js.map +1 -0
  117. package/dist_save/dd-manager.d.ts +22 -0
  118. package/dist_save/dd-manager.js +14 -0
  119. package/dist_save/dd-manager.js.map +1 -0
  120. package/dist_save/dd-resizable-handle.d.ts +14 -0
  121. package/dist_save/dd-resizable-handle.js +106 -0
  122. package/dist_save/dd-resizable-handle.js.map +1 -0
  123. package/dist_save/dd-resizable.d.ts +28 -0
  124. package/dist_save/dd-resizable.js +294 -0
  125. package/dist_save/dd-resizable.js.map +1 -0
  126. package/dist_save/dd-touch.d.ts +33 -0
  127. package/dist_save/dd-touch.js +183 -0
  128. package/dist_save/dd-touch.js.map +1 -0
  129. package/dist_save/es5/dd-base-impl.d.ts +20 -0
  130. package/dist_save/es5/dd-base-impl.js +41 -0
  131. package/dist_save/es5/dd-base-impl.js.map +1 -0
  132. package/dist_save/es5/dd-draggable.d.ts +28 -0
  133. package/dist_save/es5/dd-draggable.js +366 -0
  134. package/dist_save/es5/dd-draggable.js.map +1 -0
  135. package/dist_save/es5/dd-droppable.d.ts +26 -0
  136. package/dist_save/es5/dd-droppable.js +180 -0
  137. package/dist_save/es5/dd-droppable.js.map +1 -0
  138. package/dist_save/es5/dd-element.d.ts +27 -0
  139. package/dist_save/es5/dd-element.js +96 -0
  140. package/dist_save/es5/dd-element.js.map +1 -0
  141. package/dist_save/es5/dd-gridstack.d.ts +34 -0
  142. package/dist_save/es5/dd-gridstack.js +145 -0
  143. package/dist_save/es5/dd-gridstack.js.map +1 -0
  144. package/dist_save/es5/dd-manager.d.ts +22 -0
  145. package/dist_save/es5/dd-manager.js +17 -0
  146. package/dist_save/es5/dd-manager.js.map +1 -0
  147. package/dist_save/es5/dd-resizable-handle.d.ts +14 -0
  148. package/dist_save/es5/dd-resizable-handle.js +107 -0
  149. package/dist_save/es5/dd-resizable-handle.js.map +1 -0
  150. package/dist_save/es5/dd-resizable.d.ts +28 -0
  151. package/dist_save/es5/dd-resizable.js +316 -0
  152. package/dist_save/es5/dd-resizable.js.map +1 -0
  153. package/dist_save/es5/dd-touch.d.ts +33 -0
  154. package/dist_save/es5/dd-touch.js +186 -0
  155. package/dist_save/es5/dd-touch.js.map +1 -0
  156. package/dist_save/es5/gridstack-all.js +3 -0
  157. package/dist_save/es5/gridstack-all.js.LICENSE.txt +7 -0
  158. package/dist_save/es5/gridstack-all.js.map +1 -0
  159. package/dist_save/es5/gridstack-engine.d.ts +102 -0
  160. package/dist_save/es5/gridstack-engine.js +997 -0
  161. package/dist_save/es5/gridstack-engine.js.map +1 -0
  162. package/dist_save/es5/gridstack-poly.js +356 -0
  163. package/dist_save/es5/gridstack.d.ts +376 -0
  164. package/dist_save/es5/gridstack.js +2238 -0
  165. package/dist_save/es5/gridstack.js.map +1 -0
  166. package/dist_save/es5/types.d.ts +284 -0
  167. package/dist_save/es5/types.js +36 -0
  168. package/dist_save/es5/types.js.map +1 -0
  169. package/dist_save/es5/utils.d.ts +95 -0
  170. package/dist_save/es5/utils.js +590 -0
  171. package/dist_save/es5/utils.js.map +1 -0
  172. package/dist_save/gridstack-all.js +3 -0
  173. package/dist_save/gridstack-all.js.LICENSE.txt +7 -0
  174. package/dist_save/gridstack-all.js.map +1 -0
  175. package/dist_save/gridstack-engine.d.ts +102 -0
  176. package/dist_save/gridstack-engine.js +956 -0
  177. package/dist_save/gridstack-engine.js.map +1 -0
  178. package/dist_save/gridstack-extra.css +433 -0
  179. package/dist_save/gridstack-extra.min.css +1 -0
  180. package/dist_save/gridstack.css +226 -0
  181. package/dist_save/gridstack.d.ts +376 -0
  182. package/dist_save/gridstack.js +2162 -0
  183. package/dist_save/gridstack.js.map +1 -0
  184. package/dist_save/gridstack.min.css +1 -0
  185. package/dist_save/src/gridstack-extra.scss +27 -0
  186. package/dist_save/src/gridstack.scss +131 -0
  187. package/dist_save/types.d.ts +284 -0
  188. package/dist_save/types.js +36 -0
  189. package/dist_save/types.js.map +1 -0
  190. package/dist_save/utils.d.ts +95 -0
  191. package/dist_save/utils.js +548 -0
  192. package/dist_save/utils.js.map +1 -0
  193. package/doc/CHANGES.md +20 -0
  194. package/doc/README.md +21 -14
  195. package/package.json +25 -24
  196. /package/{dist → dist_save}/angular/README.md +0 -0
package/dist/utils.js CHANGED
@@ -1,528 +1,547 @@
1
- "use strict";
2
- /**
3
- * utils.ts 7.3.0
4
- * Copyright (c) 2021 Alain Dumesny - see GridStack root license
5
- */
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.Utils = exports.obsoleteAttr = exports.obsoleteOptsDel = exports.obsoleteOpts = exports.obsolete = void 0;
8
- /** checks for obsolete method names */
9
- // eslint-disable-next-line
10
- function obsolete(self, f, oldName, newName, rev) {
11
- let wrapper = (...args) => {
12
- console.warn('gridstack.js: Function `' + oldName + '` is deprecated in ' + rev + ' and has been replaced ' +
13
- 'with `' + newName + '`. It will be **removed** in a future release');
14
- return f.apply(self, args);
15
- };
16
- wrapper.prototype = f.prototype;
17
- return wrapper;
18
- }
19
- exports.obsolete = obsolete;
20
- /** checks for obsolete grid options (can be used for any fields, but msg is about options) */
21
- function obsoleteOpts(opts, oldName, newName, rev) {
22
- if (opts[oldName] !== undefined) {
23
- opts[newName] = opts[oldName];
24
- console.warn('gridstack.js: Option `' + oldName + '` is deprecated in ' + rev + ' and has been replaced with `' +
25
- newName + '`. It will be **removed** in a future release');
26
- }
27
- }
28
- exports.obsoleteOpts = obsoleteOpts;
29
- /** checks for obsolete grid options which are gone */
30
- function obsoleteOptsDel(opts, oldName, rev, info) {
31
- if (opts[oldName] !== undefined) {
32
- console.warn('gridstack.js: Option `' + oldName + '` is deprecated in ' + rev + info);
33
- }
34
- }
35
- exports.obsoleteOptsDel = obsoleteOptsDel;
36
- /** checks for obsolete Jquery element attributes */
37
- function obsoleteAttr(el, oldName, newName, rev) {
38
- let oldAttr = el.getAttribute(oldName);
39
- if (oldAttr !== null) {
40
- el.setAttribute(newName, oldAttr);
41
- console.warn('gridstack.js: attribute `' + oldName + '`=' + oldAttr + ' is deprecated on this object in ' + rev + ' and has been replaced with `' +
42
- newName + '`. It will be **removed** in a future release');
43
- }
44
- }
45
- exports.obsoleteAttr = obsoleteAttr;
46
- /**
47
- * Utility methods
48
- */
49
- class Utils {
50
- /** convert a potential selector into actual list of html elements */
51
- static getElements(els) {
52
- if (typeof els === 'string') {
53
- let list = document.querySelectorAll(els);
54
- if (!list.length && els[0] !== '.' && els[0] !== '#') {
55
- list = document.querySelectorAll('.' + els);
56
- if (!list.length) {
57
- list = document.querySelectorAll('#' + els);
58
- }
59
- }
60
- return Array.from(list);
61
- }
62
- return [els];
63
- }
64
- /** convert a potential selector into actual single element */
65
- static getElement(els) {
66
- if (typeof els === 'string') {
67
- if (!els.length)
68
- return null;
69
- if (els[0] === '#') {
70
- return document.getElementById(els.substring(1));
71
- }
72
- if (els[0] === '.' || els[0] === '[') {
73
- return document.querySelector(els);
74
- }
75
- // if we start with a digit, assume it's an id (error calling querySelector('#1')) as class are not valid CSS
76
- if (!isNaN(+els[0])) { // start with digit
77
- return document.getElementById(els);
78
- }
79
- // finally try string, then id then class
80
- let el = document.querySelector(els);
81
- if (!el) {
82
- el = document.getElementById(els);
83
- }
84
- if (!el) {
85
- el = document.querySelector('.' + els);
86
- }
87
- return el;
88
- }
89
- return els;
90
- }
91
- /** returns true if a and b overlap */
92
- static isIntercepted(a, b) {
93
- return !(a.y >= b.y + b.h || a.y + a.h <= b.y || a.x + a.w <= b.x || a.x >= b.x + b.w);
94
- }
95
- /** returns true if a and b touch edges or corners */
96
- static isTouching(a, b) {
97
- return Utils.isIntercepted(a, { x: b.x - 0.5, y: b.y - 0.5, w: b.w + 1, h: b.h + 1 });
98
- }
99
- /** returns the area a and b overlap */
100
- static areaIntercept(a, b) {
101
- let x0 = (a.x > b.x) ? a.x : b.x;
102
- let x1 = (a.x + a.w < b.x + b.w) ? a.x + a.w : b.x + b.w;
103
- if (x1 <= x0)
104
- return 0; // no overlap
105
- let y0 = (a.y > b.y) ? a.y : b.y;
106
- let y1 = (a.y + a.h < b.y + b.h) ? a.y + a.h : b.y + b.h;
107
- if (y1 <= y0)
108
- return 0; // no overlap
109
- return (x1 - x0) * (y1 - y0);
110
- }
111
- /** returns the area */
112
- static area(a) {
113
- return a.w * a.h;
114
- }
115
- /**
116
- * Sorts array of nodes
117
- * @param nodes array to sort
118
- * @param dir 1 for asc, -1 for desc (optional)
119
- * @param width width of the grid. If undefined the width will be calculated automatically (optional).
120
- **/
121
- static sort(nodes, dir, column) {
122
- column = column || nodes.reduce((col, n) => Math.max(n.x + n.w, col), 0) || 12;
123
- if (dir === -1)
124
- return nodes.sort((a, b) => (b.x + b.y * column) - (a.x + a.y * column));
125
- else
126
- return nodes.sort((b, a) => (b.x + b.y * column) - (a.x + a.y * column));
127
- }
128
- /**
129
- * creates a style sheet with style id under given parent
130
- * @param id will set the 'gs-style-id' attribute to that id
131
- * @param parent to insert the stylesheet as first child,
132
- * if none supplied it will be appended to the document head instead.
133
- */
134
- static createStylesheet(id, parent, options) {
135
- let style = document.createElement('style');
136
- const nonce = options === null || options === void 0 ? void 0 : options.nonce;
137
- if (nonce)
138
- style.nonce = nonce;
139
- style.setAttribute('type', 'text/css');
140
- style.setAttribute('gs-style-id', id);
141
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
142
- if (style.styleSheet) { // TODO: only CSSImportRule have that and different beast ??
143
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
144
- style.styleSheet.cssText = '';
145
- }
146
- else {
147
- style.appendChild(document.createTextNode('')); // WebKit hack
148
- }
149
- if (!parent) {
150
- // default to head
151
- parent = document.getElementsByTagName('head')[0];
152
- parent.appendChild(style);
153
- }
154
- else {
155
- parent.insertBefore(style, parent.firstChild);
156
- }
157
- return style.sheet;
158
- }
159
- /** removed the given stylesheet id */
160
- static removeStylesheet(id) {
161
- let el = document.querySelector('STYLE[gs-style-id=' + id + ']');
162
- if (el && el.parentNode)
163
- el.remove();
164
- }
165
- /** inserts a CSS rule */
166
- static addCSSRule(sheet, selector, rules) {
167
- if (typeof sheet.addRule === 'function') {
168
- sheet.addRule(selector, rules);
169
- }
170
- else if (typeof sheet.insertRule === 'function') {
171
- sheet.insertRule(`${selector}{${rules}}`);
172
- }
173
- }
174
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
175
- static toBool(v) {
176
- if (typeof v === 'boolean') {
177
- return v;
178
- }
179
- if (typeof v === 'string') {
180
- v = v.toLowerCase();
181
- return !(v === '' || v === 'no' || v === 'false' || v === '0');
182
- }
183
- return Boolean(v);
184
- }
185
- static toNumber(value) {
186
- return (value === null || value.length === 0) ? undefined : Number(value);
187
- }
188
- static parseHeight(val) {
189
- let h;
190
- let unit = 'px';
191
- if (typeof val === 'string') {
192
- let match = val.match(/^(-[0-9]+\.[0-9]+|[0-9]*\.[0-9]+|-[0-9]+|[0-9]+)(px|em|rem|vh|vw|%)?$/);
193
- if (!match) {
194
- throw new Error('Invalid height');
195
- }
196
- unit = match[2] || 'px';
197
- h = parseFloat(match[1]);
198
- }
199
- else {
200
- h = val;
201
- }
202
- return { h, unit };
203
- }
204
- /** copies unset fields in target to use the given default sources values */
205
- // eslint-disable-next-line
206
- static defaults(target, ...sources) {
207
- sources.forEach(source => {
208
- for (const key in source) {
209
- if (!source.hasOwnProperty(key))
210
- return;
211
- if (target[key] === null || target[key] === undefined) {
212
- target[key] = source[key];
213
- }
214
- else if (typeof source[key] === 'object' && typeof target[key] === 'object') {
215
- // property is an object, recursively add it's field over... #1373
216
- this.defaults(target[key], source[key]);
217
- }
218
- }
219
- });
220
- return target;
221
- }
222
- /** given 2 objects return true if they have the same values. Checks for Object {} having same fields and values (just 1 level down) */
223
- static same(a, b) {
224
- if (typeof a !== 'object')
225
- return a == b;
226
- if (typeof a !== typeof b)
227
- return false;
228
- // else we have object, check just 1 level deep for being same things...
229
- if (Object.keys(a).length !== Object.keys(b).length)
230
- return false;
231
- for (const key in a) {
232
- if (a[key] !== b[key])
233
- return false;
234
- }
235
- return true;
236
- }
237
- /** copies over b size & position (GridStackPosition), and optionally min/max as well */
238
- static copyPos(a, b, doMinMax = false) {
239
- a.x = b.x;
240
- a.y = b.y;
241
- a.w = b.w;
242
- a.h = b.h;
243
- if (doMinMax) {
244
- if (b.minW)
245
- a.minW = b.minW;
246
- if (b.minH)
247
- a.minH = b.minH;
248
- if (b.maxW)
249
- a.maxW = b.maxW;
250
- if (b.maxH)
251
- a.maxH = b.maxH;
252
- }
253
- return a;
254
- }
255
- /** true if a and b has same size & position */
256
- static samePos(a, b) {
257
- return a && b && a.x === b.x && a.y === b.y && a.w === b.w && a.h === b.h;
258
- }
259
- /** removes field from the first object if same as the second objects (like diffing) and internal '_' for saving */
260
- static removeInternalAndSame(a, b) {
261
- if (typeof a !== 'object' || typeof b !== 'object')
262
- return;
263
- for (let key in a) {
264
- let val = a[key];
265
- if (key[0] === '_' || val === b[key]) {
266
- delete a[key];
267
- }
268
- else if (val && typeof val === 'object' && b[key] !== undefined) {
269
- for (let i in val) {
270
- if (val[i] === b[key][i] || i[0] === '_') {
271
- delete val[i];
272
- }
273
- }
274
- if (!Object.keys(val).length) {
275
- delete a[key];
276
- }
277
- }
278
- }
279
- }
280
- /** removes internal fields '_' and default values for saving */
281
- static removeInternalForSave(n, removeEl = true) {
282
- for (let key in n) {
283
- if (key[0] === '_' || n[key] === null || n[key] === undefined)
284
- delete n[key];
285
- }
286
- delete n.grid;
287
- if (removeEl)
288
- delete n.el;
289
- // delete default values (will be re-created on read)
290
- if (!n.autoPosition)
291
- delete n.autoPosition;
292
- if (!n.noResize)
293
- delete n.noResize;
294
- if (!n.noMove)
295
- delete n.noMove;
296
- if (!n.locked)
297
- delete n.locked;
298
- if (n.w === 1 || n.w === n.minW)
299
- delete n.w;
300
- if (n.h === 1 || n.h === n.minH)
301
- delete n.h;
302
- }
303
- /** return the closest parent (or itself) matching the given class */
304
- static closestUpByClass(el, name) {
305
- while (el) {
306
- if (el.classList.contains(name))
307
- return el;
308
- el = el.parentElement;
309
- }
310
- return null;
311
- }
312
- /** delay calling the given function for given delay, preventing new calls from happening while waiting */
313
- static throttle(func, delay) {
314
- let isWaiting = false;
315
- return (...args) => {
316
- if (!isWaiting) {
317
- isWaiting = true;
318
- setTimeout(() => { func(...args); isWaiting = false; }, delay);
319
- }
320
- };
321
- }
322
- static removePositioningStyles(el) {
323
- let style = el.style;
324
- if (style.position) {
325
- style.removeProperty('position');
326
- }
327
- if (style.left) {
328
- style.removeProperty('left');
329
- }
330
- if (style.top) {
331
- style.removeProperty('top');
332
- }
333
- if (style.width) {
334
- style.removeProperty('width');
335
- }
336
- if (style.height) {
337
- style.removeProperty('height');
338
- }
339
- }
340
- /** @internal returns the passed element if scrollable, else the closest parent that will, up to the entire document scrolling element */
341
- static getScrollElement(el) {
342
- if (!el)
343
- return document.scrollingElement || document.documentElement; // IE support
344
- const style = getComputedStyle(el);
345
- const overflowRegex = /(auto|scroll)/;
346
- if (overflowRegex.test(style.overflow + style.overflowY)) {
347
- return el;
348
- }
349
- else {
350
- return this.getScrollElement(el.parentElement);
351
- }
352
- }
353
- /** @internal */
354
- static updateScrollPosition(el, position, distance) {
355
- // is widget in view?
356
- let rect = el.getBoundingClientRect();
357
- let innerHeightOrClientHeight = (window.innerHeight || document.documentElement.clientHeight);
358
- if (rect.top < 0 ||
359
- rect.bottom > innerHeightOrClientHeight) {
360
- // set scrollTop of first parent that scrolls
361
- // if parent is larger than el, set as low as possible
362
- // to get entire widget on screen
363
- let offsetDiffDown = rect.bottom - innerHeightOrClientHeight;
364
- let offsetDiffUp = rect.top;
365
- let scrollEl = this.getScrollElement(el);
366
- if (scrollEl !== null) {
367
- let prevScroll = scrollEl.scrollTop;
368
- if (rect.top < 0 && distance < 0) {
369
- // moving up
370
- if (el.offsetHeight > innerHeightOrClientHeight) {
371
- scrollEl.scrollTop += distance;
372
- }
373
- else {
374
- scrollEl.scrollTop += Math.abs(offsetDiffUp) > Math.abs(distance) ? distance : offsetDiffUp;
375
- }
376
- }
377
- else if (distance > 0) {
378
- // moving down
379
- if (el.offsetHeight > innerHeightOrClientHeight) {
380
- scrollEl.scrollTop += distance;
381
- }
382
- else {
383
- scrollEl.scrollTop += offsetDiffDown > distance ? distance : offsetDiffDown;
384
- }
385
- }
386
- // move widget y by amount scrolled
387
- position.top += scrollEl.scrollTop - prevScroll;
388
- }
389
- }
390
- }
391
- /**
392
- * @internal Function used to scroll the page.
393
- *
394
- * @param event `MouseEvent` that triggers the resize
395
- * @param el `HTMLElement` that's being resized
396
- * @param distance Distance from the V edges to start scrolling
397
- */
398
- static updateScrollResize(event, el, distance) {
399
- const scrollEl = this.getScrollElement(el);
400
- const height = scrollEl.clientHeight;
401
- // #1727 event.clientY is relative to viewport, so must compare this against position of scrollEl getBoundingClientRect().top
402
- // #1745 Special situation if scrollEl is document 'html': here browser spec states that
403
- // clientHeight is height of viewport, but getBoundingClientRect() is rectangle of html element;
404
- // this discrepancy arises because in reality scrollbar is attached to viewport, not html element itself.
405
- const offsetTop = (scrollEl === this.getScrollElement()) ? 0 : scrollEl.getBoundingClientRect().top;
406
- const pointerPosY = event.clientY - offsetTop;
407
- const top = pointerPosY < distance;
408
- const bottom = pointerPosY > height - distance;
409
- if (top) {
410
- // This also can be done with a timeout to keep scrolling while the mouse is
411
- // in the scrolling zone. (will have smoother behavior)
412
- scrollEl.scrollBy({ behavior: 'smooth', top: pointerPosY - distance });
413
- }
414
- else if (bottom) {
415
- scrollEl.scrollBy({ behavior: 'smooth', top: distance - (height - pointerPosY) });
416
- }
417
- }
418
- /** single level clone, returning a new object with same top fields. This will share sub objects and arrays */
419
- static clone(obj) {
420
- if (obj === null || obj === undefined || typeof (obj) !== 'object') {
421
- return obj;
422
- }
423
- // return Object.assign({}, obj);
424
- if (obj instanceof Array) {
425
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
426
- return [...obj];
427
- }
428
- return Object.assign({}, obj);
429
- }
430
- /**
431
- * Recursive clone version that returns a full copy, checking for nested objects and arrays ONLY.
432
- * Note: this will use as-is any key starting with double __ (and not copy inside) some lib have circular dependencies.
433
- */
434
- static cloneDeep(obj) {
435
- // list of fields we will skip during cloneDeep (nested objects, other internal)
436
- const skipFields = ['parentGrid', 'el', 'grid', 'subGrid', 'engine'];
437
- // return JSON.parse(JSON.stringify(obj)); // doesn't work with date format ?
438
- const ret = Utils.clone(obj);
439
- for (const key in ret) {
440
- // NOTE: we don't support function/circular dependencies so skip those properties for now...
441
- if (ret.hasOwnProperty(key) && typeof (ret[key]) === 'object' && key.substring(0, 2) !== '__' && !skipFields.find(k => k === key)) {
442
- ret[key] = Utils.cloneDeep(obj[key]);
443
- }
444
- }
445
- return ret;
446
- }
447
- /** deep clone the given HTML node, removing teh unique id field */
448
- static cloneNode(el) {
449
- const node = el.cloneNode(true);
450
- node.removeAttribute('id');
451
- return node;
452
- }
453
- static appendTo(el, parent) {
454
- let parentNode;
455
- if (typeof parent === 'string') {
456
- parentNode = document.querySelector(parent);
457
- }
458
- else {
459
- parentNode = parent;
460
- }
461
- if (parentNode) {
462
- parentNode.appendChild(el);
463
- }
464
- }
465
- // public static setPositionRelative(el: HTMLElement): void {
466
- // if (!(/^(?:r|a|f)/).test(window.getComputedStyle(el).position)) {
467
- // el.style.position = "relative";
468
- // }
469
- // }
470
- static addElStyles(el, styles) {
471
- if (styles instanceof Object) {
472
- for (const s in styles) {
473
- if (styles.hasOwnProperty(s)) {
474
- if (Array.isArray(styles[s])) {
475
- // support fallback value
476
- styles[s].forEach(val => {
477
- el.style[s] = val;
478
- });
479
- }
480
- else {
481
- el.style[s] = styles[s];
482
- }
483
- }
484
- }
485
- }
486
- }
487
- static initEvent(e, info) {
488
- const evt = { type: info.type };
489
- const obj = {
490
- button: 0,
491
- which: 0,
492
- buttons: 1,
493
- bubbles: true,
494
- cancelable: true,
495
- target: info.target ? info.target : e.target
496
- };
497
- // don't check for `instanceof DragEvent` as Safari use MouseEvent #1540
498
- if (e.dataTransfer) {
499
- evt['dataTransfer'] = e.dataTransfer; // workaround 'readonly' field.
500
- }
501
- ['altKey', 'ctrlKey', 'metaKey', 'shiftKey'].forEach(p => evt[p] = e[p]); // keys
502
- ['pageX', 'pageY', 'clientX', 'clientY', 'screenX', 'screenY'].forEach(p => evt[p] = e[p]); // point info
503
- return Object.assign(Object.assign({}, evt), obj);
504
- }
505
- /** copies the MouseEvent properties and sends it as another event to the given target */
506
- static simulateMouseEvent(e, simulatedType, target) {
507
- const simulatedEvent = document.createEvent('MouseEvents');
508
- simulatedEvent.initMouseEvent(simulatedType, // type
509
- true, // bubbles
510
- true, // cancelable
511
- window, // view
512
- 1, // detail
513
- e.screenX, // screenX
514
- e.screenY, // screenY
515
- e.clientX, // clientX
516
- e.clientY, // clientY
517
- e.ctrlKey, // ctrlKey
518
- e.altKey, // altKey
519
- e.shiftKey, // shiftKey
520
- e.metaKey, // metaKey
521
- 0, // button
522
- e.target // relatedTarget
523
- );
524
- (target || e.target).dispatchEvent(simulatedEvent);
525
- }
526
- }
527
- exports.Utils = Utils;
1
+ /**
2
+ * utils.ts 8.0.1
3
+ * Copyright (c) 2021 Alain Dumesny - see GridStack root license
4
+ */
5
+ /** checks for obsolete method names */
6
+ // eslint-disable-next-line
7
+ export function obsolete(self, f, oldName, newName, rev) {
8
+ let wrapper = (...args) => {
9
+ console.warn('gridstack.js: Function `' + oldName + '` is deprecated in ' + rev + ' and has been replaced ' +
10
+ 'with `' + newName + '`. It will be **removed** in a future release');
11
+ return f.apply(self, args);
12
+ };
13
+ wrapper.prototype = f.prototype;
14
+ return wrapper;
15
+ }
16
+ /** checks for obsolete grid options (can be used for any fields, but msg is about options) */
17
+ export function obsoleteOpts(opts, oldName, newName, rev) {
18
+ if (opts[oldName] !== undefined) {
19
+ opts[newName] = opts[oldName];
20
+ console.warn('gridstack.js: Option `' + oldName + '` is deprecated in ' + rev + ' and has been replaced with `' +
21
+ newName + '`. It will be **removed** in a future release');
22
+ }
23
+ }
24
+ /** checks for obsolete grid options which are gone */
25
+ export function obsoleteOptsDel(opts, oldName, rev, info) {
26
+ if (opts[oldName] !== undefined) {
27
+ console.warn('gridstack.js: Option `' + oldName + '` is deprecated in ' + rev + info);
28
+ }
29
+ }
30
+ /** checks for obsolete Jquery element attributes */
31
+ export function obsoleteAttr(el, oldName, newName, rev) {
32
+ let oldAttr = el.getAttribute(oldName);
33
+ if (oldAttr !== null) {
34
+ el.setAttribute(newName, oldAttr);
35
+ console.warn('gridstack.js: attribute `' + oldName + '`=' + oldAttr + ' is deprecated on this object in ' + rev + ' and has been replaced with `' +
36
+ newName + '`. It will be **removed** in a future release');
37
+ }
38
+ }
39
+ /**
40
+ * Utility methods
41
+ */
42
+ export class Utils {
43
+ /** convert a potential selector into actual list of html elements. optional root which defaults to document (for shadow dom) */
44
+ static getElements(els, root = document) {
45
+ if (typeof els === 'string') {
46
+ // Note: very common for people use to id='1,2,3' which is only legal as HTML5 id, but not CSS selectors
47
+ // so if we start with a number, assume it's an id and just return that one item...
48
+ // see https://github.com/gridstack/gridstack.js/issues/2234#issuecomment-1523796562
49
+ if (!isNaN(+els[0])) { // start with digit
50
+ const el = root.getElementById(els);
51
+ return el ? [el] : [];
52
+ }
53
+ let list = root.querySelectorAll(els);
54
+ if (!list.length && els[0] !== '.' && els[0] !== '#') {
55
+ list = root.querySelectorAll('.' + els);
56
+ if (!list.length) {
57
+ list = root.querySelectorAll('#' + els);
58
+ }
59
+ }
60
+ return Array.from(list);
61
+ }
62
+ return [els];
63
+ }
64
+ /** convert a potential selector into actual single element. optional root which defaults to document (for shadow dom) */
65
+ static getElement(els, root = document) {
66
+ if (typeof els === 'string') {
67
+ if (!els.length)
68
+ return null;
69
+ if (els[0] === '#') {
70
+ return root.getElementById(els.substring(1));
71
+ }
72
+ if (els[0] === '.' || els[0] === '[') {
73
+ return root.querySelector(els);
74
+ }
75
+ // if we start with a digit, assume it's an id (error calling querySelector('#1')) as class are not valid CSS
76
+ if (!isNaN(+els[0])) { // start with digit
77
+ return root.getElementById(els);
78
+ }
79
+ // finally try string, then id, then class
80
+ let el = root.querySelector(els);
81
+ if (!el) {
82
+ el = root.getElementById(els);
83
+ }
84
+ if (!el) {
85
+ el = root.querySelector('.' + els);
86
+ }
87
+ return el;
88
+ }
89
+ return els;
90
+ }
91
+ /** returns true if a and b overlap */
92
+ static isIntercepted(a, b) {
93
+ return !(a.y >= b.y + b.h || a.y + a.h <= b.y || a.x + a.w <= b.x || a.x >= b.x + b.w);
94
+ }
95
+ /** returns true if a and b touch edges or corners */
96
+ static isTouching(a, b) {
97
+ return Utils.isIntercepted(a, { x: b.x - 0.5, y: b.y - 0.5, w: b.w + 1, h: b.h + 1 });
98
+ }
99
+ /** returns the area a and b overlap */
100
+ static areaIntercept(a, b) {
101
+ let x0 = (a.x > b.x) ? a.x : b.x;
102
+ let x1 = (a.x + a.w < b.x + b.w) ? a.x + a.w : b.x + b.w;
103
+ if (x1 <= x0)
104
+ return 0; // no overlap
105
+ let y0 = (a.y > b.y) ? a.y : b.y;
106
+ let y1 = (a.y + a.h < b.y + b.h) ? a.y + a.h : b.y + b.h;
107
+ if (y1 <= y0)
108
+ return 0; // no overlap
109
+ return (x1 - x0) * (y1 - y0);
110
+ }
111
+ /** returns the area */
112
+ static area(a) {
113
+ return a.w * a.h;
114
+ }
115
+ /**
116
+ * Sorts array of nodes
117
+ * @param nodes array to sort
118
+ * @param dir 1 for asc, -1 for desc (optional)
119
+ * @param width width of the grid. If undefined the width will be calculated automatically (optional).
120
+ **/
121
+ static sort(nodes, dir, column) {
122
+ column = column || nodes.reduce((col, n) => Math.max(n.x + n.w, col), 0) || 12;
123
+ if (dir === -1)
124
+ return nodes.sort((a, b) => (b.x + b.y * column) - (a.x + a.y * column));
125
+ else
126
+ return nodes.sort((b, a) => (b.x + b.y * column) - (a.x + a.y * column));
127
+ }
128
+ /**
129
+ * creates a style sheet with style id under given parent
130
+ * @param id will set the 'gs-style-id' attribute to that id
131
+ * @param parent to insert the stylesheet as first child,
132
+ * if none supplied it will be appended to the document head instead.
133
+ */
134
+ static createStylesheet(id, parent, options) {
135
+ let style = document.createElement('style');
136
+ const nonce = options?.nonce;
137
+ if (nonce)
138
+ style.nonce = nonce;
139
+ style.setAttribute('type', 'text/css');
140
+ style.setAttribute('gs-style-id', id);
141
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
142
+ if (style.styleSheet) { // TODO: only CSSImportRule have that and different beast ??
143
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
144
+ style.styleSheet.cssText = '';
145
+ }
146
+ else {
147
+ style.appendChild(document.createTextNode('')); // WebKit hack
148
+ }
149
+ if (!parent) {
150
+ // default to head
151
+ parent = document.getElementsByTagName('head')[0];
152
+ parent.appendChild(style);
153
+ }
154
+ else {
155
+ parent.insertBefore(style, parent.firstChild);
156
+ }
157
+ return style.sheet;
158
+ }
159
+ /** removed the given stylesheet id */
160
+ static removeStylesheet(id) {
161
+ let el = document.querySelector('STYLE[gs-style-id=' + id + ']');
162
+ if (el && el.parentNode)
163
+ el.remove();
164
+ }
165
+ /** inserts a CSS rule */
166
+ static addCSSRule(sheet, selector, rules) {
167
+ if (typeof sheet.addRule === 'function') {
168
+ sheet.addRule(selector, rules);
169
+ }
170
+ else if (typeof sheet.insertRule === 'function') {
171
+ sheet.insertRule(`${selector}{${rules}}`);
172
+ }
173
+ }
174
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
175
+ static toBool(v) {
176
+ if (typeof v === 'boolean') {
177
+ return v;
178
+ }
179
+ if (typeof v === 'string') {
180
+ v = v.toLowerCase();
181
+ return !(v === '' || v === 'no' || v === 'false' || v === '0');
182
+ }
183
+ return Boolean(v);
184
+ }
185
+ static toNumber(value) {
186
+ return (value === null || value.length === 0) ? undefined : Number(value);
187
+ }
188
+ static parseHeight(val) {
189
+ let h;
190
+ let unit = 'px';
191
+ if (typeof val === 'string') {
192
+ let match = val.match(/^(-[0-9]+\.[0-9]+|[0-9]*\.[0-9]+|-[0-9]+|[0-9]+)(px|em|rem|vh|vw|%)?$/);
193
+ if (!match) {
194
+ throw new Error('Invalid height');
195
+ }
196
+ unit = match[2] || 'px';
197
+ h = parseFloat(match[1]);
198
+ }
199
+ else {
200
+ h = val;
201
+ }
202
+ return { h, unit };
203
+ }
204
+ /** copies unset fields in target to use the given default sources values */
205
+ // eslint-disable-next-line
206
+ static defaults(target, ...sources) {
207
+ sources.forEach(source => {
208
+ for (const key in source) {
209
+ if (!source.hasOwnProperty(key))
210
+ return;
211
+ if (target[key] === null || target[key] === undefined) {
212
+ target[key] = source[key];
213
+ }
214
+ else if (typeof source[key] === 'object' && typeof target[key] === 'object') {
215
+ // property is an object, recursively add it's field over... #1373
216
+ this.defaults(target[key], source[key]);
217
+ }
218
+ }
219
+ });
220
+ return target;
221
+ }
222
+ /** given 2 objects return true if they have the same values. Checks for Object {} having same fields and values (just 1 level down) */
223
+ static same(a, b) {
224
+ if (typeof a !== 'object')
225
+ return a == b;
226
+ if (typeof a !== typeof b)
227
+ return false;
228
+ // else we have object, check just 1 level deep for being same things...
229
+ if (Object.keys(a).length !== Object.keys(b).length)
230
+ return false;
231
+ for (const key in a) {
232
+ if (a[key] !== b[key])
233
+ return false;
234
+ }
235
+ return true;
236
+ }
237
+ /** copies over b size & position (GridStackPosition), and optionally min/max as well */
238
+ static copyPos(a, b, doMinMax = false) {
239
+ if (b.x !== undefined)
240
+ a.x = b.x;
241
+ if (b.y !== undefined)
242
+ a.y = b.y;
243
+ if (b.w !== undefined)
244
+ a.w = b.w;
245
+ if (b.h !== undefined)
246
+ a.h = b.h;
247
+ if (doMinMax) {
248
+ if (b.minW)
249
+ a.minW = b.minW;
250
+ if (b.minH)
251
+ a.minH = b.minH;
252
+ if (b.maxW)
253
+ a.maxW = b.maxW;
254
+ if (b.maxH)
255
+ a.maxH = b.maxH;
256
+ }
257
+ return a;
258
+ }
259
+ /** true if a and b has same size & position */
260
+ static samePos(a, b) {
261
+ return a && b && a.x === b.x && a.y === b.y && a.w === b.w && a.h === b.h;
262
+ }
263
+ /** given a node, makes sure it's min/max are valid */
264
+ static sanitizeMinMax(node) {
265
+ // remove 0, undefine, null
266
+ if (!node.minW) {
267
+ delete node.minW;
268
+ }
269
+ if (!node.minH) {
270
+ delete node.minH;
271
+ }
272
+ if (!node.maxW) {
273
+ delete node.maxW;
274
+ }
275
+ if (!node.maxH) {
276
+ delete node.maxH;
277
+ }
278
+ }
279
+ /** removes field from the first object if same as the second objects (like diffing) and internal '_' for saving */
280
+ static removeInternalAndSame(a, b) {
281
+ if (typeof a !== 'object' || typeof b !== 'object')
282
+ return;
283
+ for (let key in a) {
284
+ let val = a[key];
285
+ if (key[0] === '_' || val === b[key]) {
286
+ delete a[key];
287
+ }
288
+ else if (val && typeof val === 'object' && b[key] !== undefined) {
289
+ for (let i in val) {
290
+ if (val[i] === b[key][i] || i[0] === '_') {
291
+ delete val[i];
292
+ }
293
+ }
294
+ if (!Object.keys(val).length) {
295
+ delete a[key];
296
+ }
297
+ }
298
+ }
299
+ }
300
+ /** removes internal fields '_' and default values for saving */
301
+ static removeInternalForSave(n, removeEl = true) {
302
+ for (let key in n) {
303
+ if (key[0] === '_' || n[key] === null || n[key] === undefined)
304
+ delete n[key];
305
+ }
306
+ delete n.grid;
307
+ if (removeEl)
308
+ delete n.el;
309
+ // delete default values (will be re-created on read)
310
+ if (!n.autoPosition)
311
+ delete n.autoPosition;
312
+ if (!n.noResize)
313
+ delete n.noResize;
314
+ if (!n.noMove)
315
+ delete n.noMove;
316
+ if (!n.locked)
317
+ delete n.locked;
318
+ if (n.w === 1 || n.w === n.minW)
319
+ delete n.w;
320
+ if (n.h === 1 || n.h === n.minH)
321
+ delete n.h;
322
+ }
323
+ /** return the closest parent (or itself) matching the given class */
324
+ static closestUpByClass(el, name) {
325
+ while (el) {
326
+ if (el.classList.contains(name))
327
+ return el;
328
+ el = el.parentElement;
329
+ }
330
+ return null;
331
+ }
332
+ /** delay calling the given function for given delay, preventing new calls from happening while waiting */
333
+ static throttle(func, delay) {
334
+ let isWaiting = false;
335
+ return (...args) => {
336
+ if (!isWaiting) {
337
+ isWaiting = true;
338
+ setTimeout(() => { func(...args); isWaiting = false; }, delay);
339
+ }
340
+ };
341
+ }
342
+ static removePositioningStyles(el) {
343
+ let style = el.style;
344
+ if (style.position) {
345
+ style.removeProperty('position');
346
+ }
347
+ if (style.left) {
348
+ style.removeProperty('left');
349
+ }
350
+ if (style.top) {
351
+ style.removeProperty('top');
352
+ }
353
+ if (style.width) {
354
+ style.removeProperty('width');
355
+ }
356
+ if (style.height) {
357
+ style.removeProperty('height');
358
+ }
359
+ }
360
+ /** @internal returns the passed element if scrollable, else the closest parent that will, up to the entire document scrolling element */
361
+ static getScrollElement(el) {
362
+ if (!el)
363
+ return document.scrollingElement || document.documentElement; // IE support
364
+ const style = getComputedStyle(el);
365
+ const overflowRegex = /(auto|scroll)/;
366
+ if (overflowRegex.test(style.overflow + style.overflowY)) {
367
+ return el;
368
+ }
369
+ else {
370
+ return this.getScrollElement(el.parentElement);
371
+ }
372
+ }
373
+ /** @internal */
374
+ static updateScrollPosition(el, position, distance) {
375
+ // is widget in view?
376
+ let rect = el.getBoundingClientRect();
377
+ let innerHeightOrClientHeight = (window.innerHeight || document.documentElement.clientHeight);
378
+ if (rect.top < 0 ||
379
+ rect.bottom > innerHeightOrClientHeight) {
380
+ // set scrollTop of first parent that scrolls
381
+ // if parent is larger than el, set as low as possible
382
+ // to get entire widget on screen
383
+ let offsetDiffDown = rect.bottom - innerHeightOrClientHeight;
384
+ let offsetDiffUp = rect.top;
385
+ let scrollEl = this.getScrollElement(el);
386
+ if (scrollEl !== null) {
387
+ let prevScroll = scrollEl.scrollTop;
388
+ if (rect.top < 0 && distance < 0) {
389
+ // moving up
390
+ if (el.offsetHeight > innerHeightOrClientHeight) {
391
+ scrollEl.scrollTop += distance;
392
+ }
393
+ else {
394
+ scrollEl.scrollTop += Math.abs(offsetDiffUp) > Math.abs(distance) ? distance : offsetDiffUp;
395
+ }
396
+ }
397
+ else if (distance > 0) {
398
+ // moving down
399
+ if (el.offsetHeight > innerHeightOrClientHeight) {
400
+ scrollEl.scrollTop += distance;
401
+ }
402
+ else {
403
+ scrollEl.scrollTop += offsetDiffDown > distance ? distance : offsetDiffDown;
404
+ }
405
+ }
406
+ // move widget y by amount scrolled
407
+ position.top += scrollEl.scrollTop - prevScroll;
408
+ }
409
+ }
410
+ }
411
+ /**
412
+ * @internal Function used to scroll the page.
413
+ *
414
+ * @param event `MouseEvent` that triggers the resize
415
+ * @param el `HTMLElement` that's being resized
416
+ * @param distance Distance from the V edges to start scrolling
417
+ */
418
+ static updateScrollResize(event, el, distance) {
419
+ const scrollEl = this.getScrollElement(el);
420
+ const height = scrollEl.clientHeight;
421
+ // #1727 event.clientY is relative to viewport, so must compare this against position of scrollEl getBoundingClientRect().top
422
+ // #1745 Special situation if scrollEl is document 'html': here browser spec states that
423
+ // clientHeight is height of viewport, but getBoundingClientRect() is rectangle of html element;
424
+ // this discrepancy arises because in reality scrollbar is attached to viewport, not html element itself.
425
+ const offsetTop = (scrollEl === this.getScrollElement()) ? 0 : scrollEl.getBoundingClientRect().top;
426
+ const pointerPosY = event.clientY - offsetTop;
427
+ const top = pointerPosY < distance;
428
+ const bottom = pointerPosY > height - distance;
429
+ if (top) {
430
+ // This also can be done with a timeout to keep scrolling while the mouse is
431
+ // in the scrolling zone. (will have smoother behavior)
432
+ scrollEl.scrollBy({ behavior: 'smooth', top: pointerPosY - distance });
433
+ }
434
+ else if (bottom) {
435
+ scrollEl.scrollBy({ behavior: 'smooth', top: distance - (height - pointerPosY) });
436
+ }
437
+ }
438
+ /** single level clone, returning a new object with same top fields. This will share sub objects and arrays */
439
+ static clone(obj) {
440
+ if (obj === null || obj === undefined || typeof (obj) !== 'object') {
441
+ return obj;
442
+ }
443
+ // return Object.assign({}, obj);
444
+ if (obj instanceof Array) {
445
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
446
+ return [...obj];
447
+ }
448
+ return { ...obj };
449
+ }
450
+ /**
451
+ * Recursive clone version that returns a full copy, checking for nested objects and arrays ONLY.
452
+ * Note: this will use as-is any key starting with double __ (and not copy inside) some lib have circular dependencies.
453
+ */
454
+ static cloneDeep(obj) {
455
+ // list of fields we will skip during cloneDeep (nested objects, other internal)
456
+ const skipFields = ['parentGrid', 'el', 'grid', 'subGrid', 'engine'];
457
+ // return JSON.parse(JSON.stringify(obj)); // doesn't work with date format ?
458
+ const ret = Utils.clone(obj);
459
+ for (const key in ret) {
460
+ // NOTE: we don't support function/circular dependencies so skip those properties for now...
461
+ if (ret.hasOwnProperty(key) && typeof (ret[key]) === 'object' && key.substring(0, 2) !== '__' && !skipFields.find(k => k === key)) {
462
+ ret[key] = Utils.cloneDeep(obj[key]);
463
+ }
464
+ }
465
+ return ret;
466
+ }
467
+ /** deep clone the given HTML node, removing teh unique id field */
468
+ static cloneNode(el) {
469
+ const node = el.cloneNode(true);
470
+ node.removeAttribute('id');
471
+ return node;
472
+ }
473
+ static appendTo(el, parent) {
474
+ let parentNode;
475
+ if (typeof parent === 'string') {
476
+ parentNode = Utils.getElement(parent);
477
+ }
478
+ else {
479
+ parentNode = parent;
480
+ }
481
+ if (parentNode) {
482
+ parentNode.appendChild(el);
483
+ }
484
+ }
485
+ // public static setPositionRelative(el: HTMLElement): void {
486
+ // if (!(/^(?:r|a|f)/).test(window.getComputedStyle(el).position)) {
487
+ // el.style.position = "relative";
488
+ // }
489
+ // }
490
+ static addElStyles(el, styles) {
491
+ if (styles instanceof Object) {
492
+ for (const s in styles) {
493
+ if (styles.hasOwnProperty(s)) {
494
+ if (Array.isArray(styles[s])) {
495
+ // support fallback value
496
+ styles[s].forEach(val => {
497
+ el.style[s] = val;
498
+ });
499
+ }
500
+ else {
501
+ el.style[s] = styles[s];
502
+ }
503
+ }
504
+ }
505
+ }
506
+ }
507
+ static initEvent(e, info) {
508
+ const evt = { type: info.type };
509
+ const obj = {
510
+ button: 0,
511
+ which: 0,
512
+ buttons: 1,
513
+ bubbles: true,
514
+ cancelable: true,
515
+ target: info.target ? info.target : e.target
516
+ };
517
+ // don't check for `instanceof DragEvent` as Safari use MouseEvent #1540
518
+ if (e.dataTransfer) {
519
+ evt['dataTransfer'] = e.dataTransfer; // workaround 'readonly' field.
520
+ }
521
+ ['altKey', 'ctrlKey', 'metaKey', 'shiftKey'].forEach(p => evt[p] = e[p]); // keys
522
+ ['pageX', 'pageY', 'clientX', 'clientY', 'screenX', 'screenY'].forEach(p => evt[p] = e[p]); // point info
523
+ return { ...evt, ...obj };
524
+ }
525
+ /** copies the MouseEvent properties and sends it as another event to the given target */
526
+ static simulateMouseEvent(e, simulatedType, target) {
527
+ const simulatedEvent = document.createEvent('MouseEvents');
528
+ simulatedEvent.initMouseEvent(simulatedType, // type
529
+ true, // bubbles
530
+ true, // cancelable
531
+ window, // view
532
+ 1, // detail
533
+ e.screenX, // screenX
534
+ e.screenY, // screenY
535
+ e.clientX, // clientX
536
+ e.clientY, // clientY
537
+ e.ctrlKey, // ctrlKey
538
+ e.altKey, // altKey
539
+ e.shiftKey, // shiftKey
540
+ e.metaKey, // metaKey
541
+ 0, // button
542
+ e.target // relatedTarget
543
+ );
544
+ (target || e.target).dispatchEvent(simulatedEvent);
545
+ }
546
+ }
528
547
  //# sourceMappingURL=utils.js.map