From efb521aeb532892e298acc6a9d0762f198370d8e Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Sat, 24 Nov 2012 13:30:31 -0400 Subject: [PATCH] Move oauth details into connection --- js/iD/Connection.js | 50 ++++++++++++++++++++++++++++++++++++++++++- js/iD/OAuth.js | 4 +--- js/iD/id.js | 18 +++++----------- js/iD/renderer/Map.js | 24 ++------------------- 4 files changed, 57 insertions(+), 39 deletions(-) diff --git a/js/iD/Connection.js b/js/iD/Connection.js index 24760a66b..55a6aa8bf 100644 --- a/js/iD/Connection.js +++ b/js/iD/Connection.js @@ -2,7 +2,8 @@ iD.Connection = function() { var apiURL = 'http://www.openstreetmap.org/api/0.6/', connection = {}, refNodes = {}, - user = {}; + user = {}, + oauth = iD.OAuth().setAPI(apiURL); // Request data within the bbox from an external OSM server. function bboxFromAPI(box, callback) { @@ -88,9 +89,52 @@ iD.Connection = function() { return iD.Graph(entities); } + function authenticate(callback) { + return oauth.authenticate(callback); + } + + function authenticated() { + return oauth.authenticated(); + } + + function createChangeset(modified) { + oauth.xhr({ + method: 'PUT', + path: '/changeset/create', + options: { header: { 'Content-Type': 'text/xml' } }, + content: iD.format.XML.changeset() + }, + function (changeset_id) { + oauth.xhr({ + method: 'POST', + path: '/changeset/' + changeset_id + '/upload', + options: { header: { 'Content-Type': 'text/xml' } }, + content: iD.format.XML.osmChange(user.id, changeset_id, modified) + }, function () { + oauth.xhr({ + method: 'PUT', + path: '/changeset/' + changeset_id + '/close' + }, function () { + alert('saved! ' + apiURL.replace('/api/0.6/', '/browse') + '/changeset/' + changeset_id); + }); + }); + }); + } + + function userDetails(callback) { + oauth.xhr({ method: 'GET', path: '/user/details' }, function(user_details) { + var u = user_details.getElementsByTagName('user')[0]; + callback({ + display_name: u.attributes.display_name.nodeValue, + id: u.attributes.id.nodeValue + }); + }); + } + connection.url = function(x) { if (!arguments.length) return apiURL; apiURL = x; + oauth.setAPI(x); return connection; }; @@ -103,6 +147,10 @@ iD.Connection = function() { connection.bboxFromAPI = bboxFromAPI; connection.wayFromAPI = wayFromAPI; connection.loadFromURL = loadFromURL; + connection.userDetails = userDetails; + connection.authenticate = authenticate; + connection.authenticated = authenticated; + connection.createChangeset = createChangeset; connection.objectData = objectData; connection.apiURL = apiURL; diff --git a/js/iD/OAuth.js b/js/iD/OAuth.js index c6b1218db..85bbdf78a 100644 --- a/js/iD/OAuth.js +++ b/js/iD/OAuth.js @@ -1,4 +1,4 @@ -iD.OAuth = function(map) { +iD.OAuth = function() { var baseurl = 'http://api06.dev.openstreetmap.org', apibase = 'http://api06.dev.openstreetmap.org/api/0.6', oauth_secret = 'aMnOOCwExO2XYtRVWJ1bI9QOdqh1cay2UgpbhA6p', @@ -90,7 +90,5 @@ iD.OAuth = function(map) { return oauth; }; - map.oauth = oauth; - return oauth; }; diff --git a/js/iD/id.js b/js/iD/id.js index 21aa6319f..d2498bcbb 100644 --- a/js/iD/id.js +++ b/js/iD/id.js @@ -11,9 +11,6 @@ var iD = function(container) { var controller = iD.Controller(map); - var oauth = iD.OAuth(map) - .setAPI('http://api06.dev.openstreetmap.org/api/0.6'); - var bar = container.append('div') .attr('id', 'bar'); @@ -75,7 +72,7 @@ var iD = function(container) { .attr('id', 'save') .html("Save") .on('click', function() { - oauth.authenticate(function() { + connection.authenticate(function() { map.commit(); }); }); @@ -136,15 +133,10 @@ var iD = function(container) { }); } - if (oauth.authenticated()) { - oauth.xhr({ method: 'GET', path: '/user/details' }, function(user_details) { - var u = user_details.getElementsByTagName('user')[0]; - connection.user({ - display_name: u.attributes.display_name.nodeValue, - id: u.attributes.id.nodeValue - }); - d3.select('.messages').text('logged in as ' + - connection.user().display_name); + if (connection.authenticated()) { + connection.userDetails(function(user_details) { + connection.user(user_details); + d3.select('.messages').text('logged in as ' + user_details.display_name); }); } }; diff --git a/js/iD/renderer/Map.js b/js/iD/renderer/Map.js index 2615ce1ec..c6cf90b2b 100644 --- a/js/iD/renderer/Map.js +++ b/js/iD/renderer/Map.js @@ -535,29 +535,9 @@ iD.Map = function(elem, connection) { } function commit() { - var modified = _.filter(history.graph().entities, function(e) { + connection.createChangeset(_.filter(history.graph().entities, function(e) { return e.modified; - }); - var userid = connection.user().id; - map.oauth.xhr({ - method: 'PUT', - path: '/changeset/create', - options: { header: { 'Content-Type': 'text/xml' } }, - content: iD.format.XML.changeset() }, function(changeset_id) { - map.oauth.xhr({ - method: 'POST', - path: '/changeset/' + changeset_id + '/upload', - options: { header: { 'Content-Type': 'text/xml' } }, - content: iD.format.XML.osmChange(userid, changeset_id, modified) - }, function() { - map.oauth.xhr({ - method: 'PUT', - path: '/changeset/' + changeset_id + '/close' - }, function() { - alert('saved! ' + connection.url().replace('/api/0.6/', '/browse') + '/changeset/' + changeset_id); - }); - }); - }); + })); } map.handleDrag = handleDrag;