Split and test relations

This commit is contained in:
Tom MacWright
2012-10-16 16:54:12 -04:00
parent 2c9b377b5e
commit 64bc7d5ea5
6 changed files with 50 additions and 71 deletions
+5 -5
View File
@@ -24,7 +24,7 @@ declare("iD.Connection", null, {
constructor:function(apiURL) {
// summary: The data store, including methods to fetch data from (and, eventually, save data to)
// an OSM API server.
// an OSM API server.
this.nodes={};
this.ways={};
this.relations={};
@@ -37,12 +37,12 @@ declare("iD.Connection", null, {
_assign:function(obj) {
// summary: Save an entity to the data store.
switch (obj.entityType) {
case "node": this.nodes[obj.id]=obj; break;
case "way": this.ways[obj.id]=obj; break;
case "relation": this.relations[obj.id]=obj; break;
case "node": this.nodes[obj.id]=obj; break;
case "way": this.ways[obj.id]=obj; break;
case "relation": this.relations[obj.id]=obj; break;
}
},
getNode:function(id) {
// summary: Return a node by id.
return this.nodes[id]; // iD.Node
+17 -40
View File
@@ -1,43 +1,20 @@
// iD/Relation.js
if (typeof iD === 'undefined') iD = {};
define(['dojo/_base/declare','dojo/_base/array','dojo/_base/lang',
'iD/Entity','iD/actions/AddNodeToWayAction','iD/actions/MoveNodeAction'
], function(declare,array,lang){
iD.Relation = function(conn, id, members, tags, loaded) {
this.entityType = 'relation';
this.connection = conn;
this.id = id;
this.members = members;
this.tags = tags;
this.modified = this.id < 0;
this.loaded = (loaded === undefined) ? true : loaded;
_.each(members, _.bind(function(member) {
member.entity.addParent(this);
}, this));
};
// ----------------------------------------------------------------------
// Relation class
declare("iD.Relation", [iD.Entity], {
members:null,
entityType:"relation",
constructor:function(conn,id,members,tags,loaded) {
// summary: An OSM relation.
this.connection=conn;
this.id=Number(id);
this.members=members;
this.tags=tags;
this.modified=this.id<0;
this.loaded=(loaded===undefined) ? true : loaded;
_.each(members, _.bind(function(member) {
member.entity.addParent(this);
}, this));
}
});
// ----------------------------------------------------------------------
// RelationMember class
declare("iD.RelationMember", [], {
entity:null,
role:"",
constructor:function(entity,role) {
// summary: An object containing both the entity that is in the relation, and its role.
this.entity=entity;
this.role=role;
}
});
// ----------------------------------------------------------------------
// End of module
});
iD.RelationMember = function(entity, role) {
this.entity = entity;
this.role = role;
};
+10 -25
View File
@@ -17,11 +17,6 @@ iD.Way = function(conn, id, nodes, tags, loaded) {
};
iD.Way.prototype = {
length:function() {
// summary: Return the number of nodes in the way.
return this.nodes.length;
},
isClosed:function() {
// summary: Is this a closed way (first and last nodes the same)?
return this.nodes[this.nodes.length-1]==this.nodes[0]; // Boolean
@@ -37,22 +32,8 @@ iD.Way.prototype = {
return false; // Boolean
},
getNode:function(index) {
// summary: Return the node at the given position.
return this.nodes[index]; // iD.Node
},
getFirstNode:function() {
// summary: Return the first node in the way.
return this.nodes[0]; // iD.Node
},
getLastNode:function() {
// summary: Return the last node in the way.
return this.nodes[this.nodes.length-1]; // iD.Node
},
// ---------------------
// Bounding-box handling
within:function(left,right,top,bottom) {
if (!this.edgel ||
(this.edgel<left && this.edger<left ) ||
@@ -90,14 +71,14 @@ iD.Way.prototype = {
doPrependNode:function(node, performAction) {
// summary: Add a node to the start of the way, using an undo stack.
// returns: New length of the way.
if (node!=this.getFirstNode()) performAction(new iD.actions.AddNodeToWayAction(this, node, this.nodes, 0, true));
if (node!=this.nodes[0]) performAction(new iD.actions.AddNodeToWayAction(this, node, this.nodes, 0, true));
return this.nodes.length + 1; // int
},
doInsertNode:function(index, node, performAction) {
// summary: Add a node at a given index within the way, using an undo stack.
if (index > 0 && this.getNode(index - 1)==node) return;
if (index < this.nodes.length - 1 && this.getNode(index)==node) return;
if (index > 0 && this.nodes[index - 1]==node) return;
if (index < this.nodes.length - 1 && this.nodes[index]==node) return;
performAction(new iD.actions.AddNodeToWayAction(this, node, this.nodes, index, false));
},
@@ -110,8 +91,8 @@ iD.Way.prototype = {
snapped;
for (var i = 0; i < this.nodes.length - 1; i++) {
var node1 = this.getNode(i);
var node2 = this.getNode(i+1);
var node1 = this.nodes[i];
var node2 = this.nodes[i + 1];
var directDist = this._pythagoras(node1, node2);
var viaNewDist = this._pythagoras(node1, newNode) +
this._pythagoras(node2, newNode);
@@ -129,7 +110,11 @@ iD.Way.prototype = {
return newIndex; // int
},
_pythagoras:function(node1, node2) { return (Math.sqrt(Math.pow(node1.lon-node2.lon,2)+Math.pow(node1.latp-node2.latp,2))); },
_pythagoras:function(node1, node2) {
return (Math.sqrt(Math.pow(node1.lon-node2.lon,2) +
Math.pow(node1.latp-node2.latp,2)));
},
_calculateSnappedPoint:function(node1, node2, newNode) {
var w = node2.lon - node1.lon;
var h = node2.latp - node1.latp;
+2
View File
@@ -13,12 +13,14 @@
<script type="text/javascript" src="../js/lib/underscore-min.js"></script>
<script type="text/javascript" src="../js/lib/jshashtable.js"></script>
<script type="text/javascript" src="../js/iD/Node.js"></script>
<script type="text/javascript" src="../js/iD/Relation.js"></script>
<script type="text/javascript" src="../js/iD/Entity.js"></script>
<script type="text/javascript" src="../js/iD/Way.js"></script>
<!-- include spec files here... -->
<script type="text/javascript" src="spec/Node.js"></script>
<script type="text/javascript" src="spec/Entity.js"></script>
<script type="text/javascript" src="spec/Relation.js"></script>
<script type="text/javascript" src="spec/Way.js"></script>
<script type="text/javascript">
+11
View File
@@ -0,0 +1,11 @@
describe('Relation Member', function() {
var rm;
beforeEach(function() {
rm = new iD.RelationMember();
});
it('is instantiated', function() {
expect(rm).toBeTruthy();
});
});
+5 -1
View File
@@ -10,6 +10,10 @@ describe('Way', function() {
});
it('has zero nodes by default', function() {
expect(way.length()).toEqual(0);
expect(way.nodes.length).toEqual(0);
});
it('is closed by default', function() {
expect(way.isClosed()).toEqual(true);
});
});