Skip to content

Commit

Permalink
New address-formats structure, WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
MKuranowski committed Dec 4, 2016
1 parent 955b8de commit 257b6bb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
15 changes: 11 additions & 4 deletions data/address-formats.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
},
{
"countryCodes": ["vn"],
"format": [["housenumber", "street"], ["subdistrict"], ["district"], ["city"], ["province", "postcode"]]
"format": [["housenumber", "street"], ["subdistrict"], ["district"], ["city"], ["province", "postcode"]],
"customPlaceholders": ["subdistrict", "district", "city"]
},
{
"countryCodes": ["us"],
"format": [["housenumber", "street"], ["city", "state", "postcode"]]
"format": [["housenumber", "street"], ["city", "state", "postcode"]],
"customPlaceholders": ["postcode"]
},
{
"countryCodes": ["ca"],
Expand All @@ -40,8 +42,13 @@
"format": [["postcode", "city", "district"], ["place", "street"], ["housenumber", "floor"]]
},
{
"countryCodes": ["jp"],
"format": [["postcode", "province", "district"], ["city", "suburb"], ["quarter", "neighbourhood"], ["block_number", "housenumber"]]
"countryCodes": ["jp"],
"format": [["postcode", "province", "district"], ["city", "suburb"], ["quarter", "neighbourhood"], ["block_number", "housenumber"]],
"customPlaceholders": ["province", "city", "suburb", "quarter", "neighbourhood", "block_number", "housenumber"],
"dropdowns": ["postcode", "province", "district", "city", "suburb", "quarter", "neighbourhood", "block_number"],
"placeKeys": {
"province": ["province"]
}
}
]
}
61 changes: 38 additions & 23 deletions modules/ui/fields/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export function uiFieldAddress(field, context) {
.sort(function(a, b) {
return a.dist - b.dist;
});

return _.uniqBy(streets, 'value');

function isAddressable(d) {
Expand All @@ -77,7 +76,6 @@ export function uiFieldAddress(field, context) {
.sort(function(a, b) {
return a.dist - b.dist;
});

return _.uniqBy(cities, 'value');


Expand Down Expand Up @@ -105,21 +103,42 @@ export function uiFieldAddress(field, context) {
box = geoExtent(l).padByMeters(200);

var results = context.intersects(box)
.filter(function hasTag(d) {
return d.tags[key];
})
.map(function(d) {
return {
.filter(function hasTag(d) { return d.tags[key]; })
.map(function(d) { return {
title: d.tags[key],
value: d.tags[key],
dist: geoSphericalDistance(d.extent(context.graph()).center(), l)
}; })
.sort(function(a, b) { return a.dist - b.dist; });
alert("Values: " + key + ": " + JSON.stringify(_.uniqBy(results, 'value')));
return _.uniqBy(results, 'value');
}

function getNearPlaces(addrKey, placeValues) {
var extent = entity.extent(context.graph()),
l = extent.center(),
box = geoExtent(l).padByMeters(200);

var places = context.intersects(box)
.filter(isAddressable)
.map(function(d) {
return {
title: d.tags['addr' + addrKey] || d.tags.name,
value: d.tags['addr' + addrKey] || d.tags.name,
dist: geoSphericalDistance(d.extent(context.graph()).center(), l)
};
})
.sort(function(a, b) {
return a.dist - b.dist;
});
alert("Places: " + addrKey + ": " + JSON.stringify(_.uniqBy(places, "value")));
return _.uniqBy(places, 'value');

return _.uniqBy(results, 'value');
function isAddressable(d) {
if (d.tags.place && d.tags.name && (placeValues.indexOf(d.tags.place) != -1)) { return true; }
else if (d.tags["addr" + addrKey]) { return true; }
else { return false; }
}
}


Expand All @@ -130,13 +149,6 @@ export function uiFieldAddress(field, context) {
return a && a.countryCodes && _.includes(a.countryCodes, countryCode);
}) || _.first(dataAddressFormats);

// Country specific addr:* placeholders.
var customPlaceholders = {
us: ["postcode"],
jp: ["province", "city", "suburb", "quarter", "neighbourhood", "block_number", "housenumber"],
vn: ["subdistrict", "district", "city"]
};

function row(r) {
// Normalize widths.
var total = _.reduce(r, function(sum, field) {
Expand All @@ -162,23 +174,26 @@ export function uiFieldAddress(field, context) {
.append('input')
.property('type', 'text')
.attr('placeholder', function (d) {
var countryInserter = ""
if (customPlaceholders[countryCode].indexOf(d.id) != -1) { countryInserter = "!" + countryCode; }
var countryInserter = "";
if (addressFormat.customPlaceholders.indexOf(d.id) != -1) { countryInserter = "!" + countryCode; }
return field.t('placeholders.' + d.id + countryInserter); })
.attr('class', function (d) { return 'addr-' + d.id; })
.style('width', function (d) { return d.width * 100 + '%'; });

// Update
// setup dropdowns for common address tags
var addrTags = [
if (typeof addressFormat.dropdowns == "object") { var addrTags = addressFormat.dropdowns; }
else { var addrTags = [
'street', 'city', 'state', 'province', 'district',
'subdistrict', 'suburb', 'place', 'postcode'
];
'subdistrict', 'suburb', 'place', 'postcode']; }

alert("placeKeys: " + JSON.stringify(addressFormat.placeKeys))

addrTags.forEach(function(tag) {
var nearValues = (tag === 'street') ? getNearStreets
: (tag === 'city') ? getNearCities
: getNearValues;
var nearValues = (typeof addressFormat.placeKeys.tag == "object") ? getNearPlaces(tag, addressFormat.placeKeys.tag)
: (tag === "street") ? getNearStreets()
: (tag === "city") ? getNearCities()
: getNearValues(tag);

wrap.selectAll('.addr-' + tag)
.call(d3combobox()
Expand Down

0 comments on commit 257b6bb

Please sign in to comment.