2 * jLayout Grid Layout - JavaScript Layout Algorithms v0.41
4 * Licensed under the new BSD License.
5 * Copyright 2008-2009, Bram Stein
10 jLayout = typeof jLayout === 'undefined' ? {} : jLayout;
12 jLayout.grid = function (spec, shared) {
13 var my = shared || {},
16 my.hgap = spec.hgap || 0;
17 my.vgap = spec.vgap || 0;
19 // initialize the number of columns to the number of items
21 my.items = spec.items || [];
22 my.columns = spec.columns || my.items.length;
23 my.rows = spec.rows || 0;
24 my.fillVertical = spec.fill && spec.fill === 'vertical';
27 my.columns = Math.floor((my.items.length + my.rows - 1) / my.rows);
29 my.rows = Math.floor((my.items.length + my.columns - 1) / my.columns);
32 that.items = function () {
34 Array.prototype.push.apply(r, my.items);
38 that.layout = function (container) {
40 insets = container.insets(),
43 width = (container.bounds().width - (insets.left + insets.right) - (my.columns - 1) * my.hgap) / my.columns,
44 height = (container.bounds().height - (insets.top + insets.bottom) - (my.rows - 1) * my.vgap) / my.rows;
46 for (i = 0, j = 1; i < my.items.length; i += 1, j += 1) {
47 my.items[i].bounds({'x': x, 'y': y, 'width': width, 'height': height});
49 if (!my.fillVertical) {
50 if (j >= my.columns) {
51 y += height + my.vgap;
64 y += height + my.vgap;
67 my.items[i].doLayout();
72 function typeLayout(type) {
73 return function (container) {
78 insets = container.insets();
80 for (; i < my.items.length; i += 1) {
81 type_size = my.items[i][type + 'Size']();
82 width = Math.max(width, type_size.width);
83 height = Math.max(height, type_size.height);
86 'width': insets.left + insets.right + my.columns * width + (my.columns - 1) * my.hgap,
87 'height': insets.top + insets.bottom + my.rows * height + (my.rows - 1) * my.vgap
92 // this creates the min and preferred size methods, as they
93 // only differ in the function they call.
94 that.preferred = typeLayout('preferred');
95 that.minimum = typeLayout('minimum');
96 that.maximum = typeLayout('maximum');