@@ -103,6 +103,20 @@ function osmAPI($base64, $http, $q, osmSettingsService, osmUtilsService, options
103
103
104
104
// ------------------ INTERNAL CALL SERVER (API) -----------------
105
105
106
+ function isElement ( obj ) {
107
+ try {
108
+ //Using W3 DOM2 (works for FF, Opera and Chrom)
109
+ return obj instanceof HTMLElement ;
110
+ }
111
+ catch ( e ) {
112
+ //Browsers not supporting W3 DOM2 don't have HTMLElement and
113
+ //an exception is thrown and we end up here. Testing some
114
+ //properties that all elements have. (works on IE7)
115
+ return ( typeof obj === "object" ) &&
116
+ ( obj . nodeType === 1 ) && ( typeof obj . style === "object" ) &&
117
+ ( typeof obj . ownerDocument === "object" ) ;
118
+ }
119
+ }
106
120
107
121
/**
108
122
* @ngdoc method
@@ -112,8 +126,8 @@ function osmAPI($base64, $http, $q, osmSettingsService, osmUtilsService, options
112
126
* @comment
113
127
* ```
114
128
var options = {
115
- method: 'GET' // POST, DELETE, PUT
116
- path: '/0.6/changesets' //without the /api,
129
+ method: 'GET', // POST, DELETE, PUT
130
+ path: '/0.6/changesets', //without the /api
117
131
data: content //if you need a payload
118
132
};
119
133
osmAPI.xhr(options);
@@ -133,19 +147,33 @@ function osmAPI($base64, $http, $q, osmSettingsService, osmUtilsService, options
133
147
}
134
148
promise = this . _oauth . xhr ( options ) ;
135
149
} else {
136
- let fct = options . method . toLowerCase ( ) ;
137
- if ( options . config === undefined ) {
138
- options . config = { } ;
139
- }
140
- options . config . headers = { Authorization : this . getAuthorization ( ) } ;
141
- promise = $http [ fct ] ( options . path , options . config ) ;
150
+ options . url = this . url + options . path ;
151
+ options . headers = {
152
+ Authorization : this . getAuthorization ( )
153
+ } ;
154
+ promise = $http ( options ) ;
142
155
}
143
156
promise . then ( function ( data ) {
157
+ var d ;
158
+ var t = function ( d ) {
159
+ if ( ! d ) {
160
+ return d ;
161
+ }
162
+ if ( d . startsWith ) {
163
+ if ( d . startsWith ( '<?xml' ) ) {
164
+ return osmUtilsService . xml2js ( d ) ;
165
+ }
166
+ } else if ( isElement ( d ) ) {
167
+ return osmUtilsService . x2js . dom2js ( d ) ;
168
+ }
169
+ return d ;
170
+ } ;
144
171
if ( hasOauth ) {
145
- deferred . resolve ( osmUtilsService . x2js . dom2js ( data ) ) ;
172
+ d = data ;
146
173
} else {
147
- deferred . resolve ( osmUtilsService . xml2js ( data . data ) ) ;
174
+ d = data . data ;
148
175
}
176
+ deferred . resolve ( t ( d ) ) ;
149
177
} , function ( error ) {
150
178
deferred . reject ( error ) ;
151
179
} ) ;
@@ -200,10 +228,13 @@ function osmAPI($base64, $http, $q, osmSettingsService, osmUtilsService, options
200
228
* @returns {Promise } $http response
201
229
*/
202
230
this . put = function ( method , content , config ) {
231
+ if ( ! config ) {
232
+ config = { } ;
233
+ }
203
234
var _config = angular . copy ( config ) ;
204
- config . method = 'PUT' ;
205
- config . path = method ;
206
- config . data = content ;
235
+ _config . method = 'PUT' ;
236
+ _config . path = method ;
237
+ _config . data = osmUtilsService . js2xml ( content ) ;
207
238
return this . xhr ( _config ) ;
208
239
} ;
209
240
/**
@@ -216,9 +247,12 @@ function osmAPI($base64, $http, $q, osmSettingsService, osmUtilsService, options
216
247
* @returns {Promise } $http response
217
248
*/
218
249
this . delete = function ( method , config ) {
250
+ if ( ! config ) {
251
+ config = { } ;
252
+ }
219
253
var _config = angular . copy ( config ) ;
220
- config . method = 'DELETE' ;
221
- config . path = method ;
254
+ _config . method = 'DELETE' ;
255
+ _config . path = method ;
222
256
return this . xhr ( _config ) ;
223
257
} ;
224
258
@@ -266,7 +300,10 @@ function osmAPI($base64, $http, $q, osmSettingsService, osmUtilsService, options
266
300
if ( changesets . length > 0 ) {
267
301
osmSettingsService . setChangeset ( changesets [ 0 ] . id ) ;
268
302
deferred . resolve ( changesets [ 0 ] . id ) ;
269
- } else {
303
+ } else if ( changesets . _id ) {
304
+ osmSettingsService . setChangeset ( changesets . _id ) ;
305
+ deferred . resolve ( changesets . _id ) ;
306
+ } else {
270
307
osmSettingsService . setChangeset ( ) ;
271
308
deferred . resolve ( ) ;
272
309
}
0 commit comments