Add some utility methods to Way

This commit is contained in:
John Firebaugh
2013-01-18 13:19:07 -08:00
parent 020c56b3a2
commit 95800741fd
4 changed files with 38 additions and 4 deletions
+13 -1
View File
@@ -14,12 +14,24 @@ iD.Way = iD.Entity.extend({
});
},
first: function() {
return this.nodes[0];
},
last: function() {
return this.nodes[this.nodes.length - 1];
},
contains: function(node) {
return this.nodes.indexOf(node) >= 0;
},
isOneWay: function() {
return this.tags.oneway === 'yes';
},
isClosed: function() {
return this.nodes.length > 0 && this.nodes[this.nodes.length - 1] === this.nodes[0];
return this.nodes.length > 0 && this.first() === this.last();
},
// a way is an area if:
+1 -1
View File
@@ -13,7 +13,7 @@ iD.modes.DrawArea = function(wayId) {
headId = (way.nodes.length == 1) ?
way.nodes[0] :
way.nodes[way.nodes.length - 2],
tailId = _.first(way.nodes),
tailId = way.first(),
node = iD.Node({loc: map.mouseCoordinates()});
map.dblclickEnable(false)
+2 -2
View File
@@ -12,8 +12,8 @@ iD.modes.DrawLine = function(wayId, direction) {
way = history.graph().entity(wayId),
node = iD.Node({loc: map.mouseCoordinates()}),
index = (direction === 'forward') ? undefined : 0,
headId = (direction === 'forward') ? _.last(way.nodes) : _.first(way.nodes),
tailId = (direction === 'forward') ? _.first(way.nodes) : _.last(way.nodes);
headId = (direction === 'forward') ? way.last() : way.first(),
tailId = (direction === 'forward') ? way.first() : way.last();
iD.behavior.Hover()(surface);
+22
View File
@@ -35,6 +35,28 @@ describe('iD.Way', function() {
expect(iD.Way({tags: {foo: 'bar'}}).tags).to.eql({foo: 'bar'});
});
describe("#first", function () {
it("returns the first node", function () {
expect(iD.Way({nodes: ['a', 'b', 'c']}).first()).to.equal('a');
});
});
describe("#last", function () {
it("returns the last node", function () {
expect(iD.Way({nodes: ['a', 'b', 'c']}).last()).to.equal('c');
});
});
describe("#contains", function () {
it("returns true if the way contains the given node", function () {
expect(iD.Way({nodes: ['a', 'b', 'c']}).contains('b')).to.be.true;
});
it("returns false if the way does not contain the given node", function () {
expect(iD.Way({nodes: ['a', 'b', 'c']}).contains('d')).to.be.false;
});
});
describe("#extent", function () {
it("returns the minimal extent containing all member nodes", function () {
var node1 = iD.Node({loc: [0, 0]}),