Modify drag handler for easier subclassing. Thanks for the team effort on this one (closes #827).
git-svn-id: http://svn.openlayers.org/trunk/openlayers@4096 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -76,6 +76,56 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
|
||||
OpenLayers.Handler.prototype.initialize.apply(this, arguments);
|
||||
},
|
||||
|
||||
/**
|
||||
* The four methods below (down, move, up, and out) are used by subclasses
|
||||
* to do their own processing related to these mouse events.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Method: down
|
||||
* This method is called during the handling of the mouse down event.
|
||||
* Subclasses can do their own processing here.
|
||||
*
|
||||
* Parameters:
|
||||
* evt - {Event} The mouse down event
|
||||
*/
|
||||
down: function(evt) {
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: move
|
||||
* This method is called during the handling of the mouse move event.
|
||||
* Subclasses can do their own processing here.
|
||||
*
|
||||
* Parameters:
|
||||
* evt - {Event} The mouse move event
|
||||
*
|
||||
*/
|
||||
move: function(evt) {
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: up
|
||||
* This method is called during the handling of the mouse up event.
|
||||
* Subclasses can do their own processing here.
|
||||
*
|
||||
* Parameters:
|
||||
* evt - {Event} The mouse up event
|
||||
*/
|
||||
up: function(evt) {
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: out
|
||||
* This method is called during the handling of the mouse out event.
|
||||
* Subclasses can do their own processing here.
|
||||
*
|
||||
* Parameters:
|
||||
* evt - {Event} The mouse out event
|
||||
*/
|
||||
out: function(evt) {
|
||||
},
|
||||
|
||||
/**
|
||||
* The methods below are part of the magic of event handling. Because
|
||||
* they are named like browser events, they are registered as listeners
|
||||
@@ -100,6 +150,7 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
|
||||
this.last = evt.xy;
|
||||
// TBD replace with CSS classes
|
||||
this.map.div.style.cursor = "move";
|
||||
this.down(evt);
|
||||
this.callback("down", [evt.xy]);
|
||||
OpenLayers.Event.stop(evt);
|
||||
return false;
|
||||
@@ -121,6 +172,7 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
|
||||
if (this.started) {
|
||||
if(evt.xy.x != this.last.x || evt.xy.y != this.last.y) {
|
||||
this.dragging = true;
|
||||
this.move(evt);
|
||||
this.callback("move", [evt.xy]);
|
||||
if(!this.oldOnselectstart) {
|
||||
this.oldOnselectstart = document.onselectstart;
|
||||
@@ -148,6 +200,7 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
|
||||
this.dragging = false;
|
||||
// TBD replace with CSS classes
|
||||
this.map.div.style.cursor = "";
|
||||
this.up(evt);
|
||||
this.callback("up", [evt.xy]);
|
||||
this.callback("done", [evt.xy]);
|
||||
document.onselectstart = this.oldOnselectstart;
|
||||
@@ -171,6 +224,7 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
|
||||
this.dragging = false;
|
||||
// TBD replace with CSS classes
|
||||
this.map.div.style.cursor = "";
|
||||
this.out(evt);
|
||||
this.callback("out", []);
|
||||
if(document.onselectstart) {
|
||||
document.onselectstart = this.oldOnselectstart;
|
||||
@@ -210,12 +264,12 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
|
||||
* {Boolean} The handler was successfully activated.
|
||||
*/
|
||||
activate: function() {
|
||||
var activated = false;
|
||||
if(OpenLayers.Handler.prototype.activate.apply(this, arguments)) {
|
||||
this.dragging = false;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
activated = true;
|
||||
}
|
||||
return activated;
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -226,12 +280,12 @@ OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
|
||||
* {Boolean} The handler was successfully deactivated.
|
||||
*/
|
||||
deactivate: function() {
|
||||
var deactivated = false;
|
||||
if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
|
||||
this.dragging = false;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
deactivated = true;
|
||||
}
|
||||
return deactivated;
|
||||
},
|
||||
|
||||
CLASS_NAME: "OpenLayers.Handler.Drag"
|
||||
|
||||
@@ -246,6 +246,63 @@
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
function test_Handler_Drag_submethods(t) {
|
||||
t.plan(4);
|
||||
|
||||
var map = new OpenLayers.Map('map', {controls: []});
|
||||
|
||||
var control = new OpenLayers.Control();
|
||||
map.addControl(control);
|
||||
|
||||
|
||||
var handler = new OpenLayers.Handler.Drag(control, {});
|
||||
// set test events
|
||||
var events = ["down", "move", "up", "out"];
|
||||
var testEvents = {};
|
||||
var type, px;
|
||||
for(var i=0; i<events.length; ++i) {
|
||||
type = events[i];
|
||||
px = new OpenLayers.Pixel(Math.random(), Math.random());
|
||||
testEvents[type] = {xy: px};
|
||||
setMethod(type);
|
||||
}
|
||||
function setMethod(type) {
|
||||
handler[type] = function(evt) {
|
||||
t.ok(evt.xy.x == testEvents[type].xy.x &&
|
||||
evt.xy.y == testEvents[type].xy.y,
|
||||
"handler." + type + " called with the right event");
|
||||
}
|
||||
}
|
||||
handler.activate();
|
||||
|
||||
// test mousedown
|
||||
handler.checkModifiers = function(evt) {
|
||||
return true;
|
||||
}
|
||||
var oldIsLeftClick = OpenLayers.Event.isLeftClick;
|
||||
OpenLayers.Event.isLeftClick = function(evt) {
|
||||
return true;
|
||||
}
|
||||
map.events.triggerEvent("mousedown", testEvents.down);
|
||||
OpenLayers.Event.isLeftClick = oldIsLeftClick;
|
||||
|
||||
// test mousemove
|
||||
map.events.triggerEvent("mousemove", testEvents.move);
|
||||
|
||||
// test mouseup
|
||||
map.events.triggerEvent("mouseup", testEvents.up);
|
||||
|
||||
// test mouseout
|
||||
var oldMouseLeft = OpenLayers.Util.mouseLeft;
|
||||
OpenLayers.Util.mouseLeft = function() {
|
||||
return true;
|
||||
};
|
||||
handler.started = true;
|
||||
map.events.triggerEvent("mouseout", testEvents.out);
|
||||
OpenLayers.Util.mouseLeft = oldMouseLeft;
|
||||
|
||||
}
|
||||
|
||||
function test_Handler_Drag_deactivate(t) {
|
||||
|
||||
Reference in New Issue
Block a user