e753ab91e5afac9c33183e0249f660120287ea0d
[framework/web/web-ui-fw.git] / libs / js / jlayout / jquery.jlayout.js
1
2 /*!
3  * jLayout JQuery Plugin v0.17
4  *
5  * Licensed under the new BSD License.
6  * Copyright 2008-2009 Bram Stein
7  * All rights reserved.
8  */
9 /*global jQuery jLayout*/
10 if (jQuery && jLayout) {
11         (function ($) {
12                 /**
13                  * This wraps jQuery objects in another object that supplies
14                  * the methods required for the layout algorithms.
15                  */
16                 function wrap(item, resize) {
17                         var that = {};
18
19                         $.each(['min', 'max'], function (i, name) {
20                                 that[name + 'imumSize'] = function (value) {
21                     var l = item.data('jlayout');
22                     
23                                         if (l) {
24                                                 return l[name + 'imum'](that);
25                                         } else {
26                                                 return item[name + 'Size'](value);
27                                         }
28                                 };
29                         });
30
31                         $.extend(that, {
32                                 doLayout: function () {
33                     var l = item.data('jlayout');
34                     
35                                         if (l) {
36                         l.layout(that);
37                                         }
38                                         item.css({position: 'absolute'});
39                                 },
40                                 isVisible: function () {
41                                         return item.isVisible();
42                                 },
43                                 insets: function () {
44                                         var p = item.padding(),
45                                                 b = item.border();
46
47                                         return {
48                         'top': p.top, 
49                                                 'bottom': p.bottom + b.bottom + b.top, 
50                                                 'left': p.left, 
51                                                 'right': p.right + b.right + b.left
52                     };
53                                 },
54                                 bounds: function (value) {
55                                         var tmp = {};
56
57                                         if (value) {
58                                                 if (typeof value.x === 'number') {
59                                                         tmp.left = value.x;
60                                                 }
61                                                 if (typeof value.y === 'number') {
62                                                         tmp.top = value.y;
63                                                 }
64                                                 if (typeof value.width === 'number') {
65                                                         tmp.width = (value.width - (item.outerWidth(true) - item.width()));
66                                                         tmp.width = (tmp.width >= 0) ? tmp.width : 0;
67                                                 }
68                                                 if (typeof value.height === 'number') {
69                                                         tmp.height = value.height - (item.outerHeight(true) - item.height());
70                                                         tmp.height = (tmp.height >= 0) ? tmp.height : 0;
71                                                 }
72                                                 item.css(tmp);
73                                                 return item;
74                                         } else {
75                                                 tmp = item.position();
76                                                 return {
77                                 'x': tmp.left,
78                                 'y': tmp.top,
79                                                         'width': item.outerWidth(false),
80                                                         'height': item.outerHeight(false)
81                         };
82                                         }
83                                 },
84                                 preferredSize: function () {
85                                         var minSize,
86                                                 maxSize,
87                                                 margin = item.margin(),
88                                                 size = {width: 0, height: 0},
89                         l = item.data('jlayout');
90
91                                         if (l && resize) {
92                                                 size = l.preferred(that);
93
94                                                 minSize = that.minimumSize();
95                                                 maxSize = that.maximumSize();
96
97                                                 size.width += margin.left + margin.right;
98                                                 size.height += margin.top + margin.bottom;
99
100                                                 if (size.width < minSize.width || size.height < minSize.height) {
101                                                         size.width = Math.max(size.width, minSize.width);
102                                                         size.height = Math.max(size.height, minSize.height);
103                                                 } else if (size.width > maxSize.width || size.height > maxSize.height) {
104                                                         size.width = Math.min(size.width, maxSize.width);
105                                                         size.height = Math.min(size.height, maxSize.height);
106                                                 }
107                                         } else {
108                         size = that.bounds();
109                                                 size.width += margin.left + margin.right;
110                                                 size.height += margin.top + margin.bottom;
111                                         }
112                                         return size;
113                                 }
114                         });
115                         return that;
116                 }
117
118                 $.fn.layout = function (options) {
119                         var opts = $.extend({}, $.fn.layout.defaults, options);
120                         return $.each(this, function () {
121                                 var element = $(this),
122                                         o = $.metadata && element.metadata().layout ? $.extend(opts, element.metadata().layout) : opts,
123                                         elementWrapper = wrap(element, o.resize);
124
125                                 if (o.type === 'border' && typeof jLayout.border !== 'undefined') {                
126                                         $.each(['north', 'south', 'west', 'east', 'center'], function (i, name) {
127                                                 if (element.children().hasClass(name)) {
128                                                         o[name] = wrap(element.children('.' + name + ':first'));
129                                                 }
130                                         });
131                                         element.data('jlayout', jLayout.border(o));
132                                 } else if (o.type === 'grid' && typeof jLayout.grid !== 'undefined') {
133                                         o.items = [];
134                                         element.children().each(function (i) {
135                                                 if (!$(this).hasClass('ui-resizable-handle')) {
136                                                         o.items[i] = wrap($(this));
137                                                 }
138                                         });
139                                         element.data('jlayout', jLayout.grid(o));
140                                 } else if (o.type === 'flexGrid' && typeof jLayout.flexGrid !== 'undefined') {
141                                         o.items = [];
142                                         element.children().each(function (i) {
143                                                 if (!$(this).hasClass('ui-resizable-handle')) {
144                                                         o.items[i] = wrap($(this));
145                                                 }
146                                         });
147                                         element.data('jlayout', jLayout.flexGrid(o));
148                                 } else if (o.type === 'column' && typeof jLayout.column !== 'undefined') {
149                                         o.items = [];
150                                         element.children().each(function (i) {
151                                                 if (!$(this).hasClass('ui-resizable-handle')) {
152                                                         o.items[i] = wrap($(this));
153                                                 }
154                                         });
155                                         element.data('jlayout', jLayout.column(o));
156                                 } else if (o.type === 'flow' && typeof jLayout.flow !== 'undefined') {
157                                         o.items = [];
158                                         element.children().each(function (i) {
159                                                 if (!$(this).hasClass('ui-resizable-handle')) {
160                                                         o.items[i] = wrap($(this));
161                                                 }
162                                         });
163                                         element.data('jlayout', jLayout.flow(o));                                       
164                                 }
165                 
166                                 if (o.resize) {
167                                         elementWrapper.bounds(elementWrapper.preferredSize());
168                                 }
169                 
170                                 elementWrapper.doLayout();
171                                 element.css({position: 'relative'});
172                                 if ($.ui !== undefined) {
173                                         element.addClass('ui-widget');
174                                 }
175                         });
176                 };
177
178                 $.fn.layout.defaults = {
179                         resize: true,
180                         type: 'grid'
181                 };
182         })(jQuery);
183 }