2 * jLayout Flow Layout - JavaScript Layout Algorithms v0.12
4 * Licensed under the new BSD License.
5 * Copyright 2008-2009, Bram Stein
10 jLayout = typeof jLayout === 'undefined' ? {} : jLayout;
12 jLayout.flow = function (options) {
17 my.hgap = typeof options.hgap === 'number' && !isNaN(options.hgap) ? options.hgap : 5;
18 my.vgap = typeof options.vgap === 'number' && !isNaN(options.vgap) ? options.vgap : 5;
19 my.items = options.items || [];
20 my.alignment = (options.alignment && (options.alignment === 'center' || options.alignment === 'right' || options.alignment === 'left') && options.alignment) || 'left';
22 that.items = function () {
24 Array.prototype.push.apply(r, my.items);
28 that.layout = function (container) {
29 var parentSize = container.bounds(),
30 insets = container.insets(),
32 len = my.items.length,
44 parentSize.width -= insets.left + insets.right;
45 parentSize.height -= insets.top + insets.bottom;
47 for (; i < len; i += 1) {
48 if (my.items[i].isVisible()) {
49 itemSize = my.items[i].preferredSize();
51 if ((rowSize.width + itemSize.width) > parentSize.width) {
52 align(currentRow, offset, rowSize, parentSize);
55 offset.y += rowSize.height;
56 offset.x = insets.left;
60 rowSize.height = Math.max(rowSize.height, itemSize.height + my.vgap);
61 rowSize.width += itemSize.width + my.hgap;
63 currentRow.push(my.items[i]);
66 align(currentRow, offset, rowSize, parentSize);
70 function align(row, offset, rowSize, parentSize) {
78 switch (my.alignment) {
80 location.x += (my.hgap + parentSize.width - rowSize.width) / 2;
84 location.x += parentSize.width - rowSize.width + my.hgap;
89 for (; i < len; i += 1) {
90 location.y = offset.y;
91 row[i].bounds(location);
93 location.x += row[i].bounds().width + my.hgap;
97 function typeLayout(type) {
98 return function (container) {
103 firstComponent = false,
104 insets = container.insets();
106 for (; i < my.items.length; i += 1) {
107 if (my.items[i].isVisible()) {
108 typeSize = my.items[i][type + 'Size']();
109 height = Math.max(height, typeSize.height);
110 width += typeSize.width;
115 'width': width + insets.left + insets.right + (my.items.length - 1) * my.hgap,
116 'height': height + insets.top + insets.bottom
121 that.preferred = typeLayout('preferred');
122 that.minimum = typeLayout('minimum');
123 that.maximum = typeLayout('maximum');