From 80ea085f3522896331dde9c44f450fe794135c71 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Wed, 28 Nov 2012 15:13:31 -0500 Subject: [PATCH] Get node-dependent osmChange working --- js/id/actions/modes.js | 24 ++++++++++++++++++------ js/id/format/xml.js | 31 ++++++++++++++++++++++++------- js/id/graph/entity.js | 6 +++--- js/id/graph/graph.js | 12 ++++++++++-- js/id/graph/history.js | 4 +--- js/id/ui/commit.js | 8 +++++--- js/id/util.js | 6 ++++++ 7 files changed, 67 insertions(+), 24 deletions(-) diff --git a/js/id/actions/modes.js b/js/id/actions/modes.js index 713885edf..161d58e4a 100644 --- a/js/id/actions/modes.js +++ b/js/id/actions/modes.js @@ -55,7 +55,11 @@ iD.modes.AddPlace = { iD.modes.AddRoad = { title: "+ Road", way: function() { - return iD.Way({tags: {highway: 'residential'}}); + return iD.Way({ + tags: { + highway: 'residential' + } + }); }, enter: function() { var surface = this.map.surface; @@ -73,7 +77,7 @@ iD.modes.AddRoad = { }); }); - surface.on('click.addroad', function() { + function addRoad() { var t = d3.select(d3.event.target), node, way = this.way(); @@ -91,7 +95,9 @@ iD.modes.AddRoad = { this.map.perform(iD.actions.addWayNode(way, node)); this.map.selectClick(way); this.controller.enter(iD.modes.DrawRoad(way.id)); - }.bind(this)); + } + + surface.on('click.addroad', addRoad.bind(this)); d3.select(document).on('keydown.addroad', function() { if (d3.event.keyCode === 27) this.exit(); @@ -130,7 +136,7 @@ iD.modes.DrawRoad = function(way_id) { this.map.redraw(only); }.bind(this)); - surface.on('click.drawroad', function() { + function drawRoad() { var t = d3.select(d3.event.target); d3.event.stopPropagation(); if (t.data() && t.data()[0] && t.data()[0].type === 'node') { @@ -152,7 +158,9 @@ iD.modes.DrawRoad = function(way_id) { this.map.perform(iD.actions.addWayNode(way, node)); way.nodes = way.nodes.slice(); this.controller.enter(iD.modes.DrawRoad(way_id)); - }.bind(this)); + } + + surface.on('click.drawroad', drawRoad.bind(this)); }, exit: function() { this.map.surface.on('mousemove.drawroad', null); @@ -166,7 +174,11 @@ iD.modes.DrawRoad = function(way_id) { iD.modes.AddArea = { title: "+ Area", way: function() { - return iD.Way({tags: {building: 'yes'}}); + return iD.Way({ + tags: { + building: 'yes' + } + }); }, enter: function() { var surface = this.map.surface; diff --git a/js/id/format/xml.js b/js/id/format/xml.js index aa08237bb..cf8b4111e 100644 --- a/js/id/format/xml.js +++ b/js/id/format/xml.js @@ -7,6 +7,8 @@ iD.format.XML = { rep: function(entity) { if (iD.format.XML.reps[entity.type]) { return iD.format.XML.reps[entity.type](entity); + } else { + if (typeof console !== 'undefined') console.log(entity.type); } }, decode: function(s) { @@ -31,17 +33,30 @@ iD.format.XML = { // Generate [osmChange](http://wiki.openstreetmap.org/wiki/OsmChange) // XML. Returns a string. osmChange: function(userid, changeset_id, changes) { - return (new XMLSerializer()).serializeToString( - JXON.unbuild({ + function nest(x) { + var groups = {}; + for (var i = 0; i < x.length; i++) { + var tagName = Object.keys(x[i])[0]; + if (!groups[tagName]) groups[tagName] = []; + groups[tagName].push(x[i][tagName]); + } + var order = ['node', 'way', 'relation']; + var ordered = {}; + order.forEach(function(o) { + if (groups[o]) ordered[o] = groups[o]; + }); + return ordered; + } + var rep = { osmChange: { '@version': 0.3, '@generator': 'iD', // TODO: copy elements first - create: changes.create.map(function(c) { - var x = Object.create(c); + create: nest(changes.create.map(function(c) { + var x = iD.Entity(c); x.changeset = changeset_id; return x; - }).map(iD.format.XML.rep), + }).map(iD.format.XML.rep)), modify: changes.modify.map(function(c) { var x = Object.create(c); x.changeset = changeset_id; @@ -53,7 +68,9 @@ iD.format.XML = { return x; }).map(iD.format.XML.rep) } - })); + }; + console.log(rep); + return (new XMLSerializer()).serializeToString(JXON.unbuild(rep)); }, reps: { node: function(entity) { @@ -75,7 +92,7 @@ iD.format.XML = { way: { '@id': entity.id.replace('w', ''), nd: entity.nodes.map(function(e) { - return { keyAttributes: { ref: e.id } }; + return { keyAttributes: { ref: e.id.replace('n', '') } }; }), tag: _.map(entity.tags, function(v, k) { return { keyAttributes: { k: k, v: v } }; diff --git a/js/id/graph/entity.js b/js/id/graph/entity.js index 1c908cb65..36fe4c8b1 100644 --- a/js/id/graph/entity.js +++ b/js/id/graph/entity.js @@ -34,13 +34,13 @@ iD.Entity.prototype = { }; iD.Node = function (attrs) { - return iD.Entity(_.extend({}, attrs, {type: 'node'})); + return iD.Entity(_.extend({}, attrs || {}, {type: 'node'})); }; iD.Way = function (attrs) { - return iD.Entity(_.extend({}, attrs, {type: 'way', nodes: []})); + return iD.Entity(_.extend({}, attrs || {}, {type: 'way', nodes: []})); }; iD.Relation = function (attrs) { - return iD.Entity(_.extend({}, attrs, {type: 'relation'})); + return iD.Entity(_.extend({}, attrs || {}, {type: 'relation'})); }; diff --git a/js/id/graph/graph.js b/js/id/graph/graph.js index 952bb7e76..992c1d7fe 100644 --- a/js/id/graph/graph.js +++ b/js/id/graph/graph.js @@ -99,10 +99,18 @@ iD.Graph.prototype = { }, modifications: function() { - return _.filter(this.entities, function(entity) { return entity.modified(); }); + return _.filter(this.entities, function(entity) { + return entity.modified(); + }).map(function(e) { + return this.fetch(e.id); + }.bind(this)); }, creations: function() { - return _.filter(this.entities, function(entity) { return entity.created(); }); + return _.filter(this.entities, function(entity) { + return entity.created(); + }).map(function(e) { + return this.fetch(e.id); + }.bind(this)); } }; diff --git a/js/id/graph/history.js b/js/id/graph/history.js index 12a15ed0d..2a828c458 100644 --- a/js/id/graph/history.js +++ b/js/id/graph/history.js @@ -70,9 +70,7 @@ iD.History.prototype = { return _.difference( _.pluck(this.stack[0].entities, 'id'), _.pluck(this.stack[this.index].entities, 'id') - ).map(function(id) { - return this.stack[0].entity(id); - }.bind(this)); + ); }, changes: function() { diff --git a/js/id/ui/commit.js b/js/id/ui/commit.js index 8028cd5cb..b0d518bb3 100644 --- a/js/id/ui/commit.js +++ b/js/id/ui/commit.js @@ -29,9 +29,11 @@ iD.commit = function() { li.append('strong').text(function(d) { return d.type + ' '; }); - li.append('span').text(function(d) { - return iD.Util.friendlyName(d); - }); + li.append('span') + .text(function(d) { + return iD.Util.friendlyName(d); + }) + .attr('title', iD.Util.tagText); body.append('textarea') .attr('class', 'changeset-comment') diff --git a/js/id/util.js b/js/id/util.js index 52ba8898b..918750aaa 100644 --- a/js/id/util.js +++ b/js/id/util.js @@ -43,3 +43,9 @@ iD.Util.codeWindow = function(content) { top.win.document.writeln('
' + content + '
'); top.win.document.close(); }; + +iD.Util.tagText = function(entity) { + return d3.entries(entity.tags).map(function(e) { + return e.key + ': ' + e.value; + }).join('\n'); +};