add geo option to Ox.api (send combined area with totals)

This commit is contained in:
rolux 2012-04-02 17:41:28 +02:00
parent 9a61fdbbab
commit b9ebb11d48

View file

@ -8,6 +8,7 @@ Ox.api <f> Turns an array into a list API
items <[o]> An array of objects (key/value stores)
options <o> Options object
enums <o> Enumerables, for example <code>{size: ['S', 'M', 'L', 'XL']}</code>
geo <b|false> If true, return combined area with totals
sort <[o]|[s]> Default sort, for example <code> ['+name', '-age']
sums <[s]> List of keys to be included in totals
unique <s|'id'> The name of the unique key
@ -116,6 +117,7 @@ Ox.api = function(items, options) {
var api = {
enums: options.enums ? parseEnums(options.enums) : {},
geo: options.geo,
sort: options.sort || [],
sums: options.sums || [],
unique: options.unique || 'id'
@ -244,11 +246,11 @@ Ox.api = function(items, options) {
sort[key] = function(value) {
return api.enums[key].indexOf(value.toLowerCase());
};
} else if (Ox.isArray(items[0][key])) {
} /*else if (Ox.isArray(items[0][key])) {
sort[key] = function(value) {
return value.join(', ');
};
}
}*/
});
result.data.items = Ox.sortBy(result.data.items, options.sort, sort);
}
@ -268,6 +270,20 @@ Ox.api = function(items, options) {
}));
})
data.items = result.data.items.length;
if (api.geo) {
data.area = Ox.joinAreas(result.data.items.map(function(item) {
return {
sw: {lat: item.south, lng: item.west},
ne: {lat: item.north, lng: item.east}
};
}));
data.area = {
south: data.area.sw.lat,
west: data.area.sw.lng,
north: data.area.ne.lat,
east: data.area.ne.lng
};
}
result.data = data;
} else {
// return items