-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcolumn.js
67 lines (56 loc) · 1.94 KB
/
column.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
define(function(require, exports, module) {
function MosaicColumn( width, left ) {
this.left = left;
this.width = width;
this.tiles = [];
this.top = 0;
this.wastedAreas = [];
}
MosaicColumn.prototype.placeTile = function( tile, top ) {
this.tiles.push(tile);
if (top !== this.top) {
this.wastedAreas.push({top: this.top, height: top - this.top, tileAreaIsAbove: tile});
}
this.top = top + tile.height;
};
MosaicColumn.prototype.placeTileInWastedArea = function( tile, area ) {
if (tile.height > area.height) {
throw new Error( 'tried to place a tile in a place that is not big enough' );
}
var indexOfTileAfterThisOne = this.tiles.indexOf(area.tileAreaIsAbove);
this.tiles.splice(indexOfTileAfterThisOne, 0, tile);
area.height -= tile.height;
if (area.height <= 0) {
this.wastedAreas.splice(this.wastedAreas.indexOf(area)-1, 1);
}
};
MosaicColumn.prototype.findWastedSpacesAlignedWithArea = function( areaToMatch ) {
var matchingBottom = areaToMatch.top + areaToMatch.height;
return this.wastedAreas.filter(function( area ) {
var areaBottom = area.top + area.height;
var areaStartsAboveMatch = area.top <= areaToMatch.top;
var areaStartsInsideMatch = area.top > areaToMatch.top && area.top < matchingBottom;
var areaEndsBelowMatch = areaBottom >= matchingBottom;
var areaEndsInsideMatch = areaBottom > areaToMatch.top && areaBottom < matchingBottom;
return ( areaStartsInsideMatch || areaEndsInsideMatch || (areaStartsAboveMatch && areaEndsBelowMatch) );
});
}
MosaicColumn.prototype.reset = function() {
this.tiles.length = 0;
this.wastedAreas.length = 0;
this.top = 0;
}
exports.beget = function( width, left ) {
return new MosaicColumn( width, left );
};
exports.begetUniformColumns = function( count, width ) {
var columns = [];
var left = 0;
while( count ) {
columns.push( exports.beget( width, left ) );
left += width;
count -= 1;
}
return columns;
}
});