mirror of
https://github.com/FoggedLens/iD.git
synced 2026-03-21 10:33:34 +00:00
92 lines
2.3 KiB
JavaScript
92 lines
2.3 KiB
JavaScript
iD.Way = iD.Entity.way = function iD_Way() {
|
|
if (!(this instanceof iD_Way)) {
|
|
return (new iD_Way()).initialize(arguments);
|
|
} else if (arguments.length) {
|
|
this.initialize(arguments);
|
|
}
|
|
};
|
|
|
|
iD.Way.prototype = Object.create(iD.Entity.prototype);
|
|
|
|
_.extend(iD.Way.prototype, {
|
|
type: "way",
|
|
nodes: [],
|
|
|
|
extent: function(resolver) {
|
|
return resolver.transient(this, 'extent', function() {
|
|
var extent = iD.geo.Extent();
|
|
for (var i = 0, l = this.nodes.length; i < l; i++) {
|
|
var node = this.nodes[i];
|
|
if (node.loc === undefined) node = resolver.entity(node);
|
|
extent = extent.extend(node.loc);
|
|
}
|
|
return extent;
|
|
});
|
|
},
|
|
|
|
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.first() === this.last();
|
|
},
|
|
|
|
// a way is an area if:
|
|
//
|
|
// - area=yes
|
|
// - closed and
|
|
// - doesn't have area=no
|
|
// - doesn't have highway tag
|
|
isArea: function() {
|
|
return this.tags.area === 'yes' ||
|
|
(this.isClosed() &&
|
|
this.tags.area !== 'no' &&
|
|
!this.tags.highway &&
|
|
!this.tags.barrier);
|
|
},
|
|
|
|
isDegenerate: function() {
|
|
return _.uniq(this.nodes).length < (this.isArea() ? 3 : 2);
|
|
},
|
|
|
|
geometry: function() {
|
|
return this.isArea() ? 'area' : 'line';
|
|
},
|
|
|
|
addNode: function(id, index) {
|
|
var nodes = this.nodes.slice();
|
|
nodes.splice(index === undefined ? nodes.length : index, 0, id);
|
|
return this.update({nodes: nodes});
|
|
},
|
|
|
|
updateNode: function(id, index) {
|
|
var nodes = this.nodes.slice();
|
|
nodes.splice(index, 1, id);
|
|
return this.update({nodes: nodes});
|
|
},
|
|
|
|
removeNode: function(id) {
|
|
var nodes = _.without(this.nodes, id);
|
|
|
|
// Preserve circularity
|
|
if (this.nodes.length > 1 && this.first() === id && this.last() === id) {
|
|
nodes.push(nodes[0]);
|
|
}
|
|
|
|
return this.update({nodes: nodes});
|
|
}
|
|
});
|