OLD | NEW |
1 | 1 |
2 | 2 |
3 /** | 3 /** |
4 Polymer.IronFitBehavior fits an element in another element using `max-height` an
d `max-width`, and | 4 Polymer.IronFitBehavior fits an element in another element using `max-height` an
d `max-width`, and |
5 optionally centers it in the window or another element. | 5 optionally centers it in the window or another element. |
6 | 6 |
7 The element will only be sized and/or positioned if it has not already been size
d and/or positioned | 7 The element will only be sized and/or positioned if it has not already been size
d and/or positioned |
8 by CSS. | 8 by CSS. |
9 | 9 |
10 CSS properties | Action | 10 CSS properties | Action |
11 -----------------------------|------------------------------------------- | 11 -----------------------------|------------------------------------------- |
12 `position` set | Element is not centered horizontally or verticall
y | 12 `position` set | Element is not centered horizontally or verticall
y |
13 `top` or `bottom` set | Element is not vertically centered | 13 `top` or `bottom` set | Element is not vertically centered |
14 `left` or `right` set | Element is not horizontally centered | 14 `left` or `right` set | Element is not horizontally centered |
15 `max-height` or `height` set | Element respects `max-height` or `height` | 15 `max-height` or `height` set | Element respects `max-height` or `height` |
16 `max-width` or `width` set | Element respects `max-width` or `width` | 16 `max-width` or `width` set | Element respects `max-width` or `width` |
17 | 17 |
18 @demo demo/index.html | 18 @demo demo/index.html |
19 @polymerBehavior | 19 @polymerBehavior |
20 */ | 20 */ |
21 | 21 |
22 Polymer.IronFitBehavior = { | 22 Polymer.IronFitBehavior = { |
23 | 23 |
24 properties: { | 24 properties: { |
25 | 25 |
26 /** | 26 /** |
27 * The element that will receive a `max-height`/`width`. By default it is
the same as `this`, | 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 | 28 * but it can be set to a child element. This is useful, for example, for
implementing a |
29 * scrolling region inside the element. | 29 * scrolling region inside the element. |
| 30 * @type {!Element} |
30 */ | 31 */ |
31 sizingTarget: { | 32 sizingTarget: { |
32 type: Object, | 33 type: Object, |
33 value: function() { | 34 value: function() { |
34 return this; | 35 return this; |
35 } | 36 } |
36 }, | 37 }, |
37 | 38 |
38 /** | 39 /** |
39 * The element to fit `this` into. | 40 * The element to fit `this` into. |
40 */ | 41 */ |
41 fitInto: { | 42 fitInto: { |
42 type: Object, | 43 type: Object, |
43 value: window | 44 value: window |
44 }, | 45 }, |
45 | 46 |
46 /** | 47 /** |
47 * Set to true to auto-fit on attach. | 48 * Set to true to auto-fit on attach. |
48 */ | 49 */ |
49 autoFitOnAttach: { | 50 autoFitOnAttach: { |
50 type: Boolean, | 51 type: Boolean, |
51 value: false | 52 value: false |
52 }, | 53 }, |
53 | 54 |
| 55 /** @type {?Object} */ |
54 _fitInfo: { | 56 _fitInfo: { |
55 type: Object | 57 type: Object |
56 } | 58 } |
57 | 59 |
58 }, | 60 }, |
59 | 61 |
60 get _fitWidth() { | 62 get _fitWidth() { |
61 var fitWidth; | 63 var fitWidth; |
62 if (this.fitInto === window) { | 64 if (this.fitInto === window) { |
63 fitWidth = this.fitInto.innerWidth; | 65 fitWidth = this.fitInto.innerWidth; |
64 } else { | 66 } else { |
65 fitWidth = this.fitInto.getBoundingClientRect().width; | 67 fitWidth = this.fitInto.getBoundingClientRect().width; |
66 } | 68 } |
67 return fitWidth; | 69 return fitWidth; |
68 }, | 70 }, |
69 | 71 |
70 get _fitHeight() { | 72 get _fitHeight() { |
71 var fitHeight; | 73 var fitHeight; |
72 if (this.fitInto === window) { | 74 if (this.fitInto === window) { |
73 fitHeight = this.fitInto.innerHeight; | 75 fitHeight = this.fitInto.innerHeight; |
74 } else { | 76 } else { |
75 fitHeight = this.fitInto.getBoundingClientRect().height; | 77 fitHeight = this.fitInto.getBoundingClientRect().height; |
76 } | 78 } |
77 return fitHeight; | 79 return fitHeight; |
78 }, | 80 }, |
79 | 81 |
| 82 get _fitLeft() { |
| 83 var fitLeft; |
| 84 if (this.fitInto === window) { |
| 85 fitLeft = 0; |
| 86 } else { |
| 87 fitLeft = this.fitInto.getBoundingClientRect().left; |
| 88 } |
| 89 return fitLeft; |
| 90 }, |
| 91 |
| 92 get _fitTop() { |
| 93 var fitTop; |
| 94 if (this.fitInto === window) { |
| 95 fitTop = 0; |
| 96 } else { |
| 97 fitTop = this.fitInto.getBoundingClientRect().top; |
| 98 } |
| 99 return fitTop; |
| 100 }, |
| 101 |
80 attached: function() { | 102 attached: function() { |
81 if (this.autoFitOnAttach) { | 103 if (this.autoFitOnAttach) { |
82 if (window.getComputedStyle(this).display === 'none') { | 104 if (window.getComputedStyle(this).display === 'none') { |
83 setTimeout(function() { | 105 setTimeout(function() { |
84 this.fit(); | 106 this.fit(); |
85 }.bind(this)); | 107 }.bind(this)); |
86 } else { | 108 } else { |
87 this.fit(); | 109 this.fit(); |
88 } | 110 } |
89 } | 111 } |
(...skipping 11 matching lines...) Expand all Loading... |
101 /** | 123 /** |
102 * Memoize information needed to position and size the target element. | 124 * Memoize information needed to position and size the target element. |
103 */ | 125 */ |
104 _discoverInfo: function() { | 126 _discoverInfo: function() { |
105 if (this._fitInfo) { | 127 if (this._fitInfo) { |
106 return; | 128 return; |
107 } | 129 } |
108 var target = window.getComputedStyle(this); | 130 var target = window.getComputedStyle(this); |
109 var sizer = window.getComputedStyle(this.sizingTarget); | 131 var sizer = window.getComputedStyle(this.sizingTarget); |
110 this._fitInfo = { | 132 this._fitInfo = { |
| 133 inlineStyle: { |
| 134 top: this.style.top || '', |
| 135 left: this.style.left || '' |
| 136 }, |
111 positionedBy: { | 137 positionedBy: { |
112 vertically: target.top !== 'auto' ? 'top' : (target.bottom !== 'auto'
? | 138 vertically: target.top !== 'auto' ? 'top' : (target.bottom !== 'auto'
? |
113 'bottom' : null), | 139 'bottom' : null), |
114 horizontally: target.left !== 'auto' ? 'left' : (target.right !== 'aut
o' ? | 140 horizontally: target.left !== 'auto' ? 'left' : (target.right !== 'aut
o' ? |
115 'right' : null), | 141 'right' : null), |
116 css: target.position | 142 css: target.position |
117 }, | 143 }, |
118 sizedBy: { | 144 sizedBy: { |
119 height: sizer.maxHeight !== 'none', | 145 height: sizer.maxHeight !== 'none', |
120 width: sizer.maxWidth !== 'none' | 146 width: sizer.maxWidth !== 'none' |
121 }, | 147 }, |
122 margin: { | 148 margin: { |
123 top: parseInt(target.marginTop, 10) || 0, | 149 top: parseInt(target.marginTop, 10) || 0, |
124 right: parseInt(target.marginRight, 10) || 0, | 150 right: parseInt(target.marginRight, 10) || 0, |
125 bottom: parseInt(target.marginBottom, 10) || 0, | 151 bottom: parseInt(target.marginBottom, 10) || 0, |
126 left: parseInt(target.marginLeft, 10) || 0 | 152 left: parseInt(target.marginLeft, 10) || 0 |
127 } | 153 } |
128 }; | 154 }; |
129 }, | 155 }, |
130 | 156 |
131 /** | 157 /** |
132 * Resets the target element's position and size constraints, and clear | 158 * Resets the target element's position and size constraints, and clear |
133 * the memoized data. | 159 * the memoized data. |
134 */ | 160 */ |
135 resetFit: function() { | 161 resetFit: function() { |
136 if (!this._fitInfo || !this._fitInfo.sizedBy.height) { | 162 if (!this._fitInfo || !this._fitInfo.sizedBy.height) { |
137 this.sizingTarget.style.maxHeight = ''; | 163 this.sizingTarget.style.maxHeight = ''; |
138 this.style.top = ''; | 164 this.style.top = this._fitInfo ? this._fitInfo.inlineStyle.top : ''; |
139 } | 165 } |
140 if (!this._fitInfo || !this._fitInfo.sizedBy.width) { | 166 if (!this._fitInfo || !this._fitInfo.sizedBy.width) { |
141 this.sizingTarget.style.maxWidth = ''; | 167 this.sizingTarget.style.maxWidth = ''; |
142 this.style.left = ''; | 168 this.style.left = this._fitInfo ? this._fitInfo.inlineStyle.left : ''; |
143 } | 169 } |
144 if (this._fitInfo) { | 170 if (this._fitInfo) { |
145 this.style.position = this._fitInfo.positionedBy.css; | 171 this.style.position = this._fitInfo.positionedBy.css; |
146 } | 172 } |
147 this._fitInfo = null; | 173 this._fitInfo = null; |
148 }, | 174 }, |
149 | 175 |
150 /** | 176 /** |
151 * Equivalent to calling `resetFit()` and `fit()`. Useful to call this after
the element, | 177 * Equivalent to calling `resetFit()` and `fit()`. Useful to call this after
the element, |
152 * the window, or the `fitInfo` element has been resized. | 178 * the window, or the `fitInfo` element has been resized. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 /** | 221 /** |
196 * Centers horizontally and vertically if not already positioned. This also
sets | 222 * Centers horizontally and vertically if not already positioned. This also
sets |
197 * `position:fixed`. | 223 * `position:fixed`. |
198 */ | 224 */ |
199 center: function() { | 225 center: function() { |
200 if (!this._fitInfo.positionedBy.vertically || !this._fitInfo.positionedBy.
horizontally) { | 226 if (!this._fitInfo.positionedBy.vertically || !this._fitInfo.positionedBy.
horizontally) { |
201 // need position:fixed to center | 227 // need position:fixed to center |
202 this.style.position = 'fixed'; | 228 this.style.position = 'fixed'; |
203 } | 229 } |
204 if (!this._fitInfo.positionedBy.vertically) { | 230 if (!this._fitInfo.positionedBy.vertically) { |
205 var top = (this._fitHeight - this.offsetHeight) / 2; | 231 var top = (this._fitHeight - this.offsetHeight) / 2 + this._fitTop; |
206 top -= this._fitInfo.margin.top; | 232 top -= this._fitInfo.margin.top; |
207 this.style.top = top + 'px'; | 233 this.style.top = top + 'px'; |
208 } | 234 } |
209 if (!this._fitInfo.positionedBy.horizontally) { | 235 if (!this._fitInfo.positionedBy.horizontally) { |
210 var left = (this._fitWidth - this.offsetWidth) / 2; | 236 var left = (this._fitWidth - this.offsetWidth) / 2 + this._fitLeft; |
211 left -= this._fitInfo.margin.left; | 237 left -= this._fitInfo.margin.left; |
212 this.style.left = left + 'px'; | 238 this.style.left = left + 'px'; |
213 } | 239 } |
214 } | 240 } |
215 | 241 |
216 }; | 242 }; |
217 | 243 |
OLD | NEW |