|
| 1 | +(function (n, u) { |
| 2 | + typeof exports == 'object' && typeof module < 'u' |
| 3 | + ? (module.exports = u(require('leaflet'))) |
| 4 | + : typeof define == 'function' && define.amd |
| 5 | + ? define(['leaflet'], u) |
| 6 | + : ((n = typeof globalThis < 'u' ? globalThis : n || self), (n['leaflet-path-drag'] = u(n.L))); |
| 7 | +})(this, function (n) { |
| 8 | + 'use strict'; |
| 9 | + n.SVG.include({ |
| 10 | + _resetTransformPath: function (t) { |
| 11 | + t._path.setAttributeNS(null, 'transform', ''); |
| 12 | + }, |
| 13 | + transformPath: function (t, s) { |
| 14 | + t._path.setAttributeNS(null, 'transform', 'matrix(' + s.join(' ') + ')'); |
| 15 | + }, |
| 16 | + }), |
| 17 | + n.SVG.include( |
| 18 | + n.Browser.vml |
| 19 | + ? { |
| 20 | + _resetTransformPath: function (t) { |
| 21 | + t._skew && ((t._skew.on = !1), t._path.removeChild(t._skew), (t._skew = null)); |
| 22 | + }, |
| 23 | + transformPath: function (t, s) { |
| 24 | + let i = t._skew; |
| 25 | + i || |
| 26 | + ((i = n.SVG.create('skew')), |
| 27 | + t._path.appendChild(i), |
| 28 | + (i.style.behavior = 'url(#default#VML)'), |
| 29 | + (t._skew = i)); |
| 30 | + const a = |
| 31 | + s[0].toFixed(8) + |
| 32 | + ' ' + |
| 33 | + s[1].toFixed(8) + |
| 34 | + ' ' + |
| 35 | + s[2].toFixed(8) + |
| 36 | + ' ' + |
| 37 | + s[3].toFixed(8) + |
| 38 | + ' 0 0', |
| 39 | + h = Math.floor(s[4]).toFixed() + ', ' + Math.floor(s[5]).toFixed(), |
| 40 | + o = this._path.style; |
| 41 | + let e = parseFloat(o.left), |
| 42 | + r = parseFloat(o.top), |
| 43 | + _ = parseFloat(o.width), |
| 44 | + p = parseFloat(o.height); |
| 45 | + isNaN(e) && (e = 0), |
| 46 | + isNaN(r) && (r = 0), |
| 47 | + (isNaN(_) || !_) && (_ = 1), |
| 48 | + (isNaN(p) || !p) && (p = 1); |
| 49 | + const m = (-e / _ - 0.5).toFixed(8) + ' ' + (-r / p - 0.5).toFixed(8); |
| 50 | + (i.on = 'f'), (i.matrix = a), (i.origin = m), (i.offset = h), (i.on = !0); |
| 51 | + }, |
| 52 | + } |
| 53 | + : {} |
| 54 | + ); |
| 55 | + function u() { |
| 56 | + return !0; |
| 57 | + } |
| 58 | + n.Canvas.include({ |
| 59 | + _resetTransformPath: function (t) { |
| 60 | + this._containerCopy && |
| 61 | + (delete this._containerCopy, |
| 62 | + t._containsPoint_ && |
| 63 | + ((t._containsPoint = t._containsPoint_), delete t._containsPoint_, this._requestRedraw(t))); |
| 64 | + }, |
| 65 | + transformPath: function (t, s) { |
| 66 | + let i = this._containerCopy; |
| 67 | + const a = this._ctx; |
| 68 | + let h; |
| 69 | + const o = n.Browser.retina ? 2 : 1, |
| 70 | + e = this._bounds, |
| 71 | + r = e.getSize(), |
| 72 | + _ = e.min; |
| 73 | + i || |
| 74 | + ((i = this._containerCopy = document.createElement('canvas')), |
| 75 | + (h = i.getContext('2d')), |
| 76 | + (i.width = o * r.x), |
| 77 | + (i.height = o * r.y), |
| 78 | + this._removePath(t), |
| 79 | + this._redraw(), |
| 80 | + h.translate(o * e.min.x, o * e.min.y), |
| 81 | + h.drawImage(this._container, 0, 0), |
| 82 | + this._initPath(t), |
| 83 | + (t._containsPoint_ = t._containsPoint), |
| 84 | + (t._containsPoint = u)), |
| 85 | + a.save(), |
| 86 | + a.clearRect(_.x, _.y, r.x * o, r.y * o), |
| 87 | + a.setTransform(1, 0, 0, 1, 0, 0), |
| 88 | + a.restore(), |
| 89 | + a.save(), |
| 90 | + a.drawImage(this._containerCopy, 0, 0, r.x, r.y), |
| 91 | + a.transform.apply(a, s), |
| 92 | + (this._drawing = !0), |
| 93 | + t._updatePath(), |
| 94 | + (this._drawing = !1), |
| 95 | + a.restore(); |
| 96 | + }, |
| 97 | + }); |
| 98 | + /** |
| 99 | + * Leaflet vector features drag functionality |
| 100 | + * @author Alexander Milevski <info@w8r.name> |
| 101 | + * @preserve |
| 102 | + */ |
| 103 | + n.Path.include({ |
| 104 | + _transform: function (t) { |
| 105 | + return ( |
| 106 | + this._renderer && |
| 107 | + (t |
| 108 | + ? this._renderer.transformPath(this, t) |
| 109 | + : (this._renderer._resetTransformPath(this), this._update())), |
| 110 | + this |
| 111 | + ); |
| 112 | + }, |
| 113 | + _onMouseClick: function (t) { |
| 114 | + (this.dragging && this.dragging.moved()) || |
| 115 | + (this._map.dragging && this._map.dragging.moved()) || |
| 116 | + this._fireMouseEvent(t); |
| 117 | + }, |
| 118 | + }); |
| 119 | + const f = { mousedown: 'mouseup', touchstart: 'touchend', pointerdown: 'touchend', MSPointerDown: 'touchend' }, |
| 120 | + l = { mousedown: 'mousemove', touchstart: 'touchmove', pointerdown: 'touchmove', MSPointerDown: 'touchmove' }; |
| 121 | + function P(t, s) { |
| 122 | + const i = t.x - s.x, |
| 123 | + a = t.y - s.y; |
| 124 | + return Math.sqrt(i * i + a * a); |
| 125 | + } |
| 126 | + return ( |
| 127 | + (n.Handler.PathDrag = n.Handler.extend({ |
| 128 | + statics: { DRAGGING_CLS: 'leaflet-path-draggable' }, |
| 129 | + initialize: function (t) { |
| 130 | + (this._path = t), |
| 131 | + (this._matrix = null), |
| 132 | + (this._startPoint = null), |
| 133 | + (this._dragStartPoint = null), |
| 134 | + (this._mapDraggingWasEnabled = !1), |
| 135 | + (this._path._dragMoved = !1); |
| 136 | + }, |
| 137 | + addHooks: function () { |
| 138 | + this._path.on('mousedown', this._onDragStart, this), |
| 139 | + (this._path.options.className = this._path.options.className |
| 140 | + ? this._path.options.className + ' ' + n.Handler.PathDrag.DRAGGING_CLS |
| 141 | + : n.Handler.PathDrag.DRAGGING_CLS), |
| 142 | + this._path._path && n.DomUtil.addClass(this._path._path, n.Handler.PathDrag.DRAGGING_CLS); |
| 143 | + }, |
| 144 | + removeHooks: function () { |
| 145 | + this._path.off('mousedown', this._onDragStart, this), |
| 146 | + (this._path.options.className = this._path.options.className.replace( |
| 147 | + new RegExp('\\s+' + n.Handler.PathDrag.DRAGGING_CLS), |
| 148 | + '' |
| 149 | + )), |
| 150 | + this._path._path && n.DomUtil.removeClass(this._path._path, n.Handler.PathDrag.DRAGGING_CLS); |
| 151 | + }, |
| 152 | + moved: function () { |
| 153 | + return this._path._dragMoved; |
| 154 | + }, |
| 155 | + _onDragStart: function (t) { |
| 156 | + const s = t.originalEvent._simulated ? 'touchstart' : t.originalEvent.type; |
| 157 | + (this._mapDraggingWasEnabled = !1), |
| 158 | + (this._startPoint = t.containerPoint.clone()), |
| 159 | + (this._dragStartPoint = t.containerPoint.clone()), |
| 160 | + (this._matrix = [1, 0, 0, 1, 0, 0]), |
| 161 | + n.DomEvent.stop(t.originalEvent), |
| 162 | + n.DomUtil.addClass(this._path._renderer._container, 'leaflet-interactive'), |
| 163 | + n.DomEvent.on(document, l[s], this._onDrag, this).on(document, f[s], this._onDragEnd, this), |
| 164 | + this._path._map.dragging.enabled() && |
| 165 | + (this._path._map.dragging.disable(), (this._mapDraggingWasEnabled = !0)), |
| 166 | + (this._path._dragMoved = !1), |
| 167 | + this._path._popup && this._path._popup.close(), |
| 168 | + this._replaceCoordGetters(t); |
| 169 | + }, |
| 170 | + _onDrag: function (t) { |
| 171 | + n.DomEvent.stop(t); |
| 172 | + const s = t.touches && t.touches.length >= 1 ? t.touches[0] : t, |
| 173 | + i = this._path._map.mouseEventToContainerPoint(s); |
| 174 | + if ( |
| 175 | + t.type === 'touchmove' && |
| 176 | + !this._path._dragMoved && |
| 177 | + this._dragStartPoint.distanceTo(i) <= this._path._map.options.tapTolerance |
| 178 | + ) |
| 179 | + return; |
| 180 | + const a = i.x, |
| 181 | + h = i.y, |
| 182 | + o = a - this._startPoint.x, |
| 183 | + e = h - this._startPoint.y; |
| 184 | + (o || e) && |
| 185 | + (this._path._dragMoved || |
| 186 | + ((this._path._dragMoved = !0), |
| 187 | + (this._path.options.interactive = !1), |
| 188 | + (this._path._map.dragging._draggable._moved = !0), |
| 189 | + this._path.fire('dragstart', t), |
| 190 | + this._path.bringToFront()), |
| 191 | + (this._matrix[4] += o), |
| 192 | + (this._matrix[5] += e), |
| 193 | + (this._startPoint.x = a), |
| 194 | + (this._startPoint.y = h), |
| 195 | + this._path.fire('predrag', t), |
| 196 | + this._path._transform(this._matrix), |
| 197 | + this._path.fire('drag', t)); |
| 198 | + }, |
| 199 | + _onDragEnd: function (t) { |
| 200 | + const s = this._path._map.mouseEventToContainerPoint(t), |
| 201 | + i = this.moved(); |
| 202 | + if ( |
| 203 | + (i && |
| 204 | + (this._transformPoints(this._matrix), |
| 205 | + this._path._updatePath(), |
| 206 | + this._path._project(), |
| 207 | + this._path._transform(null), |
| 208 | + n.DomEvent.stop(t)), |
| 209 | + n.DomEvent.off(document, 'mousemove touchmove', this._onDrag, this), |
| 210 | + n.DomEvent.off(document, 'mouseup touchend', this._onDragEnd, this), |
| 211 | + this._restoreCoordGetters(), |
| 212 | + i) |
| 213 | + ) { |
| 214 | + this._path.fire('dragend', { distance: P(this._dragStartPoint, s) }); |
| 215 | + const a = this._path._containsPoint; |
| 216 | + (this._path._containsPoint = n.Util.falseFn), |
| 217 | + n.Util.requestAnimFrame(function () { |
| 218 | + (this._path._dragMoved = !1), |
| 219 | + (this._path.options.interactive = !0), |
| 220 | + (this._path._containsPoint = a); |
| 221 | + }, this); |
| 222 | + } |
| 223 | + this._mapDraggingWasEnabled && this._path._map.dragging.enable(); |
| 224 | + }, |
| 225 | + _transformPoints: function (t, s) { |
| 226 | + const i = this._path, |
| 227 | + a = L.point(t[4], t[5]), |
| 228 | + h = i._map.options.crs, |
| 229 | + o = h.transformation, |
| 230 | + e = h.scale(i._map.getZoom()), |
| 231 | + r = h.projection, |
| 232 | + _ = o.untransform(a, e).subtract(o.untransform(n.point(0, 0), e)), |
| 233 | + p = !s; |
| 234 | + if (((i._bounds = new n.LatLngBounds()), i._point)) |
| 235 | + (s = r.unproject(r.project(i._latlng)._add(_))), p && ((i._latlng = s), i._point._add(a)); |
| 236 | + else if (i._rings || i._parts) { |
| 237 | + const m = i._rings || i._parts; |
| 238 | + let d = i._latlngs; |
| 239 | + (s = s || d), n.Util.isArray(d[0]) || ((d = [d]), (s = [s])); |
| 240 | + for (let g = 0, D = m.length; g < D; g++) { |
| 241 | + s[g] = s[g] || []; |
| 242 | + for (let c = 0, x = m[g].length; c < x; c++) { |
| 243 | + const v = d[g][c]; |
| 244 | + (s[g][c] = r.unproject(r.project(v)._add(_))), |
| 245 | + p && (i._bounds.extend(d[g][c]), m[g][c]._add(a)); |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + return s; |
| 250 | + }, |
| 251 | + _replaceCoordGetters: function () { |
| 252 | + this._path.getLatLng |
| 253 | + ? ((this._path.getLatLng_ = this._path.getLatLng), |
| 254 | + (this._path.getLatLng = n.Util.bind(function () { |
| 255 | + return this.dragging._transformPoints(this.dragging._matrix, {}); |
| 256 | + }, this._path))) |
| 257 | + : this._path.getLatLngs && |
| 258 | + ((this._path.getLatLngs_ = this._path.getLatLngs), |
| 259 | + (this._path.getLatLngs = n.Util.bind(function () { |
| 260 | + return this.dragging._transformPoints(this.dragging._matrix, []); |
| 261 | + }, this._path))); |
| 262 | + }, |
| 263 | + _restoreCoordGetters: function () { |
| 264 | + this._path.getLatLng_ |
| 265 | + ? ((this._path.getLatLng = this._path.getLatLng_), delete this._path.getLatLng_) |
| 266 | + : this._path.getLatLngs_ && |
| 267 | + ((this._path.getLatLngs = this._path.getLatLngs_), delete this._path.getLatLngs_); |
| 268 | + }, |
| 269 | + })), |
| 270 | + (n.Handler.PathDrag.makeDraggable = function (t) { |
| 271 | + return (t.dragging = new n.Handler.PathDrag(t)), t; |
| 272 | + }), |
| 273 | + (n.Path.prototype.makeDraggable = function () { |
| 274 | + return n.Handler.PathDrag.makeDraggable(this); |
| 275 | + }), |
| 276 | + n.Path.addInitHook(function () { |
| 277 | + this.options.draggable |
| 278 | + ? ((this.options.interactive = !0), |
| 279 | + this.dragging |
| 280 | + ? this.dragging.enable() |
| 281 | + : (n.Handler.PathDrag.makeDraggable(this), this.dragging.enable())) |
| 282 | + : this.dragging && this.dragging.disable(); |
| 283 | + }), |
| 284 | + n.Handler.PathDrag |
| 285 | + ); |
| 286 | +}); |
0 commit comments