From 4088f2e70ac93d4669ff7b00717a2d4ac27cdd10 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Fri, 5 Dec 2014 11:15:45 -0500 Subject: [PATCH] properly load changed entities into altgraph and produce diffs. --- js/id/core/graph.js | 5 +---- js/id/core/history.js | 4 ++++ js/id/id.js | 2 +- js/id/modes/save.js | 41 +++++++++++++++++++++++++++-------------- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/js/id/core/graph.js b/js/id/core/graph.js index 7d0f1452a..8a514b21e 100644 --- a/js/id/core/graph.js +++ b/js/id/core/graph.js @@ -16,10 +16,7 @@ iD.Graph = function(other, mutable) { this.transients = {}; this._childNodes = {}; - - if (!mutable) { - this.freeze(); - } + this.frozen = !mutable; }; iD.Graph.prototype = { diff --git a/js/id/core/history.js b/js/id/core/history.js index e6d948b90..b7b80fa6a 100644 --- a/js/id/core/history.js +++ b/js/id/core/history.js @@ -41,6 +41,10 @@ iD.History = function(context) { return stack[index].graph; }, + base: function() { + return stack[0].graph; + }, + merge: function(entities, extent) { stack[0].graph.rebase(entities, _.pluck(stack, 'graph'), false); tree.rebase(entities, false); diff --git a/js/id/id.js b/js/id/id.js index b1147c2bb..2bea474aa 100644 --- a/js/id/id.js +++ b/js/id/id.js @@ -56,7 +56,7 @@ window.iD = function () { connection.on('load.context', function loadContext(err, result) { if (altGraph) { - altGraph.rebase(result.data, [altGraph], false); + _.each(result.data, function(entity) { altGraph.replace(entity); }); } else { history.merge(result.data, result.extent); } diff --git a/js/id/modes/save.js b/js/id/modes/save.js index 44be0656a..3ab835a14 100644 --- a/js/id/modes/save.js +++ b/js/id/modes/save.js @@ -8,12 +8,13 @@ iD.modes.Save = function(context) { } function save(e) { - var altGraph = iD.Graph(), + var altGraph = iD.Graph(context.history().base(), true), history = context.history(), connection = context.connection(), changes = history.changes(iD.actions.DiscardTags(history.difference())), - toCheck = _.pluck(changes.modified, 'id'), loading = iD.ui.Loading(context).message(t('save.uploading')).blocking(true), + toCheck = _.pluck(changes.modified, 'id'), + toMerge = []; errors = []; context.container() @@ -21,32 +22,44 @@ iD.modes.Save = function(context) { // check for version conflicts.. reload modified entities into an alternate graph. context.altGraph(altGraph); + _.each(toCheck, check); - _.each(toCheck, function(id) { + function check(id) { connection.loadEntity(id, function(err) { - var version = context.entity(id).version, - altVersion = context.altGraph().entity(id).version; - toCheck = _.without(toCheck, id); - if (version !== altVersion) { - errors.push('Version mismatch for ' + id + ': local=' + version + ', server=' + altVersion); - } - if (err) { errors.push(err.responseText); } + else { + var entity = context.graph().entity(id), + altEntity = context.altGraph().entity(id); + + if (entity.version !== altEntity.version) { + toMerge.push(id); + errors.push('Version mismatch for ' + id + ': local=' + entity.version + ', server=' + altEntity.version); + } + } if (!toCheck.length) { finalize(); } }); - }); + } + function merge() { + var diff = context.history().difference(), + altDiff = iD.Difference(context.history().base(), context.altGraph()); + + // TODO + debugger; + } function finalize() { + if (toMerge.length) merge(); + if (errors.length) { - showErrors(errors); + showErrors(); } else { connection.putChangeset( changes, @@ -55,7 +68,7 @@ iD.modes.Save = function(context) { function(err, changeset_id) { if (err) { errors.push(err.responseText); - showErrors(errors); + showErrors(); } else { loading.close(); context.flush(); @@ -66,7 +79,7 @@ iD.modes.Save = function(context) { } - function showErrors(errors) { + function showErrors() { var confirm = iD.ui.confirm(context.container()); context.altGraph(undefined);