mirror of
https://github.com/FoggedLens/iD.git
synced 2026-06-05 14:38:05 +02:00
Start reorganizing presets
This commit is contained in:
+10
-4
@@ -32,7 +32,6 @@
|
||||
<script src='js/id/id.js'></script>
|
||||
<script src='js/id/util.js'></script>
|
||||
<script src='js/id/oauth.js'></script>
|
||||
<script src='js/id/presetdata.js'></script>
|
||||
<script src='js/id/services/taginfo.js'></script>
|
||||
|
||||
<script src='data/data.js'></script>
|
||||
@@ -158,6 +157,10 @@
|
||||
<script src='js/id/core/way.js'></script>
|
||||
<script src='js/id/core/tree.js'></script>
|
||||
|
||||
<script src='js/id/presets.js'></script>
|
||||
<script src='js/id/presets/preset.js'></script>
|
||||
<script src='js/id/presets/presets.js'></script>
|
||||
|
||||
<script src='js/id/connection.js'></script>
|
||||
<script src='js/id/validate.js'></script>
|
||||
|
||||
@@ -186,12 +189,15 @@
|
||||
|
||||
var id = iD();
|
||||
|
||||
iD.util.asyncMap(['keys.json', 'presets/presets.json'], d3.json, function(err, data) {
|
||||
iD.util.asyncMap(['keys.json', 'presets/presets.json', 'presets/defaults.json'], d3.json, function(err, data) {
|
||||
id.connection()
|
||||
.keys(data[0]);
|
||||
|
||||
id.presetData()
|
||||
.data(data[1]);
|
||||
id.presets()
|
||||
.load({
|
||||
presets: data[1],
|
||||
defaults: data[2]
|
||||
});
|
||||
|
||||
d3.select("#iD")
|
||||
.call(id.ui());
|
||||
|
||||
+3
-3
@@ -96,10 +96,10 @@ window.iD = function () {
|
||||
context.zoomOut = map.zoomOut;
|
||||
|
||||
/* Presets */
|
||||
var presetData = iD.presetData();
|
||||
var presets = iD.presets(context);
|
||||
|
||||
context.presetData = function() {
|
||||
return presetData;
|
||||
context.presets = function() {
|
||||
return presets;
|
||||
};
|
||||
|
||||
context.container = function(_) {
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
iD.presetData = function() {
|
||||
|
||||
var other = {
|
||||
name: 'other',
|
||||
title: 'Other',
|
||||
icon: 'marker-stroked',
|
||||
match: {
|
||||
tags: {},
|
||||
type: ['node', 'line', 'area']
|
||||
},
|
||||
form: []
|
||||
};
|
||||
|
||||
var presets = {},
|
||||
data = [other],
|
||||
categories = [],
|
||||
defaults = {
|
||||
node: [],
|
||||
area: [],
|
||||
line: []
|
||||
};
|
||||
|
||||
function getPreset(name) {
|
||||
return _.find(data.concat(categories), function(d) {
|
||||
return d.name === name;
|
||||
});
|
||||
}
|
||||
|
||||
presets.data = function(_) {
|
||||
if (!arguments.length) return data;
|
||||
data = _.presets.concat([other]);
|
||||
categories = _.categories;
|
||||
defaults = _.defaults;
|
||||
return presets;
|
||||
};
|
||||
|
||||
presets.defaults = function(entity) {
|
||||
var type = entity.type === 'node' ? 'node' : entity.geometry();
|
||||
return defaults[type].map(getPreset);
|
||||
};
|
||||
|
||||
presets.categories = function(category) {
|
||||
if (!arguments.length) return categories;
|
||||
return _.find(categories, function(d) {
|
||||
return d.name === category;
|
||||
}).members.map(getPreset);
|
||||
};
|
||||
|
||||
presets.match = function(entity) {
|
||||
var type = entity.type === 'node' ? 'node' : entity.geometry();
|
||||
return data.concat(categories).filter(function(d) {
|
||||
return _.contains(d.match.type, type);
|
||||
});
|
||||
};
|
||||
|
||||
presets.matchTags = function(entity) {
|
||||
var tags, count, best,
|
||||
maxcount = -1;
|
||||
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
count = 0;
|
||||
tags = data[i].match.tags;
|
||||
|
||||
for (var k in tags) {
|
||||
if (entity.tags[k] === tags[k] || (tags[k] === '*' && k in entity.tags)) count++;
|
||||
else break;
|
||||
}
|
||||
|
||||
if (Object.keys(tags).length === count && count > maxcount) {
|
||||
best = data[i];
|
||||
maxcount = count;
|
||||
}
|
||||
}
|
||||
|
||||
return best;
|
||||
};
|
||||
|
||||
return presets;
|
||||
};
|
||||
@@ -0,0 +1,44 @@
|
||||
iD.presets = function(context) {
|
||||
|
||||
var other = {
|
||||
name: 'other',
|
||||
title: 'Other',
|
||||
icon: 'marker-stroked',
|
||||
match: {
|
||||
tags: {},
|
||||
type: ['point', 'vertex', 'line', 'area']
|
||||
},
|
||||
form: []
|
||||
},
|
||||
all = iD.presets.Collection(context, [iD.presets.Preset(other)]),
|
||||
defaults = {};
|
||||
|
||||
all.load = function(d) {
|
||||
if (d.presets) {
|
||||
d.presets.forEach(function(d) {
|
||||
all.collection.push(iD.presets.Preset(d));
|
||||
});
|
||||
}
|
||||
if (d.categories) {
|
||||
d.categories.forEach(function(d) {
|
||||
all.collection.push(iD.presets.Category(d, all));
|
||||
});
|
||||
}
|
||||
if (d.defaults) {
|
||||
var getItem = _.bind(all.item, all);
|
||||
defaults = {
|
||||
area: iD.presets.Collection(context, d.defaults.area.map(getItem)),
|
||||
line: iD.presets.Collection(context, d.defaults.line.map(getItem)),
|
||||
point: iD.presets.Collection(context, d.defaults.point.map(getItem)),
|
||||
vertex: iD.presets.Collection(context, d.defaults.vertex.map(getItem))
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
all.defaults = function(entity) {
|
||||
return defaults[entity.geometry(context.graph())];
|
||||
};
|
||||
|
||||
|
||||
return all;
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
iD.presets.Preset = function(preset) {
|
||||
|
||||
preset.matchType = function(entity, resolver) {
|
||||
return preset.match.type.indexOf(entity.geometry(resolver)) >= 0;
|
||||
};
|
||||
|
||||
preset.matchTags = function(entity) {
|
||||
var tags = preset.match.tags;
|
||||
for (var t in tags) {
|
||||
if (entity.tags[t] !== tags[t] &&
|
||||
(tags[t] !== '*' || t in entity.tags)) return -1;
|
||||
}
|
||||
return Object.keys(preset.match.tags).length;
|
||||
};
|
||||
|
||||
return preset;
|
||||
};
|
||||
@@ -0,0 +1,80 @@
|
||||
iD.presets.Collection = function(context, collection) {
|
||||
|
||||
var presets = {
|
||||
|
||||
collection: collection,
|
||||
|
||||
load: function(_) {
|
||||
if (_.presets) {
|
||||
_.presets.forEach(function(d) {
|
||||
collection.push(iD.presets.Preset(d));
|
||||
});
|
||||
}
|
||||
if (_.categories) {
|
||||
_.categories.forEach(function(d) {
|
||||
collection.push(iD.presets.Category(d, presets));
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
item: function(id) {
|
||||
return _.find(collection, function(d) {
|
||||
return d.name === id;
|
||||
});
|
||||
},
|
||||
|
||||
matchType: function(entity) {
|
||||
var newcollection = collection.filter(function(d) {
|
||||
return d.matchType(entity, context.graph());
|
||||
});
|
||||
|
||||
return iD.presets.Collection(context, newcollection);
|
||||
|
||||
},
|
||||
|
||||
matchTags: function(entity) {
|
||||
|
||||
var best = -1,
|
||||
match;
|
||||
|
||||
for (var i = 0; i < collection.length; i++) {
|
||||
var score = collection[i].matchTags(entity);
|
||||
if (score > best) {
|
||||
best = score;
|
||||
match = collection[i];
|
||||
}
|
||||
}
|
||||
|
||||
return match;
|
||||
},
|
||||
|
||||
search: function(value) {
|
||||
if (!value) return this;
|
||||
|
||||
value = value.toLowerCase();
|
||||
|
||||
// Uses levenshtein distance, with a couple of hacks
|
||||
// to prioritize exact substring matches
|
||||
return iD.presets.Collection(context, collection.sort(function(a, b) {
|
||||
var ia = a.name.indexOf(value) >= 0,
|
||||
ib = b.name.indexOf(value) >= 0;
|
||||
|
||||
if (ia && !ib) {
|
||||
return -1;
|
||||
} else if (ib && !ia) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return iD.util.editDistance(value, a.name) - iD.util.editDistance(value, b.name);
|
||||
}).filter(function(d) {
|
||||
return iD.util.editDistance(value, d.name) - d.name.length + value.length < 3 ||
|
||||
d.name === 'other';
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
return presets;
|
||||
};
|
||||
+13
-34
@@ -1,14 +1,14 @@
|
||||
iD.ui.PresetGrid = function(context) {
|
||||
var event = d3.dispatch('choose'),
|
||||
entity,
|
||||
presetData = context.presetData(),
|
||||
presets = context.presets(),
|
||||
taginfo = iD.taginfo();
|
||||
|
||||
function presetgrid(selection, preset) {
|
||||
|
||||
selection.html('');
|
||||
|
||||
var viable = presetData.match(entity);
|
||||
presets = presets.matchType(entity);
|
||||
|
||||
var messagewrap = selection.append('div')
|
||||
.attr('class', 'message inspector-inner fillL');
|
||||
@@ -21,7 +21,7 @@ iD.ui.PresetGrid = function(context) {
|
||||
|
||||
var grid = selection.append('div')
|
||||
.attr('class', 'preset-grid fillD inspector-body ' + entity.geometry(context.graph()))
|
||||
.call(drawGrid, filter(''));
|
||||
.call(drawGrid, context.presets().defaults(entity));
|
||||
|
||||
var search = searchwrap.append('input')
|
||||
.attr('class', 'preset-grid-search')
|
||||
@@ -31,11 +31,15 @@ iD.ui.PresetGrid = function(context) {
|
||||
if (d3.event.keyCode === 13) {
|
||||
choose(grid.selectAll('.grid-entry:first-child').datum());
|
||||
} else {
|
||||
var value = search.property('value'),
|
||||
presets = filter(value);
|
||||
message.text(t('inspector.results', {n: presets.length, search: value}));
|
||||
grid.call(drawGrid, presets);
|
||||
grid.classed('filtered', value.length);
|
||||
var value = search.property('value');
|
||||
if (value.length) {
|
||||
var results = presets.search(value);
|
||||
message.text(t('inspector.results', {n: results.length, search: value}));
|
||||
grid.call(drawGrid, results);
|
||||
grid.classed('filtered', value.length);
|
||||
} else {
|
||||
grid.call(drawGrid, context.presets().defaults(entity));
|
||||
}
|
||||
}
|
||||
});
|
||||
search.node().focus();
|
||||
@@ -46,31 +50,6 @@ iD.ui.PresetGrid = function(context) {
|
||||
.call(drawButtons);
|
||||
}
|
||||
|
||||
function filter(value) {
|
||||
if (!value) return presetData.defaults(entity);
|
||||
|
||||
value = value.toLowerCase();
|
||||
|
||||
// Uses levenshtein distance, with a couple of hacks
|
||||
// to prioritize exact substring matches
|
||||
return viable.sort(function(a, b) {
|
||||
var ia = a.name.indexOf(value) >= 0,
|
||||
ib = b.name.indexOf(value) >= 0;
|
||||
|
||||
if (ia && !ib) {
|
||||
return -1;
|
||||
} else if (ib && !ia) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return iD.util.editDistance(value, a.name) - iD.util.editDistance(value, b.name);
|
||||
}).filter(function(d) {
|
||||
return iD.util.editDistance(value, d.name) - d.name.length + value.length < 3 ||
|
||||
d.name === 'other';
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function choose(d) {
|
||||
// Category
|
||||
if (d.members) {
|
||||
@@ -90,7 +69,7 @@ iD.ui.PresetGrid = function(context) {
|
||||
|
||||
var entries = selection
|
||||
.selectAll('button.grid-entry')
|
||||
.data(presets.slice(0, 12), name);
|
||||
.data(presets.collection.slice(0, 12), name);
|
||||
|
||||
var entered = entries.enter()
|
||||
.append('button')
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
iD.ui.TagEditor = function(context) {
|
||||
var event = d3.dispatch('changeTags', 'choose', 'close'),
|
||||
presetData = context.presetData(),
|
||||
presets = context.presets(),
|
||||
entity,
|
||||
tags,
|
||||
name,
|
||||
@@ -44,7 +44,7 @@ iD.ui.TagEditor = function(context) {
|
||||
}
|
||||
}
|
||||
|
||||
presetMatch = preset || presetMatch || presetData.matchTags(entity);
|
||||
presetMatch = preset || presetMatch || presets.matchType(entity).matchTags(entity);
|
||||
|
||||
selection.html('');
|
||||
|
||||
@@ -163,7 +163,7 @@ iD.ui.TagEditor = function(context) {
|
||||
if (presetUI && tagList) {
|
||||
|
||||
// change preset if necessary (undos/redos)
|
||||
var newmatch = presetData.matchTags(entity.update({ tags: tags }));
|
||||
var newmatch = presets.matchType(entity).matchTags(entity.update({ tags: tags }));
|
||||
if (newmatch !== presetMatch) {
|
||||
return tageditor(selection_, newmatch);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"area": [
|
||||
"building",
|
||||
"park",
|
||||
"water",
|
||||
"parking",
|
||||
"hospital",
|
||||
"place of worship",
|
||||
"cafe",
|
||||
"restaurant",
|
||||
"bar",
|
||||
"fast food",
|
||||
"bank",
|
||||
"other"
|
||||
],
|
||||
"line": [
|
||||
"residential road",
|
||||
"primary road",
|
||||
"secondary road",
|
||||
"tertiary road",
|
||||
"foot path",
|
||||
"cycle path",
|
||||
"motorway",
|
||||
"trunk highway",
|
||||
"service road",
|
||||
"river",
|
||||
"rail",
|
||||
"other"
|
||||
],
|
||||
"point": [
|
||||
"bus stop",
|
||||
"park",
|
||||
"hospital",
|
||||
"place of worship",
|
||||
"cafe",
|
||||
"restaurant",
|
||||
"bar",
|
||||
"fast food",
|
||||
"bank",
|
||||
"cinema",
|
||||
"supermarket",
|
||||
"other"
|
||||
],
|
||||
"vertex": [
|
||||
"other"
|
||||
]
|
||||
}
|
||||
+661
-703
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user