4 Polymer('core-layout-grid', {
14 nodesChanged: function() {
18 layoutChanged: function() {
22 autoNodes: function() {
23 this.nodes = this.parentNode.children.array().filter(
25 switch(node.localName) {
26 case 'core-layout-grid':
35 invalidate: function() {
36 if (this.layout && this.layout.length) {
37 // job debounces layout, only letting it occur every N ms
38 this.layoutJob = this.job(this.layoutJob, this.relayout);
42 relayout: function() {
43 if (!this.nodes || this.auto) {
46 layout(this.layout, this.nodes);
47 this.asyncFire('core-layout');
56 function line(axis, p, d) {
57 var l = document.createElement('line');
58 var extent = (axis === 'left' ? 'width' :
59 (axis === 'top' ? 'height' : axis));
60 l.setAttribute('extent', extent);
62 axis = (axis === 'left' ? 'right' :
63 (axis === 'top' ? 'bottom' : axis));
66 l.style[axis] = p + 'px';
67 l.style[extent] = '0px';
68 lineParent.appendChild(l);
71 var colCount, colOwners, rowCount, rowOwners;
73 function matrixillate(matrix) {
74 // mesaure the matrix, must be rectangular
75 rowCount = matrix.length;
76 colCount = rowCount && matrix[0].length || 0;
79 for (var i=0; i<colCount; i++) {
81 for (var j=0; j<rowCount; j++) {
86 // assign sizing control
87 colOwners = findOwners(matrix);
88 rowOwners = findOwners(transpose);
89 //console.log('colOwners', colOwners);
90 //console.log('rowOwners', rowOwners);
93 function findOwners(matrix) {
94 var majCount = matrix.length;
95 var minCount = majCount && matrix[0].length || 0;
97 // for each column (e.g.)
98 for (var i=0; i<minCount; i++) {
99 // array of contained areas
101 // look at each row to find a containing area
102 for (var j=0; j<majCount; j++) {
103 // get the row vector
104 var vector = matrix[j]
105 // node index at [i,j]
106 var nodei = vector[i];
107 // if a node is there
109 // determine if it bounds this column
112 owns = (i === minCount-1) || (nodei !== vector[i+1]);
113 } else if (i === minCount - 1) {
114 owns = (i === 0) || (nodei !== vector[i-1]);
116 owns = nodei !== vector[i-1] && nodei !== vector[i+1];
119 contained[nodei] = 1;
122 // store the owners for this column
123 owners[i] = contained;
131 function colWidth(i) {
132 for (var col in colOwners[i]) {
137 var node = nodes[col - 1];
138 if (node.hasAttribute('h-flex') || node.hasAttribute('flex')) {
141 var w = node.offsetWidth;
142 //console.log('colWidth(' + i + ') ==', w);
148 function rowHeight(i) {
149 for (var row in rowOwners[i]) {
154 var node = nodes[row - 1];
155 if (node.hasAttribute('v-flex') || node.hasAttribute('flex')) {
158 var h = node.offsetHeight;
159 //console.log('rowHeight(' + i + ') ==', h);
167 function railize(count, sizeFn) {
169 // create rails for `count` tracks using
170 // sizing function `sizeFn(trackNo)`
172 // for n tracks there are (n+1) rails
174 // |track|track|track|
175 // 0|->sz0|->sz1|<-sz2|0
177 // |track|track|track|
180 // there can be one elastic track per set
182 // |track|track|track|track|
183 // 0|-->s0|-->s1|<--s1|<--s2|0
185 // sz1 spans multiple tracks which makes
186 // it elastic (it's underconstrained)
190 for (var i=0, x; i<count; i++) {
191 rails[i] = {p: a, s: 1};
192 x = sizeFn(i) + m + m;
199 rails[i] = {p: 0, s: -1};
202 for (var ii=count, x; ii>i; ii--) {
203 rails[ii] = {p: b, s: -1};
204 x = sizeFn(ii - 1) + m + m;
212 // TODO(sjmiles): this code tries to preserve actual position,
213 // so 'unposition' is really 'naturalize' or something
214 function unposition(box) {
215 var style = box.style;
216 //style.right = style.bottom = style.width = style.height = '';
217 style.position = 'absolute';
218 style.display = 'inline-block';
219 style.boxSizing = style.mozBoxSizing = 'border-box';
222 function _position(style, maj, min, ext, a, b) {
223 style[maj] = style[min] = '';
225 if (a.s < 0 && b.s < 0) {
226 var siz = a.p - b.p - m - m;
227 style[ext] = siz + 'px';
228 var c = 'calc(100% - ' + (b.p + siz + m) + 'px' + ')';
229 style[maj] = '-webkit-' + c;
231 } else if (b.s < 0) {
232 style[maj] = a.p + m + 'px';
233 style[min] = b.p + m + 'px';
235 style[maj] = a.p + m + 'px';
236 style[ext] = b.p - a.p - m - m + 'px';
240 function position(elt, left, right, top, bottom) {
241 _position(elt.style, 'top', 'bottom', 'height', rows[top],
243 _position(elt.style, 'left', 'right', 'width', columns[left],
247 function layout(matrix, anodes, alineParent) {
248 //console.group('layout');
250 lineParent = alineParent;
252 matrixillate(matrix);
254 nodes.forEach(unposition);
256 columns = railize(colCount, colWidth);
257 rows = railize(rowCount, rowHeight);
260 //console.group('column rails');
261 columns.forEach(function(c) {
262 //console.log(c.p, c.s);
263 line('left', c.p, c.s);
265 //console.groupEnd();
267 //console.group('row rails');
268 rows.forEach(function(r) {
269 //console.log(r.p, r.s);
270 line('top', r.p, r.s);
272 //console.groupEnd();
275 //console.group('rail boundaries');
276 nodes.forEach(function(node, i) {
277 // node indices are 1-based
280 var l, r, t = 1e10, b = -1e10;
281 matrix.forEach(function(vector, i) {
282 var f = vector.indexOf(n);
285 r = vector.lastIndexOf(n) + 1;
287 b = Math.max(b, i) + 1;
290 if (l == undefined) {
291 //console.log('unused');
292 node.style.position = 'absolute';
293 var offscreen = node.getAttribute('offscreen');
296 node.style.zIndex = 0;
300 node.style[offscreen] = node.offsetWidth * -2 + 'px';
303 node.style.left = node.offsetParent.offsetWidth
304 + node.offsetWidth + 'px';
307 node.style.top = node.parentNode.offsetHeight
308 + node.offsetHeight + 'px';
311 node.style[Math.random() >= 0.5 ? 'left' : 'top'] = '-110%';
313 //node.style.opacity = 0;
314 node.style.pointerEvents = 'none';
316 node.style.pointerEvents = '';
317 //node.style.opacity = '';
318 //console.log(l, r, t, b);
319 position(node, l, r, t, b);
322 //console.groupEnd();
323 //console.groupEnd();