mirror of
https://github.com/FoggedLens/iD.git
synced 2026-02-13 01:02:58 +00:00
Move transients to graph (fixes #285)
This commit is contained in:
@@ -2,13 +2,12 @@ iD.Entity = function(a, b, c) {
|
||||
if (!(this instanceof iD.Entity)) return new iD.Entity(a, b, c);
|
||||
|
||||
this.tags = {};
|
||||
this.transients = {};
|
||||
|
||||
var sources = [a, b, c], source;
|
||||
for (var i = 0; i < sources.length; ++i) {
|
||||
source = sources[i];
|
||||
for (var prop in source) {
|
||||
if (prop !== 'transients' && Object.prototype.hasOwnProperty.call(source, prop)) {
|
||||
if (Object.prototype.hasOwnProperty.call(source, prop)) {
|
||||
this[prop] = source[prop];
|
||||
}
|
||||
}
|
||||
@@ -59,35 +58,34 @@ iD.Entity.prototype = {
|
||||
return this._updated && this.osmId() > 0;
|
||||
},
|
||||
|
||||
intersects: function(extent, resolver) {
|
||||
extent: function(resolver) {
|
||||
if (this.type === 'node') {
|
||||
return this.loc[0] > extent[0][0] &&
|
||||
this.loc[0] < extent[1][0] &&
|
||||
this.loc[1] < extent[0][1] &&
|
||||
this.loc[1] > extent[1][1];
|
||||
} else if (this.type === 'way') {
|
||||
var _extent = this.transients.extent;
|
||||
|
||||
if (!_extent) {
|
||||
_extent = this.transients.extent = [[-Infinity, Infinity], [Infinity, -Infinity]];
|
||||
return [this.loc, this.loc];
|
||||
} else if (this.type == 'way') {
|
||||
return resolver.transient(this, 'extent', function() {
|
||||
var extent = [[-Infinity, Infinity], [Infinity, -Infinity]];
|
||||
for (var i = 0, l = this.nodes.length; i < l; i++) {
|
||||
var node = resolver.entity(this.nodes[i]);
|
||||
if (node.loc[0] > _extent[0][0]) _extent[0][0] = node.loc[0];
|
||||
if (node.loc[0] < _extent[1][0]) _extent[1][0] = node.loc[0];
|
||||
if (node.loc[1] < _extent[0][1]) _extent[0][1] = node.loc[1];
|
||||
if (node.loc[1] > _extent[1][1]) _extent[1][1] = node.loc[1];
|
||||
if (node.loc[0] > extent[0][0]) extent[0][0] = node.loc[0];
|
||||
if (node.loc[0] < extent[1][0]) extent[1][0] = node.loc[0];
|
||||
if (node.loc[1] < extent[0][1]) extent[0][1] = node.loc[1];
|
||||
if (node.loc[1] > extent[1][1]) extent[1][1] = node.loc[1];
|
||||
}
|
||||
}
|
||||
|
||||
return _extent[0][0] > extent[0][0] &&
|
||||
_extent[1][0] < extent[1][0] &&
|
||||
_extent[0][1] < extent[0][1] &&
|
||||
_extent[1][1] > extent[1][1];
|
||||
return extent;
|
||||
});
|
||||
} else {
|
||||
return false;
|
||||
return [[NaN, NaN], [NaN, NaN]];
|
||||
}
|
||||
},
|
||||
|
||||
intersects: function(extent, resolver) {
|
||||
var _extent = this.extent(resolver);
|
||||
return _extent[0][0] > extent[0][0] &&
|
||||
_extent[1][0] < extent[1][0] &&
|
||||
_extent[0][1] < extent[0][1] &&
|
||||
_extent[1][1] > extent[1][1];
|
||||
},
|
||||
|
||||
hasInterestingTags: function() {
|
||||
return _.keys(this.tags).some(function (key) {
|
||||
return key != "attribution" &&
|
||||
|
||||
@@ -10,6 +10,8 @@ iD.Graph = function(entities) {
|
||||
this.entities = entities || {};
|
||||
}
|
||||
|
||||
this.transients = {};
|
||||
|
||||
if (iD.debug) {
|
||||
Object.freeze(this);
|
||||
Object.freeze(this.entities);
|
||||
@@ -21,6 +23,17 @@ iD.Graph.prototype = {
|
||||
return this.entities[id];
|
||||
},
|
||||
|
||||
transient: function(entity, key, fn) {
|
||||
var id = entity.id,
|
||||
transients = this.transients[id] || (this.transients[id] = {});
|
||||
|
||||
if (transients[key]) {
|
||||
return transients[key];
|
||||
}
|
||||
|
||||
return transients[key] = fn.call(entity);
|
||||
},
|
||||
|
||||
parentStructure: function(ways) {
|
||||
var nodes = {};
|
||||
ways.forEach(function(w) {
|
||||
|
||||
@@ -7,10 +7,6 @@ describe('iD.Entity', function () {
|
||||
it("freezes tags", function () {
|
||||
expect(Object.isFrozen(iD.Entity().tags)).to.be.true;
|
||||
});
|
||||
|
||||
it("does not freeze transients", function () {
|
||||
expect(Object.isFrozen(iD.Entity().transients)).to.be.false;
|
||||
});
|
||||
}
|
||||
|
||||
describe(".id", function () {
|
||||
@@ -57,12 +53,6 @@ describe('iD.Entity', function () {
|
||||
expect(attrs).to.eql({tags: {foo: 'bar'}});
|
||||
});
|
||||
|
||||
it("doesn't copy transients", function () {
|
||||
var entity = iD.Entity();
|
||||
entity.transients['foo'] = 'bar';
|
||||
expect(entity.update({}).transients).not.to.have.property('foo');
|
||||
});
|
||||
|
||||
it("doesn't copy prototype properties", function () {
|
||||
expect(iD.Entity().update({})).not.to.have.ownProperty('update');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user