-
Notifications
You must be signed in to change notification settings - Fork 4
/
bric.js
executable file
·248 lines (203 loc) · 8.58 KB
/
bric.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
var bricLayout = function(images, type) {
var count = images.length, box;
if (count >= 2) {
var images2 = [].concat(images);
var images1 = images2.splice(0, Math.floor(count / 2));
box = {
type: 'box',
box_type: type ? 'vertical' : 'horizontal',
pixelCheck: false,
leafs: {
one: bricLayout(images1, !type),
two: bricLayout(images2, !type)
}
};
box.leafs.one.parentBoxType = box.leafs.two.parentBoxType = box.box_type;
box.leafs.one.pixelCheck = false;
box.leafs.two.pixelCheck = true;
var dimensions;
if (type) {
dimensions = {
width: box.leafs.one.totalWidth
};
}else {
dimensions = {
height: box.leafs.one.totalHeight
};
}
box.leafs.two = bricLayoutScaleBox(box.leafs.two, dimensions);
if (type) {
// Horizontal contact; vertical box type.
box.totalHeight = box.leafs.one.totalHeight + box.leafs.two.totalHeight;
box.totalWidth = box.leafs.one.totalWidth;
}else {
// Vertical contact; horizontal box type.
box.totalWidth = box.leafs.one.totalWidth + box.leafs.two.totalWidth;
box.totalHeight = box.leafs.one.totalHeight;
}
box.leafs.one.parentTotalWidth = box.leafs.two.parentTotalWidth = box.totalWidth;
box.leafs.one.parentTotalHeight = box.leafs.two.parentTotalHeight = box.totalHeight;
box.leafs.one.siblingsTotalWidth = box.leafs.two.totalWidth;
box.leafs.one.siblingsTotalHeight = box.leafs.two.totalHeight;
box.leafs.two.siblingsTotalWidth = box.leafs.one.totalWidth;
box.leafs.two.siblingsTotalHeight = box.leafs.one.totalHeight;
}else if (count === 1) {
box = images.pop();
}
return box;
};
var bricLayoutScaleBox = function(box, dimensions) {
var dimensions1, dimensions2;
// If it is an image - just resize it (change dimensions).
if (box.type == 'image') {
if (dimensions.hasOwnProperty('width')) {
box.totalHeight = (dimensions.width / box.totalWidth) * box.totalHeight;
box.totalWidth = dimensions.width;
}
else if (dimensions.hasOwnProperty('height')) {
box.totalWidth = (dimensions.height / box.totalHeight) * box.totalWidth;
box.totalHeight = dimensions.height;
}
return box;
}
// If it is a box - then it should consist of two box elements;
// Determine sizes of elements and resize them.
if (dimensions.hasOwnProperty('width')) {
// Vertical box type; horizontal contact.
if (box.box_type === 'vertical') {
dimensions1 = dimensions2 = dimensions;
}
// Horizontal box type; vertical contact.
else if (box.box_type === 'horizontal') {
dimensions1 = {'width' : (box.leafs.one.totalWidth / (box.leafs.one.totalWidth + box.leafs.two.totalWidth)) * dimensions.width};
dimensions2 = {'width' : (box.leafs.two.totalWidth / (box.leafs.one.totalWidth + box.leafs.two.totalWidth)) * dimensions.width};
}
}
else if (dimensions.hasOwnProperty('height')) {
// Vertical box type; horizontal contact.
if (box.box_type === 'vertical') {
dimensions1 = {'height' : (box.leafs.one.totalHeight / (box.leafs.one.totalHeight + box.leafs.two.totalHeight)) * dimensions.height};
dimensions2 = {'height' : (box.leafs.two.totalHeight / (box.leafs.one.totalHeight + box.leafs.two.totalHeight)) * dimensions.height};
}
// Horizontal box type; vertical contact.
else if (box.box_type === 'horizontal') {
dimensions1 = dimensions2 = dimensions;
}
}
box.leafs.one = bricLayoutScaleBox(box.leafs.one, dimensions1);
box.leafs.two = bricLayoutScaleBox(box.leafs.two, dimensions2);
if (box.box_type === 'vertical') {
box.totalHeight = box.leafs.one.totalHeight + box.leafs.two.totalHeight;
box.totalWidth = box.leafs.one.totalWidth;
}
else if (box.box_type === 'horizontal') {
box.totalWidth = box.leafs.one.totalWidth + box.leafs.two.totalWidth;
box.totalHeight = box.leafs.one.totalHeight;
}
box.leafs.one.parentTotalWidth = box.leafs.two.parentTotalWidth = box.totalWidth;
box.leafs.one.parentTotalHeight = box.leafs.two.parentTotalHeight = box.totalHeight;
box.leafs.one.siblingsTotalWidth = box.leafs.two.totalWidth;
box.leafs.one.siblingsTotalHeight = box.leafs.two.totalHeight;
box.leafs.two.siblingsTotalWidth = box.leafs.one.totalWidth;
box.leafs.two.siblingsTotalHeight = box.leafs.one.totalHeight;
return box;
};
var bricLayoutCreateTree = function(box, options) {
var output = {};
if (box.hasOwnProperty('parentBoxType')) {
if (box.parentBoxType === 'vertical') {
box.totalWidth = box.parentTotalWidth;
} else if (box.parentBoxType === 'horizontal') {
box.totalHeight = box.parentTotalHeight;
}
}
if (box.pixelCheck) {
if (box.parentBoxType == 'vertical') {
box.totalHeight += Math.floor(box.parentTotalHeight) - Math.floor(box.totalHeight) - Math.floor(box.siblingsTotalHeight);
}
else if (box.parentBoxType == 'horizontal') {
box.totalWidth += Math.floor(box.parentTotalWidth) - Math.floor(box.totalWidth) - Math.floor(box.siblingsTotalWidth);
}
}
if (box.type === 'box') {
box.leafs.one.parentTotalHeight = box.leafs.two.parentTotalHeight = box.totalHeight;
box.leafs.one.parentTotalWidth = box.leafs.two.parentTotalWidth = box.totalWidth;
}
if (box.type === 'image') {
var image_width, image_height;
image_width = Math.floor(box.totalWidth - 2 * options.border - options.spacing);
image_height = Math.floor(box.totalHeight - 2 * options.border - options.spacing);
var imageWrapper = {
style: 'float:left;width:' + Math.floor(box.totalWidth - options.spacing) + 'px;height:' + Math.floor(box.totalHeight - options.spacing) + 'px;margin-top:' + options.spacing + 'px;margin-left:' + options.spacing + 'px;',
image: {
style: 'border:' + options.border + 'px solid ' + box.border_color || '',
width: Math.floor(box.totalWidth - options.spacing),
height: Math.floor(box.totalHeight - options.spacing),
image: box
}
};
// var image_wrapper = document.createElement("div");
// image_wrapper.style["float"] = "left";
// image_wrapper.style.width = Math.floor(box.totalWidth - options.spacing) + 'px';
// image_wrapper.style.height = Math.floor(box.totalHeight - options.spacing) + 'px';
// image_wrapper.style["margin-top"] = options.spacing + "px";
// image_wrapper.style["margin-left"] = options.spacing + "px";
// var image = document.createElement("img");
// image.src = box.render.image_link;
// image.style.border = options.border + "px solid " + box.render.border_color;
// image.width = image_width;
// image.height = image_height;
// image_wrapper.appendChild(image);
output = imageWrapper;
}else if (box.type == 'box') {
var newContainer = {
style: 'float:left;width: ' + Math.floor(box.totalWidth) + 'px;height:' + Math.floor(box.totalHeight) + 'px;',
children: [bricLayoutCreateTree(box.leafs.one, options), bricLayoutCreateTree(box.leafs.two, options)]
};
// var new_container = document.createElement("div");
// new_container.style["float"] = "left";
// new_container.style.width = Math.floor(box.totalWidth) + "px";
// new_container.style.height = Math.floor(box.totalHeight) + "px";
// var child1 = bricLayoutCreateTree(box.leafs.one);
// var child2 = bricLayoutCreateTree(box.leafs.two);
// new_container.appendChild(child1);
// new_container.appendChild(child2);
output = newContainer;
}
return output;
};
/**
images = [{
width:
height:
... Custom info
}]
options = {
width: int*,
height: int*,
spacing: int,
border: int,
layout: "horizontal" - default | "vertical"
}
* Mutual Exclusive
**/
var bricLayoutFitPics = function(images, options) {
options.border = options.border || 0;
options.spacing = options.spacing || 0;
var layout;
if(options.layout === "vertical"){
layout = false;
}else if(options.layout === "horizontal"){
layout = true;
}else{
layout = false;
}
for (var i = 0; i < images.length; i++) {
images[i].type = 'image';
images[i].totalWidth = images[i].width + 2 * options.spacing + 2 * options.border;
images[i].totalHeight = images[i].height + 2 * options.spacing + 2 * options.border;
}
var box = bricLayout(images, layout);
box = bricLayoutScaleBox(box, options);
return bricLayoutCreateTree(box, options);
};