This repository has been archived by the owner on May 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.compactWall.js
256 lines (237 loc) · 13.2 KB
/
jquery.compactWall.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
249
250
251
252
253
254
255
256
/**
* jQuery CompactWall v2.1.04
* A jQuery plugin to organizes blocks in a compact way.
* http://github.com/archiloque/compact-wall
*
* Licensed under the MIT license.
* Copyright 2012 Julien Kirch
*/
(function ($) {
$.fn.compactWall = function (blocks) {
/**
* Represents a position of block.
* @param containerWith the with of the container.
* @param positionedBlocks the already positioned blocks.
* @param slots the list of slots.
* @param height the current position's height.
*/
var BlocksPosition = function (positionedBlocks, slots, height, containerWith) {
this.positionedBlocks = positionedBlocks;
this.height = height;
this.slots = slots;
// slots represents position where blocks can be placed
// they are sorted from left to right
/**
* Add a block to this position
* @param block the block to add
* @maxHeight the max height we want to reach
* @param callback the callback to be called with the new positions.
*/
this.addBlock = function (block, maxHeight, callback) {
if (height < maxHeight) {
$.each(slots, function (slotIndex, slot) {
if (((slot.top + block.height) < maxHeight) &&
(slot.availableWidth >= block.width) &&
(slot.availableHeight >= block.height)) {
var positionedBlock = {
top: slot.top,
left: slot.left,
block: block
};
var newSlots = [];
if (slot.availableHeight == block.height) {
// the block has the same height than the slot
if (slot.availableWidth == block.width) {
// same height and same width: we kill the slot
newSlots = slots.slice(0, slotIndex)
.concat(slots.slice(slotIndex + 1));
} else {
// same height but narrower : we move the slot to the right
if ((slots.length > (slotIndex + 1)) &&
(slots[slotIndex + 1].left == (slot.left + block.width)) &&
(slots[slotIndex + 1].availableWidth == (slot.availableWidth - block.width))) {
// wannabe new position is aligned with the next slot
// this design could have been reached by another organisation
// but visually it is interesting
newSlots = slots.slice(0, slotIndex)
.concat([
{
top: slots[slotIndex + 1].top,
left: slots[slotIndex + 1].left,
availableWidth: slots[slotIndex + 1].availableWidth,
availableHeight: (slots[slotIndex + 1].availableHeight + block.height)
}
]).concat(slots.slice(slotIndex + 2));
} else {
newSlots = slots.slice(0, slotIndex)
.concat([
{
top: slot.top,
left: slot.left + block.width,
availableWidth: (slot.availableWidth - block.width),
availableHeight: block.height
}
]).concat(slots.slice(slotIndex + 1));
}
}
} else {
// the blocks is smaller
if (block.width == slot.availableWidth) {
// same width but smaller : we move the slot to the bottom
newSlots = slots.slice(0, slotIndex)
.concat([
{
top: slot.top + block.height,
left: slot.left,
availableWidth: slot.availableWidth,
availableHeight: (slot.availableHeight - block.height)
}
]).concat(slots.slice(slotIndex + 1));
} else {
// smaller width and smaller height: we move the slot to the bottom
// and add another slot for the upper right corner of the block
if ((slots.length > (slotIndex + 1)) &&
(slots[slotIndex + 1].left == (slot.left + block.width)) &&
(slots[slotIndex + 1].availableWidth == (slot.availableWidth - block.width))) {
// wannabe new slot is aligned with the next slot
// this design could have been reached by another organisation
// but visually it is interesting
if (slotIndex == 0) {
// the current slot is the first one
// move it to the bottom and increase height of next one
newSlots = [
{
top: slot.top + block.height,
left: 0,
availableWidth: slot.availableWidth,
availableHeight: Number.Infinity
},
{
top: slots[slotIndex + 1].top,
left: slots[slotIndex + 1].left,
availableWidth: slots[slotIndex + 1].availableWidth,
availableHeight: (slots[slotIndex + 1].availableHeight + block.height)
}
].concat(slots.slice(slotIndex + 2));
} else {
// current slot is not the first one
// increase the size of next one
newSlots = slots.slice(0, slotIndex)
.concat([
{
top: slots[slotIndex + 1].top,
left: slots[slotIndex + 1].left,
availableWidth: slots[slotIndex + 1].availableWidth,
availableHeight: (slots[slotIndex + 1].availableHeight + block.height)
}
]).concat(slots.slice(slotIndex + 2));
}
} else {
newSlots = slots.slice(0, slotIndex)
.concat([
{
top: slot.top + block.height,
left: slot.left,
availableWidth: slot.availableWidth,
availableHeight: (slot.availableHeight - block.height)
},
{
top: slot.top,
left: slot.left + block.width,
availableWidth: (slot.availableWidth - block.width),
availableHeight: block.height
}
]).concat(slots.slice(slotIndex + 1));
}
}
}
callback(new BlocksPosition(
positionedBlocks.concat([positionedBlock]),
newSlots,
Math.max(height, slot.top + block.height),
containerWith));
}
});
}
}
};
var bestFit = function (blocksList, containerWidth) {
var initialSlot = {
top: 0,
left: 0,
availableWidth: containerWidth,
availableHeight: Number.POSITIVE_INFINITY
};
var position = new BlocksPosition([], [initialSlot], 0, containerWidth);
return bestFitInternal(blocksList, position, Number.POSITIVE_INFINITY);
};
var bestFitInternal = function (remainingBlocks, position, maxHeight) {
if (remainingBlocks.length == 1) {
return bestFitLastBlock(remainingBlocks[0], position, maxHeight);
} else {
var bestPosition = null;
var minHeight = maxHeight;
$.each(remainingBlocks, function (index, block) {
var bestPositionCandidate = nextFitNextBlock(remainingBlocks, block, index, position, maxHeight);
if (bestPositionCandidate && (bestPositionCandidate.height < minHeight)) {
minHeight = bestPositionCandidate.height;
bestPosition = bestPositionCandidate;
}
});
return bestPosition;
}
};
var bestFitLastBlock = function (remainingBlock, position, maxHeight) {
var bestPosition = null;
var minHeight = maxHeight;
position.addBlock(remainingBlock, minHeight, function (newPosition) {
if (newPosition.height < minHeight) {
minHeight = newPosition.height;
bestPosition = newPosition;
}
});
return bestPosition;
};
var nextFitNextBlock = function (remainingBlocks, block, index, position, maxHeight) {
var bestPosition = null;
var minHeight = maxHeight;
position.addBlock(block, minHeight, function (newPosition) {
var bestPositionCandidate = bestFitInternal(
remainingBlocks.
slice(0, index).
concat(remainingBlocks.slice(index + 1)),
newPosition,
minHeight);
if (bestPositionCandidate && (bestPositionCandidate.height < minHeight)) {
minHeight = bestPositionCandidate.height;
bestPosition = bestPositionCandidate;
}
});
return bestPosition;
};
return this.each(function (i, container) {
// the block lists
// all have an height, a width, and a block
var blockList = $.map(blocks, function (block) {
var blockJ = $(block);
return {
width: blockJ.outerWidth(true),
height: blockJ.outerHeight(true),
block: block
};
});
var position = bestFit(blockList, $(container).innerWidth());
if (position) {
$.each(position.positionedBlocks, function (index, positionedBlock) {
$(positionedBlock.block.block).
css('position', 'absolute').
css('top', positionedBlock.top).
css('left', positionedBlock.left);
});
$(container).
css('position', 'relative').
css('height', position.height);
}
});
};
})(jQuery);