-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcartong-arcgis-attachments.js
379 lines (333 loc) · 10.7 KB
/
cartong-arcgis-attachments.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
var CartONG = window.CartONG || {};
$(function() {
/**
* Check if attachements are enabled for the service, by reading 'hasAttachment' property in the service definition.
* If the service definition is not loaded, it does the request for it using loadDefinition() function.
* Returns true/false in promise response.
*/
CartONG.ArcgisService.prototype.isAttachmentEnabled = function() {
var promise = $.Deferred()
// request service definition, to check if attachments are enabled.
if (this.definition) {
promise.resolve(this.definition.hasAttachments)
}
else {
//load definition and continue attaching
this.loadDefinition()
.done(function() {
promise.resolve(this.definition.hasAttachments)
}.bind(this))
.fail(function() {
console.log('Load definition error.')
promise.reject()
})
}
return promise
}
/**
* Gets the attachments of a feature.
* Returns an array of file properties, including id (number), contentType (string, e.g. "image/png"), size (number, bytes), name (string)
* @param {number} feature - Object ID of the feature.
*/
CartONG.ArcgisService.prototype.getAttachments = function(feature){
var promise = $.Deferred()
function onError(message) {
promise.resolve({
success: false,
message: message
})
return false
}
//control input parameters
if (!feature) {
onError('Parameters are not correct.')
}
else {
var getAttachmentURL = this.url + '/' + feature + '/attachments'
$.ajax({
url: getAttachmentURL,
data: {f:'json'},
dataType: 'json'
})
.done(function(res) {
if (res.attachmentInfos) {
promise.resolve(res.attachmentInfos)
}
else {
onError('Get attachment error.')
}
})
.fail(function(err) {
onError('Connection error.')
})
}
return promise
}
/**
* Delete several attachments to a feature through the ArcGIS REST API.
* @param {array} attachments - Array of attachment objects, being ID mandatory attribute and name optional.
*/
CartONG.ArcgisService.prototype.deleteAttachments = function(feature, attachments) {
var promise = $.Deferred()
var attachmentIndex = {}
var summary = []
function onDeleted(res) {
if (res) {
for (var i=0; i<res.length; i++) {
summary.push({
id: res[i].objectId,
name: attachmentIndex[res[i].objectId] ? attachmentIndex[res[i].objectId] : res[i].objectId,
success: res[i].success
})
}
}
promise.resolve(summary)
}
function onError() {
prmmise.reject(err)
return false
}
var self = this
this.isAttachmentEnabled()
.done(function(hasAttachments) {
if (hasAttachments) {
deleteThem()
}
else {
onError({message: 'The service does not have attachments enabled.'})
}
})
.fail(function() {
onError({message: 'Load definition error.'})
})
/** function to continue adding attachments if service is enabled. */
function deleteThem() {
//control input parameters
if (!feature || !attachments) {
onError({message: 'Parameters are not correct.'})
}
else if (!attachments.length) {
onDeleted()
}
else {
//attachments are deleted with a single POST request, sending an array of attachement ID and feature ID
//loop attachments to get the ID from the array, as it could come in object format together with the name.
//attachmentIndex will help to give a better error message to the user
var idArray = []
if (attachments[0].id) {
for (var i = 0; i < attachments.length; i++) {
attachmentIndex[attachments[i].id] = attachments[i].name
idArray.push(Number(attachments[i].id))
}
}
else {
idArray = attachments.map(function(id) {
return Number(id)
})
}
//POST request
var deleteAttachmentURL = self.url + '/' + feature + '/deleteAttachments'
$.ajax({
url: deleteAttachmentURL,
method: 'POST',
data: {
f:'json',
attachmentIds: idArray.toString(),
token: self.getToken()
},
dataType: 'json'
})
.done(function(res) {
if (res.deleteAttachmentResults) {
onDeleted(res.deleteAttachmentResults)
}
else {
onError('Delete attachment error.')
}
})
.fail(function(err) {
onError('Connection error.')
})
}
}
return promise
}
/**
* Add several attachments to a feature through the ArcGIS REST API.
* @param {number} feature - Feature's object ID.
* @param {array} files - Array of Files from file input.
*/
CartONG.ArcgisService.prototype.addAttachments = function(feature, files, opts){
var promise = $.Deferred()
opts = opts || {}
var pendingFiles = 0;
var summary = []
/** function to run when attachment is sent --> promise resolve. */
function onSent() {
pendingFiles--
if (pendingFiles < 1) {
//console.log(summary)
promise.resolve(summary)
}
}
/** function for error --> promise resolve. */
function onError(err) {
//console.log(err)
promise.reject(err)
return false
}
var self = this
this.isAttachmentEnabled()
.done(function(hasAttachments) {
if (hasAttachments) {
addThem()
}
else {
onError({message: 'The service does not have attachments enabled.'})
}
})
.fail(function() {
onError({message: 'Load definition error.'})
})
/** function to continue adding attachments if service is enabled. */
function addThem() {
//control input parameters
if (!feature || !files) { // || !Array.isArray(files)) {
onError({message: 'Parameters are not correct.'})
}
else if (!files.length) {
onSent()
}
else {
//loop files, send each one
pendingFiles = files.length
for (var i = 0; i < files.length; i++) {
var file = files[i]
//send attachment
self.addAttachment(feature, file, opts)
.done(function(res, file) {
//debugger
//TODO: here is where optional attributes should be sent.
//res format: {"addAttachmentResult":{"objectId":5602,"success":true}} --> use objectid combined with attachment table in feature server
summary.push({
file: file,
res: res,
success: true
})
onSent()
})
.fail(function(err, file) {
//debugger
summary.push({
file: file,
success: false,
error: err
})
onSent()
})
}
}
}
return promise
}
/**
* Add an attachment to a feature through the ArcGIS REST API.
* @param {number} feature - Feature's object ID.
* @param {array} files - A single File from file input.
*/
CartONG.ArcgisService.prototype.addAttachment = function(feature, file, opts){
var promise = $.Deferred()
opts = opts || {}
/** function for error --> promise resolve */
function onError(err) {
//console.log(err)
promise.reject(err)
return false
}
var self = this
this.isAttachmentEnabled()
.done(function(hasAttachments) {
if (hasAttachments) {
addIt()
}
else {
onError({message: 'The service does not have attachments enabled.'})
}
})
.fail(function() {
onError({message: 'Load definition error.'})
})
function addIt() {
//control input parameters
if (!feature) {
onError({message: 'Parameters are not correct.'})
}
else {
//control file - how??
var addAttachmentURL = self.url + '/' + feature + '/addAttachment'
var formData = new FormData()
var xhr = new XMLHttpRequest()
formData.append('f', 'json')
formData.append('token', self.getToken())
formData.append('attachment', file)
xhr.onerror = function(err) {
debugger
promise.reject(err, file)
};
//xhr.onload = function(e) {
xhr.onreadystatechange = function(e) {
try {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
var responseTextJson = JSON.parse(xhr.responseText)
if (responseTextJson.error) {
// error returned by arcgis rest api
promise.reject(responseTextJson.error, file)
}
else {
// success
//send attributes if requested and url of attachment feature service is given
if (opts.sendAttributes && self.attachmentAttributeService) {
//get attachmentid
const attachmentid = responseTextJson.addAttachmentResult.objectId
//build object needed to update attachment's attributes
let attrsToSend = {
attachmentid: attachmentid
}
for (var attr in opts.sendAttributes) {
attrsToSend[attr] = file[attr]
}
//save attributes
self.attachmentAttributeService.save([{attributes: attrsToSend}], 'add')
.done(function() {
promise.resolve(responseTextJson, file);
})
.fail(function() {
promise.reject('Error', file)
})
}
else {
promise.resolve(responseTextJson, file);
}
}
}
else {
// error returned by arcgis server
promise.reject('Error', file)
}
}
}
catch(err) {
// connection error in the process
promise.reject(err, file)
}
};
xhr.onprogress = function(e) {}
xhr.onabort = function(err) {}
xhr.open('POST', addAttachmentURL)
xhr.send(formData);
}
}
return promise
}
});