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.
- package/README.md +9 -2
- package/dist/dd-base-impl.d.ts +20 -20
- package/dist/dd-base-impl.js +31 -35
- package/dist/dd-base-impl.js.map +1 -1
- package/dist/dd-draggable.d.ts +28 -28
- package/dist/dd-draggable.js +336 -344
- package/dist/dd-draggable.js.map +1 -1
- package/dist/dd-droppable.d.ts +26 -26
- package/dist/dd-droppable.js +146 -148
- package/dist/dd-droppable.js.map +1 -1
- package/dist/dd-element.d.ts +27 -27
- package/dist/dd-element.js +90 -94
- package/dist/dd-element.js.map +1 -1
- package/dist/dd-gridstack.d.ts +34 -34
- package/dist/dd-gridstack.js +127 -124
- package/dist/dd-gridstack.js.map +1 -1
- package/dist/dd-manager.d.ts +22 -22
- package/dist/dd-manager.js +9 -13
- package/dist/dd-manager.js.map +1 -1
- package/dist/dd-resizable-handle.d.ts +14 -14
- package/dist/dd-resizable-handle.js +102 -105
- package/dist/dd-resizable-handle.js.map +1 -1
- package/dist/dd-resizable.d.ts +28 -28
- package/dist/dd-resizable.js +290 -296
- package/dist/dd-resizable.js.map +1 -1
- package/dist/dd-touch.d.ts +33 -33
- package/dist/dd-touch.js +173 -182
- package/dist/dd-touch.js.map +1 -1
- package/dist/es5/dd-base-impl.d.ts +20 -20
- package/dist/es5/dd-base-impl.js +40 -40
- package/dist/es5/dd-base-impl.js.map +1 -1
- package/dist/es5/dd-draggable.d.ts +28 -28
- package/dist/es5/dd-draggable.js +366 -367
- package/dist/es5/dd-draggable.js.map +1 -1
- package/dist/es5/dd-droppable.d.ts +26 -26
- package/dist/es5/dd-droppable.js +181 -179
- package/dist/es5/dd-droppable.js.map +1 -1
- package/dist/es5/dd-element.d.ts +27 -27
- package/dist/es5/dd-element.js +95 -95
- package/dist/es5/dd-element.js.map +1 -1
- package/dist/es5/dd-gridstack.d.ts +34 -34
- package/dist/es5/dd-gridstack.js +144 -144
- package/dist/es5/dd-gridstack.js.map +1 -1
- package/dist/es5/dd-manager.d.ts +22 -22
- package/dist/es5/dd-manager.js +16 -16
- package/dist/es5/dd-manager.js.map +1 -1
- package/dist/es5/dd-resizable-handle.d.ts +14 -14
- package/dist/es5/dd-resizable-handle.js +105 -106
- package/dist/es5/dd-resizable-handle.js.map +1 -1
- package/dist/es5/dd-resizable.d.ts +28 -28
- package/dist/es5/dd-resizable.js +317 -318
- package/dist/es5/dd-resizable.js.map +1 -1
- package/dist/es5/dd-touch.d.ts +33 -33
- package/dist/es5/dd-touch.js +185 -185
- package/dist/es5/dd-touch.js.map +1 -1
- package/dist/es5/gridstack-all.js +1 -1
- package/dist/es5/gridstack-all.js.LICENSE.txt +1 -1
- package/dist/es5/gridstack-all.js.map +1 -1
- package/dist/es5/gridstack-engine.d.ts +102 -102
- package/dist/es5/gridstack-engine.js +1000 -995
- package/dist/es5/gridstack-engine.js.map +1 -1
- package/dist/es5/gridstack-poly.js +1 -1
- package/dist/es5/gridstack.d.ts +392 -376
- package/dist/es5/gridstack.js +2254 -2233
- package/dist/es5/gridstack.js.map +1 -1
- package/dist/es5/types.d.ts +279 -284
- package/dist/es5/types.js +47 -35
- package/dist/es5/types.js.map +1 -1
- package/dist/es5/utils.d.ts +95 -93
- package/dist/es5/utils.js +600 -569
- package/dist/es5/utils.js.map +1 -1
- package/dist/gridstack-all.js +1 -1
- package/dist/gridstack-all.js.LICENSE.txt +1 -1
- package/dist/gridstack-all.js.map +1 -1
- package/dist/gridstack-engine.d.ts +102 -102
- package/dist/gridstack-engine.js +950 -954
- package/dist/gridstack-engine.js.map +1 -1
- package/dist/gridstack-extra.css +0 -390
- package/dist/gridstack-extra.min.css +1 -1
- package/dist/gridstack.css +19 -97
- package/dist/gridstack.d.ts +392 -376
- package/dist/gridstack.js +2155 -2157
- package/dist/gridstack.js.map +1 -1
- package/dist/gridstack.min.css +1 -1
- package/dist/ng/README.md +154 -0
- package/dist/ng/gridstack-item.component.d.ts +29 -0
- package/dist/ng/gridstack-item.component.js +65 -0
- package/dist/ng/gridstack-item.component.js.map +1 -0
- package/dist/ng/gridstack.component.d.ts +118 -0
- package/dist/ng/gridstack.component.js +245 -0
- package/dist/ng/gridstack.component.js.map +1 -0
- package/dist/src/gridstack-extra.scss +0 -2
- package/dist/src/gridstack.scss +14 -9
- package/dist/types.d.ts +279 -284
- package/dist/types.js +44 -35
- package/dist/types.js.map +1 -1
- package/dist/utils.d.ts +95 -93
- package/dist/utils.js +546 -527
- package/dist/utils.js.map +1 -1
- package/{dist → dist_save}/angular/gridstack-item.component.ts +5 -3
- package/{dist → dist_save}/angular/gridstack.component.ts +32 -18
- package/dist_save/dd-base-impl.d.ts +20 -0
- package/dist_save/dd-base-impl.js +36 -0
- package/dist_save/dd-base-impl.js.map +1 -0
- package/dist_save/dd-draggable.d.ts +28 -0
- package/dist_save/dd-draggable.js +343 -0
- package/dist_save/dd-draggable.js.map +1 -0
- package/dist_save/dd-droppable.d.ts +26 -0
- package/dist_save/dd-droppable.js +149 -0
- package/dist_save/dd-droppable.js.map +1 -0
- package/dist_save/dd-element.d.ts +27 -0
- package/dist_save/dd-element.js +95 -0
- package/dist_save/dd-element.js.map +1 -0
- package/dist_save/dd-gridstack.d.ts +34 -0
- package/dist_save/dd-gridstack.js +125 -0
- package/dist_save/dd-gridstack.js.map +1 -0
- package/dist_save/dd-manager.d.ts +22 -0
- package/dist_save/dd-manager.js +14 -0
- package/dist_save/dd-manager.js.map +1 -0
- package/dist_save/dd-resizable-handle.d.ts +14 -0
- package/dist_save/dd-resizable-handle.js +106 -0
- package/dist_save/dd-resizable-handle.js.map +1 -0
- package/dist_save/dd-resizable.d.ts +28 -0
- package/dist_save/dd-resizable.js +294 -0
- package/dist_save/dd-resizable.js.map +1 -0
- package/dist_save/dd-touch.d.ts +33 -0
- package/dist_save/dd-touch.js +183 -0
- package/dist_save/dd-touch.js.map +1 -0
- package/dist_save/es5/dd-base-impl.d.ts +20 -0
- package/dist_save/es5/dd-base-impl.js +41 -0
- package/dist_save/es5/dd-base-impl.js.map +1 -0
- package/dist_save/es5/dd-draggable.d.ts +28 -0
- package/dist_save/es5/dd-draggable.js +366 -0
- package/dist_save/es5/dd-draggable.js.map +1 -0
- package/dist_save/es5/dd-droppable.d.ts +26 -0
- package/dist_save/es5/dd-droppable.js +180 -0
- package/dist_save/es5/dd-droppable.js.map +1 -0
- package/dist_save/es5/dd-element.d.ts +27 -0
- package/dist_save/es5/dd-element.js +96 -0
- package/dist_save/es5/dd-element.js.map +1 -0
- package/dist_save/es5/dd-gridstack.d.ts +34 -0
- package/dist_save/es5/dd-gridstack.js +145 -0
- package/dist_save/es5/dd-gridstack.js.map +1 -0
- package/dist_save/es5/dd-manager.d.ts +22 -0
- package/dist_save/es5/dd-manager.js +17 -0
- package/dist_save/es5/dd-manager.js.map +1 -0
- package/dist_save/es5/dd-resizable-handle.d.ts +14 -0
- package/dist_save/es5/dd-resizable-handle.js +107 -0
- package/dist_save/es5/dd-resizable-handle.js.map +1 -0
- package/dist_save/es5/dd-resizable.d.ts +28 -0
- package/dist_save/es5/dd-resizable.js +316 -0
- package/dist_save/es5/dd-resizable.js.map +1 -0
- package/dist_save/es5/dd-touch.d.ts +33 -0
- package/dist_save/es5/dd-touch.js +186 -0
- package/dist_save/es5/dd-touch.js.map +1 -0
- package/dist_save/es5/gridstack-all.js +3 -0
- package/dist_save/es5/gridstack-all.js.LICENSE.txt +7 -0
- package/dist_save/es5/gridstack-all.js.map +1 -0
- package/dist_save/es5/gridstack-engine.d.ts +102 -0
- package/dist_save/es5/gridstack-engine.js +997 -0
- package/dist_save/es5/gridstack-engine.js.map +1 -0
- package/dist_save/es5/gridstack-poly.js +356 -0
- package/dist_save/es5/gridstack.d.ts +376 -0
- package/dist_save/es5/gridstack.js +2238 -0
- package/dist_save/es5/gridstack.js.map +1 -0
- package/dist_save/es5/types.d.ts +284 -0
- package/dist_save/es5/types.js +36 -0
- package/dist_save/es5/types.js.map +1 -0
- package/dist_save/es5/utils.d.ts +95 -0
- package/dist_save/es5/utils.js +590 -0
- package/dist_save/es5/utils.js.map +1 -0
- package/dist_save/gridstack-all.js +3 -0
- package/dist_save/gridstack-all.js.LICENSE.txt +7 -0
- package/dist_save/gridstack-all.js.map +1 -0
- package/dist_save/gridstack-engine.d.ts +102 -0
- package/dist_save/gridstack-engine.js +956 -0
- package/dist_save/gridstack-engine.js.map +1 -0
- package/dist_save/gridstack-extra.css +433 -0
- package/dist_save/gridstack-extra.min.css +1 -0
- package/dist_save/gridstack.css +226 -0
- package/dist_save/gridstack.d.ts +376 -0
- package/dist_save/gridstack.js +2162 -0
- package/dist_save/gridstack.js.map +1 -0
- package/dist_save/gridstack.min.css +1 -0
- package/dist_save/src/gridstack-extra.scss +27 -0
- package/dist_save/src/gridstack.scss +131 -0
- package/dist_save/types.d.ts +284 -0
- package/dist_save/types.js +36 -0
- package/dist_save/types.js.map +1 -0
- package/dist_save/utils.d.ts +95 -0
- package/dist_save/utils.js +548 -0
- package/dist_save/utils.js.map +1 -0
- package/doc/CHANGES.md +20 -0
- package/doc/README.md +21 -14
- package/package.json +25 -24
- /package/{dist → dist_save}/angular/README.md +0 -0
package/dist/utils.js
CHANGED
|
@@ -1,528 +1,547 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
let list =
|
|
54
|
-
if (!list.length && els[0] !== '.' && els[0] !== '#') {
|
|
55
|
-
list =
|
|
56
|
-
if (!list.length) {
|
|
57
|
-
list =
|
|
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
|
|
71
|
-
}
|
|
72
|
-
if (els[0] === '.' || els[0] === '[') {
|
|
73
|
-
return
|
|
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
|
|
78
|
-
}
|
|
79
|
-
// finally try string, then id then class
|
|
80
|
-
let el =
|
|
81
|
-
if (!el) {
|
|
82
|
-
el =
|
|
83
|
-
}
|
|
84
|
-
if (!el) {
|
|
85
|
-
el =
|
|
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
|
|
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
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
if (
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
if (b.
|
|
249
|
-
a.
|
|
250
|
-
if (b.
|
|
251
|
-
a.
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
return a
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
static
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
//
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
e.
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
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
|