tizen beta release
[framework/web/web-ui-fw.git] / libs / js / jlayout / jlayout.flow.js
1 /*!
2  * jLayout Flow Layout - JavaScript Layout Algorithms v0.12
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.flow = function (options) {
13                 var my = {},
14                         that = {};
15
16                 
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';         
21
22                 that.items = function () {
23                         var r = [];
24                         Array.prototype.push.apply(r, my.items);
25                         return r;
26                 };
27
28                 that.layout = function (container) {
29                         var parentSize = container.bounds(),
30                                 insets = container.insets(),
31                                 i = 0,
32                                 len = my.items.length,
33                                 itemSize,
34                                 currentRow = [],
35                                 rowSize = {
36                                         width: 0,
37                                         height: 0
38                                 },
39                                 offset = {
40                                         x: insets.left,
41                                         y: insets.top
42                                 };
43
44                         parentSize.width -= insets.left + insets.right;
45                         parentSize.height -= insets.top + insets.bottom;
46
47                         for (; i < len; i += 1) {
48                                 if (my.items[i].isVisible()) {
49                                         itemSize = my.items[i].preferredSize();
50                                         
51                                         if ((rowSize.width + itemSize.width) > parentSize.width) {
52                                                 align(currentRow, offset, rowSize, parentSize);
53
54                                                 currentRow = [];
55                                                 offset.y += rowSize.height;
56                                                 offset.x = insets.left;
57                                                 rowSize.width = 0;\r
58                                                 rowSize.height = 0;
59                                         }
60                                         rowSize.height = Math.max(rowSize.height, itemSize.height + my.vgap);
61                                         rowSize.width += itemSize.width + my.hgap;
62
63                                         currentRow.push(my.items[i]);
64                                 }
65                         }
66                         align(currentRow, offset, rowSize, parentSize);
67                         return container;
68                 };
69
70                 function align(row, offset, rowSize, parentSize) {
71                         var location = {
72                                         x: offset.x,
73                                         y: offset.y
74                                 },
75                                 i = 0,
76                                 len = row.length;
77
78                         switch (my.alignment) {
79                                 case 'center': {
80                                         location.x += (my.hgap + parentSize.width - rowSize.width) / 2;
81                                         break;
82                                 }
83                                 case 'right': {
84                                         location.x += parentSize.width - rowSize.width + my.hgap;
85                                         break;
86                                 }
87                         }
88
89                         for (; i < len; i += 1) {
90                                 location.y = offset.y;
91                                 row[i].bounds(location);
92                                 row[i].doLayout();
93                                 location.x += row[i].bounds().width + my.hgap;
94                         }
95                 }
96
97                 function typeLayout(type) {
98                         return function (container) {
99                                 var i = 0, 
100                                         width = 0, 
101                                         height = 0, 
102                                         typeSize,
103                                         firstComponent = false,
104                                         insets = container.insets();
105
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;
111                                         }
112                                 }\r
113
114                                 return {
115                                         'width': width + insets.left + insets.right + (my.items.length - 1) * my.hgap,
116                                         'height': height + insets.top + insets.bottom
117                                 };
118                         };
119                 }
120
121                 that.preferred = typeLayout('preferred');
122                 that.minimum = typeLayout('minimum');
123                 that.maximum = typeLayout('maximum');           
124
125                 return that;
126         };
127 })();