Added documentDrag option to the DragPan control and Drag handler.

When set to true, this allow pan-dragging while outside the map. 
Thanks vmx for the initial patches. r=vmx, bartvde (closes #39)


git-svn-id: http://svn.openlayers.org/trunk/openlayers@9791 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
ahocevar
2009-11-10 11:14:28 +00:00
parent 01debb1c53
commit c9df8f4d43
4 changed files with 153 additions and 15 deletions

View File

@@ -83,6 +83,20 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
* This is "private", and should be left alone.
*/
timeoutId: null,
/**
* APIProperty: documentDrag
* {Boolean} If set to true, the handler will also handle mouse moves when
* the cursor has moved out of the map viewport. Default is false.
*/
documentDrag: false,
/**
* Property: documentEvents
* {<OpenLayers.Events>} Event instance for observing document events. Will
* be set on mouseout if documentDrag is set to true.
*/
documentEvents: null,
/**
* Constructor: OpenLayers.Handler.Drag
@@ -210,6 +224,16 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
*/
mousemove: function (evt) {
if (this.started && !this.timeoutId && (evt.xy.x != this.last.x || evt.xy.y != this.last.y)) {
if(this.documentDrag === true && this.documentEvents) {
if(evt.element === document) {
this.adjustXY(evt);
// do setEvent manually because the documentEvents are not
// registered with the map
this.setEvent(evt);
} else {
this.destroyDocumentEvents();
}
}
if (this.interval > 0) {
this.timeoutId = setTimeout(OpenLayers.Function.bind(this.removeTimeout, this), this.interval);
}
@@ -245,6 +269,10 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
*/
mouseup: function (evt) {
if (this.started) {
if(this.documentDrag === true && this.documentEvents) {
this.adjustXY(evt);
this.destroyDocumentEvents();
}
var dragged = (this.start != this.last);
this.started = false;
this.dragging = false;
@@ -273,19 +301,31 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
*/
mouseout: function (evt) {
if (this.started && OpenLayers.Util.mouseLeft(evt, this.map.div)) {
var dragged = (this.start != this.last);
this.started = false;
this.dragging = false;
OpenLayers.Element.removeClass(
this.map.viewPortDiv, "olDragDown"
);
this.out(evt);
this.callback("out", []);
if(dragged) {
this.callback("done", [evt.xy]);
}
if(document.onselectstart) {
document.onselectstart = this.oldOnselectstart;
if(this.documentDrag === true) {
this.documentEvents = new OpenLayers.Events(this, document,
null, null, {includeXY: true});
this.documentEvents.on({
mousemove: this.mousemove,
mouseup: this.mouseup
});
OpenLayers.Element.addClass(
document.body, "olDragDown"
);
} else {
var dragged = (this.start != this.last);
this.started = false;
this.dragging = false;
OpenLayers.Element.removeClass(
this.map.viewPortDiv, "olDragDown"
);
this.out(evt);
this.callback("out", []);
if(dragged) {
this.callback("done", [evt.xy]);
}
if(document.onselectstart) {
document.onselectstart = this.oldOnselectstart;
}
}
}
return true;
@@ -345,6 +385,35 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
}
return deactivated;
},
/**
* Method: adjustXY
* Converts event coordinates that are relative to the document body to
* ones that are relative to the map viewport. The latter is the default in
* OpenLayers.
*
* Parameters:
* evt - {Object}
*/
adjustXY: function(evt) {
var pos = OpenLayers.Util.pagePosition(this.map.div);
evt.xy.x -= pos[0];
evt.xy.y -= pos[1];
},
/**
* Method: destroyDocumentEvents
* Destroys the events instance that gets added to the document body when
* documentDrag is true and the mouse cursor leaves the map viewport while
* dragging.
*/
destroyDocumentEvents: function() {
OpenLayers.Element.removeClass(
document.body, "olDragDown"
);
this.documentEvents.destroy();
this.documentEvents = null;
},
CLASS_NAME: "OpenLayers.Handler.Drag"
});