Refactored marker dragging into MarkerDrag and support for shadows

This commit is contained in:
Matt Spence 2011-04-11 19:15:35 +01:00
parent 805f3b39b9
commit ee93d73b69
6 changed files with 73 additions and 26 deletions

View File

@ -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);

View File

@ -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() {

View File

@ -3,6 +3,7 @@
*/
L.Handler.MapDrag = L.Handler.extend({
enable: function() {
if (this._enabled) { return; }
if (!this._draggable) {

51
src/handler/MarkerDrag.js Normal file
View File

@ -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);
}
});

View File

@ -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);
}
});

View File

@ -3,6 +3,7 @@
*/
L.Map = L.Class.extend({
includes: L.Mixin.Events,
options: {