-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlocationSelectors.js
70 lines (61 loc) · 2 KB
/
locationSelectors.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
define(function(require, exports, module) {
var selectors = {};
function getMinimum( array, prop ) {
var min = Infinity;
array.forEach(function(item) {
min = Math.min(item[prop], min);
});
return min;
}
selectors.weighted = function( jitter ) {
var wastedAreaJitter = jitter * jitter * 0.1;
var maxDistanceFromMinTop = jitter * 3;
var topSelector = selectors.top(jitter);
return function wastedArea( locations ) {
// filter out locations that waste too much space.
var currentWastedAreaJitter = wastedAreaJitter * locations[0].columns.length * 0.75;
var maxAllowedWastedArea = getMinimum(locations, 'wastedArea') + currentWastedAreaJitter;
var maxAllowedTop = getMinimum(locations, 'top') + maxDistanceFromMinTop;
console.log(maxDistanceFromMinTop);
var filteredLocations = locations.filter(function( location ) {
return ( location.wastedArea < maxAllowedWastedArea && location.top < maxAllowedTop );
});
if (filteredLocations.length === 0) {
filteredLocations = locations;
}
return topSelector(filteredLocations);
}
}
selectors.top = function( jitter ) {
jitter = jitter || 0;
return function top( locations ) {
var bestLocation = locations[0];
locations.forEach(function(location) {
var difference = bestLocation.top - location.top;
if (difference > jitter) {
bestLocation = location;
}
});
return bestLocation;
}
};
selectors.wastedArea = function( jitter ) {
jitter = jitter || 0;
return function wastedArea( locations ) {
var bestLocation = locations[0];
locations.forEach(function(location) {
var difference = bestLocation.wastedArea - location.wastedArea;
if (difference > jitter) {
bestLocation = location;
}
});
return bestLocation;
}
};
exports.beget = function( type, jitter ) {
if (!selectors[type]) {
throw new Error('No location selector for type "'+type+'". Avalable types are: '+Object.keys(selectors).join(', '));
}
return selectors[type]( jitter );
};
});