2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11 `<core-layout-trbl>` arranges nodes horizontally via absolute positioning.
12 Set the `vertical` attribute (boolean) to arrange vertically instead.
14 `<core-layout-trbl>` arranges it's <b>sibling elements</b>, not
17 One arranged node may be marked as elastic by giving it a `flex`
20 You may remove a node from layout by giving it a `nolayout`
25 `padding` is ignored on the parent node.
26 `margin` is ignored on arranged nodes.
27 `min-width` is ignored on arranged nodes, use `min-width` on the parent node
32 Arrange three `div` into columns. `flex` attribute on Column Two makes that
35 <core-layout-trbl></core-layout-trbl>
37 <div flex>Column Two</div>
38 <div>Column Three</div>
40 Remember that `<core-layout-trbl>` arranges it's sibling elements, not it's children.
42 If body has width 52 device pixels (in this case, ascii characters), call that 52px.
43 Column One has it's natural width of 12px (including border), Column Three has it's
44 natural width of 14px, body border uses 2px, and Column Two automatically uses the
45 remaining space: 24px.
48 ----------------------------------------------------
49 ||Column One|| Column Two ||Column Three||
50 ----------------------------------------------------
51 |- 12px -||- (24px) -|| 14px -|
53 As the parent node resizes, the elastic column reacts via CSS to adjust it's size.
54 No javascript is used during parent resizing, for best performance.
56 Changes in content or sibling size are not handled automatically.
58 ----------------------------------------------------------------
59 ||Column One| Column Two |Column Three||
60 ----------------------------------------------------------------
62 --------------------------------------
63 ||Column One|Column Two|Column Three||
64 --------------------------------------
66 Arrange in rows by adding the `vertical` attribute.
70 <core-layout-trbl vertical></core-layout-trbl>
72 <div flex>Row Two</div>
75 This setup will cause Row Two to stretch to fill the container.
77 ----------- -----------
78 |---------| |---------|
82 |---------| |---------|
91 ----------- |Row Three|
96 Layouts can be nested arbitrarily.
98 <core-layout-trbl></core-layout-trbl>
101 <core-layout-trbl vertical></core-layout-trbl>
108 Renders something like this
110 --------------------------------
112 || ||-----------------||
114 || ||-----------------||
117 || ||-----------------||
119 --------------------------------
121 @group Polymer Core Elements
122 @element core-layout-trbl
124 <link rel=
"import" href=
"../polymer/polymer.html">
126 <polymer-element name=
"core-layout-trbl" attributes=
"vertical">
140 Polymer('core-layout-trbl', {
145 this.setAttribute('nolayout', '');
148 attached: function() {
149 this.asyncMethod(function() {
155 prepare: function() {
156 var parent
= this.parentNode
.host
|| this.parentNode
;
157 // explicit position harmful on <body>
158 if (parent
.localName
!== 'body') {
160 var cs
= window
.getComputedStyle(parent
);
161 if (cs
.position
=== 'static') {
162 parent
.style
.position
= 'relative';
164 //parent.style.overflow = 'hidden';
166 // changes will cause another recalc at next validation step
167 var stylize
= this.stylize
, vertical
;
168 this.parentNode
.childNodes
.array().forEach(function(c
, i
) {
169 if (c
.nodeType
=== Node
.ELEMENT_NODE
&& !c
.hasAttribute('nolayout')) {
171 position
: 'absolute',
172 boxSizing
: 'border-box',
173 MozBoxSizing
: 'border-box',
175 // test for auto-vertical
176 if (vertical
=== undefined) {
177 vertical
= (c
.offsetWidth
== 0 && c
.offsetHeight
!== 0);
181 this.vertical
= this.vertical
|| vertical
;
185 * Arrange sibling nodes end-to-end in one dimension.
187 * Arrangement is horizontal unless the `vertical`
188 * attribute is applied on this node.
193 var parent
= this.parentNode
.host
|| this.parentNode
;
194 var vertical
= this.vertical
;
195 var ww
= 0, hh
= 0, pre
= [], fit
, post
= [];
197 // gather element information (at most one recalc)
198 this.parentNode
.childNodes
.array().forEach(function(c
, i
) {
199 if (c
.nodeType
===Node
.ELEMENT_NODE
&& !c
.hasAttribute('nolayout')) {
205 if (!c
.hasAttribute('fit') && !c
.hasAttribute('flex')) {
207 hh
+= c
.offsetHeight
;
216 // update layout styles (invalidate, no recalc)
218 var mxp
= 0, myp
= 0;
219 var stylize
= this.stylize
;
220 pre
.forEach(function(info
) {
222 stylize(info
.element
, {
223 top
: v
+ 'px', right
: mxp
, height
: info
.h
+ 'px', left
: mxp
226 stylize(info
.element
, {
227 top
: myp
, width
: info
.w
+ 'px', bottom
: myp
, left
: v
+ 'px'
230 v
+= vertical
? info
.h
: info
.w
;
235 top
: v
+ 'px', right
: mxp
, bottom
: hh
+ 'px', left
: mxp
239 top
: myp
, right
: ww
+ 'px', bottom
: myp
, left
: v
+ 'px'
242 v
= vertical
? hh
: ww
;
243 post
.forEach(function(info
) {
244 v
-= vertical
? info
.h
: info
.w
;
246 stylize(info
.element
, {
247 height
: info
.h
+ 'px', right
: mxp
, bottom
: v
+ 'px', left
: mxp
250 stylize(info
.element
, {
251 top
: myp
, right
: v
+ 'px', bottom
: myp
, width
: info
.w
+ 'px'
258 stylize: function(element
, styles
) {
259 var style
= element
.style
;
260 Object
.keys(styles
).forEach(function(k
){
261 style
[k
] = styles
[k
];