From b9ebb11d481ed11002f3de48799f4ca31fe7093f Mon Sep 17 00:00:00 2001 From: rolux Date: Mon, 2 Apr 2012 17:41:28 +0200 Subject: [PATCH] add geo option to Ox.api (send combined area with totals) --- source/Ox/js/Array.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/source/Ox/js/Array.js b/source/Ox/js/Array.js index d6fa7f65..317d620d 100644 --- a/source/Ox/js/Array.js +++ b/source/Ox/js/Array.js @@ -8,6 +8,7 @@ Ox.api Turns an array into a list API items <[o]> An array of objects (key/value stores) options Options object enums Enumerables, for example {size: ['S', 'M', 'L', 'XL']} + geo If true, return combined area with totals sort <[o]|[s]> Default sort, for example ['+name', '-age'] sums <[s]> List of keys to be included in totals unique 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