Add iD.{Node,Way,Relation} constructors

Even if we don't want subclasses, they are handy for
assigning the right type and an appropriate ID.
This commit is contained in:
John Firebaugh
2012-11-26 22:00:39 -04:00
parent cc7328d1c9
commit 75f8711e64
4 changed files with 46 additions and 32 deletions
+6 -25
View File
@@ -6,13 +6,10 @@ iD.modes = {};
// into actions.
iD.modes._node = function(ll) {
return iD.Entity({
type: 'node',
return iD.Node({
lat: ll[1],
lon: ll[0],
id: iD.Util.id('node'),
tags: {}
}, {});
});
};
iD.modes.AddPlace = {
@@ -57,16 +54,8 @@ iD.modes.AddPlace = {
iD.modes.AddRoad = {
title: "+ Road",
way: function(ll) {
return iD.Entity({
type: 'way',
nodes: [],
tags: {
highway: 'residential'
},
_updated: true,
id: iD.Util.id('way')
});
way: function() {
return iD.Way({tags: {highway: 'residential'}});
},
enter: function() {
var surface = this.map.surface;
@@ -177,16 +166,8 @@ iD.modes.DrawRoad = function(way) {
iD.modes.AddArea = {
title: "+ Area",
way: function(ll) {
return iD.Entity({
type: 'way',
nodes: [],
tags: {
building: 'yes'
},
_updated: true,
id: iD.Util.id('way')
});
way: function() {
return iD.Way({tags: {building: 'yes'}});
},
enter: function() {
var surface = this.map.surface;
+17
View File
@@ -7,6 +7,11 @@ iD.Entity = function(a, b) {
this._updated = true;
}
if (!this.id) {
this.id = iD.Util.id(this.type);
this._updated = true;
}
if (iD.debug) {
Object.freeze(this);
Object.freeze(this.tags);
@@ -26,3 +31,15 @@ iD.Entity.prototype = {
return this._updated && +this.id.slice(1) > 0;
}
};
iD.Node = function (attrs) {
return iD.Entity(_.extend({}, attrs, {type: 'node'}));
};
iD.Way = function (attrs) {
return iD.Entity(_.extend({}, attrs, {type: 'way'}));
};
iD.Relation = function (attrs) {
return iD.Entity(_.extend({}, attrs, {type: 'relation'}));
};
+3 -5
View File
@@ -7,8 +7,6 @@
// If a a way is _closed_, it is assumed to be an area unless it has a
// `highway` or `barrier` tag and is not also tagged `area`.
iD.Way = {
isClosed: function(w) {
return (!w.nodes.length) || w.nodes[w.nodes.length - 1].id === w.nodes[0].id;
}
};
iD.Way.isClosed = function(w) {
return (!w.nodes.length) || w.nodes[w.nodes.length - 1].id === w.nodes[0].id;
}
+20 -2
View File
@@ -16,7 +16,7 @@ describe('Entity', function () {
describe("#created", function () {
it("returns false for an unmodified Entity", function () {
expect(iD.Entity().created()).toBeFalsy();
expect(iD.Entity({id: 'w1234'}).created()).toBeFalsy();
});
it("returns false for a modified Entity with positive ID", function () {
@@ -30,7 +30,7 @@ describe('Entity', function () {
describe("#modified", function () {
it("returns false for an unmodified Entity", function () {
expect(iD.Entity().modified()).toBeFalsy();
expect(iD.Entity({id: 'w1234'}).modified()).toBeFalsy();
});
it("returns true for a modified Entity with positive ID", function () {
@@ -42,3 +42,21 @@ describe('Entity', function () {
});
});
});
describe('Node', function () {
it("returns a created Entity if no ID is specified", function () {
expect(iD.Node().created()).toBeTruthy();
});
});
describe('Way', function () {
it("returns a created Entity if no ID is specified", function () {
expect(iD.Way().created()).toBeTruthy();
});
});
describe('Relation', function () {
it("returns a created Entity if no ID is specified", function () {
expect(iD.Relation().created()).toBeTruthy();
});
});