mirror of
https://github.com/FoggedLens/iD.git
synced 2026-05-18 14:45:12 +02:00
Convert iD.format.XML to member functions
This commit is contained in:
+3
-57
@@ -1,19 +1,8 @@
|
||||
iD.format.XML = {
|
||||
mapping: function(entity) {
|
||||
if (iD.format.XML.mappings[entity.type]) {
|
||||
return iD.format.XML.mappings[entity.type](entity);
|
||||
}
|
||||
},
|
||||
rep: function(entity, changeset_id) {
|
||||
if (iD.format.XML.reps[entity.type]) {
|
||||
return iD.format.XML.reps[entity.type](entity, changeset_id);
|
||||
} else {
|
||||
if (typeof console !== 'undefined') console.log(entity.type);
|
||||
}
|
||||
},
|
||||
decode: function(s) {
|
||||
return s.replace(/>/g,'>').replace(/</g,'<').replace(/"/g,'"');
|
||||
},
|
||||
|
||||
// Generate Changeset XML. Returns a string.
|
||||
changeset: function(tags) {
|
||||
return (new XMLSerializer()).serializeToString(
|
||||
@@ -29,6 +18,7 @@ iD.format.XML = {
|
||||
}
|
||||
}));
|
||||
},
|
||||
|
||||
// Generate [osmChange](http://wiki.openstreetmap.org/wiki/OsmChange)
|
||||
// XML. Returns a string.
|
||||
osmChange: function(userid, changeset_id, changes) {
|
||||
@@ -48,7 +38,7 @@ iD.format.XML = {
|
||||
}
|
||||
|
||||
function rep(entity) {
|
||||
return iD.format.XML.rep(entity, changeset_id);
|
||||
return entity.asJXON(changeset_id);
|
||||
}
|
||||
|
||||
return (new XMLSerializer()).serializeToString(JXON.unbuild({
|
||||
@@ -65,49 +55,5 @@ iD.format.XML = {
|
||||
})
|
||||
}
|
||||
}));
|
||||
},
|
||||
reps: {
|
||||
node: function(entity, changeset_id) {
|
||||
var r = {
|
||||
node: {
|
||||
'@id': entity.id.replace('n', ''),
|
||||
'@lon': entity.loc[0],
|
||||
'@lat': entity.loc[1],
|
||||
'@version': (entity.version || 0),
|
||||
tag: _.map(entity.tags, function(v, k) {
|
||||
return { keyAttributes: { k: k, v: v } };
|
||||
})
|
||||
}
|
||||
};
|
||||
if (changeset_id) r.node['@changeset'] = changeset_id;
|
||||
return r;
|
||||
},
|
||||
way: function(entity, changeset_id) {
|
||||
var r = {
|
||||
way: {
|
||||
'@id': entity.id.replace('w', ''),
|
||||
nd: entity.nodes.map(function(id) {
|
||||
return { keyAttributes: { ref: id.replace('n', '') } };
|
||||
}),
|
||||
'@version': (entity.version || 0),
|
||||
tag: _.map(entity.tags, function(v, k) {
|
||||
return { keyAttributes: { k: k, v: v } };
|
||||
})
|
||||
}
|
||||
};
|
||||
if (changeset_id) r.way['@changeset'] = changeset_id;
|
||||
return r;
|
||||
}
|
||||
},
|
||||
mappings: {
|
||||
node: function(entity) {
|
||||
return iD.format.XML.decode((new XMLSerializer()).serializeToString(
|
||||
JXON.unbuild(iD.format.XML.reps.node(entity))
|
||||
));
|
||||
},
|
||||
way: function(entity) {
|
||||
return iD.format.XML.decode((new XMLSerializer()).serializeToString(
|
||||
JXON.unbuild(iD.format.XML.reps.way(entity))));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -23,6 +23,22 @@ _.extend(iD.Node.prototype, {
|
||||
return this.update({loc: loc});
|
||||
},
|
||||
|
||||
asJXON: function(changeset_id) {
|
||||
var r = {
|
||||
node: {
|
||||
'@id': this.id.replace('n', ''),
|
||||
'@lon': this.loc[0],
|
||||
'@lat': this.loc[1],
|
||||
'@version': (this.version || 0),
|
||||
tag: _.map(this.tags, function(v, k) {
|
||||
return { keyAttributes: { k: k, v: v } };
|
||||
})
|
||||
}
|
||||
};
|
||||
if (changeset_id) r.node['@changeset'] = changeset_id;
|
||||
return r;
|
||||
},
|
||||
|
||||
asGeoJSON: function() {
|
||||
return {
|
||||
type: 'Feature',
|
||||
|
||||
@@ -85,6 +85,23 @@ _.extend(iD.Way.prototype, {
|
||||
return this.update({nodes: nodes});
|
||||
},
|
||||
|
||||
asJXON: function(changeset_id) {
|
||||
var r = {
|
||||
way: {
|
||||
'@id': this.id.replace('w', ''),
|
||||
'@version': this.version || 0,
|
||||
nd: _.map(this.nodes, function(id) {
|
||||
return { keyAttributes: { ref: id.replace('n', '') } };
|
||||
}),
|
||||
tag: _.map(this.tags, function(v, k) {
|
||||
return { keyAttributes: { k: k, v: v } };
|
||||
})
|
||||
}
|
||||
};
|
||||
if (changeset_id) r.way['@changeset'] = changeset_id;
|
||||
return r;
|
||||
},
|
||||
|
||||
asGeoJSON: function(resolver) {
|
||||
return {
|
||||
type: 'Feature',
|
||||
|
||||
@@ -8,42 +8,10 @@ describe('iD.format.XML', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#rep', function() {
|
||||
it('converts a node to jxon', function() {
|
||||
expect(iD.format.XML.rep(node))
|
||||
.to.eql({ node : { '@id': '-1', '@lat': 38, '@lon': -77, '@version': 0, tag: [ ] } });
|
||||
});
|
||||
|
||||
it('converts a way to jxon', function() {
|
||||
expect(iD.format.XML.rep(way))
|
||||
.to.eql({ way : { '@id' : '-1', nd : [ ], '@version': 0, tag: [ ] } });
|
||||
});
|
||||
|
||||
it('includes changeset if provided', function() {
|
||||
expect(iD.format.XML.rep(node, '1234'))
|
||||
.to.eql({ node : { '@id': '-1', '@lat': 38, '@lon': -77, '@version': 0, '@changeset': '1234', tag: [ ] } });
|
||||
|
||||
expect(iD.format.XML.rep(way, '1234'))
|
||||
.to.eql({ way : { '@id' : '-1', nd : [ ], '@version': 0, '@changeset': '1234', tag: [ ] } });
|
||||
});
|
||||
});
|
||||
|
||||
describe('#osmChange', function() {
|
||||
it('converts change data to XML', function() {
|
||||
var jxon = iD.format.XML.osmChange('jfire', '1234', {created: [node], modified: [way], deleted: []});
|
||||
expect(jxon).to.eql('<osmChange version="0.3" generator="iD"><create><node id="-1" lon="-77" lat="38" version="0" changeset="1234"/></create><modify><way id="-1" version="0" changeset="1234"/></modify></osmChange>');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#mapping', function() {
|
||||
it('serializes a node to xml', function() {
|
||||
expect(iD.format.XML.mapping({ id: 'n-1', type: 'node', loc: [-77, 38] }))
|
||||
.to.equal('<node id="-1" lon="-77" lat="38" version="0"/>');
|
||||
});
|
||||
|
||||
it('serializes a way to xml', function() {
|
||||
expect(iD.format.XML.mapping({ type: 'way', nodes: [], id: 'w-1' }))
|
||||
.to.equal('<way id="-1" version="0"/>');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -52,6 +52,22 @@ describe('iD.Node', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe("#asJXON", function () {
|
||||
it('converts a node to jxon', function() {
|
||||
var node = iD.Node({id: 'n-1', loc: [-77, 38], tags: {amenity: 'cafe'}});
|
||||
expect(node.asJXON()).to.eql({node: {
|
||||
'@id': '-1',
|
||||
'@lon': -77,
|
||||
'@lat': 38,
|
||||
'@version': 0,
|
||||
tag: [{keyAttributes: {k: 'amenity', v: 'cafe'}}]}});
|
||||
});
|
||||
|
||||
it('includes changeset if provided', function() {
|
||||
expect(iD.Node({loc: [0, 0]}).asJXON('1234').node['@changeset']).to.equal('1234');
|
||||
});
|
||||
});
|
||||
|
||||
describe("#asGeoJSON", function () {
|
||||
it("converts to a GeoJSON Point features", function () {
|
||||
var node = iD.Node({tags: {amenity: 'cafe'}, loc: [1, 2]}),
|
||||
|
||||
@@ -200,6 +200,21 @@ describe('iD.Way', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe("#asJXON", function () {
|
||||
it('converts a way to jxon', function() {
|
||||
var node = iD.Way({id: 'w-1', nodes: ['n1', 'n2'], tags: {highway: 'residential'}});
|
||||
expect(node.asJXON()).to.eql({way: {
|
||||
'@id': '-1',
|
||||
'@version': 0,
|
||||
nd: [{keyAttributes: {ref: '1'}}, {keyAttributes: {ref: '2'}}],
|
||||
tag: [{keyAttributes: {k: 'highway', v: 'residential'}}]}});
|
||||
});
|
||||
|
||||
it('includes changeset if provided', function() {
|
||||
expect(iD.Way().asJXON('1234').way['@changeset']).to.equal('1234');
|
||||
});
|
||||
});
|
||||
|
||||
describe("#asGeoJSON", function () {
|
||||
it("converts to a GeoJSON LineString features", function () {
|
||||
var a = iD.Node({loc: [1, 2]}),
|
||||
|
||||
Reference in New Issue
Block a user