-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
435 lines (326 loc) · 10.1 KB
/
index.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
/**
* @module placer
*
* Places any element relative to any other element the way you define
*/
//TODO: use translate3d instead of absolute repositioning (option?)
//TODO: implement avoiding strategy (graphic editors use-case when you need to avoid placing over selected elements)
//TODO: enhance best-side strategy: choose the most closest side
var css = require('mucss/css');
var scrollbarWidth = require('mucss/scrollbar');
var isFixed = require('mucss/is-fixed');
var offsets = require('mucss/offset');
var hasScroll = require('mucss/has-scroll');
var borders = require('mucss/border');
var margins = require('mucss/margin');
var softExtend = require('soft-extend');
var align = require('aligner');
var parseValue = require('mucss/parse-value');
//shortcuts
var win = window, doc = document, root = doc.documentElement;
module.exports = place;
place.align = align;
place.toFloat = align.toFloat;
/**
* Default options
*/
var defaults = {
//an element to align relatively to
//element
target: win,
//which side to place element
//t/r/b/l, 'center', 'middle'
side: 'auto',
/**
* An alignment trbl/0..1/center
*
* @default 0
* @type {(number|string|array)}
*/
align: 0.5,
//selector/nodelist/node/[x,y]/window/function(el)
avoid: undefined,
//selector/nodelist/node/[x,y]/window/function(el)
within: window,
//look for better blacement, if doesn’t fit
auto: true
};
/**
* Place element relative to the target by the side & params passed.
*
* @main
*
* @param {Element} element An element to place
* @param {object} options Options object
*
* @return {boolean} The result of placement - whether placing succeeded
*/
function place (element, options) {
//inherit defaults
options = softExtend(options, defaults);
options.target = options.target || options.to || win;
if (!options.within) {
options.within = options.target === win ? win : root;
}
//TODO: query avoidables
// options.avoid = q(element, options.avoid, true);
//set the same position as the target or absolute
var elStyle = getComputedStyle(element);
if (elStyle.position === 'static') {
if (options.target instanceof Element && isFixed(options.target)) {
element.style.position = 'fixed';
}
else {
element.style.position = 'absolute';
}
}
//force placing into DOM
if (!document.contains(element)) (document.body || document.documentElement).appendChild(element);
//else place according to the position
var side = (options.auto || options.side === 'auto') ? getBestSide(element, options) : options.side;
placeBySide[side](element, options);
return element;
}
/**
* Set of positioning functions
* @enum {Function}
* @param {Element} placee Element to place
* @param {object} target Offsets rectangle (absolute position)
* @param {object} ignore Sides to avoid entering (usually, already tried)
*/
var placeBySide = {
center: function(placee, opts){
//get to & within rectangles
var targetRect = offsets(opts.target);
var parentRect = getParentRect(placee.offsetParent);
//align centered
var al = opts.align;
if (!(al instanceof Array)) {
if (/,/.test(al)) {
al = al.split(/\s*,\s*/);
al = [parseFloat(al[0]), parseFloat(al[1])];
}
else if (/top|bottom|middle/.test(al)) al = [.5, al];
else al = [al, .5];
}
align([opts.target, placee], al);
//apply limits
//FIXME: understand this use-case when it should be called for centered view
if (opts.within && opts.within !== window) {
trimPositionY(placee, opts, parentRect);
trimPositionX(placee, opts, parentRect);
}
//upd options
opts.side = 'center';
},
left: function(placee, opts){
var parent = placee.offsetParent || document.body || root;
var targetRect = offsets(opts.target);
var parentRect = getParentRect(parent);
//correct borders
contractRect(parentRect, borders(parent));
//place left (set css right because placee width may change)
css(placee, {
right: parentRect.right - targetRect.left,
left: 'auto'
});
//place vertically properly
align([opts.target, placee], [null, opts.align]);
//apply limits
if (opts.within) trimPositionY(placee, opts, parentRect);
//upd options
opts.side = 'left';
},
right: function (placee, opts) {
var parent = placee.offsetParent || document.body || root;
//get to & within rectangles
var targetRect = offsets(opts.target);
var parentRect = getParentRect(parent);
//correct borders
contractRect(parentRect, borders(parent));
//place right
css(placee, {
left: targetRect.right - parentRect.left,
right: 'auto',
});
//place vertically properly
align([opts.target, placee], [null, opts.align]);
//apply limits
if (opts.within) trimPositionY(placee, opts, parentRect);
//upd options
opts.side = 'right';
},
top: function(placee, opts){
var parent = placee.offsetParent || document.body || root;
var targetRect = offsets(opts.target);
var parentRect = getParentRect(parent);
//correct borders
contractRect(parentRect, borders(parent));
if (isFixed(placee)) {
parentRect.top = 0;
parentRect.bottom = window.innerHeight;
targetRect.top -= window.pageYOffset;
targetRect.bottom -= window.pageYOffset;
}
//place vertically top-side
css(placee, {
bottom: parentRect.bottom - targetRect.top,
top: 'auto'
});
//place horizontally properly
align([opts.target, placee], [opts.align]);
//apply limits
if (opts.within) trimPositionX(placee, opts, parentRect);
//upd options
opts.side = 'top';
},
bottom: function(placee, opts){
var parent = placee.offsetParent || document.body || root;
//get to & within rectangles
var targetRect = offsets(opts.target);
var parentRect = getParentRect(parent);
//correct borders
contractRect(parentRect, borders(parent));
if (isFixed(placee)) {
parentRect.top = 0;
parentRect.bottom = window.innerHeight;
targetRect.top -= window.pageYOffset;
targetRect.bottom -= window.pageYOffset;
}
//place bottom
css(placee, {
top: targetRect.bottom - parentRect.top,
bottom: 'auto',
});
//place horizontally properly
align([opts.target, placee], [opts.align]);
//apply limits
if (opts.within) trimPositionX(placee, opts, parentRect);
//upd options
opts.side = 'bottom';
}
};
/**
* Find the most appropriate side to place element
*/
function getBestSide (placee, opts) {
var initSide = opts.side === 'auto' ? 'bottom' : opts.side;
var withinRect = offsets(opts.within),
placeeRect = offsets(placee),
targetRect = offsets(opts.target);
contractRect(withinRect, borders(opts.within));
var placeeMargins = margins(placee);
//rect of "hot" area (available spaces from placer to container)
var hotRect = {
top: targetRect.top - withinRect.top,
bottom: withinRect.bottom - targetRect.bottom,
left: targetRect.left - withinRect.left,
right: withinRect.right - targetRect.right
};
//rect of available spaces
var availSpace = {
top: hotRect.top - placeeRect.height - placeeMargins.top - placeeMargins.bottom,
bottom: hotRect.bottom - placeeRect.height - placeeMargins.top - placeeMargins.bottom,
left: hotRect.left - placeeRect.width - placeeMargins.left - placeeMargins.right,
right: hotRect.right - placeeRect.width - placeeMargins.left - placeeMargins.right
};
//TODO: if avoidable el is within the hot area - specify the side limits
//if fits initial side, return it
if (availSpace[initSide] >= 0) return initSide;
//if none of sides fit, return center
if (availSpace.top < 0 && availSpace.bottom < 0 && availSpace.left < 0 && availSpace.right < 0) return 'center';
//else find the most free side within others
var maxSide = initSide, maxSpace = availSpace[maxSide];
for (var side in availSpace) {
if (availSpace[side] > maxSpace) {
maxSide = side; maxSpace = availSpace[maxSide];
}
}
return maxSide;
}
/** contract rect 1 with rect 2 */
function contractRect(rect, rect2){
//correct rect2
rect.left += rect2.left;
rect.right -= rect2.right;
rect.bottom -= rect2.bottom;
rect.top += rect2.top;
return rect;
}
/** Apply limits rectangle to the position of an element */
function trimPositionY(placee, opts, parentRect){
var within = opts.within;
var placeeRect = offsets(placee);
var withinRect = offsets(within);
var placeeMargins = margins(placee);
if (within === window && isFixed(placee)) {
withinRect.top = 0;
withinRect.left = 0;
}
contractRect(withinRect, borders(within));
//shorten withinRect by the avoidable elements
//within the set of avoidable elements find the ones
if (opts.avoid) {
}
if (withinRect.top > placeeRect.top - placeeMargins.top) {
css(placee, {
top: withinRect.top - parentRect.top,
bottom: 'auto'
});
}
else if (withinRect.bottom < placeeRect.bottom + placeeMargins.bottom) {
css(placee, {
top: 'auto',
bottom: parentRect.bottom - withinRect.bottom
});
}
}
function trimPositionX(placee, opts, parentRect){
var within = opts.within;
var placeeRect = offsets(placee);
var withinRect = offsets(within);
var placeeMargins = margins(placee);
if (within === window && isFixed(placee)) {
withinRect.top = 0;
withinRect.left = 0;
}
contractRect(withinRect, borders(within));
if (withinRect.left > placeeRect.left - placeeMargins.left) {
css(placee, {
left: withinRect.left - parentRect.left,
right: 'auto'
});
}
else if (withinRect.right < placeeRect.right + placeeMargins.right) {
css(placee, {
left: 'auto',
right: parentRect.right - withinRect.right
});
}
}
/**
* Return offsets rectangle for an element/array/any target passed.
* I. e. normalize offsets rect
*
* @param {*} el Element, selector, window, document, rect, array
*
* @return {object} Offsets rectangle
*/
function getParentRect (target) {
var rect;
//handle special static body case
if (target == null || (target === window) || (target === doc.body && getComputedStyle(target).position === 'static') || target === root) {
rect = {
left: 0,
right: win.innerWidth - (hasScroll.y() ? scrollbarWidth : 0),
width: win.innerWidth,
top: 0,
bottom: win.innerHeight - (hasScroll.x() ? scrollbarWidth : 0),
height: win.innerHeight
};
}
else {
rect = offsets(target);
}
return rect;
}