Blame view

framework/admin/thirdparty/jlayout/lib/jlayout.flow.js 3.07 KB
385d70ca   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  /**
   * @preserve jLayout Flow Layout - JavaScript Layout Algorithms v0.12
   *
   * Licensed under the new BSD License.
   * Copyright 2008-2009, Bram Stein
   * All rights reserved.
   */
  /*global jLayout:true */
  (function () {
  	jLayout = (typeof jLayout === 'undefined') ? {} : jLayout;
  
  	jLayout.flow = function (options) {
  		var my = {},
  			that = {};
  
  		
  		my.hgap = typeof options.hgap === 'number' && !isNaN(options.hgap) ? options.hgap : 5;
  		my.vgap = typeof options.vgap === 'number' && !isNaN(options.vgap) ? options.vgap : 5;
  		my.items = options.items || [];
  		my.alignment = (options.alignment && (options.alignment === 'center' || options.alignment === 'right' || options.alignment === 'left') && options.alignment) || 'left';		
  
  		that.items = function () {
  			var r = [];
  			Array.prototype.push.apply(r, my.items);
  			return r;
  		};
  
  		function align(row, offset, rowSize, parentSize) {
  			var location = {
  					x: offset.x,
  					y: offset.y
  				},
  				i = 0,
  				len = row.length;
  
  			switch (my.alignment) {
  			case 'center':
  				location.x += (my.hgap + parentSize.width - rowSize.width) / 2;
  				break;
  			case 'right':
  				location.x += parentSize.width - rowSize.width + my.hgap;
  				break;
  			}
  
  			for (; i < len; i += 1) {
  				location.y = offset.y;
  				row[i].bounds(location);
  				row[i].doLayout();
  				location.x += row[i].bounds().width + my.hgap;
  			}
  		}
  
  		that.layout = function (container) {
  			var parentSize = container.bounds(),
  				insets = container.insets(),
  				i = 0,
  				len = my.items.length,
  				itemSize,
  				currentRow = [],
  				rowSize = {
  					width: 0,
  					height: 0
  				},
  				offset = {
  					x: insets.left,
  					y: insets.top
  				};
  
  			parentSize.width -= insets.left + insets.right;
  			parentSize.height -= insets.top + insets.bottom;
  
  			for (; i < len; i += 1) {
  				if (my.items[i].isVisible()) {
  					itemSize = my.items[i].preferredSize();
  					
  					if ((rowSize.width + itemSize.width) > parentSize.width) {
  						align(currentRow, offset, rowSize, parentSize);
  
  						currentRow = [];
  						offset.y += rowSize.height;
  						offset.x = insets.left;
  						rowSize.width = 0;
  						rowSize.height = 0;
  					}
  					rowSize.height = Math.max(rowSize.height, itemSize.height + my.vgap);
  					rowSize.width += itemSize.width + my.hgap;
  
  					currentRow.push(my.items[i]);
  				}
  			}
  			align(currentRow, offset, rowSize, parentSize);
  			return container;
  		};
  
  
  
  		function typeLayout(type) {
  			return function (container) {
  				var i = 0, 
  					width = 0, 
  					height = 0, 
  					typeSize,
  					firstComponent = false,
  					insets = container.insets();
  
  				for (; i < my.items.length; i += 1) {
  					if (my.items[i].isVisible()) {
  						typeSize = my.items[i][type + 'Size']();
  						height = Math.max(height, typeSize.height);
  						width += typeSize.width;
  					}
  				}
  
  				return {
  					'width': width + insets.left + insets.right + (my.items.length - 1) * my.hgap,
  					'height': height + insets.top + insets.bottom
  				};
  			};
  		}
  
  		that.preferred = typeLayout('preferred');
  		that.minimum = typeLayout('minimum');
  		that.maximum = typeLayout('maximum');		
  
  		return that;
  	};
  }());