diff --git a/js/id/graph/relation.js b/js/id/graph/relation.js index 76ed0bbec..1ed9e8ff1 100644 --- a/js/id/graph/relation.js +++ b/js/id/graph/relation.js @@ -66,6 +66,23 @@ _.extend(iD.Relation.prototype, { return this.update({members: members}); }, + asJXON: function(changeset_id) { + var r = { + relation: { + '@id': this.osmId(), + '@version': this.version || 0, + member: _.map(this.members, function(member) { + return { keyAttributes: { type: member.type, role: member.role, ref: iD.Entity.id.toOSM(member.id) } }; + }), + tag: _.map(this.tags, function(v, k) { + return { keyAttributes: { k: k, v: v } }; + }) + } + }; + if (changeset_id) r.relation['@changeset'] = changeset_id; + return r; + }, + isRestriction: function() { return !!(this.tags.type && this.tags.type.match(/^restriction:?/)); }, diff --git a/test/spec/graph/relation.js b/test/spec/graph/relation.js index d8619726a..8edf9c1e7 100644 --- a/test/spec/graph/relation.js +++ b/test/spec/graph/relation.js @@ -140,6 +140,21 @@ describe('iD.Relation', function () { }); }); + describe("#asJXON", function () { + it('converts a relation to jxon', function() { + var relation = iD.Relation({id: 'r-1', members: [{id: 'w1', role: 'forward', type: 'way'}], tags: {type: 'route'}}); + expect(relation.asJXON()).to.eql({relation: { + '@id': '-1', + '@version': 0, + member: [{keyAttributes: {ref: '1', role: 'forward', type: 'way'}}], + tag: [{keyAttributes: {k: 'type', v: 'route'}}]}}); + }); + + it('includes changeset if provided', function() { + expect(iD.Relation().asJXON('1234').relation['@changeset']).to.equal('1234'); + }); + }); + describe("#multipolygon", function () { specify("single polygon consisting of a single way", function () { var a = iD.Node(),