Files
openlayers/lib/OpenLayers/Handler/Drag.js

239 lines
6.8 KiB
JavaScript

/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license.
* See http://svn.openlayers.org/trunk/openlayers/repository-license.txt
* for the full text of the license. */
/**
* @requires OpenLayers/Handler.js
*
* Class: OpenLayers.Handler.Drag
* The drag handler is used to deal with sequences of browser events related
* to dragging. The handler is used by controls that want to know when
* a drag sequence begins, when a drag is happening, and when it has
* finished.
*
* Controls that use the drag handler typically construct it with callbacks
* for 'down', 'move', and 'done'. Callbacks for these keys are called
* when the drag begins, with each move, and when the drag is done. In
* addition, controls can have callbacks keyed to 'up' and 'out' if they
* care to differentiate between the types of events that correspond with
* the end of a drag sequence.
*
* Create a new drag handler with the <OpenLayers.Handler.Drag> constructor.
*
* Inherits from:
* - <OpenLayers.Handler>
*/
OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
/**
* Property: started
* {Boolean} When a mousedown event is received, we want to record it, but
* not set 'dragging' until the mouse moves after starting.
*/
started: false,
/**
* Property: dragging
* {Boolean}
*/
dragging: false,
/**
* Property: last
* {<OpenLayers.Pixel>} The last pixel location of the drag.
*/
last: null,
/**
* Property: start
* {<OpenLayers.Pixel>} The first pixel location of the drag.
*/
start: null,
/**
* Property: oldOnselectstart
* {Function}
*/
oldOnselectstart: null,
/**
* Constructor: OpenLayers.Handler.Drag
* Returns OpenLayers.Handler.Drag
*
* Parameters:
* control - {<OpenLayers.Control>} The control that is making use of
* this handler. If a handler is being used without a control, the
* handlers setMap method must be overridden to deal properly with
* the map.
* callbacks - {Object} An object containing a single function to be
* called when the drag operation is finished. The callback should
* expect to recieve a single argument, the pixel location of the event.
* Callbacks for 'move' and 'done' are supported. You can also speficy
* callbacks for 'down', 'up', and 'out' to respond to those events.
* options - {Object}
*/
initialize: function(control, callbacks, options) {
OpenLayers.Handler.prototype.initialize.apply(this, arguments);
},
/**
* The methods below are part of the magic of event handling. Because
* they are named like browser events, they are registered as listeners
* for the events they represent.
*/
/**
* Method: mousedown
* Handle mousedown events
*
* Parameters:
* evt - {Event}
*
* Return:
* {Boolean} Let the event propagate.
*/
mousedown: function (evt) {
if (this.checkModifiers(evt) && OpenLayers.Event.isLeftClick(evt)) {
this.started = true;
this.dragging = false;
this.start = evt.xy;
this.last = evt.xy;
// TBD replace with CSS classes
this.map.div.style.cursor = "move";
this.callback("down", [evt.xy]);
OpenLayers.Event.stop(evt);
return false;
}
return true;
},
/**
* Method: mousemove
* Handle mousemove events
*
* Parameters:
* evt - {Event}
*
* Return:
* {Boolean} Let the event propagate.
*/
mousemove: function (evt) {
if (this.started) {
if(evt.xy.x != this.last.x || evt.xy.y != this.last.y) {
this.dragging = true;
this.callback("move", [evt.xy]);
if(!this.oldOnselectstart) {
this.oldOnselectstart = document.onselectstart;
document.onselectstart = function() {return false;}
}
this.last = evt.xy;
}
}
return true;
},
/**
* Method: mouseup
* Handle mouseup events
*
* Parameters:
* evt - {Event}
*
* Return:
* {Boolean} Let the event propagate.
*/
mouseup: function (evt) {
if (this.started) {
this.started = false;
this.dragging = false;
// TBD replace with CSS classes
this.map.div.style.cursor = "";
this.callback("up", [evt.xy]);
this.callback("done", [evt.xy]);
document.onselectstart = this.oldOnselectstart;
}
return true;
},
/**
* Method: mouseout
* Handle mouseout events
*
* Parameters:
* evt - {Event}
*
* Return:
* {Boolean} Let the event propagate.
*/
mouseout: function (evt) {
if (this.started && OpenLayers.Util.mouseLeft(evt, this.map.div)) {
this.started = false;
this.dragging = false;
// TBD replace with CSS classes
this.map.div.style.cursor = "";
this.callback("out", []);
if(document.onselectstart) {
document.onselectstart = this.oldOnselectstart;
}
this.callback("done", [evt.xy])
}
return true;
},
/**
* Method: click
* The drag handler captures the click event. If something else registers
* for clicks on the same element, its listener will not be called
* after a drag.
*
* Parameters:
* evt - {Event}
*
* Return:
* {Boolean} Let the event propagate.
*/
click: function (evt) {
// throw away the first left click event that happens after a mouse up
if (this.dragging) {
this.dragging = false;
return false;
}
this.started = false;
return true;
},
/**
* Method: activate
* Activate the handler.
*
* Return:
* {Boolean} The handler was successfully activated.
*/
activate: function() {
if(OpenLayers.Handler.prototype.activate.apply(this, arguments)) {
this.dragging = false;
return true;
} else {
return false;
}
},
/**
* Method: deactivate
* Deactivate the handler.
*
* Return:
* {Boolean} The handler was successfully deactivated.
*/
deactivate: function() {
if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
this.dragging = false;
return true;
} else {
return false;
}
},
CLASS_NAME: "OpenLayers.Handler.Drag"
});