From 2c9b377b5ed033f576bebab189ac32fbda82edba Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Tue, 16 Oct 2012 16:40:54 -0400 Subject: [PATCH] Split out and test Way --- js/iD/Entity.js | 185 +-------------------------------- js/iD/Way.js | 258 ++++++++++++++++++++++------------------------- test/index.html | 2 + test/spec/Way.js | 15 +++ 4 files changed, 144 insertions(+), 316 deletions(-) create mode 100644 test/spec/Way.js diff --git a/js/iD/Entity.js b/js/iD/Entity.js index 82839c127..d90625fca 100755 --- a/js/iD/Entity.js +++ b/js/iD/Entity.js @@ -1,178 +1,12 @@ // iD/Entity.js // Entity classes for iD - -/* -define(['dojo/_base/declare','dojo/_base/array','dojo/_base/lang', - 'iD/actions/AddNodeToWayAction','iD/actions/MoveNodeAction' - ], function(declare,array,lang){ - -// ---------------------------------------------------------------------- -// Entity base class - -declare("iD.Entity", null, { - - connection: null, - id: NaN, - loaded: false, - tags: null, - entityType: '', - parents: null, - modified: false, - deleted: false, - 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) { - // 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() { - // summary: Ask the connection to provoke redraw and other changes. - this.connection.refreshEntity(this); - }, - - // --------------- - // Clean and dirty - - _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) { - // summary: Mark entity as deleted or not. - this.deleted=isDeleted; - }, - - // ------------------------------------- - // Bounding box check (to be overridden) - - within:function(left,right,top,bottom) { - // summary: Is the entity within the specified bbox? - return !this.deleted; // Boolean - }, - - // ------------- - // Tag functions - - getTagsHash:function() { - // 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; // 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); } - if (this.tags.ref) { n.push(this.tags.ref); } - if (n.length===0) { - for (var i=0; iright && this.edger>right ) || + (this.edgebtop && this.edgeb>top ) || this.deleted) { return false; } + return true; + }, - within:function(left,right,top,bottom) { - if (!this.edgel || - (this.edgelright && 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]); } + }, - _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); - }, + 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 + // -------------- + // 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 - }, + 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 - }, + 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 0 && this.getNode(index - 1)==node) return; + if (index < this.nodes.length - 1 && this.getNode(index)==node) return; + performAction(new iD.actions.AddNodeToWayAction(this, node, this.nodes, index, false)); + }, - for (var i=0; i + +