OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 @license |
| 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> |
| 10 |
| 11 <link rel="import" href="../polymer/polymer.html"> |
| 12 |
| 13 <script> |
| 14 |
| 15 /** |
| 16 Polymer.IronFitBehavior fits an element in another element using `max-height` an
d `max-width`, and |
| 17 optionally centers it in the window or another element. |
| 18 |
| 19 The element will only be sized and/or positioned if it has not already been size
d and/or positioned |
| 20 by CSS. |
| 21 |
| 22 CSS properties | Action |
| 23 -----------------------------|------------------------------------------- |
| 24 `position` set | Element is not centered horizontally or verticall
y |
| 25 `top` or `bottom` set | Element is not vertically centered |
| 26 `left` or `right` set | Element is not horizontally centered |
| 27 `max-height` or `height` set | Element respects `max-height` or `height` |
| 28 `max-width` or `width` set | Element respects `max-width` or `width` |
| 29 |
| 30 @demo demo/index.html |
| 31 @polymerBehavior |
| 32 */ |
| 33 |
| 34 Polymer.IronFitBehavior = { |
| 35 |
| 36 properties: { |
| 37 |
| 38 /** |
| 39 * The element that will receive a `max-height`/`width`. By default it is
the same as `this`, |
| 40 * but it can be set to a child element. This is useful, for example, for
implementing a |
| 41 * scrolling region inside the element. |
| 42 * @type {!Element} |
| 43 */ |
| 44 sizingTarget: { |
| 45 type: Object, |
| 46 value: function() { |
| 47 return this; |
| 48 } |
| 49 }, |
| 50 |
| 51 /** |
| 52 * The element to fit `this` into. |
| 53 */ |
| 54 fitInto: { |
| 55 type: Object, |
| 56 value: window |
| 57 }, |
| 58 |
| 59 /** |
| 60 * Set to true to auto-fit on attach. |
| 61 */ |
| 62 autoFitOnAttach: { |
| 63 type: Boolean, |
| 64 value: false |
| 65 }, |
| 66 |
| 67 /** @type {?Object} */ |
| 68 _fitInfo: { |
| 69 type: Object |
| 70 } |
| 71 |
| 72 }, |
| 73 |
| 74 get _fitWidth() { |
| 75 var fitWidth; |
| 76 if (this.fitInto === window) { |
| 77 fitWidth = this.fitInto.innerWidth; |
| 78 } else { |
| 79 fitWidth = this.fitInto.getBoundingClientRect().width; |
| 80 } |
| 81 return fitWidth; |
| 82 }, |
| 83 |
| 84 get _fitHeight() { |
| 85 var fitHeight; |
| 86 if (this.fitInto === window) { |
| 87 fitHeight = this.fitInto.innerHeight; |
| 88 } else { |
| 89 fitHeight = this.fitInto.getBoundingClientRect().height; |
| 90 } |
| 91 return fitHeight; |
| 92 }, |
| 93 |
| 94 attached: function() { |
| 95 if (this.autoFitOnAttach) { |
| 96 if (window.getComputedStyle(this).display === 'none') { |
| 97 setTimeout(function() { |
| 98 this.fit(); |
| 99 }.bind(this)); |
| 100 } else { |
| 101 this.fit(); |
| 102 } |
| 103 } |
| 104 }, |
| 105 |
| 106 /** |
| 107 * Fits and optionally centers the element into the window, or `fitInfo` if
specified. |
| 108 */ |
| 109 fit: function() { |
| 110 this._discoverInfo(); |
| 111 this.constrain(); |
| 112 this.center(); |
| 113 }, |
| 114 |
| 115 /** |
| 116 * Memoize information needed to position and size the target element. |
| 117 */ |
| 118 _discoverInfo: function() { |
| 119 if (this._fitInfo) { |
| 120 return; |
| 121 } |
| 122 var target = window.getComputedStyle(this); |
| 123 var sizer = window.getComputedStyle(this.sizingTarget); |
| 124 this._fitInfo = { |
| 125 inlineStyle: { |
| 126 top: this.style.top || '', |
| 127 left: this.style.left || '' |
| 128 }, |
| 129 positionedBy: { |
| 130 vertically: target.top !== 'auto' ? 'top' : (target.bottom !== 'auto'
? |
| 131 'bottom' : null), |
| 132 horizontally: target.left !== 'auto' ? 'left' : (target.right !== 'aut
o' ? |
| 133 'right' : null), |
| 134 css: target.position |
| 135 }, |
| 136 sizedBy: { |
| 137 height: sizer.maxHeight !== 'none', |
| 138 width: sizer.maxWidth !== 'none' |
| 139 }, |
| 140 margin: { |
| 141 top: parseInt(target.marginTop, 10) || 0, |
| 142 right: parseInt(target.marginRight, 10) || 0, |
| 143 bottom: parseInt(target.marginBottom, 10) || 0, |
| 144 left: parseInt(target.marginLeft, 10) || 0 |
| 145 } |
| 146 }; |
| 147 }, |
| 148 |
| 149 /** |
| 150 * Resets the target element's position and size constraints, and clear |
| 151 * the memoized data. |
| 152 */ |
| 153 resetFit: function() { |
| 154 if (!this._fitInfo || !this._fitInfo.sizedBy.height) { |
| 155 this.sizingTarget.style.maxHeight = ''; |
| 156 this.style.top = this._fitInfo ? this._fitInfo.inlineStyle.top : ''; |
| 157 } |
| 158 if (!this._fitInfo || !this._fitInfo.sizedBy.width) { |
| 159 this.sizingTarget.style.maxWidth = ''; |
| 160 this.style.left = this._fitInfo ? this._fitInfo.inlineStyle.left : ''; |
| 161 } |
| 162 if (this._fitInfo) { |
| 163 this.style.position = this._fitInfo.positionedBy.css; |
| 164 } |
| 165 this._fitInfo = null; |
| 166 }, |
| 167 |
| 168 /** |
| 169 * Equivalent to calling `resetFit()` and `fit()`. Useful to call this after
the element, |
| 170 * the window, or the `fitInfo` element has been resized. |
| 171 */ |
| 172 refit: function() { |
| 173 this.resetFit(); |
| 174 this.fit(); |
| 175 }, |
| 176 |
| 177 /** |
| 178 * Constrains the size of the element to the window or `fitInfo` by setting
`max-height` |
| 179 * and/or `max-width`. |
| 180 */ |
| 181 constrain: function() { |
| 182 var info = this._fitInfo; |
| 183 // position at (0px, 0px) if not already positioned, so we can measure the
natural size. |
| 184 if (!this._fitInfo.positionedBy.vertically) { |
| 185 this.style.top = '0px'; |
| 186 } |
| 187 if (!this._fitInfo.positionedBy.horizontally) { |
| 188 this.style.left = '0px'; |
| 189 } |
| 190 // need border-box for margin/padding |
| 191 this.sizingTarget.style.boxSizing = 'border-box'; |
| 192 // constrain the width and height if not already set |
| 193 var rect = this.getBoundingClientRect(); |
| 194 if (!info.sizedBy.height) { |
| 195 this._sizeDimension(rect, info.positionedBy.vertically, 'top', 'bottom',
'Height'); |
| 196 } |
| 197 if (!info.sizedBy.width) { |
| 198 this._sizeDimension(rect, info.positionedBy.horizontally, 'left', 'right
', 'Width'); |
| 199 } |
| 200 }, |
| 201 |
| 202 _sizeDimension: function(rect, positionedBy, start, end, extent) { |
| 203 var info = this._fitInfo; |
| 204 var max = extent === 'Width' ? this._fitWidth : this._fitHeight; |
| 205 var flip = (positionedBy === end); |
| 206 var offset = flip ? max - rect[end] : rect[start]; |
| 207 var margin = info.margin[flip ? start : end]; |
| 208 var offsetExtent = 'offset' + extent; |
| 209 var sizingOffset = this[offsetExtent] - this.sizingTarget[offsetExtent]; |
| 210 this.sizingTarget.style['max' + extent] = (max - margin - offset - sizingO
ffset) + 'px'; |
| 211 }, |
| 212 |
| 213 /** |
| 214 * Centers horizontally and vertically if not already positioned. This also
sets |
| 215 * `position:fixed`. |
| 216 */ |
| 217 center: function() { |
| 218 if (!this._fitInfo.positionedBy.vertically || !this._fitInfo.positionedBy.
horizontally) { |
| 219 // need position:fixed to center |
| 220 this.style.position = 'fixed'; |
| 221 } |
| 222 if (!this._fitInfo.positionedBy.vertically) { |
| 223 var top = (this._fitHeight - this.offsetHeight) / 2; |
| 224 top -= this._fitInfo.margin.top; |
| 225 this.style.top = top + 'px'; |
| 226 } |
| 227 if (!this._fitInfo.positionedBy.horizontally) { |
| 228 var left = (this._fitWidth - this.offsetWidth) / 2; |
| 229 left -= this._fitInfo.margin.left; |
| 230 this.style.left = left + 'px'; |
| 231 } |
| 232 } |
| 233 |
| 234 }; |
| 235 |
| 236 </script> |
OLD | NEW |