Make the Graph#rebase argument an array

This commit is contained in:
John Firebaugh
2013-10-31 15:58:06 -07:00
parent 14bfc52e33
commit 492fc88aa4
10 changed files with 63 additions and 77 deletions

View File

@@ -52,7 +52,7 @@ iD.Connection = function() {
url + '/api/0.6/' + type + '/' + osmID + (type !== 'node' ? '/full' : ''),
function(err, entities) {
event.load(err, {data: entities});
if (callback) callback(err, entities && entities[id]);
if (callback) callback(err, entities && _.find(entities, function(e) { return e.id === id; }));
});
};
@@ -137,15 +137,13 @@ iD.Connection = function() {
var root = dom.childNodes[0],
children = root.childNodes,
entities = {};
entities = [];
var i, o, l;
for (i = 0, l = children.length; i < l; i++) {
for (var i = 0, l = children.length; i < l; i++) {
var child = children[i],
parser = parsers[child.nodeName];
if (parser) {
o = parser(child);
entities[o.id] = o;
entities.push(parser(child));
}
}

View File

@@ -9,17 +9,10 @@ iD.Graph = function(other, mutable) {
this.inherited = true;
} else {
if (Array.isArray(other)) {
var entities = {};
for (var i = 0; i < other.length; i++) {
entities[other[i].id] = other[i];
}
other = entities;
}
this.entities = Object.create({});
this._parentWays = Object.create({});
this._parentRels = Object.create({});
this.rebase(other || {});
this.rebase(other || []);
}
this.transients = {};
@@ -108,11 +101,12 @@ iD.Graph.prototype = {
// Merging of data only needed if graph is the base graph
if (!this.inherited) {
for (i in entities) {
if (!base.entities[i]) {
base.entities[i] = entities[i];
this._updateCalculated(undefined, entities[i],
base.parentWays, base.parentRels);
for (i = 0; i < entities.length; i++) {
var entity = entities[i];
if (!base.entities[entity.id]) {
base.entities[entity.id] = entity;
this._updateCalculated(undefined, entity,
base.parentWays, base.parentRels);
}
}
}

View File

@@ -46,7 +46,7 @@ iD.History = function(context) {
stack[i].graph.rebase(entities);
}
tree.rebase(d3.values(entities));
tree.rebase(entities);
dispatch.change(undefined, extent);
},

View File

@@ -23,7 +23,7 @@ iD.ui.intro = function(context) {
for (var key in introGraph) {
introGraph[key] = iD.Entity(introGraph[key]);
}
context.history().merge(iD.Graph().load(introGraph).entities);
context.history().merge(d3.values(iD.Graph().load(introGraph).entities));
context.background().bing();
// Block saving
@@ -59,7 +59,7 @@ iD.ui.intro = function(context) {
navwrap.remove();
d3.select('.background-layer').style('opacity', opacity);
context.connection().toggle(true).flush().loadedTiles(loadedTiles);
context.history().reset().merge(baseEntities);
context.history().reset().merge(d3.values(baseEntities));
context.background().baseLayerSource(background);
if (history) context.history().fromJSON(history);
window.location.replace(hash);

View File

@@ -1,14 +1,14 @@
describe("iD.actions.MergePolygon", function () {
function node(id, x, y) {
e[id] = iD.Node({ id: id, loc: [x, y] });
e.push(iD.Node({ id: id, loc: [x, y] }));
}
function way(id, nodes) {
e[id] = iD.Way({ id: id, nodes: nodes.map(function(n) { return 'n' + n; }) });
e.push(iD.Way({ id: id, nodes: nodes.map(function(n) { return 'n' + n; }) }));
}
var e = {};
var e = [];
node('n0', 0, 0);
node('n1', 5, 0);
@@ -86,8 +86,8 @@ describe("iD.actions.MergePolygon", function () {
});
it("moves all tags to the relation", function() {
graph = graph.replace(e.w0.update({ tags: { 'building': 'yes' }}));
graph = graph.replace(e.w1.update({ tags: { 'natural': 'water' }}));
graph = graph.replace(graph.entity('w0').update({ tags: { 'building': 'yes' }}));
graph = graph.replace(graph.entity('w1').update({ tags: { 'natural': 'water' }}));
graph = iD.actions.MergePolygon(['w0', 'w1'], 'r')(graph);
var r = graph.entity('r');
expect(graph.entity('w0').tags.building).to.equal(undefined);
@@ -97,7 +97,7 @@ describe("iD.actions.MergePolygon", function () {
});
it("doesn't copy area tags from ways", function() {
graph = graph.replace(e.w0.update({ tags: { 'area': 'yes' }}));
graph = graph.replace(graph.entity('w0').update({ tags: { 'area': 'yes' }}));
graph = iD.actions.MergePolygon(['w0', 'w1'], 'r')(graph);
var r = graph.entity('r');
expect(r.tags.area).to.equal(undefined);

View File

@@ -59,15 +59,15 @@ describe('iD.Connection', function () {
});
it('parses a node', function (done) {
c.loadFromURL('data/node.xml', function (err, graph) {
expect(graph.n356552551).to.be.instanceOf(iD.Entity);
c.loadFromURL('data/node.xml', function (err, entities) {
expect(entities[0]).to.be.instanceOf(iD.Entity);
done();
});
});
it('parses a way', function (done) {
c.loadFromURL('data/way.xml', function (err, graph) {
expect(graph.w19698713).to.be.instanceOf(iD.Entity);
c.loadFromURL('data/way.xml', function (err, entities) {
expect(entities[0]).to.be.instanceOf(iD.Entity);
done();
});
});
@@ -116,7 +116,7 @@ describe('iD.Connection', function () {
it('emits a load event', function(done) {
c.loadEntity('n1');
c.on('load', function(error, result) {
expect(result.data.n1).to.be.an.instanceOf(iD.Node);
expect(result.data[0]).to.be.an.instanceOf(iD.Node);
done();
});

View File

@@ -1,11 +1,5 @@
describe('iD.Graph', function() {
describe("constructor", function () {
it("accepts an entities Object", function () {
var entity = iD.Entity(),
graph = iD.Graph({'n-1': entity});
expect(graph.entity('n-1')).to.equal(entity);
});
it("accepts an entities Array", function () {
var entity = iD.Entity(),
graph = iD.Graph([entity]);
@@ -80,14 +74,14 @@ describe('iD.Graph', function() {
it("preserves existing entities", function () {
var node = iD.Node({id: 'n'}),
graph = iD.Graph([node]);
graph.rebase({});
graph.rebase([]);
expect(graph.entity('n')).to.equal(node);
});
it("includes new entities", function () {
var node = iD.Node({id: 'n'}),
graph = iD.Graph();
graph.rebase({'n': node});
graph.rebase([node]);
expect(graph.entity('n')).to.equal(node);
});
@@ -95,13 +89,13 @@ describe('iD.Graph', function() {
var a = iD.Node({id: 'n'}),
b = iD.Node({id: 'n'}),
graph = iD.Graph([a]);
graph.rebase({'n': b});
graph.rebase([b]);
expect(graph.entity('n')).to.equal(a);
});
it("inherits entities from base prototypally", function () {
var graph = iD.Graph();
graph.rebase({'n': iD.Node()});
graph.rebase([iD.Node()]);
expect(graph.entities).not.to.have.ownProperty('n');
});
@@ -111,7 +105,7 @@ describe('iD.Graph', function() {
w2 = iD.Way({id: 'w2', nodes: ['n']}),
graph = iD.Graph([n, w1]);
graph.rebase({ 'w2': w2 });
graph.rebase([w2]);
expect(graph.parentWays(n)).to.eql([w1, w2]);
expect(graph._parentWays.hasOwnProperty('n')).to.be.false;
});
@@ -120,7 +114,7 @@ describe('iD.Graph', function() {
var n = iD.Node({id: 'n'}),
w1 = iD.Way({id: 'w1', nodes: ['n']}),
graph = iD.Graph([n, w1]);
graph.rebase({ 'w1': w1 });
graph.rebase([w1]);
expect(graph.parentWays(n)).to.eql([w1]);
});
@@ -132,8 +126,8 @@ describe('iD.Graph', function() {
graph = iD.Graph([n, w1]),
graph2 = graph.replace(w2);
graph.rebase({ 'w3': w3 });
graph2.rebase({ 'w3': w3 });
graph.rebase([w3]);
graph2.rebase([w3]);
expect(graph2.parentWays(n)).to.eql([w1, w2, w3]);
});
@@ -146,8 +140,8 @@ describe('iD.Graph', function() {
graph = iD.Graph([n1, n2, w1]),
graph2 = graph.replace(w2);
graph.rebase({ 'w1': w1 });
graph2.rebase({ 'w1': w1 });
graph.rebase([w1]);
graph2.rebase([w1]);
expect(graph2.parentWays(n2)).to.eql([]);
});
@@ -158,8 +152,8 @@ describe('iD.Graph', function() {
graph = iD.Graph([n, w1]),
graph2 = graph.remove(w1);
graph.rebase({ 'w1': w1 });
graph2.rebase({ 'w1': w1 });
graph.rebase([w1]);
graph2.rebase([w1]);
expect(graph2.parentWays(n)).to.eql([]);
});
@@ -170,7 +164,7 @@ describe('iD.Graph', function() {
r2 = iD.Relation({id: 'r2', members: [{id: 'n'}]}),
graph = iD.Graph([n, r1]);
graph.rebase({'r2': r2});
graph.rebase([r2]);
expect(graph.parentRelations(n)).to.eql([r1, r2]);
expect(graph._parentRels.hasOwnProperty('n')).to.be.false;
@@ -183,8 +177,8 @@ describe('iD.Graph', function() {
graph = iD.Graph([n, r1]),
graph2 = graph.replace(r2);
graph.rebase({ 'r1': r1 });
graph2.rebase({ 'r1': r1 });
graph.rebase([r1]);
graph2.rebase([r1]);
expect(graph2.parentRelations(n)).to.eql([]);
});
@@ -195,8 +189,8 @@ describe('iD.Graph', function() {
graph = iD.Graph([n, r1]),
graph2 = graph.remove(r1);
graph.rebase({ 'r1': r1 });
graph2.rebase({ 'r1': r1 });
graph.rebase([r1]);
graph2.rebase([r1]);
expect(graph2.parentRelations(n)).to.eql([]);
});
@@ -209,8 +203,8 @@ describe('iD.Graph', function() {
graph = iD.Graph([n, r1]),
graph2 = graph.replace(r2);
graph.rebase({'r3': r3});
graph2.rebase({'r3': r3});
graph.rebase([r3]);
graph2.rebase([r3]);
expect(graph2.parentRelations(n)).to.eql([r1, r2, r3]);
});
@@ -227,7 +221,7 @@ describe('iD.Graph', function() {
}
expect(numParents(n)).to.equal(1);
graph.rebase({w2: w2});
graph.rebase([w2]);
expect(numParents(n)).to.equal(2);
});
});

View File

@@ -19,14 +19,14 @@ describe("iD.History", function () {
describe("#merge", function () {
it("merges the entities into all graph versions", function () {
var n = iD.Node({id: 'n'});
history.merge({n: n});
history.merge([n]);
expect(history.graph().entity('n')).to.equal(n);
});
it("emits a change event with the specified extent", function () {
var extent = {};
history.on('change', spy);
history.merge({}, extent);
history.merge([], extent);
expect(spy).to.have.been.calledWith(undefined, extent);
});
});
@@ -186,14 +186,14 @@ describe("iD.History", function () {
it("includes modified entities", function () {
var node1 = iD.Node({id: "n1"}),
node2 = node1.update({ tags: { yes: "no" } });
history.merge({ n1: node1});
history.merge([node1]);
history.perform(function (graph) { return graph.replace(node2); });
expect(history.changes().modified).to.eql([node2]);
});
it("includes deleted entities", function () {
var node = iD.Node({id: "n1"});
history.merge({ n1: node });
history.merge([node]);
history.perform(function (graph) { return graph.remove(node); });
expect(history.changes().deleted).to.eql([node]);
});
@@ -231,7 +231,7 @@ describe("iD.History", function () {
describe("#toJSON", function() {
it("generates v2 JSON", function() {
var node = iD.Node({id: 'n-1'});
history.merge({n1: iD.Node({id: 'n1'})});
history.merge([iD.Node({id: 'n1'})]);
history.perform(iD.actions.AddEntity(node));
var json = JSON.parse(history.toJSON());
expect(json.version).to.eql(2);
@@ -283,7 +283,7 @@ describe("iD.History", function () {
"index": 1
};
history.fromJSON(JSON.stringify(json));
history.merge({n1: iD.Node({id: 'n1'})});
history.merge([iD.Node({id: 'n1'})]);
expect(history.graph().hasEntity('n1')).to.be.undefined;
expect(history.undoAnnotation()).to.eql("Deleted a point.");
expect(history.imageryUsed()).to.eql(["Bing"]);
@@ -344,7 +344,7 @@ describe("iD.History", function () {
"index": 1
};
history.fromJSON(JSON.stringify(json));
history.merge({n1: iD.Node({id: 'n1'})});
history.merge([iD.Node({id: 'n1'})]);
expect(history.graph().hasEntity('n1')).to.be.undefined;
expect(history.undoAnnotation()).to.eql("Deleted a point.");
expect(history.imageryUsed()).to.eql(["Bing"]);

View File

@@ -5,7 +5,7 @@ describe("iD.Tree", function() {
tree = iD.Tree(graph),
node = iD.Node({id: 'n', loc: [1, 1]});
graph.rebase({n: node});
graph.rebase([node]);
tree.rebase([node]);
expect(tree.intersects(iD.geo.Extent([0, 0], [2, 2]), graph)).to.eql([node]);
@@ -17,11 +17,11 @@ describe("iD.Tree", function() {
node = iD.Node({id: 'n', loc: [1, 1]}),
extent = iD.geo.Extent([0, 0], [2, 2]);
graph.rebase({n: node});
graph.rebase([node]);
tree.rebase([node]);
expect(tree.intersects(extent, graph)).to.eql([node]);
graph.rebase({n: node});
graph.rebase([node]);
tree.rebase([node]);
expect(tree.intersects(extent, graph)).to.eql([node]);
});
@@ -63,11 +63,11 @@ describe("iD.Tree", function() {
relation = iD.Relation({id: 'r', members: [{id: 'n1'}, {id: 'n2'}]}),
extent = iD.geo.Extent([0.5, 0.5], [1.5, 1.5]);
graph.rebase({r: relation, n1: n1});
graph.rebase([relation, n1]);
tree.rebase([relation, n1]);
expect(tree.intersects(extent, graph)).to.eql([]);
graph.rebase({n2: n2});
graph.rebase([n2]);
tree.rebase([n2]);
expect(tree.intersects(extent, graph)).to.eql([n2, relation]);
});
@@ -83,8 +83,8 @@ describe("iD.Tree", function() {
expect(tree.intersects(extent, graph)).to.eql([]);
base.rebase({n: node});
graph.rebase({n: node});
base.rebase([node]);
graph.rebase([node]);
tree.rebase([node]);
expect(tree.intersects(extent, graph)).to.eql([node, way]);
});
@@ -140,7 +140,7 @@ describe("iD.Tree", function() {
var graph = base.replace(node).remove(node);
expect(tree.intersects(extent, graph)).to.eql([]);
base.rebase({n: node});
base.rebase([node]);
tree.rebase([node]);
expect(tree.intersects(extent, graph)).to.eql([]);
});
@@ -156,8 +156,8 @@ describe("iD.Tree", function() {
var graph = base.replace(r1).replace(r2);
expect(tree.intersects(extent, graph)).to.eql([]);
base.rebase({n: node});
graph.rebase({n: node});
base.rebase([node]);
graph.rebase([node]);
tree.rebase([node]);
expect(tree.intersects(extent, graph)).to.eql([node, r1, r2]);
});

View File

@@ -16,7 +16,7 @@ describe('iD.ui.RawTagEditor', function() {
beforeEach(function () {
entity = iD.Node({id: "n12345"});
context = iD();
context.history().merge({n12345: entity});
context.history().merge([entity]);
render({highway: 'residential'});
});