Avoid recomputing geometry in an inner loop

This commit is contained in:
John Firebaugh
2013-04-24 13:37:16 -07:00
parent b95529d5ec
commit fdd49233b1
8 changed files with 20 additions and 19 deletions
+3 -3
View File
@@ -57,9 +57,9 @@ iD.presets = function(context) {
};
all.defaults = function(entity, n) {
var rec = recent.matchGeometry(entity, context.graph()).collection.slice(0, 4),
def = _.uniq(rec.concat(defaults[entity.geometry(context.graph())].collection)).slice(0, n - 1),
geometry = entity.geometry(context.graph());
var geometry = entity.geometry(context.graph()),
rec = recent.matchGeometry(geometry).collection.slice(0, 4),
def = _.uniq(rec.concat(defaults[geometry].collection)).slice(0, n - 1);
return iD.presets.Collection(_.unique(rec.concat(def).concat(geometry === 'area' ? other_area : other)));
};
+2 -2
View File
@@ -7,8 +7,8 @@ iD.presets.Category = function(id, category, all) {
return all.item(id);
}));
category.matchGeometry = function(entity, resolver) {
return category.geometry.indexOf(entity.geometry(resolver)) >= 0;
category.matchGeometry = function(geometry) {
return category.geometry.indexOf(geometry) >= 0;
};
category.matchTags = function() { return false; };
+3 -3
View File
@@ -11,12 +11,12 @@ iD.presets.Collection = function(collection) {
},
match: function(entity, resolver) {
return presets.matchGeometry(entity, resolver).matchTags(entity);
return presets.matchGeometry(entity.geometry(resolver)).matchTags(entity);
},
matchGeometry: function(entity, resolver) {
matchGeometry: function(geometry) {
return iD.presets.Collection(collection.filter(function(d) {
return d.matchGeometry(entity, resolver);
return d.matchGeometry(geometry);
}));
},
+2 -2
View File
@@ -8,8 +8,8 @@ iD.presets.Preset = function(id, preset, fields) {
return fields[f];
}
preset.matchGeometry = function(entity, resolver) {
return preset.geometry.indexOf(entity.geometry(resolver)) >= 0;
preset.matchGeometry = function(geometry) {
return preset.geometry.indexOf(geometry) >= 0;
};
preset.matchTags = function(entity) {
+1 -1
View File
@@ -9,7 +9,7 @@ iD.ui.PresetGrid = function(context, entity) {
selection.html('');
presets = context.presets().matchGeometry(entity, context.graph());
presets = context.presets().matchGeometry(entity.geometry(context.graph()));
var messagewrap = selection.append('div')
.attr('class', 'header fillL cf');
+2 -2
View File
@@ -29,8 +29,8 @@ describe("iD.presets.Category", function() {
w = iD.Way(),
n = iD.Node(),
g = iD.Graph().replace(w);
expect(c.matchGeometry(w, g)).to.eql(true);
expect(c.matchGeometry(n, g)).to.eql(false);
expect(c.matchGeometry('line')).to.eql(true);
expect(c.matchGeometry('point')).to.eql(false);
});
});
});
+2 -2
View File
@@ -31,8 +31,8 @@ describe("iD.presets.Collection", function() {
});
describe("#matchGeometry", function() {
it("returns a new collection only containing presets matching an entity's type", function() {
expect(c.matchGeometry(w, g).collection).to.eql([p.other, p.residential]);
it("returns a new collection only containing presets matching a geometry", function() {
expect(c.matchGeometry('line').collection).to.eql([p.other, p.residential]);
});
});
+5 -4
View File
@@ -40,12 +40,13 @@ describe('iD.presets.Preset', function() {
describe('#matchGeometry', function() {
var n = iD.Node();
var g = iD.Graph().replace(n);
it("returns false if it doesn't match the entity type", function() {
expect(p['highway/residential'].matchGeometry(n, g)).to.equal(false);
it("returns false if it doesn't match", function() {
expect(p['highway/residential'].matchGeometry('point')).to.equal(false);
});
it("returns true if it does match the entity type", function() {
expect(p.other.matchGeometry(n, g)).to.equal(true);
it("returns true if it does match", function() {
expect(p.other.matchGeometry('point')).to.equal(true);
});
});