Polymorphism

This commit is contained in:
John Firebaugh
2012-12-28 22:24:52 -08:00
parent f7dfda46b2
commit d181df4c51
7 changed files with 46 additions and 20 deletions
+2 -18
View File
@@ -68,24 +68,8 @@ iD.Entity.prototype = {
return this._updated && this.osmId() > 0;
},
extent: function(resolver) {
if (this.type === 'node') {
return [this.loc, this.loc];
} else if (this.type == 'way') {
return resolver.transient(this, 'extent', function() {
var extent = [[-Infinity, Infinity], [Infinity, -Infinity]];
for (var i = 0, l = this.nodes.length; i < l; i++) {
var node = resolver.entity(this.nodes[i]);
if (node.loc[0] > extent[0][0]) extent[0][0] = node.loc[0];
if (node.loc[0] < extent[1][0]) extent[1][0] = node.loc[0];
if (node.loc[1] < extent[0][1]) extent[0][1] = node.loc[1];
if (node.loc[1] > extent[1][1]) extent[1][1] = node.loc[1];
}
return extent;
});
} else {
return [[NaN, NaN], [NaN, NaN]];
}
extent: function() {
throw Error("not implemented");
},
intersects: function(extent, resolver) {
+5 -1
View File
@@ -1,3 +1,7 @@
iD.Node = iD.Entity.extend({
type: "node"
type: "node",
extent: function() {
return [this.loc, this.loc];
}
});
+5 -1
View File
@@ -1,4 +1,8 @@
iD.Relation = iD.Entity.extend({
type: "relation",
members: []
members: [],
extent: function() {
return [[NaN, NaN], [NaN, NaN]];
}
});
+14
View File
@@ -2,6 +2,20 @@ iD.Way = iD.Entity.extend({
type: "way",
nodes: [],
extent: function(resolver) {
return resolver.transient(this, 'extent', function() {
var extent = [[-Infinity, Infinity], [Infinity, -Infinity]];
for (var i = 0, l = this.nodes.length; i < l; i++) {
var node = resolver.entity(this.nodes[i]);
if (node.loc[0] > extent[0][0]) extent[0][0] = node.loc[0];
if (node.loc[0] < extent[1][0]) extent[1][0] = node.loc[0];
if (node.loc[1] < extent[0][1]) extent[0][1] = node.loc[1];
if (node.loc[1] > extent[1][1]) extent[1][1] = node.loc[1];
}
return extent;
});
},
isOneWay: function() {
return this.tags.oneway === 'yes';
},
+6
View File
@@ -21,6 +21,12 @@ describe('iD.Node', function () {
expect(iD.Node({tags: {foo: 'bar'}}).tags).to.eql({foo: 'bar'});
});
describe("#extent", function() {
it("returns a point extent", function() {
expect(iD.Node({loc: [5, 10]}).extent()).to.eql([[5, 10], [5, 10]]);
});
});
describe("#intersects", function () {
it("returns true for a node within the given extent", function () {
expect(iD.Node({loc: [0, 0]}).intersects([[-180, 90], [180, -90]])).to.equal(true);
+4
View File
@@ -34,4 +34,8 @@ describe('iD.Relation', function () {
it("sets tags as specified", function () {
expect(iD.Relation({tags: {foo: 'bar'}}).tags).to.eql({foo: 'bar'});
});
describe("#extent", function () {
it("returns the minimal extent containing the extents of all members");
});
});
+10
View File
@@ -35,6 +35,16 @@ describe('iD.Way', function() {
expect(iD.Way({tags: {foo: 'bar'}}).tags).to.eql({foo: 'bar'});
});
describe("#extent", function () {
it("returns the minimal extent containing all member nodes", function () {
var node1 = iD.Node({loc: [0, 0]}),
node2 = iD.Node({loc: [5, 10]}),
way = iD.Way({nodes: [node1.id, node2.id]}),
graph = iD.Graph([node1, node2, way]);
expect(way.extent(graph)).to.eql([[5, 0], [0, 10]]);
});
});
describe("#intersects", function () {
it("returns true for a way with a node within the given extent", function () {
var node = iD.Node({loc: [0, 0]}),