From 0ae54abdcc289970864d146a33462536da2d73f8 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Sun, 12 May 2013 12:40:00 -0700 Subject: [PATCH] Extract iD.actions.DiscardTags --- index.html | 1 + js/id/actions/discard_tags.js | 16 ++++++++++++++++ js/id/core/history.js | 21 +++++++++------------ js/id/ui/save.js | 2 +- test/index.html | 2 ++ test/index_packaged.html | 1 + test/spec/actions/discard_tags.js | 25 +++++++++++++++++++++++++ 7 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 js/id/actions/discard_tags.js create mode 100644 test/spec/actions/discard_tags.js diff --git a/index.html b/index.html index 3b4f714a3..b1c211b11 100644 --- a/index.html +++ b/index.html @@ -125,6 +125,7 @@ + diff --git a/js/id/actions/discard_tags.js b/js/id/actions/discard_tags.js new file mode 100644 index 000000000..855db3b13 --- /dev/null +++ b/js/id/actions/discard_tags.js @@ -0,0 +1,16 @@ +iD.actions.DiscardTags = function(difference) { + return function(graph) { + function discardTags(entity) { + if (!_.isEmpty(entity.tags)) { + graph = graph.replace(entity.update({ + tags: _.omit(entity.tags, iD.data.discarded) + })); + } + } + + difference.modified().forEach(discardTags); + difference.created().forEach(discardTags); + + return graph; + } +}; diff --git a/js/id/core/history.js b/js/id/core/history.js index 498b8bb92..7a315a965 100644 --- a/js/id/core/history.js +++ b/js/id/core/history.js @@ -137,22 +137,19 @@ iD.History = function(context) { return iD.Difference(base, head); }, - changes: function() { - var difference = history.difference(); + changes: function(action) { + var base = stack[0].graph, + head = stack[index].graph; - function discardTags(entity) { - if (_.isEmpty(entity.tags)) { - return entity; - } else { - return entity.update({ - tags: _.omit(entity.tags, iD.data.discarded) - }); - } + if (action) { + head = action(head); } + var difference = iD.Difference(base, head); + return { - modified: difference.modified().map(discardTags), - created: difference.created().map(discardTags), + modified: difference.modified(), + created: difference.created(), deleted: difference.deleted() }; }, diff --git a/js/id/ui/save.js b/js/id/ui/save.js index a6c2df16d..ddcb6a9e6 100644 --- a/js/id/ui/save.js +++ b/js/id/ui/save.js @@ -38,7 +38,7 @@ iD.ui.Save = function(context) { .call(loading); connection.putChangeset( - history.changes(), + history.changes(iD.actions.DiscardTags(history.difference())), e.comment, history.imagery_used(), function(err, changeset_id) { diff --git a/test/index.html b/test/index.html index 8fa705b2c..0f1bf2166 100644 --- a/test/index.html +++ b/test/index.html @@ -110,6 +110,7 @@ + @@ -191,6 +192,7 @@ + diff --git a/test/index_packaged.html b/test/index_packaged.html index 2d7e7c55d..31110349d 100644 --- a/test/index_packaged.html +++ b/test/index_packaged.html @@ -34,6 +34,7 @@ + diff --git a/test/spec/actions/discard_tags.js b/test/spec/actions/discard_tags.js new file mode 100644 index 000000000..3a5e3f612 --- /dev/null +++ b/test/spec/actions/discard_tags.js @@ -0,0 +1,25 @@ +describe("iD.actions.DiscardTags", function() { + it("discards obsolete tags from modified entities", function() { + var way = iD.Way({id: 'w1', tags: {created_by: 'Potlatch'}}), + base = iD.Graph([way]), + head = base.replace(way.update({tags: {created_by: 'Potlatch', foo: 'bar'}})), + action = iD.actions.DiscardTags(iD.Difference(base, head)); + expect(action(head).entity(way.id).tags).to.eql({foo: 'bar'}); + }); + + it("discards obsolete tags from created entities", function() { + var way = iD.Way({tags: {created_by: 'Potlatch'}}), + base = iD.Graph(), + head = base.replace(way), + action = iD.actions.DiscardTags(iD.Difference(base, head)); + expect(action(head).entity(way.id).tags).to.eql({}); + }); + + it("doesn't modify entities without obsolete tags", function() { + var way = iD.Way(), + base = iD.Graph(), + head = base.replace(way), + action = iD.actions.DiscardTags(iD.Difference(base, head)); + expect(action(head).entity(way.id)).to.equal(way); + }); +});