-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathDistortableCollection.Edit.js
346 lines (280 loc) · 9.67 KB
/
DistortableCollection.Edit.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
L.DistortableImage = L.DistortableImage || {};
// this class holds the keybindings and toolbar API for an image collection instance
L.DistortableCollection.Edit = L.Handler.extend({
options: {
keymap: L.distortableImage.group_action_map,
},
initialize(group, options) {
this._group = group;
this._exportOpts = group.options.exportOpts;
L.setOptions(this, options);
L.distortableImage.group_action_map.Escape = '_decollectAll';
},
addHooks() {
const group = this._group;
const map = group._map;
this.editActions = this.options.actions;
this.runExporter =
L.bind(L.Utils.getNestedVal(this, '_exportOpts', 'exporter') ||
this.startExport, this);
L.DomEvent.on(document, 'keydown', this._onKeyDown, this);
if (!(map.doubleClickZoom.enabled() || map.doubleClickLabels.enabled())) {
L.DomEvent.on(map, 'click', this._decollectAll, this);
}
L.DomEvent.on(map, {
singleclickon: this._singleClickListeners,
singleclickoff: this._resetClickListeners,
singleclick: this._singleClick,
boxcollectend: this._addCollections,
}, this);
this._group.editable = true;
this._group.eachLayer(layer => layer.editing.enable());
},
removeHooks() {
const group = this._group;
const map = group._map;
L.DomEvent.off(document, 'keydown', this._onKeyDown, this);
if (!(map.doubleClickZoom.enabled() || map.doubleClickLabels.enabled())) {
L.DomEvent.off(map, 'click', this._decollectAll, this);
}
L.DomEvent.off(map, {
singleclickon: this._singleClickListeners,
singleclickoff: this._resetClickListeners,
singleclick: this._singleClick,
boxcollectend: this._addCollections,
}, this);
this._decollectAll();
this._group.editable = false;
this._group.eachLayer(layer => layer.editing.disable());
},
enable() {
this._enabled = true;
this.addHooks();
return this;
},
disable() {
this._enabled = false;
this.removeHooks();
return this;
},
_onKeyDown(e) {
const keymap = this.options.keymap;
const handlerName = keymap[e.key];
if (!this[handlerName]) { return; }
if (this._group.anyCollected()) {
this[handlerName].call(this);
}
},
_singleClick(e) {
if (e.type === 'singleclick') { this._decollectAll(e); }
else { return; }
},
_singleClickListeners() {
const map = this._group._map;
L.DomEvent.off(map, 'click', this._decollectAll, this);
L.DomEvent.on(map, 'singleclick', this._decollectAll, this);
},
_resetClickListeners() {
const map = this._group._map;
L.DomEvent.on(map, 'click', this._decollectAll, this);
L.DomEvent.off(map, 'singleclick', this._decollectAll, this);
},
_decollectAll(e) {
let oe;
if (e) { oe = e.originalEvent; }
/**
* prevents image deselection following the 'boxcollectend' event - note 'shift' must not be released until dragging is complete
* also prevents deselection following a click on a disabled img by differentiating it from the map
*/
if (oe && (oe.shiftKey || oe.target instanceof HTMLImageElement)) {
return;
}
this._group.eachLayer((layer) => {
L.DomUtil.removeClass(layer.getElement(), 'collected');
layer.deselect();
});
this._removeToolbar();
if (e) { L.DomEvent.stopPropagation(e); }
},
_unlockGroup() {
if (!this.hasTool(L.UnlockAction)) { return; }
this._group.eachLayer((layer) => {
if (this._group.isCollected(layer)) {
const edit = layer.editing;
edit._unlock();
// unlock updates the layer's handles; deselect to ensure they're hidden
layer.deselect();
}
});
},
_lockGroup() {
if (!this.hasTool(L.LockAction)) { return; }
this._group.eachLayer((layer) => {
if (this._group.isCollected(layer) ) {
const edit = layer.editing;
edit._lock();
// map.addLayer also deselects the image, so we reselect here
L.DomUtil.addClass(layer.getElement(), 'collected');
}
});
},
_addCollections(e) {
const box = e.boxCollectBounds;
const map = this._group._map;
this._group.eachLayer((layer) => {
const edit = layer.editing;
if (layer.isSelected()) { layer.deselect(); }
const zoom = map.getZoom();
const center = map.getCenter();
let imgBounds = L.latLngBounds(layer.getCorner(2), layer.getCorner(1));
imgBounds = map._latLngBoundsToNewLayerBounds(imgBounds, zoom, center);
if (box.intersects(imgBounds) && edit.enabled()) {
if (!this.toolbar) { this._addToolbar(); }
L.DomUtil.addClass(layer.getElement(), 'collected');
}
});
},
_removeGroup(e) {
if (!this.hasTool(L.DeleteAction)) { return; }
const layersToRemove = this._group._toRemove();
const n = layersToRemove.length;
if (n === 0) { return; }
const choice = L.DomUtil.confirmDeletes(n);
if (choice) {
layersToRemove.forEach((layer) => {
this._group.removeLayer(layer);
});
if (!this._group.anyCollected()) {
this._removeToolbar();
}
}
if (e) { L.DomEvent.stopPropagation(e); }
},
cancelExport() {
if (!this.customCollection) {
this._exportOpts.collection = undefined;
}
clearInterval(this.updateInterval);
},
_addToolbar() {
const group = this._group;
const map = group._map;
if (group.options.suppressToolbar || this.toolbar) { return; }
this.toolbar = L.distortableImage.controlBar({
actions: this.editActions,
position: 'topleft',
}).addTo(map, group);
},
_removeToolbar() {
const map = this._group._map;
if (this.toolbar) {
map.removeLayer(this.toolbar);
this.toolbar = false;
} else {
return false;
}
},
hasTool(value) {
return this.editActions.some(action => action === value);
},
addTool(value) {
if (value.baseClass === 'leaflet-toolbar-icon' && !this.hasTool(value)) {
this._removeToolbar();
this.editActions.push(value);
this._addToolbar();
}
return this;
},
removeTool(value) {
this.editActions.some((item, idx) => {
if (this.editActions[idx] === value) {
this._removeToolbar();
this.editActions.splice(idx, 1);
this._addToolbar();
return true;
} else {
return false;
}
});
return this;
},
startExport() {
if (!this.hasTool(L.ExportAction)) { return; }
return new Promise((resolve) => {
const opts = this._exportOpts;
opts.resolve = resolve; // allow resolving promise in user-defined functions, to stop spinner on completion
let statusUrl;
this.updateInterval = null;
// this may be overridden to update the UI to show export progress or completion
const _defaultUpdater = (data) => {
data = JSON.parse(data);
// optimization: fetch status directly from google storage:
if (data.status_url) {
if (statusUrl !== data.status_url && data.status_url.match('.json')) {
// if (data.status_url && data.status_url.substr(0,1) === "/") {
// opts.statusUrl = opts.statusUrl + data.status_url;
// } else {
statusUrl = data.status_url;
// }
}
if (data.status === 'complete') {
clearInterval(this.updateInterval);
if (!this.customCollection) {
this._exportOpts.collection = undefined;
}
resolve();
if (data.jpg !== null) {
alert('Export succeeded. ' + opts.exportUrl + data.jpg);
}
}
// TODO: update to clearInterval when status == "failed" if we update that in this file:
// https://github.com/publiclab/mapknitter-exporter/blob/main/lib/mapknitterExporter.rb
console.log(data);
}
};
// receives the URL of status.json, and starts running the updater to repeatedly fetch from status.json;
// this may be overridden to integrate with any UI
const _defaultHandleStatusRes = (data) => {
statusUrl = opts.statusUrl + data;
// repeatedly fetch the status.json
this.updateInterval = setInterval(() => {
const reqOpts = {method: 'GET'};
const req = new Request(`${statusUrl}?${Date.now()}`, reqOpts);
fetch(req).then((res) => {
if (res.ok) {
return res.text();
}
}).then(opts.updater);
}, opts.frequency);
};
// initiate the export
const _defaultFetchStatusUrl = (mergedOpts) => {
const form = new FormData();
form.append('collection', JSON.stringify(mergedOpts.collection));
form.append('scale', mergedOpts.scale);
form.append('upload', true);
const reqOpts = {method: 'POST', body: form};
const req = new Request(mergedOpts.exportStartUrl, reqOpts);
fetch(req).then((res) => {
if (res.ok) {
return res.text();
}
}).then(mergedOpts.handleStatusRes);
};
// If the user has passed collection property
this.customCollection = !!opts.collection;
if (!this.customCollection) {
opts.collection = this._group.generateExportJson().images;
}
opts.frequency = opts.frequency || 3000;
opts.scale = opts.scale || 100; // switch it to _getAvgCmPerPixel !
opts.updater = opts.updater || _defaultUpdater;
opts.handleStatusRes = opts.handleStatusRes || _defaultHandleStatusRes;
opts.fetchStatusUrl = opts.fetchStatusUrl || _defaultFetchStatusUrl;
opts.fetchStatusUrl(opts);
});
},
});
L.distortableCollection.edit = (group, options) => {
return new L.DistortableCollection.Edit(group, options);
};