-
Notifications
You must be signed in to change notification settings - Fork 2
/
windowHelper.js
465 lines (445 loc) · 14.6 KB
/
windowHelper.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
import {
WindowPositions,
WindowSlot,
posToPixel,
windowPositionFromOptions,
} from './windowManager.js';
/**
* margin in px at the border of the map target
* limiting windows to be moved out of screen
* @type {{top: number, left: number, bottom: number, right: number}}
*/
export const windowMoveMargin = {
top: 0,
right: 64,
bottom: 32,
left: 64,
};
/**
* @enum {number}
* @property {number} TOP_LEFT
* @property {number} TOP_RIGHT
* @property {number} BOTTOM_LEFT
* @property {number} BOTTOM_RIGHT
*/
export const WindowAlignment = {
TOP_LEFT: 1,
TOP_RIGHT: 2,
BOTTOM_LEFT: 3,
BOTTOM_RIGHT: 4,
};
/**
*
* @param {HTMLElement} target
* @returns {DOMRect|null}
*/
export function getTargetSize(target) {
if (!target) {
return null;
}
return target.parentElement?.parentElement?.parentElement?.getBoundingClientRect();
}
/**
* WindowPositionOptions from client position relative to a HTMLElement
* @param {number} x - client pixel position
* @param {number} y - client pixel position
* @param {HTMLElement} target - the map's target { @link @import("@vcmap/core").MapCollection }
* @param {WindowAlignment} [alignment=WindowAlignment.TOP_LEFT]
* @returns {import("./windowManager.js").WindowPositionOptions}
*/
export function getWindowPositionOptions(
x,
y,
target,
alignment = WindowAlignment.TOP_LEFT,
) {
const targetSize = getTargetSize(target);
if (!targetSize) {
return { left: x, top: y };
}
const { left, top, width, height } = targetSize;
if (alignment === WindowAlignment.TOP_LEFT) {
return { left: x - left, top: y - top };
} else if (alignment === WindowAlignment.TOP_RIGHT) {
return { right: left + width - x, top: y - top };
} else if (alignment === WindowAlignment.BOTTOM_LEFT) {
return { left: x - left, bottom: height + top - y };
}
return { right: left + width - x, bottom: height + top - y };
}
/**
* Get window position options based on a pixel in the map
* @param {import("@vcmap-cesium/engine").Cartesian2} windowPosition - the window position, as retrieved from an InteractionEvent
* @param {HTMLElement} target - the map's target { @link @import("@vcmap/core").MapCollection }
* @param {WindowAlignment} [alignment]
* @returns {import("./windowManager.js").WindowPositionOptions}
*/
export function getWindowPositionOptionsFromMapEvent(
windowPosition,
target,
alignment,
) {
const targetSize = getTargetSize(target);
if (!targetSize) {
return { left: windowPosition.x, top: windowPosition.y };
}
const { left, top } = targetSize;
return getWindowPositionOptions(
windowPosition.x + left,
windowPosition.y + top,
target,
alignment,
);
}
/**
* Fits a window aligned top left, so it fits into the parent. This will change the alignment to be bottom or right depending
* on if the window would not fit into the parent.
* @param {number} x - client pixel position
* @param {number} y - client pixel position
* @param {number} width - window width to fit
* @param {number} height - window height to fit
* @param {HTMLElement} target - the map's target { @link @import("@vcmap/core").MapCollection }
* @returns {import("./windowManager.js").WindowPositionOptions}
*/
export function getFittedWindowPositionOptions(x, y, width, height, target) {
const targetSize = getTargetSize(target);
if (!targetSize) {
return { left: x, top: y };
}
const { width: parentWidth, height: parentHeight } = targetSize;
const bottom = y + height > parentHeight;
const right = x + width > parentWidth;
let alignment = WindowAlignment.TOP_LEFT;
if (bottom) {
if (right) {
alignment = WindowAlignment.BOTTOM_RIGHT;
} else {
alignment = WindowAlignment.BOTTOM_LEFT;
}
} else if (right) {
alignment = WindowAlignment.TOP_RIGHT;
}
return getWindowPositionOptions(x, y, target, alignment);
}
/**
* Fits a window aligned top left, so it fits into currently active map. This will change the alignment to be bottom or right depending
* on if the window would not fit into active map element.
* @param {import("@vcmap-cesium/engine").Cartesian2} windowPosition - the window position, as retrieved from an InteractionEvent
* @param {number} width
* @param {number} height
* @param {HTMLElement} target - the map's target { @link @import("@vcmap/core").MapCollection }
* @returns {import("./windowManager.js").WindowPositionOptions}
*/
export function getFittedWindowPositionOptionsFromMapEvent(
windowPosition,
width,
height,
target,
) {
const targetSize = getTargetSize(target);
if (!targetSize) {
return { left: windowPosition.x, top: windowPosition.y };
}
const { left, top } = targetSize;
return getFittedWindowPositionOptions(
windowPosition.x + left,
windowPosition.y + top,
width,
height,
target,
);
}
/**
* Parses a position to numeric value. Non-numeric values return undefined.
* @param {string|number|undefined} pos
* @param {string} key - one of WindowPosition keys
* @param {DOMRect} targetSize - size of the current target
* @returns {number|undefined}
*/
export function posToNumber(pos, key, targetSize) {
if (typeof pos === 'string') {
if (pos.match(/^-?\d+\.?\d*px$/)) {
return parseInt(pos, 10);
} else if (targetSize && pos.match(/^-?\d+\.?\d*%$/)) {
const scalar = ['bottom', 'top', 'height', 'maxHeight'].includes(key)
? targetSize?.height
: targetSize?.width;
return (parseInt(pos, 10) / 100) * scalar;
}
return undefined;
}
return pos;
}
/**
* @param {number} pos
* @param {string} key - one of WindowPosition keys
* @param {DOMRect} targetSize - size of the current target
* @returns {string}
*/
export function posToPercent(pos, key, targetSize) {
if (!targetSize) {
return undefined;
}
const scalar = ['bottom', 'top', 'height', 'maxHeight'].includes(key)
? targetSize.height
: targetSize.width;
return `${((pos / scalar) * 100).toFixed(0)}%`;
}
/**
* Parses CSS position string properties to absolute numeric position properties
* @param {import("./windowManager.js").WindowPosition} windowPosition
* @param {DOMRect} targetSize - the map's target size
* @returns {import("./windowManager.js").WindowPositionOptions|null}
*/
export function optionsFromWindowPosition(windowPosition, targetSize) {
const options = {};
Object.keys(windowPosition).forEach((key) => {
if (windowPosition[key] !== undefined) {
options[key] = posToNumber(windowPosition[key], key, targetSize);
}
});
return options;
}
/**
* Returns an updated WindowPosition by applying new options keeping the original object unchanged.
* Ensures units are maintained.
* Previous values 'auto' and 'unset' will not be touched.
* @param {import("./windowManager.js").WindowPosition} previous
* @param {import("./windowManager.js").WindowPositionOptions} update
* @param {DOMRect} targetSize - the map's target size
* @returns {import("./windowManager.js").WindowPosition}
*/
export function updateWindowPosition(previous, update, targetSize) {
/**
* returns the position of a key in the same unit 'px' or '%' as previously
* @param {string} key
* @param {import("./windowManager.js").WindowPosition} prev
* @param {import("./windowManager.js").WindowPositionOptions} updated
* @returns {string}
*/
const toString = (key, prev, updated) => {
if (
prev[key] === 'auto' ||
prev[key] === 'unset' ||
updated[key] === undefined
) {
return prev[key];
} else if (updated[key] !== 'auto' && updated[key] !== 'unset') {
const numeric = posToNumber(updated[key], key, targetSize);
if (prev[key] && prev[key].match(/^-?\d+\.?\d*px$/)) {
return posToPixel(numeric);
} else if (prev[key] && prev[key].match(/^-?\d+%$/)) {
return posToPercent(numeric, key, targetSize);
}
}
return updated[key];
};
const updatedPosition = { ...update };
Object.keys(previous).forEach((key) => {
updatedPosition[key] = toString(key, previous, update);
});
return updatedPosition;
}
/**
* Move window position in x and y.
* Rightward and downward movements are positive.
* @param {string} id - the window id
* @param {{dx: number, dy: number}} translation - translation in px
* @param {import("./windowManager.js").default} windowManager
* @param {DOMRect} targetSize - the map's target size
* @param {import("./windowManager.js").WindowPosition|null} windowPosition - Optional position to be preferred over windowComponent's position as start.
*/
export function moveWindow(
id,
translation,
windowManager,
targetSize,
windowPosition = null,
) {
const { position, slot } = windowManager.get(id);
if (slot.value === WindowSlot.STATIC) {
return;
}
const windowPositionOptions = optionsFromWindowPosition(
windowPosition || position,
targetSize,
);
if (windowPositionOptions.top !== undefined) {
windowPositionOptions.top += translation.dy;
}
if (windowPositionOptions.bottom !== undefined) {
windowPositionOptions.bottom -= translation.dy;
}
if (windowPositionOptions.left !== undefined) {
windowPositionOptions.left += translation.dx;
}
if (windowPositionOptions.right !== undefined) {
windowPositionOptions.right -= translation.dx;
}
const updatedPosition = updateWindowPosition(
position,
windowPositionOptions,
targetSize,
);
windowManager.setWindowPositionOptions(id, updatedPosition);
}
/**
* Clips a provided WindowPosition corresponding to the size of its target
* @param {import("./windowManager.js").WindowPositionOptions} windowPositionOptions - numerical WindowPositionOptions
* @param {DOMRect} targetSize - the map's target size
* @returns {import("./windowManager.js").WindowPositionOptions}
*/
export function clipToTargetSize(windowPositionOptions, targetSize) {
const { width: targetWidth, height: targetHeight } = targetSize;
if (!targetWidth || !targetHeight) {
return windowPositionOptions;
}
const clippedPosition = {};
if (windowPositionOptions.top !== undefined) {
clippedPosition.top = Math.min(
Math.max(0, windowPositionOptions.top),
targetHeight - windowMoveMargin.bottom,
);
}
if (windowPositionOptions.bottom !== undefined) {
const height =
windowPositionOptions.height ||
targetHeight - windowPositionOptions.bottom - windowPositionOptions.top ||
windowMoveMargin.bottom;
clippedPosition.bottom = Math.min(
Math.max(windowPositionOptions.bottom, -height + windowMoveMargin.bottom),
targetHeight - height,
);
}
if (windowPositionOptions.left !== undefined) {
const width =
windowPositionOptions.width ||
targetWidth - windowPositionOptions.right - windowPositionOptions.left;
clippedPosition.left = Math.min(
Math.max(windowPositionOptions.left, -width + windowMoveMargin.left),
targetWidth - windowMoveMargin.left,
);
}
if (windowPositionOptions.right !== undefined) {
const width =
windowPositionOptions.width ||
targetWidth - windowPositionOptions.right - windowPositionOptions.left;
clippedPosition.right = Math.min(
Math.max(windowPositionOptions.right, -width + windowMoveMargin.right),
targetWidth - windowMoveMargin.right,
);
}
if (windowPositionOptions.width !== undefined) {
clippedPosition.width = windowPositionOptions.width;
}
if (windowPositionOptions.height !== undefined) {
clippedPosition.height = windowPositionOptions.height;
}
clippedPosition.maxWidth = targetWidth;
clippedPosition.maxHeight = targetHeight - 4; // 2px space plus 2px due to margin bottom
if (windowPositionOptions.maxWidth !== undefined) {
clippedPosition.maxWidth = Math.min(
windowPositionOptions.maxWidth,
targetWidth,
);
}
if (windowPositionOptions.maxHeight !== undefined) {
clippedPosition.maxHeight = Math.min(
windowPositionOptions.maxHeight,
targetHeight,
);
}
// max width of a top left 2 window (active static window)
const topLeft2 = posToNumber(
WindowPositions.TOP_LEFT2.left,
'left',
targetSize,
);
if (clippedPosition.left === topLeft2) {
clippedPosition.maxWidth = Math.min(
clippedPosition.maxWidth - topLeft2,
targetWidth,
);
}
return clippedPosition;
}
/**
* Derives a child window position from a parent window, placing the child top-right of the parent.
* @param {import("./windowManager.js").WindowPositionOptions} windowPositionOptions - numerical WindowPositionOptions
* @param {DOMRect} targetSize - the map's target size
* @param {import("./windowManager.js").WindowPositionOptions} parentPosition - numerical WindowPositionOptions
*/
export function applyParentPosition(
windowPositionOptions,
targetSize,
parentPosition,
) {
const {
left,
right,
top,
bottom,
width,
height,
minWidth,
minHeight,
maxWidth,
maxHeight,
} = optionsFromWindowPosition(parentPosition, targetSize);
let parentWidth = width;
if (minWidth !== undefined && minWidth > width) {
parentWidth = minWidth;
}
if (maxWidth !== undefined && maxWidth < width) {
parentWidth = maxWidth;
}
if (parentWidth === undefined) {
parentWidth = targetSize.width - right - left;
}
let parentLeft = left;
if (parentLeft === undefined) {
parentLeft = targetSize.width - right - parentWidth;
}
let parentTop = top;
if (parentTop === undefined) {
let parentHeight = height;
if (minHeight !== undefined && minHeight > height) {
parentHeight = minHeight;
}
if (maxHeight !== undefined && maxHeight < height) {
parentHeight = maxHeight;
}
parentTop = targetSize.height - bottom - parentHeight;
}
windowPositionOptions.left = parentLeft + parentWidth + 2;
windowPositionOptions.top = parentTop;
}
/**
* Returns the position applied on the target by clipping the position to the target's size.
* Maintains units of the input position.
* If parent position is provided, returned position is placed top-right of its parent
* @param {import("./windowManager.js").WindowPosition} position
* @param {DOMRect} targetSize
* @param {import("./windowManager.js").WindowPosition|null} parentPosition
* @returns {import("./windowManager.js").WindowPosition}
*/
export function getPositionAppliedOnTarget(
position,
targetSize,
parentPosition,
) {
if (!targetSize) {
return position;
}
const windowPositionOptions = optionsFromWindowPosition(position, targetSize);
if (parentPosition) {
applyParentPosition(windowPositionOptions, targetSize, parentPosition);
}
const clippedPosition = clipToTargetSize(windowPositionOptions, targetSize);
const updatedPosition = updateWindowPosition(
position,
clippedPosition,
targetSize,
);
return windowPositionFromOptions(updatedPosition);
}