From eef3f68fafae9badce1eb42f6c032e21367e1642 Mon Sep 17 00:00:00 2001 From: Richard Fairhurst Date: Thu, 12 Jul 2012 13:05:48 +0100 Subject: [PATCH] Move each entity into its own file --- js/iD/Entity.js | 283 ++++++++++------------------------------------ js/iD/Node.js | 64 +++++++++++ js/iD/Relation.js | 44 +++++++ js/iD/Way.js | 153 +++++++++++++++++++++++++ 4 files changed, 323 insertions(+), 221 deletions(-) create mode 100644 js/iD/Node.js create mode 100644 js/iD/Relation.js create mode 100644 js/iD/Way.js diff --git a/js/iD/Entity.js b/js/iD/Entity.js index 0af9e7fe8..c0d60151a 100755 --- a/js/iD/Entity.js +++ b/js/iD/Entity.js @@ -9,6 +9,7 @@ define(['dojo/_base/declare','dojo/_base/array','dojo/_base/lang', // Entity base class declare("iD.Entity", null, { + connection: null, id: NaN, loaded: false, @@ -20,49 +21,80 @@ declare("iD.Entity", null, { MAINKEYS: ['highway','amenity','railway','waterway'], constructor:function() { + // summary: The base class for an entity (way, node or relation). this.tags={}; this.parents=new Hashtable(); }, - isType:function(_type) { - return this.entityType==_type; + isType:function(type) { + // summary: Is this entity of the specified type ('node','way','relation')? + return this.entityType==type; // Boolean }, toString:function() { return this.entityType+"."+this.id; }, + // -------------------------------- // Provoke redraw and other changes - refresh:function() { this.connection.refreshEntity(this); }, + refresh:function() { + // summary: Ask the connection to provoke redraw and other changes. + this.connection.refreshEntity(this); + }, - // Clean and dirty (only called from UndoableEntityAction) + // --------------- + // Clean and dirty - markClean:function() { this.modified=false; }, - markDirty:function() { this.modified=true; }, - isDirty:function() { return this.modified; }, + _markClean:function() { + // summary: Mark entity as clean. Should only be called from UndoableEntityAction. + this.modified=false; + }, + _markDirty:function() { + // summary: Mark entity as dirty. Should only be called from UndoableEntityAction. + this.modified=true; + }, + isDirty:function() { + // summary: Is the entity dirty? + return this.modified; // Boolean + }, + // -------- // Deletion - setDeletedState:function(isDeleted) { this.deleted=isDeleted; }, + setDeletedState:function(isDeleted) { + // summary: Mark entity as deleted or not. + this.deleted=isDeleted; + }, + // ------------------------------------- // Bounding box check (to be overridden) - within:function(left,right,top,bottom) { return !this.deleted; }, + within:function(left,right,top,bottom) { + // summary: Is the entity within the specified bbox? + return !this.deleted; // Boolean + }, + // ------------- // Tag functions getTagsHash:function() { - return this.tags; + // summary: Tag getter. + // returns: The tags hash (reference to the actual object property, not a copy). + return this.tags; // Object }, numTags:function() { + // summary: Count how many tags this entity has. var c=0; for (var i in this.tags) { c++; } - return c; + return c; // int }, friendlyName:function() { + // summary: Rough-and-ready function to return a human-friendly name + // for the object. Really just a placeholder for something better. + // returns: A string such as 'river' or 'Fred's House'. if (this.numTags()==0) { return ''; } var n=[]; if (this.tags['name']) { n.push(this.tags['name']); } @@ -72,37 +104,45 @@ declare("iD.Entity", null, { if (this.tags[this.MAINKEYS[i]]) { n.push(this.tags[this.MAINKEYS[i]]); break; } } } - return n.length==0 ? 'unknown' : n.join('; '); + return n.length==0 ? 'unknown' : n.join('; '); // String }, + // --------------- // Parent-handling - addParent:function(_entity) { - this.parents.put(_entity,true); + addParent:function(entity) { + // summary: Record a parent (a relation or way which contains this entity). + this.parents.put(entity,true); }, - removeParent:function(_entity) { + removeParent:function(entity) { + // summary: Remove a parent (e.g. when node removed from a way). this.parents.remove(_entity); }, - hasParent:function(_entity) { - return this.parents.containsKey(_entity); + hasParent:function(entity) { + // summary: Does this entity have the specified parent (e.g. is it in a certain relation)? + return this.parents.containsKey(entity); // Boolean }, parentObjects:function() { - return this.parents.keys(); + // summary: List of all parents of this entity. + return this.parents.keys(); // Boolean }, hasParentWays:function() { + // summary: Does this entity have any parents which are ways? var p=this.parentObjects(); for (var i in p) { if (p[i].entityType=='way') { return true; } } - return false; + return false; // Boolean }, parentWays:function() { - return this.parentObjectsOfClass('way'); + // summary: Return an array of all ways that this entity is a member of. + return this._parentObjectsOfClass('way'); // Array }, parentRelations:function() { - return this.parentObjectsOfClass('relation'); + // summary: Return an array of all relations that this entity is a member of. + return this._parentObjectsOfClass('relation'); // Array }, - parentObjectsOfClass:function(_class) { + _parentObjectsOfClass:function(_class) { var p=this.parentObjects(), c=[]; for (var i in p) { if (p[i].entityType==_class) { c.push(p[i]); } @@ -119,205 +159,6 @@ declare("iD.Entity", null, { }); -// ---------------------------------------------------------------------- -// Node class - -declare("iD.Node", [iD.Entity], { - lat:NaN, - latp:NaN, - lon:NaN, - entityType:"node", - - constructor:function(_conn,_id,_lat,_lon,_tags,_loaded) { - this.connection=_conn; - this.id=Number(_id); - this.lat=Number(_lat); - this.lon=Number(_lon); - this.tags=_tags; - this.loaded=(_loaded==undefined) ? true : _loaded; - this.project(); - this.modified=this.id<0; - }, - - project:function() { this.latp=180/Math.PI * Math.log(Math.tan(Math.PI/4+this.lat*(Math.PI/180)/2)); }, - latp2lat:function(a) { return 180/Math.PI * (2 * Math.atan(Math.exp(a*Math.PI/180)) - Math.PI/2); }, - - within:function(left,right,top,bottom) { return (this.lon>=left) && (this.lon<=right) && (this.lat>=bottom) && (this.lat<=top) && !this.deleted; }, - - refresh:function() { - var ways=this.parentWays(); - var conn=this.connection; - array.forEach(ways,function(way) { conn.refreshEntity(way); }); - this.connection.refreshEntity(this); - }, - - doSetLonLatp:function(lon,latproj,performAction) { - performAction(new iD.actions.MoveNodeAction(this, this.latp2lat(latproj), lon, lang.hitch(this,this._setLatLonImmediate) )); - }, - - _setLatLonImmediate:function(lat,lon) { - this.lat = lat; - this.lon = lon; - this.project(); - var ways = this.parentWays(); - for (var i=0; iright && this.edger>right ) || - (this.edgebtop && this.edgeb>top ) || this.deleted) { return false; } - return true; - }, - - calculateBbox:function() { - this.edgel=999999; this.edger=-999999; - this.edgeb=999999; this.edget=-999999; - for (var i in this.nodes) { this.expandBbox(this.nodes[i]); } - }, - - expandBbox:function(node) { - this.edgel=Math.min(this.edgel,node.lon); - this.edger=Math.max(this.edger,node.lon); - this.edgeb=Math.min(this.edgeb,node.lat); - this.edget=Math.max(this.edget,node.lat); - }, - - // Action callers - - doAppendNode:function(node, performAction) { - if (node!=this.getLastNode()) performAction(new iD.actions.AddNodeToWayAction(this, node, this.nodes, -1, true)); - return this.nodes.length + 1; - }, - - doPrependNode:function(node, performAction) { - if (node!=this.getFirstNode()) performAction(new iD.actions.AddNodeToWayAction(this, node, this.nodes, 0, true)); - return this.nodes.length + 1; - }, - - doInsertNode:function(index, node, performAction) { - if (index>0 && this.getNode(index-1)==node) return; - if (index=left) && (this.lon<=right) && (this.lat>=bottom) && (this.lat<=top) && !this.deleted; }, + + refresh:function() { + var ways=this.parentWays(); + var conn=this.connection; + array.forEach(ways,function(way) { conn.refreshEntity(way); }); + this.connection.refreshEntity(this); + }, + + doSetLonLatp:function(lon,latproj,performAction) { + // summary: Change the position of a node, using an undo stack. + performAction(new iD.actions.MoveNodeAction(this, this.latp2lat(latproj), lon, lang.hitch(this,this._setLatLonImmediate) )); + }, + + _setLatLonImmediate:function(lat,lon) { + this.lat = lat; + this.lon = lon; + this.project(); + var ways = this.parentWays(); + for (var i=0; iright && this.edger>right ) || + (this.edgebtop && this.edgeb>top ) || this.deleted) { return false; } + return true; + }, + + _calculateBbox:function() { + this.edgel=999999; this.edger=-999999; + this.edgeb=999999; this.edget=-999999; + for (var i in this.nodes) { this.expandBbox(this.nodes[i]); } + }, + + expandBbox:function(node) { + // summary: Enlarge the way's bounding box to make sure it includes the co-ordinates of a supplied node. + this.edgel=Math.min(this.edgel,node.lon); + this.edger=Math.max(this.edger,node.lon); + this.edgeb=Math.min(this.edgeb,node.lat); + this.edget=Math.max(this.edget,node.lat); + }, + + // -------------- + // Action callers + + doAppendNode:function(node, performAction) { + // summary: Add a node to the end of the way, using an undo stack. + // returns: New length of the way. + if (node!=this.getLastNode()) performAction(new iD.actions.AddNodeToWayAction(this, node, this.nodes, -1, true)); + return this.nodes.length + 1; // int + }, + + 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)); + 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