Ticket #2482 (new feature)
activate and deactivate methods of Controls and Handlers cause boilerplate code
| Reported by: | dhurlburtusa | Owned by: | dhurlburtusa |
|---|---|---|---|
| Priority: | minor | Milestone: | 2.13 Release |
| Component: | Handler | Version: | 2.8 |
| Keywords: | Handler Control | Cc: | danny@… |
| State: | Needs More Work |
Description
The activate and deactivate methods of OpenLayers.Control and OpenLayers.Handler cause boilerplate code in their subclasses.
For instance, the implementation of the activate method in OpenLayers.Handler.Box is as follows:
/**
* Method: activate
*/
activate: function () {
if (OpenLayers.Handler.prototype.activate.apply(this, arguments)) {
this.dragHandler.activate();
return true;
} else {
return false;
}
},
The problem is that you have to call the superclass's activate method and return its boolean value. Now if OpenLayers.Handler added a template method called onActivate implemented as such:
/**
* Method: onActivate
* Method to be implemented by subclasses of OpenLayers.Handler to
* perform any actions necessary during activation.
*
* By default, this method does nothing.
*
* @TemplateMethod
*/
onActivate: function () {
},
and implemented OpenLayers.Handler.activate as follows:
/**
* APIMethod: activate
* Turn on the handler. Returns false if the handler was already active.
*
* Returns:
* {Boolean} The handler was activated.
*/
activate: function() {
if(this.active) {
return false;
}
// register for event handlers defined on this class.
var events = OpenLayers.Events.prototype.BROWSER_EVENTS;
for (var i=0, len=events.length; i<len; i++) {
if (this[events[i]]) {
this.register(events[i], this[events[i]]);
}
}
this.active = true;
this.onActivate(); // The only difference from the original activate method.
return true;
},
then the OpenLayers.Handler.Box would implement onActivate as follows:
/**
* Method: onActivate
*/
onActivate: function () {
OpenLayers.Handler.prototype.onActivate.apply(this, arguments);
this.dragHandler.activate();
},
instead of implementing activate along with the boilerplate code.
Similarly for deactivate, there would be an onDeactivate template method.
Similar changes can be done for OpenLayers.Controls.
Motivation:
1. Reduce the amount of boilerplate code and thereby reducing the size of OpenLayers.js which is currently about 850KB (minimized).
2. Easier to implement subclasses of OpenLayers.Control and OpenLayers.Handler.
3. Easier to read and maintain subclasses.
