tizen beta release
[platform/framework/web/web-ui-fw.git] / libs / js / jlayout / jlayout.border.js
1 /*!
2  * jLayout Border Layout - JavaScript Layout Algorithms v0.4
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.border = function (spec) {
13                 var my = {},
14                         that = {},
15                         east = spec.east,
16                         west = spec.west,
17                         north = spec.north,
18                         south = spec.south,
19                         center = spec.center;
20
21                 my.hgap = spec.hgap || 0;
22                 my.vgap = spec.vgap || 0;
23
24                 that.items = function () {
25                         var items = [];
26                         if (east) {
27                                 items.push(east);
28                         }
29
30                         if (west) {
31                                 items.push(west);
32                         }
33
34                         if (north) {
35                                 items.push(north);
36                         }
37
38                         if (south) {
39                                 items.push(south);
40                         }
41
42                         if (center) {
43                                 items.push(center);
44                         }
45                         return items;
46                 };              
47
48                 that.layout = function (container) {
49                         var size = container.bounds(),
50                                 insets = container.insets(),
51                                 top = insets.top,
52                                 bottom = size.height - insets.bottom,
53                                 left = insets.left,
54                                 right = size.width - insets.right,
55                                 tmp;
56
57                         if (north && north.isVisible()) {
58                                 tmp = north.preferredSize();
59                                 north.bounds({'x': left, 'y': top, 'width': right - left, 'height': tmp.height});
60                                 north.doLayout();
61 \r
62                                 top += tmp.height + my.vgap;
63                         }
64                         if (south && south.isVisible()) {
65                                 tmp = south.preferredSize();
66                                 south.bounds({'x': left, 'y': bottom - tmp.height, 'width': right - left, 'height': tmp.height});
67                                 south.doLayout();
68 \r
69                                 bottom -= tmp.height + my.vgap;
70                         }
71                         if (east && east.isVisible()) {
72                                 tmp = east.preferredSize();
73                                 east.bounds({'x': right - tmp.width, 'y': top, 'width': tmp.width, 'height': bottom - top});
74                                 east.doLayout();
75 \r
76                                 right -= tmp.width + my.hgap;
77                         }
78                         if (west && west.isVisible()) {
79                                 tmp = west.preferredSize();
80                                 west.bounds({'x': left, 'y': top, 'width': tmp.width, 'height': bottom - top});
81                                 west.doLayout();
82 \r
83                                 left += tmp.width + my.hgap;
84                         }
85                         if (center && center.isVisible()) {
86                                 center.bounds({'x': left, 'y': top, 'width': right - left, 'height': bottom - top});
87                                 center.doLayout();
88                         }\r
89                         return container;
90                 };
91
92                 function typeLayout(type) {
93                         return function (container) {
94                                 var insets = container.insets(),
95                                         width = 0,
96                                         height = 0,
97                                         type_size;
98
99                                 if (east && east.isVisible()) {
100                                         type_size = east[type + 'Size']();
101                                         width += type_size.width + my.hgap;
102                                         height = type_size.height;
103                                 }
104                                 if (west && west.isVisible()) {
105                                         type_size = west[type + 'Size']();
106                                         width += type_size.width + my.hgap;
107                                         height = Math.max(type_size.height, height);
108                                 }
109                                 if (center && center.isVisible()) {
110                                         type_size = center[type + 'Size']();
111                                         width += type_size.width;
112                                         height = Math.max(type_size.height, height);
113                                 }
114                                 if (north && north.isVisible()) {
115                                         type_size = north[type + 'Size']();
116                                         width = Math.max(type_size.width, width);
117                                         height += type_size.height + my.vgap;
118                                 }
119                                 if (south && south.isVisible()) {
120                                         type_size = south[type + 'Size']();
121                                         width = Math.max(type_size.width, width);
122                                         height += type_size.height + my.vgap;
123                                 }
124
125                                 return {
126                                         'width': width + insets.left + insets.right, 
127                                         'height': height + insets.top + insets.bottom
128                                 };
129                         };
130                 }
131                 that.preferred = typeLayout('preferred');
132                 that.minimum = typeLayout('minimum');
133                 that.maximum = typeLayout('maximum');
134                 return that;
135         };
136 })();