-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
viewerDragDropMixin.js
289 lines (264 loc) · 11.4 KB
/
viewerDragDropMixin.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
import defaultValue from '../../Core/defaultValue.js';
import defined from '../../Core/defined.js';
import DeveloperError from '../../Core/DeveloperError.js';
import Event from '../../Core/Event.js';
import wrapFunction from '../../Core/wrapFunction.js';
import CzmlDataSource from '../../DataSources/CzmlDataSource.js';
import GeoJsonDataSource from '../../DataSources/GeoJsonDataSource.js';
import KmlDataSource from '../../DataSources/KmlDataSource.js';
import getElement from '../getElement.js';
/**
* A mixin which adds default drag and drop support for CZML files to the Viewer widget.
* Rather than being called directly, this function is normally passed as
* a parameter to {@link Viewer#extend}, as shown in the example below.
* @exports viewerDragDropMixin
* @namespace
* @param {Viewer} viewer The viewer instance.
* @param {Object} [options] Object with the following properties:
* @param {Element|String} [options.dropTarget=viewer.container] The DOM element which will serve as the drop target.
* @param {Boolean} [options.clearOnDrop=true] When true, dropping files will clear all existing data sources first, when false, new data sources will be loaded after the existing ones.
* @param {Boolean} [options.flyToOnDrop=true] When true, dropping files will fly to the data source once it is loaded.
* @param {Boolean} [options.clampToGround=true] When true, datasources are clamped to the ground.
* @param {DefaultProxy} [options.proxy] The proxy to be used for KML network links.
*
* @exception {DeveloperError} Element with id <options.dropTarget> does not exist in the document.
* @exception {DeveloperError} dropTarget is already defined by another mixin.
* @exception {DeveloperError} dropEnabled is already defined by another mixin.
* @exception {DeveloperError} dropError is already defined by another mixin.
* @exception {DeveloperError} clearOnDrop is already defined by another mixin.
*
* @example
* // Add basic drag and drop support and pop up an alert window on error.
* var viewer = new Cesium.Viewer('cesiumContainer');
* viewer.extend(Cesium.viewerDragDropMixin);
* viewer.dropError.addEventListener(function(viewerArg, source, error) {
* window.alert('Error processing ' + source + ':' + error);
* });
*/
function viewerDragDropMixin(viewer, options) {
//>>includeStart('debug', pragmas.debug);
if (!defined(viewer)) {
throw new DeveloperError('viewer is required.');
}
if (viewer.hasOwnProperty('dropTarget')) {
throw new DeveloperError('dropTarget is already defined by another mixin.');
}
if (viewer.hasOwnProperty('dropEnabled')) {
throw new DeveloperError('dropEnabled is already defined by another mixin.');
}
if (viewer.hasOwnProperty('dropError')) {
throw new DeveloperError('dropError is already defined by another mixin.');
}
if (viewer.hasOwnProperty('clearOnDrop')) {
throw new DeveloperError('clearOnDrop is already defined by another mixin.');
}
if (viewer.hasOwnProperty('flyToOnDrop')) {
throw new DeveloperError('flyToOnDrop is already defined by another mixin.');
}
//>>includeEnd('debug');
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
//Local variables to be closed over by defineProperties.
var dropEnabled = true;
var flyToOnDrop = defaultValue(options.flyToOnDrop, true);
var dropError = new Event();
var clearOnDrop = defaultValue(options.clearOnDrop, true);
var dropTarget = defaultValue(options.dropTarget, viewer.container);
var clampToGround = defaultValue(options.clampToGround, true);
var proxy = options.proxy;
dropTarget = getElement(dropTarget);
Object.defineProperties(viewer, {
/**
* Gets or sets the element to serve as the drop target.
* @memberof viewerDragDropMixin.prototype
* @type {Element}
*/
dropTarget : {
//TODO See https://github.com/CesiumGS/cesium/issues/832
get : function() {
return dropTarget;
},
set : function(value) {
//>>includeStart('debug', pragmas.debug);
if (!defined(value)) {
throw new DeveloperError('value is required.');
}
//>>includeEnd('debug');
unsubscribe(dropTarget, handleDrop);
dropTarget = value;
subscribe(dropTarget, handleDrop);
}
},
/**
* Gets or sets a value indicating if drag and drop support is enabled.
* @memberof viewerDragDropMixin.prototype
* @type {Element}
*/
dropEnabled : {
get : function() {
return dropEnabled;
},
set : function(value) {
if (value !== dropEnabled) {
if (value) {
subscribe(dropTarget, handleDrop);
} else {
unsubscribe(dropTarget, handleDrop);
}
dropEnabled = value;
}
}
},
/**
* Gets the event that will be raised when an error is encountered during drop processing.
* @memberof viewerDragDropMixin.prototype
* @type {Event}
*/
dropError : {
get : function() {
return dropError;
}
},
/**
* Gets or sets a value indicating if existing data sources should be cleared before adding the newly dropped sources.
* @memberof viewerDragDropMixin.prototype
* @type {Boolean}
*/
clearOnDrop : {
get : function() {
return clearOnDrop;
},
set : function(value) {
clearOnDrop = value;
}
},
/**
* Gets or sets a value indicating if the camera should fly to the data source after it is loaded.
* @memberof viewerDragDropMixin.prototype
* @type {Boolean}
*/
flyToOnDrop : {
get : function() {
return flyToOnDrop;
},
set : function(value) {
flyToOnDrop = value;
}
},
/**
* Gets or sets the proxy to be used for KML.
* @memberof viewerDragDropMixin.prototype
* @type {DefaultProxy}
*/
proxy : {
get : function() {
return proxy;
},
set : function(value) {
proxy = value;
}
},
/**
* Gets or sets a value indicating if the datasources should be clamped to the ground
* @memberof viewerDragDropMixin.prototype
* @type {Boolean}
*/
clampToGround : {
get : function() {
return clampToGround;
},
set : function(value) {
clampToGround = value;
}
}
});
function handleDrop(event) {
stop(event);
if (clearOnDrop) {
viewer.entities.removeAll();
viewer.dataSources.removeAll();
}
var files = event.dataTransfer.files;
var length = files.length;
for (var i = 0; i < length; i++) {
var file = files[i];
var reader = new FileReader();
reader.onload = createOnLoadCallback(viewer, file, proxy, clampToGround);
reader.onerror = createDropErrorCallback(viewer, file);
reader.readAsText(file);
}
}
//Enable drop by default;
subscribe(dropTarget, handleDrop);
//Wrap the destroy function to make sure all events are unsubscribed from
viewer.destroy = wrapFunction(viewer, viewer.destroy, function() {
viewer.dropEnabled = false;
});
//Specs need access to handleDrop
viewer._handleDrop = handleDrop;
}
function stop(event) {
event.stopPropagation();
event.preventDefault();
}
function unsubscribe(dropTarget, handleDrop) {
var currentTarget = dropTarget;
if (defined(currentTarget)) {
currentTarget.removeEventListener('drop', handleDrop, false);
currentTarget.removeEventListener('dragenter', stop, false);
currentTarget.removeEventListener('dragover', stop, false);
currentTarget.removeEventListener('dragexit', stop, false);
}
}
function subscribe(dropTarget, handleDrop) {
dropTarget.addEventListener('drop', handleDrop, false);
dropTarget.addEventListener('dragenter', stop, false);
dropTarget.addEventListener('dragover', stop, false);
dropTarget.addEventListener('dragexit', stop, false);
}
function createOnLoadCallback(viewer, file, proxy, clampToGround) {
var scene = viewer.scene;
return function(evt) {
var fileName = file.name;
try {
var loadPromise;
if (/\.czml$/i.test(fileName)) {
loadPromise = CzmlDataSource.load(JSON.parse(evt.target.result), {
sourceUri : fileName
});
} else if (/\.geojson$/i.test(fileName) || /\.json$/i.test(fileName) || /\.topojson$/i.test(fileName)) {
loadPromise = GeoJsonDataSource.load(JSON.parse(evt.target.result), {
sourceUri : fileName,
clampToGround : clampToGround
});
} else if (/\.(kml|kmz)$/i.test(fileName)) {
loadPromise = KmlDataSource.load(file, {
sourceUri : fileName,
proxy : proxy,
camera : scene.camera,
canvas : scene.canvas,
clampToGround: clampToGround
});
} else {
viewer.dropError.raiseEvent(viewer, fileName, 'Unrecognized file: ' + fileName);
return;
}
if (defined(loadPromise)) {
viewer.dataSources.add(loadPromise).then(function(dataSource) {
if (viewer.flyToOnDrop) {
viewer.flyTo(dataSource);
}
}).otherwise(function(error) {
viewer.dropError.raiseEvent(viewer, fileName, error);
});
}
} catch (error) {
viewer.dropError.raiseEvent(viewer, fileName, error);
}
};
}
function createDropErrorCallback(viewer, file) {
return function(evt) {
viewer.dropError.raiseEvent(viewer, file.name, evt.target.error);
};
}
export default viewerDragDropMixin;