3 * jLayout Flex Grid Layout - JavaScript Layout Algorithms v0.4
4 * Based on: http://www.javaworld.com/javaworld/javatips/jw-javatip121.html
6 * Licensed under the new BSD License.
7 * Copyright 2008-2009, Bram Stein
12 jLayout = typeof jLayout === 'undefined' ? {} : jLayout;
14 // The flex grid has a dependency on the grid layout, so please make
15 // sure you include the grid layout manager before the flex grid
17 if (typeof jLayout.grid !== 'undefined') {
18 jLayout.flexGrid = function (spec) {
20 that = this.grid(spec, my);
22 function zeroArray(a, l) {
24 for (; i < l; i += 1) {
30 function typeLayout(type) {
31 return function (container) {
32 var i = 0, r = 0, c = 0, nw = 0, nh = 0,
33 w = zeroArray([], my.columns),
34 h = zeroArray([], my.rows),
36 insets = container.insets();
38 for (i = 0; i < my.items.length; i += 1) {
41 type_size = my.items[i][type + 'Size']();
42 if (w[c] < type_size.width) {
43 w[c] = type_size.width;
45 if (h[r] < type_size.height) {
46 h[r] = type_size.height;
49 for (i = 0; i < my.columns; i += 1) {
52 for (i = 0; i < my.rows; i += 1) {
56 width: insets.left + insets.right + nw + (my.columns - 1) * my.hgap,
57 height: insets.top + insets.bottom + nh + (my.rows - 1) * my.vgap
62 that.preferred = typeLayout('preferred');
63 that.minimum = typeLayout('minimum');
64 that.maximum = typeLayout('maximum');
66 that.layout = function (container) {
67 var i = 0, c = 0, r = 0,
68 pd = that.preferred(container),
69 sw = container.bounds().width / pd.width,
70 sh = container.bounds().height / pd.height,
71 w = zeroArray([], my.columns),
72 h = zeroArray([], my.rows),
73 insets = container.insets(),
78 for (i = 0; i < my.items.length; i += 1) {
81 d = my.items[i].preferredSize();
82 d.width = sw * d.width;
83 d.height = sh * d.height;
88 if (h[r] < d.height) {
93 for (c = 0; c < my.columns; c += 1) {
94 for (r = 0, y = insets.top; r < my.rows; r += 1) {
95 i = r * my.columns + c;
96 if (i < my.items.length) {
97 my.items[i].bounds({'x': x, 'y': y, 'width': w[c], 'height': h[r]});
98 my.items[i].doLayout();