diff --git a/js/iD/actions/modes.js b/js/iD/actions/modes.js index b463402d9..5ac6bb895 100644 --- a/js/iD/actions/modes.js +++ b/js/iD/actions/modes.js @@ -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; diff --git a/js/iD/graph/Entity.js b/js/iD/graph/Entity.js index 37a0f8a20..e3c2d00e6 100644 --- a/js/iD/graph/Entity.js +++ b/js/iD/graph/Entity.js @@ -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'})); +}; diff --git a/js/iD/graph/Way.js b/js/iD/graph/Way.js index c6d1f6dbc..a8f68c087 100644 --- a/js/iD/graph/Way.js +++ b/js/iD/graph/Way.js @@ -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; +} diff --git a/test/spec/Entity.js b/test/spec/Entity.js index 62a022969..deebe986e 100644 --- a/test/spec/Entity.js +++ b/test/spec/Entity.js @@ -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(); + }); +});