This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
iron-fit-behavior.js
849 lines (754 loc) · 28.4 KB
/
iron-fit-behavior.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
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/**
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at
http://polymer.github.io/LICENSE.txt The complete set of authors may be found at
http://polymer.github.io/AUTHORS.txt The complete set of contributors may be
found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as
part of the polymer project is also subject to an additional IP rights grant
found at http://polymer.github.io/PATENTS.txt
*/
import '@polymer/polymer/polymer-legacy.js';
import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js';
// IE11 has a bug where an element with (1) `overflow: auto;`, (2) size based on
// its content's natural size, (3) absolute positioning (either `absolute` or
// `fixed`), and (4) use `max-width` to constrain its width will place its
// vertical scrollbar outside the area constrained by `max-width`.
//
// Unlike other browsers, IE11 doesn't support changing the size of scrollbars
// with CSS, so we don't need to read this value live from the element in
// question when trying to work around the bug.
let verticalScrollbarMaxWidthBugOffset = undefined;
const getVerticalScrollbarMaxWidthBugOffset = () => {
if (verticalScrollbarMaxWidthBugOffset !== undefined) {
return verticalScrollbarMaxWidthBugOffset;
}
const container = document.createElement('div');
Object.assign(container.style, {
overflow: 'auto',
position: 'fixed',
left: '0px',
top: '0px',
maxWidth: '100px',
maxHeight: '100px',
});
const content = document.createElement('div');
content.style.width = '200px';
content.style.height = '200px';
container.appendChild(content);
document.body.appendChild(container);
verticalScrollbarMaxWidthBugOffset =
Math.abs(container.offsetWidth - 100) > 1 ?
container.offsetWidth - container.clientWidth :
0;
document.body.removeChild(container);
return verticalScrollbarMaxWidthBugOffset;
};
/**
`Polymer.IronFitBehavior` fits an element in another element using `max-height`
and `max-width`, and optionally centers it in the window or another element.
The element will only be sized and/or positioned if it has not already been
sized and/or positioned by CSS.
CSS properties | Action
--------------------------|-------------------------------------------
`position` set | Element is not centered horizontally or vertically
`top` or `bottom` set | Element is not vertically centered
`left` or `right` set | Element is not horizontally centered
`max-height` set | Element respects `max-height`
`max-width` set | Element respects `max-width`
`Polymer.IronFitBehavior` can position an element into another element using
`verticalAlign` and `horizontalAlign`. This will override the element's css
position.
<div class="container">
<iron-fit-impl vertical-align="top" horizontal-align="auto">
Positioned into the container
</iron-fit-impl>
</div>
Use `noOverlap` to position the element around another element without
overlapping it.
<div class="container">
<iron-fit-impl no-overlap vertical-align="auto" horizontal-align="auto">
Positioned around the container
</iron-fit-impl>
</div>
Use `horizontalOffset, verticalOffset` to offset the element from its
`positionTarget`; `Polymer.IronFitBehavior` will collapse these in order to
keep the element within `fitInto` boundaries, while preserving the element's
CSS margin values.
<div class="container">
<iron-fit-impl vertical-align="top" vertical-offset="20">
With vertical offset
</iron-fit-impl>
</div>
@demo demo/index.html
@polymerBehavior
*/
export const IronFitBehavior = {
properties: {
/**
* The element that will receive a `max-height`/`width`. By default it is
* the same as `this`, but it can be set to a child element. This is useful,
* for example, for implementing a scrolling region inside the element.
* @type {!Element}
*/
sizingTarget: {
type: Object,
value: function() {
return this;
}
},
/**
* The element to fit `this` into.
*/
fitInto: {type: Object, value: window},
/**
* Will position the element around the positionTarget without overlapping
* it.
*/
noOverlap: {type: Boolean},
/**
* The element that should be used to position the element. If not set, it
* will default to the parent node.
* @type {!Element}
*/
positionTarget: {type: Element},
/**
* The orientation against which to align the element horizontally
* relative to the `positionTarget`. Possible values are "left", "right",
* "center", "auto".
*/
horizontalAlign: {type: String},
/**
* The orientation against which to align the element vertically
* relative to the `positionTarget`. Possible values are "top", "bottom",
* "middle", "auto".
*/
verticalAlign: {type: String},
/**
* If true, it will use `horizontalAlign` and `verticalAlign` values as
* preferred alignment and if there's not enough space, it will pick the
* values which minimize the cropping.
*/
dynamicAlign: {type: Boolean},
/**
* A pixel value that will be added to the position calculated for the
* given `horizontalAlign`, in the direction of alignment. You can think
* of it as increasing or decreasing the distance to the side of the
* screen given by `horizontalAlign`.
*
* If `horizontalAlign` is "left" or "center", this offset will increase or
* decrease the distance to the left side of the screen: a negative offset
* will move the dropdown to the left; a positive one, to the right.
*
* Conversely if `horizontalAlign` is "right", this offset will increase
* or decrease the distance to the right side of the screen: a negative
* offset will move the dropdown to the right; a positive one, to the left.
*/
horizontalOffset: {type: Number, value: 0, notify: true},
/**
* A pixel value that will be added to the position calculated for the
* given `verticalAlign`, in the direction of alignment. You can think
* of it as increasing or decreasing the distance to the side of the
* screen given by `verticalAlign`.
*
* If `verticalAlign` is "top" or "middle", this offset will increase or
* decrease the distance to the top side of the screen: a negative offset
* will move the dropdown upwards; a positive one, downwards.
*
* Conversely if `verticalAlign` is "bottom", this offset will increase
* or decrease the distance to the bottom side of the screen: a negative
* offset will move the dropdown downwards; a positive one, upwards.
*/
verticalOffset: {type: Number, value: 0, notify: true},
/**
* Set to true to auto-fit on attach.
*/
autoFitOnAttach: {type: Boolean, value: false},
/**
* If true and scrollbars are added to `sizingTarget` after it is
* positioned, the size of the added scrollbars will be added to its
* `maxWidth` and `maxHeight`.
*/
expandSizingTargetForScrollbars: {type: Boolean, value: false},
/** @type {?Object} */
_fitInfo: {type: Object}
},
get _fitWidth() {
var fitWidth;
if (this.fitInto === window) {
fitWidth = this.fitInto.innerWidth;
} else {
fitWidth = this.fitInto.getBoundingClientRect().width;
}
return fitWidth;
},
get _fitHeight() {
var fitHeight;
if (this.fitInto === window) {
fitHeight = this.fitInto.innerHeight;
} else {
fitHeight = this.fitInto.getBoundingClientRect().height;
}
return fitHeight;
},
get _fitLeft() {
var fitLeft;
if (this.fitInto === window) {
fitLeft = 0;
} else {
fitLeft = this.fitInto.getBoundingClientRect().left;
}
return fitLeft;
},
get _fitTop() {
var fitTop;
if (this.fitInto === window) {
fitTop = 0;
} else {
fitTop = this.fitInto.getBoundingClientRect().top;
}
return fitTop;
},
/**
* The element that should be used to position the element,
* if no position target is configured.
*/
get _defaultPositionTarget() {
var parent = dom(this).parentNode;
if (parent && parent.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
parent = parent.host;
}
return parent;
},
/**
* The horizontal align value, accounting for the RTL/LTR text direction.
*/
get _localeHorizontalAlign() {
if (this._isRTL) {
// In RTL, "left" becomes "right".
if (this.horizontalAlign === 'right') {
return 'left';
}
if (this.horizontalAlign === 'left') {
return 'right';
}
}
return this.horizontalAlign;
},
/**
* True if the element should be positioned instead of centered.
* @private
*/
get __shouldPosition() {
return (this.horizontalAlign || this.verticalAlign) && this.positionTarget;
},
/**
* True if the component is RTL.
* @private
*/
get _isRTL() {
// Memoize this to avoid expensive calculations & relayouts.
// Make sure we do it only once
if (typeof this._memoizedIsRTL === 'undefined') {
this._memoizedIsRTL = window.getComputedStyle(this).direction == 'rtl';
}
return this._memoizedIsRTL;
},
/** @override */
attached: function() {
this.positionTarget = this.positionTarget || this._defaultPositionTarget;
if (this.autoFitOnAttach) {
if (window.getComputedStyle(this).display === 'none') {
setTimeout(function() {
this.fit();
}.bind(this));
} else {
// NOTE: shadydom applies distribution asynchronously
// for performance reasons webcomponents/shadydom#120
// Flush to get correct layout info.
window.ShadyDOM && ShadyDOM.flush();
this.fit();
}
}
},
/** @override */
detached: function() {
if (this.__deferredFit) {
clearTimeout(this.__deferredFit);
this.__deferredFit = null;
}
},
/**
* Positions and fits the element into the `fitInto` element.
*/
fit: function() {
this.position();
this.constrain();
this.center();
},
/**
* Memoize information needed to position and size the target element.
* @suppress {deprecated}
*/
_discoverInfo: function() {
if (this._fitInfo) {
return;
}
var target = window.getComputedStyle(this);
var sizer = window.getComputedStyle(this.sizingTarget);
this._fitInfo = {
inlineStyle: {
top: this.style.top || '',
left: this.style.left || '',
position: this.style.position || ''
},
sizerInlineStyle: {
maxWidth: this.sizingTarget.style.maxWidth || '',
maxHeight: this.sizingTarget.style.maxHeight || '',
boxSizing: this.sizingTarget.style.boxSizing || ''
},
positionedBy: {
vertically: target.top !== 'auto' ?
'top' :
(target.bottom !== 'auto' ? 'bottom' : null),
horizontally: target.left !== 'auto' ?
'left' :
(target.right !== 'auto' ? 'right' : null)
},
sizedBy: {
height: sizer.maxHeight !== 'none',
width: sizer.maxWidth !== 'none',
minWidth: parseInt(sizer.minWidth, 10) || 0,
minHeight: parseInt(sizer.minHeight, 10) || 0
},
margin: {
top: parseInt(target.marginTop, 10) || 0,
right: parseInt(target.marginRight, 10) || 0,
bottom: parseInt(target.marginBottom, 10) || 0,
left: parseInt(target.marginLeft, 10) || 0
}
};
},
/**
* Resets the target element's position and size constraints, and clear
* the memoized data.
*/
resetFit: function() {
var info = this._fitInfo || {};
for (var property in info.sizerInlineStyle) {
this.sizingTarget.style[property] = info.sizerInlineStyle[property];
}
for (var property in info.inlineStyle) {
this.style[property] = info.inlineStyle[property];
}
this._fitInfo = null;
},
/**
* Equivalent to calling `resetFit()` and `fit()`. Useful to call this after
* the element or the `fitInto` element has been resized, or if any of the
* positioning properties (e.g. `horizontalAlign, verticalAlign`) is updated.
* It preserves the scroll position of the sizingTarget.
*/
refit: function() {
var scrollLeft = this.sizingTarget.scrollLeft;
var scrollTop = this.sizingTarget.scrollTop;
this.resetFit();
this.fit();
this.sizingTarget.scrollLeft = scrollLeft;
this.sizingTarget.scrollTop = scrollTop;
},
/**
* Positions the element according to `horizontalAlign, verticalAlign`.
*/
position: function() {
if (!this.__shouldPosition) {
// needs to be centered, and it is done after constrain.
return;
}
this._discoverInfo();
window.ShadyDOM && window.ShadyDOM.flush();
this.style.position = 'fixed';
// Need border-box for margin/padding.
this.sizingTarget.style.boxSizing = 'border-box';
// Set to 0, 0 in order to discover any offset caused by parent stacking
// contexts.
this.style.left = '0px';
this.style.top = '0px';
var rect = this.getBoundingClientRect();
var positionRect = this.__getNormalizedRect(this.positionTarget);
var fitRect = this.__getNormalizedRect(this.fitInto);
let unpositionedOffsetWidth;
let unpositionedOffsetHeight;
let unpositionedClientWidth;
let unpositionedClientHeight;
if (this.expandSizingTargetForScrollbars) {
unpositionedOffsetWidth = this.sizingTarget.offsetWidth;
unpositionedOffsetHeight = this.sizingTarget.offsetHeight;
unpositionedClientWidth = this.sizingTarget.clientWidth;
unpositionedClientHeight = this.sizingTarget.clientHeight;
}
var margin = this._fitInfo.margin;
// Consider the margin as part of the size for position calculations.
var size = {
width: rect.width + margin.left + margin.right,
height: rect.height + margin.top + margin.bottom
};
var position = this.__getPosition(
this._localeHorizontalAlign,
this.verticalAlign,
size,
rect,
positionRect,
fitRect);
var left = position.left + margin.left;
var top = position.top + margin.top;
// We first limit right/bottom within fitInto respecting the margin,
// then use those values to limit top/left.
var right = Math.min(fitRect.right - margin.right, left + rect.width);
var bottom = Math.min(fitRect.bottom - margin.bottom, top + rect.height);
// Keep left/top within fitInto respecting the margin.
left = Math.max(
fitRect.left + margin.left,
Math.min(left, right - this._fitInfo.sizedBy.minWidth));
top = Math.max(
fitRect.top + margin.top,
Math.min(top, bottom - this._fitInfo.sizedBy.minHeight));
// Use right/bottom to set maxWidth/maxHeight, and respect
// minWidth/minHeight.
const maxWidth = Math.max(right - left, this._fitInfo.sizedBy.minWidth);
const maxHeight = Math.max(bottom - top, this._fitInfo.sizedBy.minHeight);
this.sizingTarget.style.maxWidth = maxWidth + 'px';
this.sizingTarget.style.maxHeight = maxHeight + 'px';
// Remove the offset caused by any stacking context.
const leftPosition = left - rect.left;
const topPosition = top - rect.top;
this.style.left = `${leftPosition}px`;
this.style.top = `${topPosition}px`;
if (this.expandSizingTargetForScrollbars) {
// Expand the height first - i.e. in the typical block direction - in case
// this expands the element enough to remove the vertical scrollbar.
const positionedOffsetHeight = this.sizingTarget.offsetHeight;
const positionedClientHeight = this.sizingTarget.clientHeight;
const unpositionedHeightDelta =
unpositionedOffsetHeight - unpositionedClientHeight;
const positionedHeightDelta =
positionedOffsetHeight - positionedClientHeight;
const sizingTargetScrollbarHeight =
positionedHeightDelta - unpositionedHeightDelta;
if (sizingTargetScrollbarHeight > 0) {
// Expand `maxHeight` by `sizingTargetScrollbarHeight` up to the overall
// allowed height within `fitRect`.
const fitRectMaxHeight = fitRect.height - margin.top - margin.bottom;
const newMaxHeight =
Math.min(fitRectMaxHeight, maxHeight + sizingTargetScrollbarHeight);
this.sizingTarget.style.maxHeight = `${newMaxHeight}px`;
// Measure the element's real change in height. This may not equal
// `sizingTargetScrollbarHeight` if the overflow amount is less than the
// scrollbar size.
const offsetHeight = this.sizingTarget.offsetHeight;
const addedHeight = offsetHeight - positionedOffsetHeight;
// Adjust the top position if the alignment requires it.
let newTopPosition;
if (position.verticalAlign === 'top') {
newTopPosition = topPosition;
} else if (position.verticalAlign === 'middle') {
newTopPosition = topPosition - addedHeight / 2;
} else if (position.verticalAlign === 'bottom') {
newTopPosition = topPosition - addedHeight;
}
// Constrain the new top position based on `fitRect` again.
newTopPosition = Math.max(
fitRect.top + margin.top,
Math.min(
newTopPosition, fitRect.bottom - margin.bottom - offsetHeight));
this.style.top = `${newTopPosition}px`;
}
const positionedOffsetWidth = this.sizingTarget.offsetWidth;
const positionedClientWidth = this.sizingTarget.clientWidth;
const unpositionedWidthDelta =
unpositionedOffsetWidth - unpositionedClientWidth;
const positionedWidthDelta =
positionedOffsetWidth - positionedClientWidth;
const sizingTargetScrollbarWidth =
positionedWidthDelta - unpositionedWidthDelta;
if (sizingTargetScrollbarWidth > 0) {
const maxWidthBugOffset = getVerticalScrollbarMaxWidthBugOffset();
// Expand `maxWidth` by `sizingTargetScrollbarWidth` up to the overall
// allowed width within `fitRect`.
const fitRectMaxWidth = fitRect.width - margin.left - margin.right;
const newMaxWidth = Math.min(
fitRectMaxWidth,
maxWidth + sizingTargetScrollbarWidth - maxWidthBugOffset);
this.sizingTarget.style.maxWidth = `${newMaxWidth}px`;
// Measure the element's real change in width. This may not equal
// `sizingTargetScrollbarWidth` if the overflow amount is less than the
// scrollbar size.
const offsetWidth = this.sizingTarget.offsetWidth + maxWidthBugOffset;
const addedWidth = offsetWidth - positionedOffsetWidth;
// Adjust the left position if the alignment requires it.
let newLeftPosition;
if (position.horizontalAlign === 'left') {
newLeftPosition = leftPosition;
} else if (position.horizontalAlign === 'center') {
newLeftPosition = leftPosition - addedWidth / 2;
} else if (position.horizontalAlign === 'right') {
newLeftPosition = leftPosition - addedWidth;
}
// Constrain the new left position based on `fitRect` again.
newLeftPosition = Math.max(
fitRect.left + margin.left,
Math.min(
newLeftPosition, fitRect.right - margin.right - offsetWidth));
this.style.left = `${newLeftPosition}px`;
}
}
},
/**
* Constrains the size of the element to `fitInto` by setting `max-height`
* and/or `max-width`.
*/
constrain: function() {
if (this.__shouldPosition) {
return;
}
this._discoverInfo();
var info = this._fitInfo;
// position at (0px, 0px) if not already positioned, so we can measure the
// natural size.
if (!info.positionedBy.vertically) {
this.style.position = 'fixed';
this.style.top = '0px';
}
if (!info.positionedBy.horizontally) {
this.style.position = 'fixed';
this.style.left = '0px';
}
// need border-box for margin/padding
this.sizingTarget.style.boxSizing = 'border-box';
// constrain the width and height if not already set
var rect = this.getBoundingClientRect();
if (!info.sizedBy.height) {
this.__sizeDimension(
rect, info.positionedBy.vertically, 'top', 'bottom', 'Height');
}
if (!info.sizedBy.width) {
this.__sizeDimension(
rect, info.positionedBy.horizontally, 'left', 'right', 'Width');
}
},
/**
* @protected
* @deprecated
*/
_sizeDimension: function(rect, positionedBy, start, end, extent) {
this.__sizeDimension(rect, positionedBy, start, end, extent);
},
/**
* @private
*/
__sizeDimension: function(rect, positionedBy, start, end, extent) {
var info = this._fitInfo;
var fitRect = this.__getNormalizedRect(this.fitInto);
var max = extent === 'Width' ? fitRect.width : fitRect.height;
var flip = (positionedBy === end);
var offset = flip ? max - rect[end] : rect[start];
var margin = info.margin[flip ? start : end];
var offsetExtent = 'offset' + extent;
var sizingOffset = this[offsetExtent] - this.sizingTarget[offsetExtent];
this.sizingTarget.style['max' + extent] =
(max - margin - offset - sizingOffset) + 'px';
},
/**
* Centers horizontally and vertically if not already positioned. This also
* sets `position:fixed`.
*/
center: function() {
if (this.__shouldPosition) {
return;
}
this._discoverInfo();
var positionedBy = this._fitInfo.positionedBy;
if (positionedBy.vertically && positionedBy.horizontally) {
// Already positioned.
return;
}
// Need position:fixed to center
this.style.position = 'fixed';
// Take into account the offset caused by parents that create stacking
// contexts (e.g. with transform: translate3d). Translate to 0,0 and
// measure the bounding rect.
if (!positionedBy.vertically) {
this.style.top = '0px';
}
if (!positionedBy.horizontally) {
this.style.left = '0px';
}
// It will take in consideration margins and transforms
var rect = this.getBoundingClientRect();
var fitRect = this.__getNormalizedRect(this.fitInto);
if (!positionedBy.vertically) {
var top = fitRect.top - rect.top + (fitRect.height - rect.height) / 2;
this.style.top = top + 'px';
}
if (!positionedBy.horizontally) {
var left = fitRect.left - rect.left + (fitRect.width - rect.width) / 2;
this.style.left = left + 'px';
}
},
__getNormalizedRect: function(target) {
if (target === document.documentElement || target === window) {
return {
top: 0,
left: 0,
width: window.innerWidth,
height: window.innerHeight,
right: window.innerWidth,
bottom: window.innerHeight
};
}
return target.getBoundingClientRect();
},
__getOffscreenArea: function(position, size, fitRect) {
var verticalCrop = Math.min(0, position.top) +
Math.min(0, fitRect.bottom - (position.top + size.height));
var horizontalCrop = Math.min(0, position.left) +
Math.min(0, fitRect.right - (position.left + size.width));
return Math.abs(verticalCrop) * size.width +
Math.abs(horizontalCrop) * size.height;
},
__getPosition: function(
hAlign, vAlign, size, sizeNoMargins, positionRect, fitRect) {
// All the possible configurations.
// Ordered as top-left, top-right, bottom-left, bottom-right.
var positions = [
{
verticalAlign: 'top',
horizontalAlign: 'left',
top: positionRect.top + this.verticalOffset,
left: positionRect.left + this.horizontalOffset
},
{
verticalAlign: 'top',
horizontalAlign: 'right',
top: positionRect.top + this.verticalOffset,
left: positionRect.right - size.width - this.horizontalOffset
},
{
verticalAlign: 'bottom',
horizontalAlign: 'left',
top: positionRect.bottom - size.height - this.verticalOffset,
left: positionRect.left + this.horizontalOffset
},
{
verticalAlign: 'bottom',
horizontalAlign: 'right',
top: positionRect.bottom - size.height - this.verticalOffset,
left: positionRect.right - size.width - this.horizontalOffset
}
];
if (this.noOverlap) {
// Duplicate.
for (var i = 0, l = positions.length; i < l; i++) {
var copy = {};
for (var key in positions[i]) {
copy[key] = positions[i][key];
}
positions.push(copy);
}
// Horizontal overlap only.
positions[0].top = positions[1].top += positionRect.height;
positions[2].top = positions[3].top -= positionRect.height;
// Vertical overlap only.
positions[4].left = positions[6].left += positionRect.width;
positions[5].left = positions[7].left -= positionRect.width;
}
// Consider auto as null for coding convenience.
vAlign = vAlign === 'auto' ? null : vAlign;
hAlign = hAlign === 'auto' ? null : hAlign;
if (!hAlign || hAlign === 'center') {
positions.push({
verticalAlign: 'top',
horizontalAlign: 'center',
top: positionRect.top + this.verticalOffset +
(this.noOverlap ? positionRect.height : 0),
left: positionRect.left - sizeNoMargins.width / 2 +
positionRect.width / 2 + this.horizontalOffset
});
positions.push({
verticalAlign: 'bottom',
horizontalAlign: 'center',
top: positionRect.bottom - size.height - this.verticalOffset -
(this.noOverlap ? positionRect.height : 0),
left: positionRect.left - sizeNoMargins.width / 2 +
positionRect.width / 2 + this.horizontalOffset
});
}
if (!vAlign || vAlign === 'middle') {
positions.push({
verticalAlign: 'middle',
horizontalAlign: 'left',
top: positionRect.top - sizeNoMargins.height / 2 +
positionRect.height / 2 + this.verticalOffset,
left: positionRect.left + this.horizontalOffset +
(this.noOverlap ? positionRect.width : 0)
});
positions.push({
verticalAlign: 'middle',
horizontalAlign: 'right',
top: positionRect.top - sizeNoMargins.height / 2 +
positionRect.height / 2 + this.verticalOffset,
left: positionRect.right - size.width - this.horizontalOffset -
(this.noOverlap ? positionRect.width : 0)
});
}
if (vAlign === 'middle' && hAlign === 'center') {
positions.push({
verticalAlign: 'middle',
horizontalAlign: 'center',
top: positionRect.top - sizeNoMargins.height / 2 +
positionRect.height / 2 + this.verticalOffset,
left: positionRect.left - sizeNoMargins.width / 2 +
positionRect.width / 2 + this.horizontalOffset
});
}
var position;
for (var i = 0; i < positions.length; i++) {
var candidate = positions[i];
var vAlignOk = candidate.verticalAlign === vAlign;
var hAlignOk = candidate.horizontalAlign === hAlign;
// If both vAlign and hAlign are defined, return exact match.
// For dynamicAlign and noOverlap we'll have more than one candidate, so
// we'll have to check the offscreenArea to make the best choice.
if (!this.dynamicAlign && !this.noOverlap && vAlignOk && hAlignOk) {
position = candidate;
break;
}
// Align is ok if alignment preferences are respected. If no preferences,
// it is considered ok.
var alignOk = (!vAlign || vAlignOk) && (!hAlign || hAlignOk);
// Filter out elements that don't match the alignment (if defined).
// With dynamicAlign, we need to consider all the positions to find the
// one that minimizes the cropped area.
if (!this.dynamicAlign && !alignOk) {
continue;
}
candidate.offscreenArea =
this.__getOffscreenArea(candidate, size, fitRect);
// If not cropped and respects the align requirements, keep it.
// This allows to prefer positions overlapping horizontally over the
// ones overlapping vertically.
if (candidate.offscreenArea === 0 && alignOk) {
position = candidate;
break;
}
position = position || candidate;
var diff = candidate.offscreenArea - position.offscreenArea;
// Check which crops less. If it crops equally, check if at least one
// align setting is ok.
if (diff < 0 || (diff === 0 && (vAlignOk || hAlignOk))) {
position = candidate;
}
}
return position;
}
};