Move entity to prototype

This commit is contained in:
Tom MacWright
2012-10-26 18:28:31 -04:00
parent 0c9d46bee3
commit 5f4703494b
6 changed files with 45 additions and 51 deletions

View File

@@ -44,11 +44,11 @@ iD.Connection = function(apiURL) {
return entities[id];
} else if (type === 'way') {
if (!entities[id]) {
assign(new iD.Way(connection, id, [], {}, false));
assign(new iD.Way(id, [], {}, false));
}
return entities[id];
} else if (type === 'relation') {
if (!relations[id]) assign(new iD.Relation(connection, id, [], {}, false));
if (!relations[id]) assign(new iD.Relation(id, [], {}, false));
return relations[id];
}
}
@@ -62,14 +62,14 @@ iD.Connection = function(apiURL) {
function doCreateWay(tags, nodes, perform) {
// summary: Create a new way and save it in the data store, using an undo stack.
var way = new iD.Way(connection, nextWay--, nodes.concat(), tags, true);
var way = new iD.Way(nextWay--, nodes.concat(), tags, true);
perform(new iD.actions.CreateEntityAction(way, assign));
return way;
}
function doCreateRelation(tags, members, perform) {
// summary: Create a new relation and save it in the data store, using an undo stack.
var relation = new iD.Relation(connection, nextRelation--, members.concat(), tags, true);
var relation = new iD.Relation(nextRelation--, members.concat(), tags, true);
perform(new iD.actions.CreateEntityAction(relation, assign));
return relation;
}
@@ -156,13 +156,13 @@ iD.Connection = function(apiURL) {
getTags(obj));
assign(node);
} else if (obj.nodeName === 'way') {
var way = new iD.Way(connection,
var way = new iD.Way(
getAttribute(obj, 'id'),
getNodes(obj, connection),
getTags(obj));
assign(way);
} else if (obj.nodeName === 'relation') {
var relation = new iD.Relation(connection,
var relation = new iD.Relation(
getAttribute(obj, 'id'),
getMembers(obj, connection),
getTags(obj));

View File

@@ -1,55 +1,53 @@
if (typeof iD === 'undefined') iD = {};
iD.Entity = function () {
var entity = {};
// The ID in OSM terms
entity.parents = {};
entity.connection = null;
entity._id = iD.Util.id();
entity.id = NaN;
entity.loaded = false;
entity.entityType = '';
entity.modified = false;
entity.deleted = false;
this.parents = {};
this._id = iD.Util.id();
this.id = NaN;
this.loaded = false;
this.entityType = '';
this.modified = false;
this.deleted = false;
};
iD.Entity.prototype = {
// Parent-handling
entity.addParent = function (x) {
addParent: function (x) {
// summary: Record a parent (a relation or way which contains this entity).
entity.parents[x._id] = x;
};
entity.removeParent = function (x) {
this.parents[x._id] = x;
},
removeParent: function (x) {
// summary: Remove a parent (e.g. when node removed from a way).
delete entity.parents[x._id];
};
entity.hasParent = function (x) {
delete this.parents[x._id];
},
hasParent: function (x) {
// summary: Does this entity have the specified parent (e.g. is it in a certain relation)?
return !!entity.parents[x._id];
};
entity.parentObjects = function () {
return !!this.parents[x._id];
},
parentObjects: function () {
// summary: List of all parents of this entity.
var objects = [];
for (var i in entity.parents) {
objects.push(entity.parents[i]);
for (var i in this.parents) {
objects.push(this.parents[i]);
}
return objects;
};
entity.hasParentWays = function () {
},
hasParentWays: function () {
// summary: Does this entity have any parents which are ways?
var parentObjects = entity.parentObjects();
var parentObjects = this.parentObjects();
for (var i = 0; i < parentObjects.length; i++) {
if (parentObjects[i].entityType === 'way') return true;
}
};
entity.parentWays = function () {
return _parentObjectsOfClass('way');
};
entity.parentRelations = function () {
return _parentObjectsOfClass('relation');
};
function _parentObjectsOfClass(_class) {
},
parentWays: function () {
return this._parentObjectsOfClass('way');
},
parentRelations: function () {
return this._parentObjectsOfClass('relation');
},
_parentObjectsOfClass: function(_class) {
var poc = [];
var parentObjects = entity.parentObjects();
var parentObjects = this.parentObjects();
for (var i = 0; i < parentObjects.length; i++) {
if (parentObjects[i].entityType === _class) {
poc.push(parentObjects[i]);
@@ -57,6 +55,4 @@ iD.Entity = function () {
}
return poc;
}
return entity;
};

View File

@@ -6,7 +6,7 @@ iD.Node = function(id, lat, lon, tags, loaded) {
this.entityType = 'node';
this.id = id;
this._id = iD.Util.id();
this.entity = iD.Entity();
this.entity = new iD.Entity();
this.lat = lat;
this.lon = lon;
// TODO: keep or trash this custom

View File

@@ -1,8 +1,7 @@
if (typeof iD === 'undefined') iD = {};
iD.Relation = function(connection, id, members, tags, loaded) {
iD.Relation = function(id, members, tags, loaded) {
this.entityType = 'relation';
this.connection = connection;
this.id = id;
this._id = iD.Util.id();
this.members = members;

View File

@@ -8,14 +8,13 @@ if (typeof iD === 'undefined') iD = {};
//
// If a a way is _closed_, it is assumed to be an area unless it has a
// `highway` or `barrier` tag and is not also tagged `area`.
iD.Way = function(connection, id, nodes, tags, loaded) {
iD.Way = function(id, nodes, tags, loaded) {
// summary: An OSM way.
this.connection = connection;
this.entityType = 'way';
this.id = id;
this._id = iD.Util.id();
this.deleted = false;
this.entity = iD.Entity();
this.entity = new iD.Entity();
this.tags = tags || {};
this.loaded = (loaded === undefined) ? true : loaded;
this.modified = this.id < 0;

View File

@@ -2,7 +2,7 @@ describe('Entity', function () {
var entity;
beforeEach(function () {
entity = iD.Entity();
entity = new iD.Entity();
});
it('has no entity type', function () {
@@ -23,5 +23,5 @@ describe('Entity', function () {
entity.addParent({_id: 2, entityType: 'relation'});
expect(entity.parentRelations()).toEqual([{_id: 2, entityType: 'relation'}]);
});
})
});
});