/
dimensions.js
397 lines (297 loc) · 10.3 KB
/
dimensions.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
import {css} from './style';
import {attr} from './attr';
import {isVisible} from './filter';
import {clamp, each, endsWith, includes, intersectRect, isDocument, isUndefined, isWindow, pointInRect, toFloat, toNode, ucfirst} from './lang';
const dirs = {
width: ['x', 'left', 'right'],
height: ['y', 'top', 'bottom']
};
export function positionAt(element, target, elAttach, targetAttach, elOffset, targetOffset, flip, boundary) {
elAttach = getPos(elAttach);
targetAttach = getPos(targetAttach);
const flipped = {element: elAttach, target: targetAttach};
if (!element || !target) {
return flipped;
}
const dim = getDimensions(element);
const targetDim = getDimensions(target);
const position = targetDim;
moveTo(position, elAttach, dim, -1);
moveTo(position, targetAttach, targetDim, 1);
elOffset = getOffsets(elOffset, dim.width, dim.height);
targetOffset = getOffsets(targetOffset, targetDim.width, targetDim.height);
elOffset['x'] += targetOffset['x'];
elOffset['y'] += targetOffset['y'];
position.left += elOffset['x'];
position.top += elOffset['y'];
boundary = getDimensions(boundary || window(element));
if (flip) {
each(dirs, ([dir, align, alignFlip], prop) => {
if (!(flip === true || includes(flip, dir))) {
return;
}
const elemOffset = elAttach[dir] === align
? -dim[prop]
: elAttach[dir] === alignFlip
? dim[prop]
: 0;
const targetOffset = targetAttach[dir] === align
? targetDim[prop]
: targetAttach[dir] === alignFlip
? -targetDim[prop]
: 0;
if (position[align] < boundary[align] || position[align] + dim[prop] > boundary[alignFlip]) {
const centerOffset = dim[prop] / 2;
const centerTargetOffset = targetAttach[dir] === 'center' ? -targetDim[prop] / 2 : 0;
elAttach[dir] === 'center' && (
apply(centerOffset, centerTargetOffset)
|| apply(-centerOffset, -centerTargetOffset)
) || apply(elemOffset, targetOffset);
}
function apply(elemOffset, targetOffset) {
const newVal = position[align] + elemOffset + targetOffset - elOffset[dir] * 2;
if (newVal >= boundary[align] && newVal + dim[prop] <= boundary[alignFlip]) {
position[align] = newVal;
['element', 'target'].forEach((el) => {
flipped[el][dir] = !elemOffset
? flipped[el][dir]
: flipped[el][dir] === dirs[prop][1]
? dirs[prop][2]
: dirs[prop][1];
});
return true;
}
}
});
}
offset(element, position);
return flipped;
}
export function offset(element, coordinates) {
element = toNode(element);
if (coordinates) {
const currentOffset = offset(element);
const pos = css(element, 'position');
['left', 'top'].forEach(prop => {
if (prop in coordinates) {
const value = css(element, prop);
css(element, prop, coordinates[prop] - currentOffset[prop]
+ toFloat(pos === 'absolute' && value === 'auto'
? position(element)[prop]
: value)
);
}
});
return;
}
return getDimensions(element);
}
function getDimensions(element) {
element = toNode(element);
const {pageYOffset: top, pageXOffset: left} = window(element);
if (isWindow(element)) {
const height = element.innerHeight;
const width = element.innerWidth;
return {
top,
left,
height,
width,
bottom: top + height,
right: left + width,
};
}
let style, hidden;
if (!isVisible(element)) {
style = attr(element, 'style');
hidden = attr(element, 'hidden');
attr(element, {
style: `${style || ''};display:block !important;`,
hidden: null
});
}
const rect = element.getBoundingClientRect();
if (!isUndefined(style)) {
attr(element, {style, hidden});
}
return {
height: rect.height,
width: rect.width,
top: rect.top + top,
left: rect.left + left,
bottom: rect.bottom + top,
right: rect.right + left,
};
}
export function position(element) {
element = toNode(element);
const parent = element.offsetParent || docEl(element);
const parentOffset = offset(parent);
const {top, left} = ['top', 'left'].reduce((props, prop) => {
const propName = ucfirst(prop);
props[prop] -= parentOffset[prop]
+ toFloat(css(element, `margin${propName}`))
+ toFloat(css(parent, `border${propName}Width`));
return props;
}, offset(element));
return {top, left};
}
export const height = dimension('height');
export const width = dimension('width');
function dimension(prop) {
const propName = ucfirst(prop);
return (element, value) => {
element = toNode(element);
if (isUndefined(value)) {
if (isWindow(element)) {
return element[`inner${propName}`];
}
if (isDocument(element)) {
const doc = element.documentElement;
return Math.max(doc[`offset${propName}`], doc[`scroll${propName}`]);
}
value = css(element, prop);
value = value === 'auto' ? element[`offset${propName}`] : toFloat(value) || 0;
return value - boxModelAdjust(prop, element);
} else {
css(element, prop, !value && value !== 0
? ''
: +value + boxModelAdjust(prop, element) + 'px'
);
}
};
}
function boxModelAdjust(prop, element) {
return css(element, 'boxSizing') === 'border-box'
? dirs[prop].slice(1).map(ucfirst).reduce((value, prop) =>
value
+ toFloat(css(element, `padding${prop}`))
+ toFloat(css(element, `border${prop}Width`))
, 0)
: 0;
}
function moveTo(position, attach, dim, factor) {
each(dirs, ([dir, align, alignFlip], prop) => {
if (attach[dir] === alignFlip) {
position[align] += dim[prop] * factor;
} else if (attach[dir] === 'center') {
position[align] += dim[prop] * factor / 2;
}
});
}
function getPos(pos) {
const x = /left|center|right/;
const y = /top|center|bottom/;
pos = (pos || '').split(' ');
if (pos.length === 1) {
pos = x.test(pos[0])
? pos.concat(['center'])
: y.test(pos[0])
? ['center'].concat(pos)
: ['center', 'center'];
}
return {
x: x.test(pos[0]) ? pos[0] : 'center',
y: y.test(pos[1]) ? pos[1] : 'center'
};
}
function getOffsets(offsets, width, height) {
const [x, y] = (offsets || '').split(' ');
return {
x: x ? toFloat(x) * (endsWith(x, '%') ? width / 100 : 1) : 0,
y: y ? toFloat(y) * (endsWith(y, '%') ? height / 100 : 1) : 0
};
}
export function flipPosition(pos) {
switch (pos) {
case 'left':
return 'right';
case 'right':
return 'left';
case 'top':
return 'bottom';
case 'bottom':
return 'top';
default:
return pos;
}
}
export function isInView(element, topOffset = 0, leftOffset = 0, relativeToViewport) {
if (!isVisible(element)) {
return false;
}
element = toNode(element);
const win = window(element);
let client, bounding;
if (relativeToViewport) {
client = element.getBoundingClientRect();
bounding = {
top: -topOffset,
left: -leftOffset,
bottom: topOffset + height(win),
right: leftOffset + width(win)
};
} else {
const [elTop, elLeft] = offsetPosition(element);
const {pageYOffset: top, pageXOffset: left} = win;
client = {
top: elTop,
left: elLeft,
bottom: elTop + element.offsetHeight,
right: elTop + element.offsetWidth
};
bounding = {
top: top - topOffset,
left: left - leftOffset,
bottom: top + topOffset + height(win),
right: left + leftOffset + width(win)
};
}
return intersectRect(client, bounding) || pointInRect({x: client.left, y: client.top}, bounding);
}
export function scrolledOver(element, heightOffset = 0) {
if (!isVisible(element)) {
return 0;
}
element = toNode(element);
const win = window(element);
const doc = document(element);
const elHeight = element.offsetHeight + heightOffset;
const [top] = offsetPosition(element);
const vp = height(win);
const vh = vp + Math.min(0, top - vp);
const diff = Math.max(0, vp - (height(doc) + heightOffset - (top + elHeight)));
return clamp(((vh + win.pageYOffset - top) / ((vh + (elHeight - (diff < vp ? diff : 0))) / 100)) / 100);
}
export function scrollTop(element, top) {
element = toNode(element);
if (isWindow(element) || isDocument(element)) {
const {scrollTo, pageXOffset} = window(element);
scrollTo(pageXOffset, top);
} else {
element.scrollTop = top;
}
}
function offsetPosition(element) {
const offset = [0, 0];
do {
offset[0] += element.offsetTop;
offset[1] += element.offsetLeft;
if (css(element, 'position') === 'fixed') {
const win = window(element);
offset[0] += win.pageYOffset;
offset[1] += win.pageXOffset;
return offset;
}
} while ((element = element.offsetParent));
return offset;
}
function window(element) {
return isWindow(element) ? element : document(element).defaultView;
}
function document(element) {
return toNode(element).ownerDocument;
}
function docEl(element) {
return document(element).documentElement;
}