-
Notifications
You must be signed in to change notification settings - Fork 52
/
infowindow_exporthandler.js
415 lines (379 loc) · 13.4 KB
/
infowindow_exporthandler.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import download from 'downloadjs';
import getSpinner from './utils/spinner';
import exportToFile from './utils/exporttofile';
import Icon from './ui/icon';
const defaultIcon = '#fa-download';
const defaultText = 'Ladda ner';
let viewerId;
export function simpleExportHandler(simpleExportUrl, activeLayer, selectedItems, exportedFileName) {
if (!simpleExportUrl) {
// Here we cannot simply throw, because it won't be catched. We need to rejecet it as the calling function expects a promise.
return Promise.reject(new Error('Export URL is not specified.'));
}
const features = {};
selectedItems.forEach((item) => {
const layerName = item.getLayer().get('name');
if (!features[layerName]) {
features[layerName] = [];
}
const obj = item.getFeature().getProperties();
const geometryName = item.getFeature().getGeometryName() || 'geom';
if (obj[geometryName]) delete obj[geometryName];
features[layerName].push(obj);
});
// eslint-disable-next-line consistent-return
return fetch(simpleExportUrl, {
method: 'POST', // or 'PUT'
body: JSON.stringify(features), // data can be `string` or {object}!
headers: {
'Content-Type': 'application/json'
}
})
.then((response) => {
if (response.status !== 200) {
// Note: Throwing here has practically the same effect as returning a Promise.reject() and both will be catched down here in the catch function;
throw response.statusText;
}
return response.blob();
})
.then((blob) => {
download(blob, exportedFileName);
})
.catch((err) => {
// Throwing here cause the whole fetch function be rejected, the same effect as returning Promise.reject();
throw err;
});
}
export function layerSpecificExportHandler(url, activeLayer, selectedItems, attributesToSendToExport, exportedFileName) {
if (!url) {
throw new Error('Export URL is not specified.');
}
const features = {};
selectedItems.forEach((item) => {
const layerName = item.getLayer().get('name');
if (!features[layerName]) {
features[layerName] = [];
}
const properties = item.getFeature().getProperties();
const geometryName = item.getFeature().getGeometryName() || 'geom';
let obj = {};
if (attributesToSendToExport) {
attributesToSendToExport.forEach((att) => {
if (att in properties) {
obj[att] = properties[att];
}
});
} else {
obj = properties;
}
if (obj[geometryName]) delete obj[geometryName];
features[layerName].push(obj);
});
// eslint-disable-next-line consistent-return
return fetch(url, {
method: 'POST', // or 'PUT'
body: JSON.stringify(features), // data can be `string` or {object}!
headers: {
'Content-Type': 'application/json'
}
})
// eslint-disable-next-line consistent-return
.then((response) => {
const contentType = response.headers.get('content-type');
switch (contentType) {
case 'application/json':
return response.json();
default:
if (response.status !== 200) {
throw response.statusText;
}
response.blob().then((blob) => {
download(blob, exportedFileName, contentType);
});
break;
}
})
.catch((err) => {
throw err;
});
}
export function listExportHandler(exportUrl, exportObject, exportedFileName) {
if (!exportUrl) {
// Here we cannot simply throw, because it won't be catched. We need to rejecet it as the calling function expects a promise.
return Promise.reject(new Error('Export URL is not specified.'));
}
// eslint-disable-next-line consistent-return
return fetch(exportUrl, {
method: 'POST', // or 'PUT'
body: JSON.stringify(exportObject), // data can be `string` or {object}!
headers: {
'Content-Type': 'application/json'
}
})
.then((response) => {
if (response.status !== 200) {
// Note: Throwing here has practically the same effect as returning a Promise.reject() and both will be catched down here in the catch function;
throw response.statusText;
}
return response.blob();
})
.then((blob) => {
download(blob, exportedFileName);
})
.catch((err) => {
// Throwing here cause the whole fetch function be rejected, the same effect as returning Promise.reject();
throw err;
});
}
function createExportButton(buttonText) {
const container = document.createElement('div');
const spinner = getSpinner();
spinner.style.visibility = 'hidden';
const button = document.createElement('button');
button.classList.add('export-button');
button.textContent = buttonText;
container.appendChild(button);
container.appendChild(spinner);
button.loadStart = () => {
button.disabled = true;
button.classList.add('disabled');
spinner.style.visibility = 'visible';
};
button.loadStop = () => {
button.disabled = false;
button.classList.remove('disabled');
spinner.style.visibility = 'hidden';
};
return container;
}
function createCustomExportButton(roundButtonIcon, roundButtonTooltipText) {
const container = document.createElement('div');
container.classList.add('inline-block', 'padding-smallest');
const iconComponent = Icon({
icon: roundButtonIcon,
title: ''
});
const button = document.createElement('button');
button.classList.add(
'padding-small',
'margin-bottom-smaller',
'icon-smaller',
'round',
'light',
'box-shadow',
'o-tooltip',
'margin-right-small'
);
button.style = 'position: relative';
button.innerHTML = `<span class="icon" style="z-index: 10000">${iconComponent.render()}</span><span data-tooltip="${roundButtonTooltipText}" data-placement="east" style="transition:unset"></span>`;
container.appendChild(button);
const spinner = getSpinner();
spinner.classList.add('spinner-large');
button.loadStart = () => {
button.disabled = true;
button.classList.add('disabled');
button.replaceWith(spinner);
};
button.loadStop = () => {
button.disabled = false;
button.classList.remove('disabled');
spinner.replaceWith(button);
};
return container;
}
function createToaster(status, exportOptions, message) {
let msg = message;
const toaster = document.createElement('div');
toaster.style.fontSize = '12px';
if (!message) {
const successMsg = exportOptions.toasterMessages && exportOptions.toasterMessages.success ? exportOptions.toasterMessages.success : 'Success!';
const failMsg = exportOptions.toasterMessages && exportOptions.toasterMessages.fail ? exportOptions.toasterMessages.fail : 'Sorry, something went wrong. Please contact your administrator';
msg = status === 'ok' ? successMsg : failMsg;
}
// It cannot be appended to infowindow bcuz in mobile tranform:translate is used css, and it causes that position: fixed loses its effect.
const parentElement = document.getElementById(viewerId);
parentElement.appendChild(toaster);
setTimeout(() => {
// message must be added here inside timeout otherwise it will be shown 50 ms before it take the effect of the css
toaster.textContent = msg;
toaster.classList.add('toaster');
if (status === 'ok') {
toaster.classList.add('toaster-successful');
} else {
toaster.classList.add('toaster-unsuccessful');
}
}, 50);
setTimeout(() => {
toaster.parentNode.removeChild(toaster);
}, 5000);
}
function createExportButtons(
obj,
attributesToSendToExportPerLayer,
selectionGroup,
activeLayer,
selectionManager,
exportOptions
) {
const roundButton = obj.button.roundButton || false;
const buttonText = obj.button.buttonText || defaultText;
const url = obj.url;
const layerSpecificExportedFileName = obj.exportedFileName;
const attributesToSendToExport = obj.attributesToSendToExport
? obj.attributesToSendToExport
: attributesToSendToExportPerLayer;
const exportBtn = roundButton
? createCustomExportButton(
obj.button.roundButtonIcon || defaultIcon,
obj.button.roundButtonTooltipText || defaultText
)
: createExportButton(buttonText);
const btn = exportBtn.querySelector('button');
btn.addEventListener('click', () => {
if (!url) {
createToaster('fail', exportOptions);
return;
}
btn.loadStart();
const selectedItems = selectionManager.getSelectedItemsForASelectionGroup(selectionGroup);
layerSpecificExportHandler(
url,
activeLayer,
selectedItems,
attributesToSendToExport,
layerSpecificExportedFileName
)
.then((data) => {
if (data) {
switch (data.status) {
case 'ok':
createToaster('ok', exportOptions);
break;
case 'fail':
createToaster('fail', exportOptions);
break;
default:
break;
}
}
btn.loadStop();
})
.catch((err) => {
console.error(err);
createToaster('fail', exportOptions);
btn.loadStop();
});
});
return exportBtn;
}
export function createSubexportComponent({ selectionGroup, viewer, exportOptions }) {
viewerId = viewer.getId();
const selectionManager = viewer.getSelectionManager();
// OBS! selectionGroup corresponds to a layer with the same name in most cases, but in case of a group layer it can contain selected items from all the layers in that GroupLayer.
let layerSpecificExportOptions;
const activeLayer = viewer.getLayer(selectionGroup);
const subexportContainer = document.createElement('div');
subexportContainer.classList.add('export-buttons-container');
if (exportOptions.layerSpecificExport) {
layerSpecificExportOptions = exportOptions.layerSpecificExport.find(
(i) => i.layer === selectionGroup
);
}
if (layerSpecificExportOptions) {
const exportUrls = layerSpecificExportOptions.exportUrls || [];
const attributesToSendToExportPerLayer = layerSpecificExportOptions.attributesToSendToExport;
const customButtonExportUrls = exportUrls.filter(
(e) => e.button.roundButton
);
const standardButtonExportUrls = exportUrls.filter(
(e) => !e.button.roundButton
);
customButtonExportUrls.forEach((obj) => {
const button = createExportButtons(
obj,
attributesToSendToExportPerLayer,
selectionGroup,
activeLayer,
selectionManager,
exportOptions
);
subexportContainer.appendChild(button);
});
standardButtonExportUrls.forEach((obj) => {
const button = createExportButtons(
obj,
attributesToSendToExportPerLayer,
selectionGroup,
activeLayer,
selectionManager,
exportOptions
);
subexportContainer.appendChild(button);
});
}
if (exportOptions.simpleExport && exportOptions.simpleExport.url) {
const simpleExport = exportOptions.simpleExport;
const simpleExportLayers = Array.isArray(simpleExport.layers) ? simpleExport.layers : [];
const exportAllowed = simpleExportLayers.length === 0 || simpleExportLayers.find((l) => l === selectionGroup);
if (exportAllowed) {
const simpleExportUrl = simpleExport.url || false;
const buttonText = simpleExport.button.buttonText || defaultText;
const exportedFileName = `${selectionGroup}.xlsx`;
const roundButton = simpleExport.button.roundButton || false;
const exportBtn = roundButton
? createCustomExportButton(
simpleExport.button.roundButtonIcon || defaultIcon,
simpleExport.button.roundButtonTooltipText || defaultText
)
: createExportButton(buttonText);
const btn = exportBtn.querySelector('button');
btn.addEventListener('click', () => {
btn.loadStart();
const selectedItems = selectionManager.getSelectedItemsForASelectionGroup(selectionGroup);
simpleExportHandler(
simpleExportUrl,
activeLayer,
selectedItems,
exportedFileName
)
.then(() => {
btn.loadStop();
})
.catch((err) => {
console.error(err);
createToaster('fail', exportOptions);
btn.loadStop();
});
});
subexportContainer.appendChild(exportBtn);
}
}
if (exportOptions.clientExport) {
const clientExport = exportOptions.clientExport;
const clientExportLayers = Array.isArray(clientExport.layers) ? clientExport.layers : [];
const exportAllowed = clientExportLayers.length === 0 || clientExportLayers.find((l) => l === selectionGroup);
if (exportAllowed) {
const roundButton = clientExport.button.roundButton || false;
const buttonText = clientExport.button.buttonText || defaultText;
const exportBtn = roundButton
? createCustomExportButton(
clientExport.button.roundButtonIcon || defaultIcon,
clientExport.button.roundButtonTooltipText || defaultText
)
: createExportButton(buttonText);
const btn = exportBtn.querySelector('button');
btn.addEventListener('click', () => {
btn.loadStart();
const selectedItems = selectionManager.getSelectedItemsForASelectionGroup(selectionGroup);
const features = selectedItems.map(i => i.getFeature());
exportToFile(features, clientExport.format, {
featureProjection: viewer.getProjection().getCode(),
filename: selectionGroup
});
btn.loadStop();
});
subexportContainer.appendChild(exportBtn);
}
}
return subexportContainer;
}