From 0c24821c8769d1684ac12368ca33552aa3200d2c Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Fri, 1 Mar 2013 16:13:48 -0500 Subject: [PATCH] re-add preset category support --- index.html | 6 ++++-- js/id/presets.js | 16 +++++++++++----- js/id/presets/category.js | 14 ++++++++++++++ js/id/presets/presets.js | 23 +++++------------------ js/id/ui/preset_grid.js | 6 +++--- js/id/ui/tag_editor.js | 4 ++-- presets/categories.json | 14 ++++++++++++++ 7 files changed, 53 insertions(+), 30 deletions(-) create mode 100644 js/id/presets/category.js create mode 100644 presets/categories.json diff --git a/index.html b/index.html index 1631cd634..d65be30cb 100644 --- a/index.html +++ b/index.html @@ -159,6 +159,7 @@ + @@ -189,14 +190,15 @@ var id = iD(); - iD.util.asyncMap(['keys.json', 'presets/presets.json', 'presets/defaults.json'], d3.json, function(err, data) { + iD.util.asyncMap(['keys.json', 'presets/presets.json', 'presets/defaults.json', 'presets/categories.json'], d3.json, function(err, data) { id.connection() .keys(data[0]); id.presets() .load({ presets: data[1], - defaults: data[2] + defaults: data[2], + categories: data[3] }); d3.select("#iD") diff --git a/js/id/presets.js b/js/id/presets.js index d9dc2439b..c73b9a088 100644 --- a/js/id/presets.js +++ b/js/id/presets.js @@ -1,5 +1,8 @@ iD.presets = function(context) { + // an iD.presets.Collection with methods for + // loading new data and returning defaults + var other = { name: 'other', title: 'Other', @@ -10,27 +13,30 @@ iD.presets = function(context) { }, form: [] }, - all = iD.presets.Collection(context, [iD.presets.Preset(other)]), + all = iD.presets.Collection([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)) + area: iD.presets.Collection(d.defaults.area.map(getItem)), + line: iD.presets.Collection(d.defaults.line.map(getItem)), + point: iD.presets.Collection(d.defaults.point.map(getItem)), + vertex: iD.presets.Collection(d.defaults.vertex.map(getItem)) }; } }; diff --git a/js/id/presets/category.js b/js/id/presets/category.js new file mode 100644 index 000000000..079838c3c --- /dev/null +++ b/js/id/presets/category.js @@ -0,0 +1,14 @@ +iD.presets.Category = function(category, all) { + + category.members = iD.presets.Collection(category.members.map(function(name) { + return all.item(name); + })); + + category.matchType = function(entity, resolver) { + return category.match.type.indexOf(entity.geometry(resolver)) >= 0; + }; + + category.matchTags = function() { return false; }; + + return category; +}; diff --git a/js/id/presets/presets.js b/js/id/presets/presets.js index 7dd0fc5e7..eec2bddfa 100644 --- a/js/id/presets/presets.js +++ b/js/id/presets/presets.js @@ -1,34 +1,21 @@ -iD.presets.Collection = function(context, collection) { +iD.presets.Collection = function(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) { + matchType: function(entity, resolver) { var newcollection = collection.filter(function(d) { - return d.matchType(entity, context.graph()); + return d.matchType(entity, resolver); }); - return iD.presets.Collection(context, newcollection); + return iD.presets.Collection(newcollection); }, @@ -55,7 +42,7 @@ iD.presets.Collection = function(context, collection) { // Uses levenshtein distance, with a couple of hacks // to prioritize exact substring matches - return iD.presets.Collection(context, collection.sort(function(a, b) { + return iD.presets.Collection(collection.sort(function(a, b) { var ia = a.name.indexOf(value) >= 0, ib = b.name.indexOf(value) >= 0; diff --git a/js/id/ui/preset_grid.js b/js/id/ui/preset_grid.js index c4697cad2..e981ee3d4 100644 --- a/js/id/ui/preset_grid.js +++ b/js/id/ui/preset_grid.js @@ -8,7 +8,7 @@ iD.ui.PresetGrid = function(context) { selection.html(''); - presets = presets.matchType(entity); + presets = presets.matchType(entity, context.graph()); var messagewrap = selection.append('div') .attr('class', 'message inspector-inner fillL'); @@ -54,8 +54,8 @@ iD.ui.PresetGrid = function(context) { // Category if (d.members) { search.property('value', ''); - viable = presetData.categories(d.name); - drawGrid(selection, viable); + presets = d.members; + drawGrid(selection, presets); // Preset } else { diff --git a/js/id/ui/tag_editor.js b/js/id/ui/tag_editor.js index 35eea1e12..cad838376 100644 --- a/js/id/ui/tag_editor.js +++ b/js/id/ui/tag_editor.js @@ -44,7 +44,7 @@ iD.ui.TagEditor = function(context) { } } - presetMatch = preset || presetMatch || presets.matchType(entity).matchTags(entity); + presetMatch = preset || presetMatch || presets.matchType(entity, context.graph()).matchTags(entity); selection.html(''); @@ -163,7 +163,7 @@ iD.ui.TagEditor = function(context) { if (presetUI && tagList) { // change preset if necessary (undos/redos) - var newmatch = presets.matchType(entity).matchTags(entity.update({ tags: tags })); + var newmatch = presets.matchType(entity, context.graph()).matchTags(entity.update({ tags: tags })); if (newmatch !== presetMatch) { return tageditor(selection_, newmatch); } diff --git a/presets/categories.json b/presets/categories.json new file mode 100644 index 000000000..367e74231 --- /dev/null +++ b/presets/categories.json @@ -0,0 +1,14 @@ +[ +{ + "match": { + "type": "line" + }, + "icon": "road", + "name": "roads", + "members": [ + "residential road", + "primary road" + ] +} +] +