Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
discover info in position, constrain, center (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
valdrinkoshi authored Jul 18, 2016
1 parent f682f87 commit 881febc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
6 changes: 5 additions & 1 deletion iron-fit-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@
* Positions and fits the element into the `fitInto` element.
*/
fit: function() {
this._discoverInfo();
this.position();
this.constrain();
this.center();
Expand Down Expand Up @@ -337,6 +336,7 @@
// needs to be centered, and it is done after constrain.
return;
}
this._discoverInfo();

this.style.position = 'fixed';
// Need border-box for margin/padding.
Expand Down Expand Up @@ -397,6 +397,8 @@
if (this.horizontalAlign || this.verticalAlign) {
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) {
Expand Down Expand Up @@ -451,6 +453,8 @@
if (this.horizontalAlign || this.verticalAlign) {
return;
}
this._discoverInfo();

var positionedBy = this._fitInfo.positionedBy;
if (positionedBy.vertically && positionedBy.horizontally) {
// Already positioned.
Expand Down
40 changes: 40 additions & 0 deletions test/iron-fit-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@

<div class="constrain"></div>

<test-fixture id="basic">
<template>
<test-fit>
Basic
</test-fit>
</template>
</test-fixture>

<test-fixture id="absolute">
<template>
<test-fit auto-fit-on-attach class="absolute">
Expand Down Expand Up @@ -202,6 +210,38 @@
return !(r2.left >= r1.right || r2.right <= r1.left || r2.top >= r1.bottom || r2.bottom <= r1.top);
}

suite('basic', function() {

var el;
setup(function() {
el = fixture('basic');
});

test('position() works without autoFitOnAttach', function() {
el.verticalAlign = 'top';
el.horizontalAlign = 'left';
el.position();
var rect = el.getBoundingClientRect();
assert.equal(rect.top, 0, 'top ok');
assert.equal(rect.left, 0, 'left ok');
});

test('constrain() works without autoFitOnAttach', function() {
el.constrain();
var style = getComputedStyle(el);
assert.equal(style.maxWidth, window.innerWidth + 'px', 'maxWidth ok');
assert.equal(style.maxHeight, window.innerHeight + 'px', 'maxHeight ok');
});

test('center() works without autoFitOnAttach', function() {
el.center();
var rect = el.getBoundingClientRect();
assert.closeTo(rect.left - (window.innerWidth - rect.right), 0, 5, 'centered horizontally');
assert.closeTo(rect.top - (window.innerHeight - rect.bottom), 0, 5, 'centered vertically');
});

});

suite('manual positioning', function() {

test('css positioned element is not re-positioned', function() {
Expand Down

0 comments on commit 881febc

Please sign in to comment.