00d76b7123025aaf1a861fd04ba1bcd5eafae66d
[platform/framework/web/web-ui-fw.git] / libs / js / jlayout / jlayout.flexgrid.js
1
2 /*!
3  * jLayout Flex Grid Layout - JavaScript Layout Algorithms v0.4
4  * Based on: http://www.javaworld.com/javaworld/javatips/jw-javatip121.html
5  *
6  * Licensed under the new BSD License.
7  * Copyright 2008-2009, Bram Stein
8  * All rights reserved.
9  */
10 /*global jLayout */
11 (function () {
12         jLayout = typeof jLayout === 'undefined' ? {} : jLayout;
13
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
16         // layout manager.
17         if (typeof jLayout.grid !== 'undefined') {
18                 jLayout.flexGrid = function (spec) {
19                         var my = {},
20                                 that = this.grid(spec, my);
21
22                         function zeroArray(a, l) {
23                                 var i = 0;
24                                 for (; i < l; i += 1) {
25                                         a[i] = 0;
26                                 }
27                                 return a;
28                         }
29
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),
35                                                 type_size,
36                                                 insets = container.insets();
37                         
38                                         for (i = 0; i < my.items.length; i += 1) {
39                                                 r = i / my.columns;
40                                                 c = i % my.columns;
41                                                 type_size = my.items[i][type + 'Size']();
42                                                 if (w[c] < type_size.width) {
43                                                         w[c] = type_size.width;
44                                                 }
45                                                 if (h[r] < type_size.height) {
46                                                         h[r] = type_size.height;
47                                                 }
48                                         }
49                                         for (i = 0; i < my.columns; i += 1) {
50                                                 nw += w[i];
51                                         }
52                                         for (i = 0; i < my.rows; i += 1) {
53                                                 nh += h[i];
54                                         }
55                                         return {
56                                                 width: insets.left + insets.right + nw + (my.columns - 1) * my.hgap,
57                                                 height: insets.top + insets.bottom + nh + (my.rows - 1) * my.vgap
58                                         };
59                                 };
60                         }
61
62                         that.preferred = typeLayout('preferred');
63                         that.minimum = typeLayout('minimum');
64                         that.maximum = typeLayout('maximum');
65
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(),
74                                         x = insets.left,
75                                         y = insets.top,
76                                         d;
77
78                                 for (i = 0; i < my.items.length; i += 1) {
79                                         r = i / my.columns;
80                                         c = i % my.columns;
81                                         d = my.items[i].preferredSize();
82                                         d.width = sw * d.width;
83                                         d.height = sh * d.height;
84
85                                         if (w[c] < d.width) {
86                                                 w[c] = d.width;
87                                         }
88                                         if (h[r] < d.height) {
89                                                 h[r] = d.height;
90                                         }
91                                 }
92
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();
99                                                 }
100                                                 y += h[r] + my.vgap;
101                                         }
102                                         x += w[c] + my.hgap;
103                                 }
104
105                                 return container;
106                         };
107                         return that;
108                 };
109         }
110 })();