From ee93d73b69f07eac7ee550074d06e5d026c6f306 Mon Sep 17 00:00:00 2001 From: Matt Spence Date: Mon, 11 Apr 2011 19:15:35 +0100 Subject: [PATCH] Refactored marker dragging into MarkerDrag and support for shadows --- src/core/Class.js | 3 +++ src/handler/Handler.js | 9 +++++-- src/handler/MapDrag.js | 1 + src/handler/MarkerDrag.js | 51 ++++++++++++++++++++++++++++++++++++++ src/layer/marker/Marker.js | 34 ++++++++----------------- src/map/Map.js | 1 + 6 files changed, 73 insertions(+), 26 deletions(-) create mode 100644 src/handler/MarkerDrag.js diff --git a/src/core/Class.js b/src/core/Class.js index 5ae58a3a..09a9e539 100644 --- a/src/core/Class.js +++ b/src/core/Class.js @@ -24,6 +24,9 @@ L.Class.extend = function(/*Object*/ props) /*-> Class*/ { // add superclass access proto.superclass = this.prototype; + // add class name + //proto.className = props; + // mix static properties into the class if (props.statics) { L.Util.extend(NewClass, props.statics); diff --git a/src/handler/Handler.js b/src/handler/Handler.js index 195986b8..4e6f207d 100644 --- a/src/handler/Handler.js +++ b/src/handler/Handler.js @@ -3,8 +3,13 @@ */ L.Handler = L.Class.extend({ - initialize: function(map) { - this._map = map; + initialize: function(handlee) { + // not sure this is the best name for this property + this._handlee = handlee; + // this ensures map handles looking for ._map can still find it + // I would remove this and change them but with out full test coverage + // am worried I will break something + this._map = handlee; }, enabled: function() { diff --git a/src/handler/MapDrag.js b/src/handler/MapDrag.js index 997a66ad..49c47af6 100644 --- a/src/handler/MapDrag.js +++ b/src/handler/MapDrag.js @@ -3,6 +3,7 @@ */ L.Handler.MapDrag = L.Handler.extend({ + enable: function() { if (this._enabled) { return; } if (!this._draggable) { diff --git a/src/handler/MarkerDrag.js b/src/handler/MarkerDrag.js new file mode 100644 index 00000000..960489af --- /dev/null +++ b/src/handler/MarkerDrag.js @@ -0,0 +1,51 @@ +/* + * L.Handler.MarkerDrag is used internally by L.Marker to make the markers draggable. + */ + +L.Handler.MarkerDrag = L.Handler.extend({ + + enable: function() { + if (this._enabled) { return; } + if (!this._draggable) { + this._draggable = new L.Draggable(this._handlee._icon, this._handlee._icon); + this._draggable.on('dragstart', this._onDragStart, this); + this._draggable.on('drag', this._onDrag, this); + this._draggable.on('dragend', this._onDragEnd, this); + } + this._draggable.enable(); + this._enabled = true; + }, + + disable: function() { + if (!this._enabled) { return; } + this._draggable.disable(); + this._enabled = false; + }, + + moved: function() { + return this._draggable._moved; + }, + + _onDragStart: function(e) { + this._handlee.fire('movestart',this); + this._handlee.fire('dragstart',"hello"); + this._dragStartShadowPos = L.DomUtil.getPosition(this._handlee._shadow); + }, + + _onDrag: function(e) { + + // update shadow position + var newShadowPos = this._dragStartShadowPos.add(e.target._offset); + L.DomUtil.setPosition(this._handlee._shadow, newShadowPos); + + this._handlee.fire('move',this); + this._handlee.fire('drag',this); + + }, + + _onDragEnd: function() { + this._handlee.fire('moveend',this); + this._handlee.fire('dragend',this); + } + +}); diff --git a/src/layer/marker/Marker.js b/src/layer/marker/Marker.js index 46e3fdce..64655fb8 100644 --- a/src/layer/marker/Marker.js +++ b/src/layer/marker/Marker.js @@ -4,6 +4,7 @@ L.Marker = L.Class.extend({ + includes: L.Mixin.Events, options: { @@ -67,22 +68,21 @@ L.Marker = L.Class.extend({ L.DomEvent.addListener(this._icon, 'dblclick', this._fireMouseEvent, this); } - if (this.options.draggable) { - this._draggable = new L.Draggable(this._icon, this._icon); - this._draggable.on('dragstart', this._onDragStart, this); - this._draggable.on('drag', this._onDrag, this); - this._draggable.on('dragend', this._onDragEnd, this); - this._draggable.enable(); + var handlers = { + draggable: L.Handler.MarkerDrag + } + + for (var i in handlers) { + if (handlers.hasOwnProperty(i) && handlers[i]) { + this[i] = new handlers[i](this); + if (this.options[i]) this[i].enable(); + } } }, _fireMouseEvent: function(e) { this.fire(e.type); - if (e.type == 'mouseup') { - // Draggable stops listening on document mouseup so because we are stopping propagation we will explicitaly fire it - L.DomEvent.fireEvent(document,'mouseup'); - } L.DomEvent.stopPropagation(e); }, @@ -90,19 +90,5 @@ L.Marker = L.Class.extend({ return this._draggable._moved; }, - _onDragStart: function(e) { - this.fire('movestart',this); - this.fire('dragstart',"hello"); - }, - - _onDrag: function() { - this.fire('move',this); - this.fire('drag',this); - }, - - _onDragEnd: function() { - this.fire('moveend',this); - this.fire('dragend',this); - } }); \ No newline at end of file diff --git a/src/map/Map.js b/src/map/Map.js index 286ec560..49b14e71 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -3,6 +3,7 @@ */ L.Map = L.Class.extend({ + includes: L.Mixin.Events, options: {