Skip to content

Commit e179a94

Browse files
committed
move existing third-party export libs to lib folder.
add third-party export libs from remote urls to local folder use JSZip v3.x for KMZ and shapefiles (ZIP). JSZip v2.x still required for xlsx export. minify all third-party libs fix projection of features to a WKID other than 4326. remove download link when either WKID or export type has changed.
1 parent 467046b commit e179a94

11 files changed

+122
-32
lines changed

widgets/Export.js

+75-25
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ define([
7070
i18n: i18n,
7171

7272
topicID: 'exportWidget',
73+
modulesPath: module.uri.substring(0, module.uri.lastIndexOf('.')) + '/lib',
7374

7475
defaultOptions: {
7576
excel: true, // allow attributes to be exported to Excel
@@ -143,11 +144,11 @@ define([
143144

144145
this.addTopics();
145146
this.setExportDefaults();
146-
this.loadXLSXParser();
147-
this.loadFeatureParser();
148147
this.initExportSelect();
149148
this.onWkidChange(this.inputWkid.get('value'));
150149
this.own(on(this.inputWkid, 'keyup', lang.hitch(this, 'wkidOnKeyUp')));
150+
151+
window.setTimeout(lang.hitch(this, 'loadParsers'), 1000);
151152
},
152153

153154
addTopics: function () {
@@ -222,17 +223,25 @@ define([
222223
domStyle.set(this.divWkidSection, 'display', 'none');
223224
this.inputWkid.set('disabled', true);
224225
}
226+
this.resetWkid();
225227
this.removeLink();
226228
this.btnExport.set('disabled', false);
227229
},
228230

229231
onWkidChange: function () {
230232
var wkid = this.inputWkid.get('value');
233+
this.removeLink();
231234
if (wkid && !isNaN(wkid) && wkid.length > 3) {
232235
wkid = parseInt(wkid, 10);
233236
if (wkid === 102100) { // ESRI --> EPSG
234237
wkid = 3857;
235238
}
239+
240+
var key = this.proj4Catalog + ':' + String(wkid);
241+
if (window.proj4 && !window.proj4.defs[key]) {
242+
require([this.proj4BaseURL + String(wkid) + '.js']);
243+
}
244+
236245
require(['dojo/text!' + this.proj4BaseURL + String(wkid) + '.esriwkt'], lang.hitch(this, function (prj) {
237246
if (wkid !== 4326) {
238247
this.proj4DestWKT = prj;
@@ -272,6 +281,12 @@ define([
272281
this.wkidChangeTimeoutID = window.setTimeout(lang.hitch(this, 'onWkidChange'), 200);
273282
},
274283

284+
resetWkid: function () {
285+
this.proj4DestWkid = 4326;
286+
this.proj4DestKey = 'EPSG:4326';
287+
this.proj4DestWKT = null;
288+
},
289+
275290
openDialog: function (options) {
276291
this.getExportDefaults();
277292
this.featureSet = options.featureSet;
@@ -297,6 +312,7 @@ define([
297312
this.topojsonOptions = (typeof(options.topojsonOptions) !== 'undefined') ? options.topojsonOptions : this.topojsonOptions;
298313

299314
this.initExportSelect();
315+
this.removeLink();
300316

301317
if (options.show) {
302318
this.parentWidget.show();
@@ -448,6 +464,7 @@ define([
448464
exportToGeoJSON: function () {
449465
// force export to 4326
450466
this.inputWkid.set('value', 4326);
467+
this.resetWkid();
451468

452469
var geojson = this.createGeoJSON();
453470
if (!geojson) {
@@ -462,6 +479,7 @@ define([
462479
exportToKML: function () {
463480
// force export to 4326
464481
this.inputWkid.set('value', 4326);
482+
this.resetWkid();
465483

466484
var geojson = this.createGeoJSON();
467485
if (!geojson) {
@@ -472,7 +490,7 @@ define([
472490
// customized version of mapbox's tokml for higher fidelity exports
473491
// handles more attributes than defined in the simple-spec v1.1
474492
// source: https://cdn.rawgit.com/mapbox/tokml/v0.4.0/tokml.js'
475-
require([module.id + '/tokml'], lang.hitch(this, function (tokml) {
493+
require([this.modulesPath + '/tokml.min.js'], lang.hitch(this, function (tokml) {
476494
var kml = tokml(geojson, this.kmlOptions);
477495
if (!kml) {
478496
this.reportError(i18n.errorKML);
@@ -482,13 +500,15 @@ define([
482500
if (exportType === 'kml') {
483501
this.downloadFile(kml, 'application/vnd.google-earth.kml+xml;charset=utf-8;', this.getFileName('.kml'), true);
484502
} else {
485-
/*global JSZip */
486-
var jszip = new JSZip();
503+
/*global JSZip3 */
504+
var jszip = new JSZip3();
487505
jszip.file(this.getFileName('.kml'), kml);
488-
var zipFile = jszip.generate({
489-
compression: 'STORE'}
490-
);
491-
this.downloadFile(zipFile, 'application/vnd.google-earth.kmz;base64;', this.getFileName('.kmz'), false);
506+
jszip.generateAsync({
507+
type: 'blob',
508+
compression: 'STORE'
509+
}).then(lang.hitch(this, function (zipFile) {
510+
this.downloadFile(zipFile, 'application/vnd.google-earth.kmz;base64;', this.getFileName('.kmz'), true);
511+
}));
492512
}
493513

494514
}));
@@ -499,6 +519,7 @@ define([
499519
var wkid = this.inputWkid.get('value');
500520
if (!wkid || wkid === '') {
501521
this.inputWkid.set('value', 4326);
522+
this.resetWkid();
502523
}
503524

504525
var geojson = this.createGeoJSON();
@@ -507,30 +528,33 @@ define([
507528
return;
508529
}
509530

510-
require([module.id + '/shpwrite'], lang.hitch(this, function (shpWrite) {
531+
require([this.modulesPath + '/shpwrite.min.js'], lang.hitch(this, function (shpWrite) {
511532
var options = lang.clone(this.shapefileOptions);
512533
options.wkt = this.proj4DestWKT;
513534
var zipFile = shpWrite.zip(geojson, options);
514535
if (!zipFile) {
515536
this.reportError(i18n.errorShapeFile);
516537
return;
517538
}
518-
this.downloadFile(zipFile, 'application/zip;base64;', this.getFileName('.zip'), false);
539+
zipFile.then(lang.hitch(this, function (content) {
540+
this.downloadFile(content, 'application/zip;base64;', this.getFileName('.zip'), true);
541+
}));
519542
}));
520543

521544
},
522545

523546
exportToTopoJSON: function () {
524547
// force export to 4326
525548
this.inputWkid.set('value', 4326);
549+
this.resetWkid();
526550

527551
var geojson = this.createGeoJSON();
528552
if (!geojson) {
529553
this.reportError(i18n.errorTopoJSON);
530554
return;
531555
}
532556

533-
require([module.id + '/topojson'], lang.hitch(this, function () {
557+
require([this.modulesPath + '/topojson.min.js'], lang.hitch(this, function () {
534558
var options = lang.clone(this.topojsonOptions);
535559
if (options['property-transform'] === null) {
536560
//options['property-transform'] = this.allProperties;
@@ -553,6 +577,7 @@ define([
553577
var wkid = this.inputWkid.get('value');
554578
if (!wkid || wkid === '') {
555579
this.inputWkid.set('value', 4326);
580+
this.resetWkid();
556581
}
557582

558583
var geojson = this.createGeoJSON();
@@ -561,7 +586,7 @@ define([
561586
return;
562587
}
563588

564-
require(['https://cdn.rawgit.com/mapbox/wellknown/v0.4.2/wellknown.js'], lang.hitch(this, function (wellknown) {
589+
require([this.modulesPath + '/wellknown-v0.4.2.min.js'], lang.hitch(this, function (wellknown) {
565590
var wkt = geojson.features.map(wellknown.stringify).join('\n');
566591
if (!wkt) {
567592
this.reportError (i18n.errorWKT);
@@ -767,21 +792,27 @@ define([
767792
includeStyle = true;
768793
}
769794

795+
var sourceProj = window.proj4.defs[this.proj4SrcKey];
796+
var destProj = window.proj4.defs[this.proj4DestKey];
770797
array.forEach(features, lang.hitch(this, function (feature) {
771798
var attr = feature.attributes;
772799
if (typeof(attr.feature) === 'object') {
773800
delete attr.feature;
774801
}
802+
var newFeature = {
803+
attributes: lang.clone(attr),
804+
geometry: lang.clone(feature.geometry)
805+
};
775806
if (feature.symbol && includeStyle) {
776-
feature.attributes = this.convertSymbolToAttributes(feature);
807+
newFeature.attributes = this.convertSymbolToAttributes(feature);
777808
}
778809

779-
if (feature.geometry) {
780-
if (window.proj4.defs[this.proj4SrcKey] && window.proj4.defs[this.proj4DestKey]) {
781-
feature.geometry = this.projectGeometry(feature.geometry, exportType);
810+
if (newFeature.geometry) {
811+
if (sourceProj && destProj) {
812+
newFeature.geometry = this.projectGeometry(newFeature.geometry);
782813
}
783814

784-
var geoFeature = window.Terraformer.ArcGIS.parse(feature);
815+
var geoFeature = window.Terraformer.ArcGIS.parse(newFeature);
785816
geojson.features.push(geoFeature);
786817
} else {
787818
topic.publish('viewer/handleError', 'feature has no geometry');
@@ -792,7 +823,6 @@ define([
792823
return geojson;
793824
},
794825

795-
796826
/*******************************
797827
* Projection Functions
798828
*******************************/
@@ -945,19 +975,39 @@ define([
945975
* load parsers
946976
*******************************/
947977

978+
loadParsers: function () {
979+
980+
window.dojoConfig.packages.push({
981+
name: 'JSZip3',
982+
location: this.modulesPath,
983+
main: 'jszip-v3.1.3.min'
984+
});
985+
require(window.dojoConfig, [
986+
'JSZip3'
987+
], lang.hitch(this, function (JSZip3) {
988+
if (!window.JSZip3) {
989+
window.JSZip3 = JSZip3;
990+
}
991+
this.loadXLSXParser();
992+
this.loadFeatureParser();
993+
}));
994+
995+
},
996+
948997
loadXLSXParser: function () {
949998
if (this.excel || this.csv || this.xlsExcel) {
999+
//xlsx requires jszip version 2.x. version 3.x for everything else
9501000
require([
951-
'https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.8/xlsx.core.min.js'
1001+
this.modulesPath + '/xlsx.core-v0.9.12.min.js'
9521002
]);
9531003
}
9541004
},
9551005

9561006
loadFeatureParser: function () {
9571007
if (this.geojson || this.kml || this.kmz || this.shapefile || this.topojson || this.wkt) {
9581008
require([
959-
'https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.14/proj4.js',
960-
'https://cdn-geoweb.s3.amazonaws.com/terraformer/1.0.5/terraformer.min.js'
1009+
'proj4js/proj4',
1010+
this.modulesPath + '/terraformer-v1.0.8.min.js'
9611011
], lang.hitch(this, function (proj4) {
9621012
if (!window.proj4) {
9631013
window.proj4 = proj4;
@@ -966,14 +1016,14 @@ define([
9661016

9671017
// arcgis parser must be loaded after the terraformer core module
9681018
require([
969-
'https://cdn-geoweb.s3.amazonaws.com/terraformer-arcgis-parser/1.0.4/terraformer-arcgis-parser.min.js'
1019+
this.modulesPath + '/terraformer-arcgis-parser-v1.0.5.min.js'
9701020
]);
9711021
}));
9721022
}
9731023
},
9741024

9751025
loadSourceProj4: function () {
976-
// which wikid are we projecting from?
1026+
// which wkid are we projecting from?
9771027
if (window.proj4 && this.featureSet && this.featureSet.features) {
9781028
var features = this.featureSet.features;
9791029
if (features && features.length > 0) {
@@ -1037,7 +1087,7 @@ define([
10371087
},
10381088

10391089
removeLink: function () {
1040-
if (this.link) {
1090+
if (this.link && this.divExportLink) {
10411091
this.divExportLink.removeChild(this.link);
10421092
this.divExportLink.innerHTML = ' ';
10431093
}

widgets/Export/lib/jszip-v3.1.3.min.js

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

widgets/Export/shpwrite.js widgets/Export/lib/shpwrite.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -9786,7 +9786,7 @@ var write = require('./write'),
97869786

97879787
module.exports = function(gj, options) {
97889788

9789-
var zip = new JSZip(),
9789+
var zip = new JSZip3(),
97909790
wkt = (options && options.wkt) ? options.wkt : prj;
97919791

97929792
[geojson.point(gj), geojson.line(gj), geojson.polygon(gj)]
@@ -9809,13 +9809,12 @@ module.exports = function(gj, options) {
98099809
}
98109810
});
98119811

9812-
var generateOptions = { compression:'STORE' };
9813-
9814-
if (!process.browser) {
9815-
generateOptions.type = 'nodebuffer';
9816-
}
9812+
var generateOptions = {
9813+
type: 'blob',
9814+
compression: 'STORE'
9815+
};
98179816

9818-
return zip.generate(generateOptions);
9817+
return zip.generateAsync(generateOptions);
98199818
};
98209819

98219820
}).call(this,require('_process'))

widgets/Export/lib/shpwrite.min.js

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

widgets/Export/lib/terraformer-arcgis-parser-v1.0.5.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

widgets/Export/lib/terraformer-v1.0.8.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

0 commit comments

Comments
 (0)