Refactor Control to allow multiple handler types (move, hover, click)

This commit is contained in:
Matthew Perry
2012-02-06 15:22:28 -08:00
parent fb791e14fa
commit 9e3bd972f4
+71 -120
View File
@@ -5,6 +5,8 @@
/** /**
* @requires OpenLayers/Control.js * @requires OpenLayers/Control.js
* @requires OpenLayers/Handler/Hover.js
* @requires OpenLayers/Handler/Click.js
*/ */
/** /**
@@ -31,122 +33,70 @@ OpenLayers.Control.UTFGrid = OpenLayers.Class(OpenLayers.Control, {
element: null, element: null,
debugElement: null, debugElement: null,
/**
* APIProperty: prefix
* {String}
*/
prefix: '',
/**
* APIProperty: separator
* {String}
*/
separator: ', ',
/**
* APIProperty: suffix
* {String}
*/
suffix: '',
/**
* APIProperty: numDigits
* {Integer}
*/
numDigits: 5,
/**
* APIProperty: granularity
* {Integer}
*/
granularity: 10,
/**
* APIProperty: emptyString
* {String} Set this to some value to set when the mouse is outside the
* map.
*/
emptyString: null,
/** /**
* Property: lastXy * Property: lastXy
* {<OpenLayers.Pixel>} * {<OpenLayers.Pixel>}
*/ */
lastXy: null, lastXy: null,
/**
* APIProperty: displayProjection
* {<OpenLayers.Projection>} The projection in which the
* mouse position is displayed
*/
displayProjection: null,
/** /**
* Constructor: OpenLayers.Control.UTFGrid * Constructor: OpenLayers.Control.UTFGrid
*
* Parameters: * Parameters:
* options - {Object} Options for control. * options - {Object} Options for control.
*/ */
defaultHandlerOptions: {
'delay': 500,
'pixelTolerance': null,
'stopMove': false,
'single': true,
'double': false,
'pixelTolerance': 0,
'stopSingle': false,
'stopDouble': false
},
/** handlerMode: 'move',
* Method: destroy
*/
destroy: function() {
this.deactivate();
OpenLayers.Control.prototype.destroy.apply(this, arguments);
},
/** setHandler: function() {
* APIMethod: activate this.handler = null;
*/ if (this.handlerMode == 'hover') {
activate: function() { // Handle this event on hover
if (OpenLayers.Control.prototype.activate.apply(this, arguments)) { this.handler = new OpenLayers.Handler.Hover(
this.map.events.register('mousemove', this, this.redraw); this,
this.map.events.register('mouseout', this, this.reset); {'pause': this.handleEvent, 'move': this.reset},
//this.map.events.register('click', this, this.redraw); this.handlerOptions
this.redraw(); );
return true; } else if (this.handlerMode == 'click') {
} else { // Handle this event on click
return false; this.handler = new OpenLayers.Handler.Click(
this, {
'click': this.handleEvent
}, this.handlerOptions
);
} else if (this.handlerMode == 'move') {
this.handler = new OpenLayers.Handler.Hover(
this,
// Handle this event while hovering OR moving
{'pause': this.handleEvent, 'move': this.handleEvent},
this.handlerOptions
);
} }
}, },
/** initialize: function(options) {
* APIMethod: deactivate this.handlerOptions = OpenLayers.Util.extend(
*/ {}, this.defaultHandlerOptions
deactivate: function() { );
if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) { OpenLayers.Control.prototype.initialize.apply(
//this.map.events.unregister('click', this, this.redraw); this, arguments
this.map.events.unregister('mousemove', this, this.redraw); );
this.map.events.unregister('mouseout', this, this.reset); this.setHandler();
this.element.innerHTML = "";
return true;
} else {
return false;
}
}, },
/** /**
* Method: draw * Method: handleEvent
* {DOMElement}
*/ */
draw: function() { handleEvent: function(evt) {
OpenLayers.Control.prototype.draw.apply(this, arguments);
if (!this.element) {
this.div.left = "";
this.div.top = "";
this.element = this.div;
}
return this.div;
},
/**
* Method: redraw
*/
redraw: function(evt) {
var lonLat; var lonLat;
if (evt == null) { if (evt == null) {
@@ -163,7 +113,7 @@ OpenLayers.Control.UTFGrid = OpenLayers.Class(OpenLayers.Control, {
lonLat = this.map.getLonLatFromPixel(evt.xy); lonLat = this.map.getLonLatFromPixel(evt.xy);
if (!lonLat) { if (!lonLat) {
// map has not yet been properly initialized // map may not have been properly initialized
return; return;
} }
this.lastXy = evt.xy; this.lastXy = evt.xy;
@@ -174,33 +124,15 @@ OpenLayers.Control.UTFGrid = OpenLayers.Class(OpenLayers.Control, {
var layer; var layer;
for (var i=0, len=layers.length; i<len; i++) { for (var i=0, len=layers.length; i<len; i++) {
layer = layers[i]; layer = layers[i];
var info = layer.getTileInfo( lonLat ); var info = layer.getTileInfo( lonLat );
//MP TODO make function this.writeDebugInfo(info);
if (this.debugElement) {
var debug = "<ul>";
debug += "<li>i :" + info.i + "</li>";
debug += "<li>j :" + info.j + "</li>";
debug += "<li>globalrow :" + info.globalRow + "</li>";
debug += "<li>globalcol :" + info.globalCol + "</li>";
debug += "<li>gridrow :" + info.gridRow + "</li>";
debug += "<li>gridcol :" + info.gridCol + "</li>";
debug += "<li>gridrow offset :" + info.gridRowOffset + "</li>";
debug += "<li>gridcol offset :" + info.gridColOffset + "</li>";
debug += "</ul>";
this.debugElement.innerHTML = debug;
}
var tile = info.tile; var tile = info.tile;
/*
MP TODO Sanity checks
if ((Math.floor(info.i) >= tileSize) ||
(Math.floor(info.j) >= tileSize)) alert("TOO BIG");
*/
var attrs = null; var attrs = null;
var resolution = 4; var resolution = 4; //TODO autodetect?
if (tile !== null && typeof(tile) !== 'undefined') { if (tile !== null && typeof(tile) !== 'undefined') {
var data = tile.json var data = tile.json
if (data !== null) { if (data !== null) {
//val = data;
var code = this.resolveCode(data.grid[ var code = this.resolveCode(data.grid[
Math.floor((info.j) / resolution) Math.floor((info.j) / resolution)
].charCodeAt( ].charCodeAt(
@@ -214,6 +146,25 @@ OpenLayers.Control.UTFGrid = OpenLayers.Class(OpenLayers.Control, {
} }
}, },
/** Method:
*
*/
writeDebugInfo: function(info) {
var debug = "<ul>";
debug += "<li>i :" + info.i + "</li>";
debug += "<li>j :" + info.j + "</li>";
debug += "<li>globalrow :" + info.globalRow + "</li>";
debug += "<li>globalcol :" + info.globalCol + "</li>";
debug += "<li>gridrow :" + info.gridRow + "</li>";
debug += "<li>gridcol :" + info.gridCol + "</li>";
debug += "<li>gridrow offset :" + info.gridRowOffset + "</li>";
debug += "<li>gridcol offset :" + info.gridColOffset + "</li>";
debug += "</ul>";
if (this.debugElement) {
this.debugElement.innerHTML = debug;
}
},
/** /**
* Method: callback * Method: callback
* MP TODO * MP TODO
@@ -254,9 +205,9 @@ OpenLayers.Control.UTFGrid = OpenLayers.Class(OpenLayers.Control, {
* Method: reset * Method: reset
*/ */
reset: function(evt) { reset: function(evt) {
if (this.emptyString != null) { this.callback(null);
if (this.element)
this.element.innerHTML = this.emptyString; this.element.innerHTML = this.emptyString;
}
}, },
/** /**