--- /dev/null
+/*\r
+ * L.Draggable allows you to add dragging capabilities to any element. Supports mobile devices too.\r
+ */\r
+\r
+L.Draggable = L.Class.extend({\r
+ includes: L.Mixin.Events,\r
+ \r
+ statics: {\r
+ START: L.Browser.touch ? 'touchstart' : 'mousedown',\r
+ END: L.Browser.touch ? 'touchend' : 'mouseup',\r
+ MOVE: L.Browser.touch ? 'touchmove' : 'mousemove',\r
+ TAP_TOLERANCE: 15\r
+ },\r
+ \r
+ initialize: function(element, dragStartTarget) {\r
+ this._element = element;\r
+ this._dragStartTarget = dragStartTarget || element;\r
+ },\r
+ \r
+ enable: function() {\r
+ if (this._enabled) { return; }\r
+ L.DomEvent.addListener(this._dragStartTarget, L.Draggable.START, this._onDown, this);\r
+ this._enabled = true;\r
+ },\r
+ \r
+ disable: function() {\r
+ if (!this._enabled) { return; }\r
+ L.DomEvent.removeListener(this._dragStartTarget, L.Draggable.START, this._onDown);\r
+ this._enabled = false;\r
+ },\r
+\r
+ _onDown: function(e) {\r
+ if (e.shiftKey || ((e.which != 1) && (e.button != 1) && !e.touches)) { return; }\r
+ \r
+ if (e.touches && e.touches.length > 1) { return; }\r
+\r
+ var first = (e.touches && e.touches.length == 1 ? e.touches[0] : e);\r
+ \r
+ L.DomEvent.preventDefault(e);\r
+ \r
+ if (L.Browser.mobileWebkit) {\r
+ first.target.className += ' leaflet-active';\r
+ }\r
+ \r
+ this._moved = false;\r
+ \r
+ L.DomUtil.disableTextSelection();\r
+ this._setMovingCursor();\r
+ \r
+ this._startPos = this._newPos = L.DomUtil.getPosition(this._element);\r
+ this._startPoint = new L.Point(first.clientX, first.clientY);\r
+ \r
+ L.DomEvent.addListener(document, L.Draggable.MOVE, this._onMove, this);\r
+ L.DomEvent.addListener(document, L.Draggable.END, this._onUp, this);\r
+ },\r
+ \r
+ _onMove: function(e) {\r
+ if (e.touches && e.touches.length > 1) { return; }\r
+\r
+ L.DomEvent.preventDefault(e);\r
+ \r
+ var first = (e.touches && e.touches.length == 1 ? e.touches[0] : e);\r
+ \r
+ if (!this._moved) {\r
+ this.fire('dragstart');\r
+ this._moved = true;\r
+ }\r
+\r
+ var newPoint = new L.Point(first.clientX, first.clientY);\r
+ this._newPos = this._startPos.add(newPoint).subtract(this._startPoint);\r
+ \r
+ L.Util.requestAnimFrame(this._updatePosition, this, true);\r
+ \r
+ this.fire('drag');\r
+ },\r
+ \r
+ _updatePosition: function() {\r
+ L.DomUtil.setPosition(this._element, this._newPos);\r
+ },\r
+ \r
+ _onUp: function(e) {\r
+ if (e.changedTouches) {\r
+ var first = e.changedTouches[0],\r
+ el = first.target,\r
+ dist = (this._newPos && this._newPos.distanceTo(this._startPos)) || 0;\r
+ \r
+ el.className = el.className.replace(' leaflet-active', '');\r
+ \r
+ if (dist < L.Draggable.TAP_TOLERANCE) {\r
+ this._simulateEvent('click', first);\r
+ }\r
+ }\r
+ \r
+ L.DomUtil.enableTextSelection();\r
+ \r
+ this._restoreCursor();\r
+ \r
+ L.DomEvent.removeListener(document, L.Draggable.MOVE, this._onMove);\r
+ L.DomEvent.removeListener(document, L.Draggable.END, this._onUp);\r
+ \r
+ if (this._moved) {\r
+ this.fire('dragend');\r
+ }\r
+ },\r
+ \r
+ _removeActiveClass: function(el) {\r
+ },\r
+ \r
+ _setMovingCursor: function() {\r
+ this._bodyCursor = document.body.style.cursor;\r
+ document.body.style.cursor = 'move';\r
+ },\r
+ \r
+ _restoreCursor: function() {\r
+ document.body.style.cursor = this._bodyCursor;\r
+ },\r
+ \r
+ _simulateEvent: function(type, e) {\r
+ var simulatedEvent = document.createEvent('MouseEvent');\r
+ \r
+ simulatedEvent.initMouseEvent(\r
+ type, true, true, window, 1, \r
+ e.screenX, e.screenY, \r
+ e.clientX, e.clientY, \r
+ false, false, false, false, 0, null);\r
+ \r
+ e.target.dispatchEvent(simulatedEvent);\r
+ }\r
+});\r