4 Polymer.IronFitBehavior fits an element in another element using `max-height` and `max-width`, and
5 optionally centers it in the window or another element.
7 The element will only be sized and/or positioned if it has not already been sized and/or positioned
10 CSS properties | Action
11 -----------------------------|-------------------------------------------
12 `position` set | Element is not centered horizontally or vertically
13 `top` or `bottom` set | Element is not vertically centered
14 `left` or `right` set | Element is not horizontally centered
15 `max-height` or `height` set | Element respects `max-height` or `height`
16 `max-width` or `width` set | Element respects `max-width` or `width`
22 Polymer.IronFitBehavior = {
27 * The element that will receive a `max-height`/`width`. By default it is the same as `this`,
28 * but it can be set to a child element. This is useful, for example, for implementing a
29 * scrolling region inside the element.
40 * The element to fit `this` into.
48 * Set to true to auto-fit on attach.
55 /** @type {?Object} */
64 if (this.fitInto === window) {
65 fitWidth = this.fitInto.innerWidth;
67 fitWidth = this.fitInto.getBoundingClientRect().width;
74 if (this.fitInto === window) {
75 fitHeight = this.fitInto.innerHeight;
77 fitHeight = this.fitInto.getBoundingClientRect().height;
84 if (this.fitInto === window) {
87 fitLeft = this.fitInto.getBoundingClientRect().left;
94 if (this.fitInto === window) {
97 fitTop = this.fitInto.getBoundingClientRect().top;
102 attached: function() {
103 if (this.autoFitOnAttach) {
104 if (window.getComputedStyle(this).display === 'none') {
105 setTimeout(function() {
115 * Fits and optionally centers the element into the window, or `fitInfo` if specified.
118 this._discoverInfo();
124 * Memoize information needed to position and size the target element.
126 _discoverInfo: function() {
130 var target = window.getComputedStyle(this);
131 var sizer = window.getComputedStyle(this.sizingTarget);
134 top: this.style.top || '',
135 left: this.style.left || ''
138 vertically: target.top !== 'auto' ? 'top' : (target.bottom !== 'auto' ?
140 horizontally: target.left !== 'auto' ? 'left' : (target.right !== 'auto' ?
145 height: sizer.maxHeight !== 'none',
146 width: sizer.maxWidth !== 'none'
149 top: parseInt(target.marginTop, 10) || 0,
150 right: parseInt(target.marginRight, 10) || 0,
151 bottom: parseInt(target.marginBottom, 10) || 0,
152 left: parseInt(target.marginLeft, 10) || 0
158 * Resets the target element's position and size constraints, and clear
161 resetFit: function() {
162 if (!this._fitInfo || !this._fitInfo.sizedBy.height) {
163 this.sizingTarget.style.maxHeight = '';
164 this.style.top = this._fitInfo ? this._fitInfo.inlineStyle.top : '';
166 if (!this._fitInfo || !this._fitInfo.sizedBy.width) {
167 this.sizingTarget.style.maxWidth = '';
168 this.style.left = this._fitInfo ? this._fitInfo.inlineStyle.left : '';
171 this.style.position = this._fitInfo.positionedBy.css;
173 this._fitInfo = null;
177 * Equivalent to calling `resetFit()` and `fit()`. Useful to call this after the element,
178 * the window, or the `fitInfo` element has been resized.
186 * Constrains the size of the element to the window or `fitInfo` by setting `max-height`
187 * and/or `max-width`.
189 constrain: function() {
190 var info = this._fitInfo;
191 // position at (0px, 0px) if not already positioned, so we can measure the natural size.
192 if (!this._fitInfo.positionedBy.vertically) {
193 this.style.top = '0px';
195 if (!this._fitInfo.positionedBy.horizontally) {
196 this.style.left = '0px';
198 // need border-box for margin/padding
199 this.sizingTarget.style.boxSizing = 'border-box';
200 // constrain the width and height if not already set
201 var rect = this.getBoundingClientRect();
202 if (!info.sizedBy.height) {
203 this._sizeDimension(rect, info.positionedBy.vertically, 'top', 'bottom', 'Height');
205 if (!info.sizedBy.width) {
206 this._sizeDimension(rect, info.positionedBy.horizontally, 'left', 'right', 'Width');
210 _sizeDimension: function(rect, positionedBy, start, end, extent) {
211 var info = this._fitInfo;
212 var max = extent === 'Width' ? this._fitWidth : this._fitHeight;
213 var flip = (positionedBy === end);
214 var offset = flip ? max - rect[end] : rect[start];
215 var margin = info.margin[flip ? start : end];
216 var offsetExtent = 'offset' + extent;
217 var sizingOffset = this[offsetExtent] - this.sizingTarget[offsetExtent];
218 this.sizingTarget.style['max' + extent] = (max - margin - offset - sizingOffset) + 'px';
222 * Centers horizontally and vertically if not already positioned. This also sets
226 if (!this._fitInfo.positionedBy.vertically || !this._fitInfo.positionedBy.horizontally) {
227 // need position:fixed to center
228 this.style.position = 'fixed';
230 if (!this._fitInfo.positionedBy.vertically) {
231 var top = (this._fitHeight - this.offsetHeight) / 2 + this._fitTop;
232 top -= this._fitInfo.margin.top;
233 this.style.top = top + 'px';
235 if (!this._fitInfo.positionedBy.horizontally) {
236 var left = (this._fitWidth - this.offsetWidth) / 2 + this._fitLeft;
237 left -= this._fitInfo.margin.left;
238 this.style.left = left + 'px';