3 * jLayout JQuery Plugin v0.17
5 * Licensed under the new BSD License.
6 * Copyright 2008-2009 Bram Stein
9 /*global jQuery jLayout*/
10 if (jQuery && jLayout) {
13 * This wraps jQuery objects in another object that supplies
14 * the methods required for the layout algorithms.
16 function wrap(item, resize) {
19 $.each(['min', 'max'], function (i, name) {
20 that[name + 'imumSize'] = function (value) {
21 var l = item.data('jlayout');
24 return l[name + 'imum'](that);
26 return item[name + 'Size'](value);
32 doLayout: function () {
33 var l = item.data('jlayout');
38 item.css({position: 'absolute'});
40 isVisible: function () {
41 return item.isVisible();
44 var p = item.padding(),
49 'bottom': p.bottom + b.bottom + b.top,
51 'right': p.right + b.right + b.left
54 bounds: function (value) {
58 if (typeof value.x === 'number') {
61 if (typeof value.y === 'number') {
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;
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;
75 tmp = item.position();
79 'width': item.outerWidth(false),
80 'height': item.outerHeight(false)
84 preferredSize: function () {
87 margin = item.margin(),
88 size = {width: 0, height: 0},
89 l = item.data('jlayout');
92 size = l.preferred(that);
94 minSize = that.minimumSize();
95 maxSize = that.maximumSize();
97 size.width += margin.left + margin.right;
98 size.height += margin.top + margin.bottom;
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);
108 size = that.bounds();
109 size.width += margin.left + margin.right;
110 size.height += margin.top + margin.bottom;
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);
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'));
131 element.data('jlayout', jLayout.border(o));
132 } else if (o.type === 'grid' && typeof jLayout.grid !== 'undefined') {
134 element.children().each(function (i) {
135 if (!$(this).hasClass('ui-resizable-handle')) {
136 o.items[i] = wrap($(this));
139 element.data('jlayout', jLayout.grid(o));
140 } else if (o.type === 'flexGrid' && typeof jLayout.flexGrid !== 'undefined') {
142 element.children().each(function (i) {
143 if (!$(this).hasClass('ui-resizable-handle')) {
144 o.items[i] = wrap($(this));
147 element.data('jlayout', jLayout.flexGrid(o));
148 } else if (o.type === 'column' && typeof jLayout.column !== 'undefined') {
150 element.children().each(function (i) {
151 if (!$(this).hasClass('ui-resizable-handle')) {
152 o.items[i] = wrap($(this));
155 element.data('jlayout', jLayout.column(o));
156 } else if (o.type === 'flow' && typeof jLayout.flow !== 'undefined') {
158 element.children().each(function (i) {
159 if (!$(this).hasClass('ui-resizable-handle')) {
160 o.items[i] = wrap($(this));
163 element.data('jlayout', jLayout.flow(o));
167 elementWrapper.bounds(elementWrapper.preferredSize());
170 elementWrapper.doLayout();
171 element.css({position: 'relative'});
172 if ($.ui !== undefined) {
173 element.addClass('ui-widget');
178 $.fn.layout.defaults = {