Export 0.1.45
[framework/web/web-ui-fw.git] / libs / js / jlayout / jlayout.grid.js
1 /*!
2  * jLayout Grid Layout - JavaScript Layout Algorithms v0.41
3  *
4  * Licensed under the new BSD License.
5  * Copyright 2008-2009, Bram Stein
6  * All rights reserved.
7  */
8 /*global jLayout */
9 (function () {
10         jLayout = typeof jLayout === 'undefined' ? {} : jLayout;
11
12         jLayout.grid = function (spec, shared) {
13                 var my = shared || {},
14                         that = {};
15
16                 my.hgap = spec.hgap || 0;
17                 my.vgap = spec.vgap || 0;
18
19                 // initialize the number of columns to the number of items
20                 // we're laying out.
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';
25
26                 if (my.rows > 0) {
27                         my.columns = Math.floor((my.items.length + my.rows - 1) / my.rows); 
28                 } else {
29                         my.rows = Math.floor((my.items.length + my.columns - 1) / my.columns);
30                 }
31         
32                 that.items = function () {
33                         var r = [];
34                         Array.prototype.push.apply(r, my.items);
35                         return r;
36                 };
37
38                 that.layout = function (container) {
39                         var i, j,
40                                 insets = container.insets(),
41                                 x = insets.left,
42                                 y = insets.top,
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;
45
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});
48
49                                 if (!my.fillVertical) {
50                                         if (j >= my.columns) {
51                                                 y += height + my.vgap;
52                                                 x = insets.left;
53                                                 j = 0;
54                                         }
55                                         else {
56                                                 x += width + my.hgap;
57                                         }
58                                 } else {
59                                         if (j >= my.rows) {
60                                                 x += width + my.hgap;
61                                                 y = insets.top;
62                                                 j = 0;
63                                         } else {
64                                                 y += height + my.vgap;
65                                         }
66                                 }
67                                 my.items[i].doLayout();
68                         }
69                         return container;
70                 };
71
72                 function typeLayout(type) {
73                         return function (container) {
74                                 var i = 0, 
75                                         width = 0, 
76                                         height = 0, 
77                                         type_size,
78                                         insets = container.insets();
79
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);
84                                 }
85                                 return {
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
88                                 };
89                         };
90                 }
91
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');
97                 return that;
98         };
99 })();