From 0d2a742e1892d28ae5026148292f3495eac6a8fb Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Sun, 24 Jan 2016 12:44:33 -0800 Subject: [PATCH] Autocomplete changeset comments from previous changeset comments Fixes #2002 --- js/id/core/connection.js | 17 ++++++++++++++++ js/id/ui/commit.js | 27 +++++++++++++++++++++---- test/spec/core/connection.js | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/js/id/core/connection.js b/js/id/core/connection.js index d73eda007..c000305a5 100644 --- a/js/id/core/connection.js +++ b/js/id/core/connection.js @@ -322,6 +322,23 @@ iD.Connection = function(useHttps) { oauth.xhr({ method: 'GET', path: '/api/0.6/user/details' }, done); }; + connection.userChangesets = function(callback) { + connection.userDetails(function(err, user) { + if (err) return callback(err); + + function done(changesets) { + callback(undefined, Array.prototype.map.call(changesets.getElementsByTagName('changeset'), + function (changeset) { + return { tags: getTags(changeset) }; + })); + } + + d3.xml(url + '/api/0.6/changesets?user=' + user.id).get() + .on('load', done) + .on('error', callback); + }); + }; + connection.status = function(callback) { function done(capabilities) { var apiStatus = capabilities.getElementsByTagName('status'); diff --git a/js/id/ui/commit.js b/js/id/ui/commit.js index 54faceb8d..e3f5343f2 100644 --- a/js/id/ui/commit.js +++ b/js/id/ui/commit.js @@ -38,16 +38,35 @@ iD.ui.Commit = function(context) { .attr('placeholder', t('commit.description_placeholder')) .attr('maxlength', 255) .property('value', context.storage('comment') || '') - .on('input.save', function() { - d3.selectAll('.save-section .save-button') - .attr('disabled', (this.value.length ? null : true)); - }) + .on('input.save', enableDisableSaveButton) + .on('change.save', enableDisableSaveButton) .on('blur.save', function() { context.storage('comment', this.value); }); + function enableDisableSaveButton() { + d3.selectAll('.save-section .save-button') + .attr('disabled', (this.value.length ? null : true)); + } + commentField.node().select(); + context.connection().userChangesets(function (err, changesets) { + if (err) return; + + var comments = []; + + for (var i = 0; i < changesets.length; i++) { + if (changesets[i].tags.comment) { + comments.push({ + title: changesets[i].tags.comment, + value: changesets[i].tags.comment + }); + } + } + + commentField.call(d3.combobox().data(comments)); + }); // Warnings var warnings = body.selectAll('div.warning-section') diff --git a/test/spec/core/connection.js b/test/spec/core/connection.js index 11ae4470e..04f14355f 100644 --- a/test/spec/core/connection.js +++ b/test/spec/core/connection.js @@ -248,6 +248,44 @@ describe('iD.Connection', function () { }); }); + describe('#userChangesets', function() { + var server, + changesetsXML = '' + + '' + + '' + + '' + + '' + + ''; + + beforeEach(function() { + server = sinon.fakeServer.create(); + }); + + afterEach(function() { + server.restore(); + }); + + it('loads user changesets', function(done) { + c.userDetails = function (callback) { + callback(undefined, { id: 1 }); + }; + + c.userChangesets(function(err, changesets) { + expect(changesets).to.deep.equal([{ + tags: { + comment: 'Caprice Court has been extended', + created_by: 'iD 1.8.5' + } + }]); + done(); + }); + + server.respondWith("GET", "http://www.openstreetmap.org/api/0.6/changesets?user=1", + [200, { "Content-Type": "text/xml" }, changesetsXML]); + server.respond(); + }); + }); + describe('#changesetTags', function() { it('omits comment when empty', function() { expect(c.changesetTags('', [])).not.to.have.property('comment');