-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathDistortableImage.Edit.js
658 lines (520 loc) · 17 KB
/
DistortableImage.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/* eslint-disable valid-jsdoc */
L.DistortableImage = L.DistortableImage || {};
// holds the keybindings & toolbar API for an individual image instance
L.DistortableImage.Edit = L.Handler.extend({
options: {
opacity: 0.7,
outline: '1px solid red',
keymap: L.distortableImage.action_map,
modes: ['scale', 'distort', 'rotate', 'freeRotate', 'lock'],
},
initialize: function(overlay, options) {
this._overlay = overlay;
this._toggledImage = false;
/* Interaction modes. TODO - create API for
* limiting modes similar to toolbar actions API */
this._modes = this.options.modes;
this._mode = this._modes[this._modes.indexOf(overlay.options.mode)];
this._selected = this._overlay.options.selected || false;
this._transparent = false;
this._outlined = false;
L.setOptions(this, options);
L.distortableImage.action_map.Escape = '_deselect';
},
/* Run on image selection. */
addHooks: function() {
var overlay = this._overlay;
var map = overlay._map;
var eventParents = overlay._eventParents;
/* bring the selected image into view */
overlay.bringToFront();
this._initHandles();
this._appendHandlesandDragable(this._mode);
this.editActions = this.options.actions;
if (this._selected && !overlay.options.suppressToolbar) {
this._addToolbar();
}
this._overlay._dragStartPoints = {
0: L.point(0, 0),
1: L.point(0, 0),
2: L.point(0, 0),
3: L.point(0, 0),
};
if (eventParents) {
var eP = eventParents[Object.keys(eventParents)[0]];
if (eP) { this.parentGroup = eP; }
else { this.parentGroup = false; }
}
/**
* custom events fired from DoubleClickLabels.js. Used to differentiate
* single / dblclick to not deselect images on map dblclick.
*/
if (!(map.doubleClickZoom.enabled() || map.doubleClickLabels.enabled())) {
L.DomEvent.on(map, 'click', this._deselect, this);
}
L.DomEvent.on(map, {
singleclickon: this._singleClickListeners,
singleclickoff: this._resetClickListeners,
singleclick: this._singleClick,
}, this);
L.DomEvent.on(overlay._image, {
click: this._select,
dblclick: this._nextMode,
}, this);
L.DomEvent.on(window, 'keydown', this._onKeyDown, this);
},
/* Run on image deselection. */
removeHooks: function() {
var overlay = this._overlay;
var map = overlay._map;
var eP = this.parentGroup;
// First, check if dragging exists - it may be off due to locking
if (this.dragging) { this.dragging.disable(); }
delete this.dragging;
if (this.toolbar) { this._removeToolbar(); }
if (this.editing) { this.editing.disable(); }
map.removeLayer(this._handles[this._mode]);
/**
* ensures if you disable an image while it is multi-selected
* additional deselection logic is run
*/
if (L.DomUtil.hasClass(overlay.getElement(), 'selected')) {
L.DomUtil.removeClass(overlay.getElement(), 'selected');
}
if (eP && (!eP.anySelected() && eP.editing.toolbar)) {
eP.editing._removeToolbar();
}
if (!(map.doubleClickZoom.enabled() || map.doubleClickLabels.enabled())) {
L.DomEvent.off(map, 'click', this._deselect, this);
}
L.DomEvent.off(map, {
singleclickon: this._singleClickListeners,
singleclickoff: this._resetClickListeners,
singleclick: this._singleClick,
}, this);
L.DomEvent.off(overlay._image, {
click: this._select,
dblclick: this._nextMode,
}, this);
L.DomEvent.off(window, 'keydown', this._onKeyDown, this);
},
disable: function() {
if (!this._enabled) { return this; }
this._enabled = false;
this.removeHooks();
return this;
},
_initHandles: function() {
var overlay = this._overlay;
var i;
this._scaleHandles = L.layerGroup();
for (i = 0; i < 4; i++) {
this._scaleHandles.addLayer(new L.ScaleHandle(overlay, i));
}
this._distortHandles = L.layerGroup();
for (i = 0; i < 4; i++) {
this._distortHandles.addLayer(new L.DistortHandle(overlay, i));
}
this._rotateHandles = L.layerGroup(); // individual rotate
for (i = 0; i < 4; i++) {
this._rotateHandles.addLayer(new L.RotateHandle(overlay, i));
}
// handle includes rotate AND scale
this._freeRotateHandles = L.layerGroup();
for (i = 0; i < 4; i++) {
this._freeRotateHandles.addLayer(new L.RotateScaleHandle(overlay, i));
}
this._lockHandles = L.layerGroup();
for (i = 0; i < 4; i++) {
this._lockHandles.addLayer(
new L.LockHandle(overlay, i, {draggable: false})
);
}
this._handles = {
scale: this._scaleHandles,
distort: this._distortHandles,
rotate: this._rotateHandles,
freeRotate: this._freeRotateHandles,
lock: this._lockHandles,
};
},
_appendHandlesandDragable: function(mode) {
var overlay = this._overlay;
var map = overlay._map;
map.addLayer(this._handles[mode]);
if (mode !== 'lock') {
if (!this._selected) {
this._handles[mode].eachLayer(function(layer) {
layer.setOpacity(0);
layer.dragging.disable();
layer.options.draggable = false;
});
}
this._enableDragging();
}
},
_onKeyDown: function(e) {
var keymap = this.options.keymap;
var handlerName = keymap[e.key];
var ov = this._overlay;
var eP = this.parentGroup;
if (eP && eP.anySelected()) { return; }
if (this[handlerName] !== undefined && !ov.options.suppressToolbar) {
if (this._selected && this.toolbar) {
this[handlerName].call(this);
}
}
},
addTool: function(value) {
if (value.baseClass === 'leaflet-toolbar-icon' && !this.hasTool(value)) {
this._removeToolbar();
this.editActions.push(value);
this._addToolbar();
} else {
return false;
}
},
hasTool: function(value) {
return this.editActions.some(function(action) {
return action === value;
});
},
removeTool: function(value) {
this.editActions.some(function(item, idx) {
if (this.editActions[idx] === value) {
this._removeToolbar();
this.editActions.splice(idx, 1);
this._addToolbar();
return true;
} else {
return false;
}
}, this);
},
_removeToolbar: function() {
var overlay = this._overlay;
var map = overlay._map;
if (this.toolbar) {
map.removeLayer(this.toolbar);
this.toolbar = false;
}
},
_enableDragging: function() {
var overlay = this._overlay;
var map = overlay._map;
this.dragging = new L.Draggable(overlay.getElement());
this.dragging.enable();
/* Hide toolbars and markers while dragging; click will re-show it */
this.dragging.on('dragstart', function() {
overlay.fire('dragstart');
this._removeToolbar();
}, this);
/*
* Adjust default behavior of L.Draggable.
* By default, L.Draggable overwrites the CSS3 distort transform
* that we want when it calls L.DomUtil.setPosition.
*/
this.dragging._updatePosition = function() {
var topLeft = overlay.getCorner(0);
var delta = this._newPos.subtract(map.latLngToLayerPoint(topLeft));
var currentPoint;
var corners = {0: '', 1: '', 2: '', 3: ''};
var i;
this.fire('predrag');
for (i = 0; i < 4; i++) {
currentPoint = map.latLngToLayerPoint(overlay.getCorner(i));
corners[i] = map.layerPointToLatLng(currentPoint.add(delta));
}
overlay.setCorners(corners);
overlay.fire('drag');
this.fire('drag');
};
},
_scaleMode: function() {
if (!this.hasTool(L.ScaleAction)) { return; }
this._setMode('scale');
},
_distortMode: function() {
if (!this.hasTool(L.DistortAction)) { return; }
this._setMode('distort');
},
_rotateMode: function() {
if (!this.hasTool(L.RotateAction)) { return; }
this._setMode('rotate');
},
_freeRotateMode: function() {
if (!this.hasTool(L.FreeRotateAction)) { return; }
this._setMode('freeRotate');
},
_toggleLockMode: function() {
if (!this.hasTool(L.LockAction)) { return; }
if (this._mode === 'lock') { this._unlock(); }
else { this._lock(); }
},
_toggleOpacity: function() {
var image = this._overlay.getElement();
var opacity;
if (!this.hasTool(L.OpacityAction)) { return; }
this._transparent = !this._transparent;
opacity = this._transparent ? this.options.opacity : 1;
L.DomUtil.setOpacity(image, opacity);
image.setAttribute('opacity', opacity);
this._refresh();
},
_toggleBorder: function() {
var image = this._overlay.getElement();
var opacity;
var outline;
if (!this.hasTool(L.BorderAction)) { return; }
this._outlined = !this._outlined;
outline = this._outlined ? this.options.outline : 'none';
L.DomUtil.setOpacity(image, opacity);
image.setAttribute('opacity', opacity);
image.style.outline = outline;
this._refresh();
},
// compare this to using overlay zIndex
_toggleOrder: function() {
if (!this.hasTool(L.StackAction)) { return; }
if (this._toggledImage) { this._stackUp(); }
else { this._stackDown(); }
},
_removeOverlay: function() {
var ov = this._overlay;
var eP = this.parentGroup;
var m = this._mode;
if (m === 'lock' || !this.hasTool(L.DeleteAction)) { return; }
var choice = L.DomUtil.confirmDelete();
if (!choice) { return; }
this._removeToolbar();
if (eP) { eP.removeLayer(ov); }
else { ov._map.removeLayer(ov); }
},
// Based on https://github.com/publiclab/mapknitter/blob/8d94132c81b3040ae0d0b4627e685ff75275b416/app/assets/javascripts/mapknitter/Map.js#L47-L82
_getExport: function() {
var overlay = this._overlay;
var map = overlay._map;
if (!this.hasTool(L.ExportAction)) { return; }
// make a new image
var downloadable = new Image();
downloadable.id = downloadable.id || 'tempId12345';
document.body.appendChild(downloadable);
downloadable.onload = function onLoadDownloadableImage() {
var height = downloadable.height;
var width = downloadable.width;
var nw = map.latLngToLayerPoint(overlay.getCorner(0));
var ne = map.latLngToLayerPoint(overlay.getCorner(1));
var sw = map.latLngToLayerPoint(overlay.getCorner(2));
var se = map.latLngToLayerPoint(overlay.getCorner(3));
// I think this is to move the image to the upper left corner,
// eslint-disable-next-line max-len
// jywarren: i think we may need these or the image goes off the edge of the canvas
// jywarren: but these seem to break the distortion math...
// jywarren: i think it should be rejiggered so it
// finds the most negative values of x and y and then
// adds those to all coordinates
// nw.x -= nw.x;
// ne.x -= nw.x;
// se.x -= nw.x;
// sw.x -= nw.x;
// nw.y -= nw.y;
// ne.y -= nw.y;
// se.y -= nw.y;
// sw.y -= nw.y;
// run once warping is complete
downloadable.onload = function() {
L.DomUtil.remove(downloadable);
};
if (window && window.hasOwnProperty('warpWebGl')) {
warpWebGl(
downloadable.id,
[0, 0, width, 0, width, height, 0, height],
[nw.x, nw.y, ne.x, ne.y, se.x, se.y, sw.x, sw.y],
true // trigger download
);
}
};
downloadable.src = overlay.options.fullResolutionSrc || overlay._image.src;
},
_stackUp: function() {
var t = this._toggledImage;
if (!t || !this.hasTool(L.StackAction)) { return; }
this._toggledImage = false;
this._overlay.bringToFront();
this._refresh();
},
_stackDown: function() {
var t = this._toggledImage;
if (t || !this.hasTool(L.StackAction)) { return; }
this._toggledImage = true;
this._overlay.bringToBack();
this._refresh();
},
_unlock: function() {
var ov = this._overlay;
var map = ov._map;
var m = this._mode;
if (m !== 'lock' || !this.hasTool(L.LockAction)) { return; }
map.removeLayer(this._handles[m]);
if (ov.options.mode === 'lock') {
this._mode = 'distort';
} else {
this._mode = ov.options.mode;
}
this._enableDragging();
map.addLayer(this._handles[this._mode]);
this._refresh();
},
_lock: function() {
var map = this._overlay._map;
var m = this._mode;
if (m === 'lock' || !this.hasTool(L.LockAction)) { return; }
map.removeLayer(this._handles[m]);
this._mode = 'lock';
if (this.dragging) {
this.dragging.disable();
delete this.dragging;
}
map.addLayer(this._handles[this._mode]);
this._refresh();
},
_singleClick: function(e) {
if (e.type === 'singleclick') { this._deselect(); }
else { return; }
},
_singleClickListeners: function() {
var map = this._overlay._map;
L.DomEvent.off(map, 'click', this._deselect, this);
},
_resetClickListeners: function() {
var map = this._overlay._map;
L.DomEvent.on(map, 'click', this._deselect, this);
},
_select: function(e) {
this._selected = true;
this._addToolbar();
this._showMarkers();
if (e) { L.DomEvent.stopPropagation(e); }
},
_deselect: function() {
this._selected = false;
this._removeToolbar();
if (this._mode !== 'lock') {
this._hideMarkers();
}
},
_showMarkers: function() {
var eP = this.parentGroup;
var m = this._mode;
// mutli-image interface doesn't have markers so check if its on & return early if true
if (this._mode === 'lock' || eP && eP.anySelected()) { return; }
var currentHandle = this._handles[m];
currentHandle.eachLayer(function(layer) {
var drag = layer.dragging;
var opts = layer.options;
layer.setOpacity(1);
if (drag) { drag.enable(); }
if (opts.draggable) { opts.draggable = true; }
});
},
_hideMarkers: function() {
// workaround for race condition w/ feature group
if (!this._handles) { this._initHandles(); }
var m = this._mode;
var currentHandle = this._handles[m];
currentHandle.eachLayer(function(layer) {
var drag = layer.dragging;
var opts = layer.options;
if (m !== 'lock') { layer.setOpacity(0); }
if (drag) { drag.disable(); }
if (opts.draggable) { opts.draggable = false; }
});
},
_addToolbar: function() {
var ov = this._overlay;
var eP = this.parentGroup;
var map = ov._map;
// Find the topmost point on the image.
var corners = ov.getCorners();
var maxLat = -Infinity;
if (eP && eP.anySelected()) {
eP.editing._addToolbar();
return;
}
if (ov.options.suppressToolbar || this.toolbar) { return; }
for (var i = 0; i < corners.length; i++) {
if (corners[i].lat > maxLat) {
maxLat = corners[i].lat;
}
}
// Longitude is based on the centroid of the image.
var raisedPoint = ov.getCenter();
raisedPoint.lat = maxLat;
try {
this.toolbar = L.distortableImage.popupBar(raisedPoint, {
actions: this.editActions,
}).addTo(map, ov);
ov.fire('toolbar:created');
} catch (e) { }
},
_refresh: function() {
if (this.toolbar) { this._removeToolbar(); }
this._addToolbar();
},
_updateToolbarPos: function() {
var overlay = this._overlay;
// Find the topmost point on the image.
var corners = overlay.getCorners();
var toolbar = this.toolbar;
var maxLat = -Infinity;
if (toolbar && toolbar instanceof L.DistortableImage.PopupBar) {
for (var i = 0; i < corners.length; i++) {
if (corners[i].lat > maxLat) {
maxLat = corners[i].lat;
}
}
// Longitude is based on the centroid of the image.
var raisedPoint = overlay.getCenter();
raisedPoint.lat = maxLat;
if (!overlay.options.suppressToolbar) {
this.toolbar.setLatLng(raisedPoint);
}
}
},
getMode: function() {
return this._mode;
},
_setMode: function(newMode) {
var map = this._overlay._map;
var eP = this.parentGroup;
var m = this._mode;
if ((eP && eP.anySelected()) || !this.enabled()) { return false; }
if (newMode === m || !this.toolbar) { return false; }
this.toolbar.clickTool(newMode);
if (this._modes.indexOf(newMode) !== -1) {
if (m === 'lock' && !this.dragging) { this._enableDragging(); }
map.removeLayer(this._handles[m]);
this._mode = newMode;
map.addLayer(this._handles[this._mode]);
}
this._refresh();
return this;
},
/**
* need to attach a stop to img dblclick or it will propagate to
* the map and fire the handler that shows map location labels on map dblclick.
*/
_nextMode: function(e) {
var m = this._mode;
var idx = this._modes.indexOf(m);
var nextIdx = (idx + 1) % this._modes.length;
var newMode = this._modes[nextIdx];
if (e) { L.DomEvent.stop(e); }
if (this._modes.indexOf(newMode) !== -1) {
return this._setMode(newMode);
} else { return false; }
},
});
L.distortableImage.edit = function(overlay, options) {
return new L.DistortableImage.Edit(overlay, options);
};