Merge pull request #1950 from systemed/name-suggest

Autocomplete on name=*
This commit is contained in:
John Firebaugh
2013-11-04 13:26:35 -08:00
13 changed files with 4661 additions and 12 deletions

View File

@@ -88,6 +88,10 @@ translations:
imagery:
npm install editor-imagery-index@git://github.com/osmlab/editor-imagery-index.git#gh-pages && node data/update_imagery
suggestions:
npm install name-suggestion-index@git://github.com/osmlab/name-suggestion-index.git
cp node_modules/name-suggestion-index/name-suggestions.json data/name-suggestions.json
SPRITE = inkscape --export-area-page
dist/img/line-presets.png: svg/line-presets.svg

View File

@@ -139,5 +139,6 @@ fs.writeFileSync('data/data.js', 'iD.data = ' + stringify({
featureIcons: r('feature-icons.json'),
operations: r('operations-sprite.json'),
locales: r('locales.json'),
en: read('dist/locales/en.json')
en: read('dist/locales/en.json'),
suggestions: r('name-suggestions.json')
}) + ';');

View File

@@ -17,7 +17,8 @@ iD.data = {
path + 'data/feature-icons.json',
path + 'data/operations-sprite.json',
path + 'data/locales.json',
path + 'dist/locales/en.json'
path + 'dist/locales/en.json',
path + 'data/name-suggestions.json'
], d3.json, function (err, data) {
iD.data = {
@@ -35,7 +36,8 @@ iD.data = {
featureIcons: data[9],
operations: data[10],
locales: data[11],
en: data[12]
en: data[12],
suggestions: data[13]
};
callback();

4565
data/name-suggestions.json Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -38,6 +38,7 @@
<script src='js/id/util.js'></script>
<script src='js/id/util/session_mutex.js'></script>
<script src='js/id/util/suggest_names.js'></script>
<script src='js/id/services/taginfo.js'></script>
<script src='js/id/services/wikipedia.js'></script>

View File

@@ -12,11 +12,7 @@ iD.ui.preset = function(context) {
field.input = iD.ui.preset[field.type](field, context)
.on('change', event.change);
if (field.type === 'address' ||
field.type === 'wikipedia' ||
field.type === 'maxspeed') {
field.input.entity(entity);
}
if (field.input.entity) field.input.entity(entity);
field.keys = field.keys || [field.key];

View File

@@ -1,8 +1,9 @@
iD.ui.preset.localized = function(field) {
iD.ui.preset.localized = function(field, context) {
var event = d3.dispatch('change'),
wikipedia = iD.wikipedia(),
input, localizedInputs, wikiTitles;
input, localizedInputs, wikiTitles,
entity;
function i(selection) {
input = selection.selectAll('.localized-main')
@@ -18,6 +19,13 @@ iD.ui.preset.localized = function(field) {
.on('blur', change)
.on('change', change);
if (field.id === 'name') {
var preset = context.presets().match(entity, context.graph());
input.call(d3.combobox().fetcher(
iD.util.SuggestNames(preset, iD.data.suggestions)
));
}
var translateButton = selection.selectAll('.localized-add')
.data([0]);
@@ -216,5 +224,9 @@ iD.ui.preset.localized = function(field) {
input.node().focus();
};
i.entity = function(_) {
entity = _;
};
return d3.rebind(i, event, 'on');
};

View File

@@ -0,0 +1,28 @@
iD.util.SuggestNames = function(preset, suggestions) {
preset = preset.id.split('/', 2);
var k = preset[0],
v = preset[1];
return function(value, callback) {
var result = [];
if (value && value.length > 2) {
if (suggestions[k] && suggestions[k][v]) {
for (var sugg in suggestions[k][v]) {
var dist = iD.util.editDistance(value, sugg.substring(0, value.length));
if (dist < 3) {
result.push({
title: sugg,
value: sugg,
dist: dist
});
}
}
}
result.sort(function(a, b) {
return a.dist - b.dist;
});
}
result = result.slice(0,3);
callback(result);
};
};

View File

@@ -36,7 +36,8 @@
"request": "~2.16.2",
"jsonschema": "~0.3.2",
"delve": "0.2",
"editor-imagery-index": "git://github.com/osmlab/editor-imagery-index.git#gh-pages"
"editor-imagery-index": "git://github.com/osmlab/editor-imagery-index.git#gh-pages",
"name-suggestion-index": "0.0.1"
},
"engines": {
"node": ">=0.10.0"

View File

@@ -192,6 +192,7 @@
<script src='../js/id/presets/field.js'></script>
<script src='../js/id/util/session_mutex.js'></script>
<script src='../js/id/util/suggest_names.js'></script>
<script src='../js/id/validate.js'></script>
@@ -273,6 +274,7 @@
<script src="spec/util.js"></script>
<script src='spec/util/session_mutex.js'></script>
<script src='spec/util/suggest_names.js'></script>
<script src="spec/behavior/hash.js"></script>
<script src="spec/behavior/hover.js"></script>

View File

@@ -91,6 +91,7 @@
<script src="spec/util.js"></script>
<script src='spec/util/session_mutex.js'></script>
<script src='spec/util/suggest_names.js'></script>
<script src="spec/behavior/hash.js"></script>
<script src="spec/behavior/hover.js"></script>

View File

@@ -3,7 +3,7 @@ describe('iD.ui.preset.localized', function() {
beforeEach(function() {
selection = d3.select(document.createElement('div'));
field = iD().presets().field('name');
field = iD.presets.Field('test', {key: 'name'});
});
it("adds a blank set of fields when the + button is clicked", function() {

View File

@@ -0,0 +1,36 @@
describe("iD.util.SuggestNames", function() {
var suggestions = {
'key': {
'value': {
'abcdef': {},
'ghijkl': {}
}
}
};
var preset = {
'id': 'key/value'
};
var a = iD.util.SuggestNames(preset, suggestions);
it('provides suggestions for an entered value', function(done) {
a('abcd', function(result) {
expect(result).to.eql([
{
title: 'abcdef',
value: 'abcdef',
dist: 0
}
]);
done();
});
});
it('provides no suggestions for short values', function(done){
a('ab', function(result) {
expect(result).to.eql([]);
done();
});
});
});