| 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 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| // FIXME should handle all geo-referenced data, not just vector data
goog.provide('ol.interaction.DragAndDrop');
goog.require('ol');
goog.require('ol.functions');
goog.require('ol.events');
goog.require('ol.events.Event');
goog.require('ol.events.EventType');
goog.require('ol.interaction.Interaction');
goog.require('ol.proj');
/**
* @classdesc
* Handles input of vector data by drag and drop.
*
* @constructor
* @extends {ol.interaction.Interaction}
* @fires ol.interaction.DragAndDrop.Event
* @param {olx.interaction.DragAndDropOptions=} opt_options Options.
* @api
*/
ol.interaction.DragAndDrop = function(opt_options) {
var options = opt_options ? opt_options : {};
ol.interaction.Interaction.call(this, {
handleEvent: ol.interaction.DragAndDrop.handleEvent
});
/**
* @private
* @type {Array.<function(new: ol.format.Feature)>}
*/
this.formatConstructors_ = options.formatConstructors ?
options.formatConstructors : [];
/**
* @private
* @type {ol.proj.Projection}
*/
this.projection_ = options.projection ?
ol.proj.get(options.projection) : null;
/**
* @private
* @type {Array.<ol.EventsKey>}
*/
this.dropListenKeys_ = null;
/**
* @private
* @type {ol.source.Vector}
*/
this.source_ = options.source || null;
/**
* @private
* @type {Element}
*/
this.target = options.target ? options.target : null;
};
ol.inherits(ol.interaction.DragAndDrop, ol.interaction.Interaction);
/**
* @param {Event} event Event.
* @this {ol.interaction.DragAndDrop}
* @private
*/
ol.interaction.DragAndDrop.handleDrop_ = function(event) {
var files = event.dataTransfer.files;
var i, ii, file;
for (i = 0, ii = files.length; i < ii; ++i) {
file = files.item(i);
var reader = new FileReader();
reader.addEventListener(ol.events.EventType.LOAD,
this.handleResult_.bind(this, file));
reader.readAsText(file);
}
};
/**
* @param {Event} event Event.
* @private
*/
ol.interaction.DragAndDrop.handleStop_ = function(event) {
event.stopPropagation();
event.preventDefault();
event.dataTransfer.dropEffect = 'copy';
};
/**
* @param {File} file File.
* @param {Event} event Load event.
* @private
*/
ol.interaction.DragAndDrop.prototype.handleResult_ = function(file, event) {
var result = event.target.result;
var map = this.getMap();
var projection = this.projection_;
if (!projection) {
var view = map.getView();
projection = view.getProjection();
}
var formatConstructors = this.formatConstructors_;
var features = [];
var i, ii;
for (i = 0, ii = formatConstructors.length; i < ii; ++i) {
/**
* Avoid "cannot instantiate abstract class" error.
* @type {Function}
*/
var formatConstructor = formatConstructors[i];
/**
* @type {ol.format.Feature}
*/
var format = new formatConstructor();
features = this.tryReadFeatures_(format, result, {
featureProjection: projection
});
if (features && features.length > 0) {
break;
}
}
if (this.source_) {
this.source_.clear();
this.source_.addFeatures(features);
}
this.dispatchEvent(
new ol.interaction.DragAndDrop.Event(
ol.interaction.DragAndDrop.EventType_.ADD_FEATURES, file,
features, projection));
};
/**
* Handles the {@link ol.MapBrowserEvent map browser event} unconditionally and
* neither prevents the browser default nor stops event propagation.
* @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
* @return {boolean} `false` to stop event propagation.
* @this {ol.interaction.DragAndDrop}
* @api
*/
ol.interaction.DragAndDrop.handleEvent = ol.functions.TRUE;
/**
* @private
*/
ol.interaction.DragAndDrop.prototype.registerListeners_ = function() {
var map = this.getMap();
if (map) {
var dropArea = this.target ? this.target : map.getViewport();
this.dropListenKeys_ = [
ol.events.listen(dropArea, ol.events.EventType.DROP,
ol.interaction.DragAndDrop.handleDrop_, this),
ol.events.listen(dropArea, ol.events.EventType.DRAGENTER,
ol.interaction.DragAndDrop.handleStop_, this),
ol.events.listen(dropArea, ol.events.EventType.DRAGOVER,
ol.interaction.DragAndDrop.handleStop_, this),
ol.events.listen(dropArea, ol.events.EventType.DROP,
ol.interaction.DragAndDrop.handleStop_, this)
];
}
};
/**
* @inheritDoc
*/
ol.interaction.DragAndDrop.prototype.setActive = function(active) {
ol.interaction.Interaction.prototype.setActive.call(this, active);
if (active) {
this.registerListeners_();
} else {
this.unregisterListeners_();
}
};
/**
* @inheritDoc
*/
ol.interaction.DragAndDrop.prototype.setMap = function(map) {
this.unregisterListeners_();
ol.interaction.Interaction.prototype.setMap.call(this, map);
if (this.getActive()) {
this.registerListeners_();
}
};
/**
* @param {ol.format.Feature} format Format.
* @param {string} text Text.
* @param {olx.format.ReadOptions} options Read options.
* @private
* @return {Array.<ol.Feature>} Features.
*/
ol.interaction.DragAndDrop.prototype.tryReadFeatures_ = function(format, text, options) {
try {
return format.readFeatures(text, options);
} catch (e) {
return null;
}
};
/**
* @private
*/
ol.interaction.DragAndDrop.prototype.unregisterListeners_ = function() {
if (this.dropListenKeys_) {
this.dropListenKeys_.forEach(ol.events.unlistenByKey);
this.dropListenKeys_ = null;
}
};
/**
* @enum {string}
* @private
*/
ol.interaction.DragAndDrop.EventType_ = {
/**
* Triggered when features are added
* @event ol.interaction.DragAndDrop.Event#addfeatures
* @api
*/
ADD_FEATURES: 'addfeatures'
};
/**
* @classdesc
* Events emitted by {@link ol.interaction.DragAndDrop} instances are instances
* of this type.
*
* @constructor
* @extends {ol.events.Event}
* @implements {oli.interaction.DragAndDropEvent}
* @param {ol.interaction.DragAndDrop.EventType_} type Type.
* @param {File} file File.
* @param {Array.<ol.Feature>=} opt_features Features.
* @param {ol.proj.Projection=} opt_projection Projection.
*/
ol.interaction.DragAndDrop.Event = function(type, file, opt_features, opt_projection) {
ol.events.Event.call(this, type);
/**
* The features parsed from dropped data.
* @type {Array.<ol.Feature>|undefined}
* @api
*/
this.features = opt_features;
/**
* The dropped file.
* @type {File}
* @api
*/
this.file = file;
/**
* The feature projection.
* @type {ol.proj.Projection|undefined}
* @api
*/
this.projection = opt_projection;
};
ol.inherits(ol.interaction.DragAndDrop.Event, ol.events.Event);
|