-
Notifications
You must be signed in to change notification settings - Fork 303
/
Copy pathDragNDrop.js
215 lines (194 loc) · 8.02 KB
/
DragNDrop.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
/* global itowns */
/**
* This module can be added to a web page or in a web application. It provides a
* simple behavior where single files can be drag and dropped onto a viewer. No
* relationship between a type of file and the way it is read, parsed and
* displayed are stored in the plugin. Use the method `register` to declare the
* way a file is read, parsed and displayed.
*
* Note: only files with the crs projection `EPSG:4326` can be projected correctly
* using this plugin.
*
* @module DragNDrop
*
* @example
* <script src="js/DragNDrop.js"></script>
* <script type="text/javascript">
* var view = new itowns.GlobeView(document.getElementById('viewerDiv'));
*
* DragNDrop.setView(view);
* DragNDrop.register('geojson', DragNDrop.JSON, itowns.GeoJsonParser.parse, DragNDrop.COLOR);
* DragNDrop.register('gpx', DragNDrop.XML, itowns.GpxParser.parse, DragNDrop.GEOMETRY);
* </script>
*
* @example
* require('./js/itowns.js');
* require('./plugins/DragNDrop.js');
*
* const view = new itowns.GlobeView(document.getElementById('viewerDiv'));
*
* DragNDrop.setView(view);
* DragNDrop.register('geojson', DragNDrop.JSON, itowns.GeoJsonParser.parse, DragNDrop.COLOR);
* DragNDrop.register('gpx', DragNDrop.XML, itowns.GpxParser.parse, DragNDrop.GEOMETRY);
*/
const DragNDrop = (function _() {
// TYPE
const _TEXT = 1;
const _JSON = 2;
const _BINARY = 3;
const _IMAGE = 4;
const _XML = 5;
// MODE
const _COLOR = 6;
const _GEOMETRY = 7;
const extensionsMap = {};
const fileReader = new FileReader();
let _view;
function addFiles(event, files) {
event.preventDefault();
// Read each file
for (let i = 0; i < files.length; i++) {
const file = files[i];
const extension = extensionsMap[file.name.split('.').pop().toLowerCase()];
// eslint-disable-next-line no-loop-func
fileReader.onload = function onload(e) {
let data = e.target.result;
if (extension.type == _JSON) {
data = JSON.parse(data);
} else if (extension.type == _XML) {
data = new window.DOMParser().parseFromString(data, 'text/xml');
}
const crs = extension.mode == _GEOMETRY ? _view.referenceCrs : _view.tileLayer.extent.crs;
extension.parser(data, {
in: {
crs: 'EPSG:4326',
},
out: {
crs: crs,
buildExtent: true,
mergeFeatures: true,
structure: (extension.mode == _GEOMETRY ? '3d' : '2d'),
forcedExtentCrs: crs != 'EPSG:4978' ? crs : 'EPSG:4326',
},
}).then(function _(features) {
const source = new itowns.FileSource({
features: features,
crs: 'EPSG:4326',
});
const randomColor = Math.round(Math.random() * 0xffffff);
let layer;
if (extension.mode == _COLOR) {
layer = new itowns.ColorLayer(file.name, {
transparent: true,
style: {
fill: {
color: '#' + randomColor.toString(16),
opacity: 0.7,
},
stroke: {
color: '#' + randomColor.toString(16),
},
},
source: source,
});
} else if (extension.mode == _GEOMETRY) {
layer = new itowns.FeatureGeometryLayer(
file.name,
{
style: {
fill: {
color: 'red',
extrusion_height: 200,
},
},
source: source,
opacity: 0.7,
});
} else {
throw new Error('Mode of file not supported, please add it using DragNDrop.register');
}
_view.addLayer(layer);
const extent = features.extent.clone();
// Transform local extent to data.crs projection.
if (extent.crs == features.crs) {
extent.applyMatrix4(features.matrixWorld);
}
// Move the camera
itowns.CameraUtils.transformCameraToLookAtTarget(_view, _view.camera3D, extent);
});
};
switch (extension.type) {
case _TEXT:
case _JSON:
case _XML:
fileReader.readAsText(file);
break;
case _BINARY:
fileReader.readAsArrayBuffer(file);
break;
case _IMAGE:
fileReader.readAsBinaryString(file);
break;
default:
throw new Error('Type of file not supported, please add it using DragNDrop.register');
}
}
}
// Listen to drag and drop actions
document.addEventListener('dragenter', function _(e) { e.preventDefault(); }, false);
document.addEventListener('dragover', function _(e) { e.preventDefault(); }, false);
document.addEventListener('dragleave', function _(e) { e.preventDefault(); }, false);
document.addEventListener('drop', function _(e) { addFiles(e, e.dataTransfer.files); }, false);
document.addEventListener('paste', function _(e) { addFiles(e, e.clipboardData.files); }, false);
return {
TEXT: _TEXT,
JSON: _JSON,
BINARY: _BINARY,
IMAGE: _IMAGE,
XML: _XML,
COLOR: _COLOR,
GEOMETRY: _GEOMETRY,
/**
* Register a type of file to read after a drag and drop on the viewer.
* The file will be processed following its extension and instructions
* given here.
*
* @param {string} extension - The extension to register. Each file
* dropped ending with this extension will follow the instructions given
* by the others parameters of this function.
* @param {number} type - The type of file to register. Can be
* `DragNDrop.TEXT` (equivalent to `Fetcher.text`), `DragNDrop.JSON`
* (equivalent to `Fetcher.json`), `DragNDrop.BINARY` (equivalent to
* `Fetcher.arrayBuffer`), `DragNDrop.IMAGE` (equivalent to
* `Fetcher.texture`) or `DragNDrop.XML` (equivalent to `Fetcher.xml`).
* @param {Function} parser - The method to parse the content of the
* added file.
* @param {number} mode - Choose the mode the file is displayed: either
* `DragNDrop.COLOR` (equivalent to a `ColorLayer`) or
* `DragNDrop.GEOMETRY` (equivalent to a `GeometryLayer`).
*
* @memberof module:DragNDrop
*/
register: function _(extension, type, parser, mode) {
extensionsMap[extension.toLowerCase()] = {
type: type,
parser: parser,
mode: mode,
};
},
/**
* The DragNDrop plugin needs to be binded to a view. Specified it using
* this method.
*
* @param {View} view - The view to bind to the DragNDrop interface.
*
* @memberof module:DragNDrop
*/
setView: function _(view) {
_view = view;
},
};
}());
if (typeof module != 'undefined' && module.exports) {
module.exports = DragNDrop;
}