From 2e4207f99b443136babd0bca21f240f74e5e4cbd Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 17 May 2013 14:31:36 -0700 Subject: [PATCH] Cache geometry --- js/id/behavior/draw_way.js | 2 +- js/id/core/node.js | 4 +++- js/id/core/relation.js | 6 ++++-- js/id/core/way.js | 6 ++++-- js/id/operations/rotate.js | 2 +- test/spec/core/relation.js | 8 ++++++-- test/spec/core/way.js | 4 ++-- 7 files changed, 21 insertions(+), 11 deletions(-) diff --git a/js/id/behavior/draw_way.js b/js/id/behavior/draw_way.js index 72bdff73e..b3365c6c7 100644 --- a/js/id/behavior/draw_way.js +++ b/js/id/behavior/draw_way.js @@ -1,6 +1,6 @@ iD.behavior.DrawWay = function(context, wayId, index, mode, baseGraph) { var way = context.entity(wayId), - isArea = way.geometry() === 'area', + isArea = context.geometry(wayId) === 'area', finished = false, annotation = t((way.isDegenerate() ? 'operations.start.annotation.' : diff --git a/js/id/core/node.js b/js/id/core/node.js index 3a9170705..1c97a6d62 100644 --- a/js/id/core/node.js +++ b/js/id/core/node.js @@ -16,7 +16,9 @@ _.extend(iD.Node.prototype, { }, geometry: function(graph) { - return graph.isPoi(this) ? 'point' : 'vertex'; + return graph.transient(this, 'geometry', function() { + return graph.isPoi(this) ? 'point' : 'vertex'; + }); }, move: function(loc) { diff --git a/js/id/core/relation.js b/js/id/core/relation.js index 91adba9e4..e96858bd8 100644 --- a/js/id/core/relation.js +++ b/js/id/core/relation.js @@ -25,8 +25,10 @@ _.extend(iD.Relation.prototype, { }); }, - geometry: function() { - return this.isMultipolygon() ? 'area' : 'relation'; + geometry: function(graph) { + return graph.transient(this, 'geometry', function() { + return this.isMultipolygon() ? 'area' : 'relation'; + }); }, // Return the first member with the given role. A copy of the member object diff --git a/js/id/core/way.js b/js/id/core/way.js index ddfb164e9..a8fde5f8d 100644 --- a/js/id/core/way.js +++ b/js/id/core/way.js @@ -70,8 +70,10 @@ _.extend(iD.Way.prototype, { return false; }, - geometry: function() { - return this.isArea() ? 'area' : 'line'; + geometry: function(graph) { + return graph.transient(this, 'geometry', function() { + return this.isArea() ? 'area' : 'line'; + }); }, addNode: function(id, index) { diff --git a/js/id/operations/rotate.js b/js/id/operations/rotate.js index c4e53783e..b9b3ed2c5 100644 --- a/js/id/operations/rotate.js +++ b/js/id/operations/rotate.js @@ -8,7 +8,7 @@ iD.operations.Rotate = function(selection, context) { operation.available = function() { return selection.length === 1 && context.entity(entityId).type === 'way' && - context.entity(entityId).geometry() === 'area'; + context.geometry(entityId) === 'area'; }; operation.disabled = function() { diff --git a/test/spec/core/relation.js b/test/spec/core/relation.js index 39b748e0a..d257f2ff7 100644 --- a/test/spec/core/relation.js +++ b/test/spec/core/relation.js @@ -47,8 +47,12 @@ describe('iD.Relation', function () { }); describe("#geometry", function () { - it("returns 'relation'", function () { - expect(iD.Relation().geometry()).to.equal('relation'); + it("returns 'area' for multipolygons", function () { + expect(iD.Relation({tags: {type: 'multipolygon'}}).geometry(iD.Graph())).to.equal('area'); + }); + + it("returns 'relation' for other relations", function () { + expect(iD.Relation().geometry(iD.Graph())).to.equal('relation'); }); }); diff --git a/test/spec/core/way.js b/test/spec/core/way.js index 9a4a90d8b..6db0f6b5c 100644 --- a/test/spec/core/way.js +++ b/test/spec/core/way.js @@ -176,11 +176,11 @@ describe('iD.Way', function() { describe("#geometry", function() { it("returns 'line' when the way is not an area", function () { - expect(iD.Way().geometry()).to.equal('line'); + expect(iD.Way().geometry(iD.Graph())).to.equal('line'); }); it("returns 'area' when the way is an area", function () { - expect(iD.Way({tags: { area: 'yes' }}).geometry()).to.equal('area'); + expect(iD.Way({tags: { area: 'yes' }}).geometry(iD.Graph())).to.equal('area'); }); });