Adding strategy and protocol base classes for better vector behavior. This just sets the common API for strategy and protocol. In general, a vector layer can have many strategies. A setLayer method gets called on each of these layers. When a layer is added to a map, any strategies on that layer get activated. When the layer is destroyed, any strategies get destroyed (this could be changed to deactivate for symmetry). A vector layer may also have a protocol. A protocol is typically constructed with a format. The layer doesn't need to know about the format. The protocol doesn't need to know about the layer. Strategies coordinate feature management for a layer. Specific strategies and protocols to follow. r=crschmidt (closes #1646)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@7650 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2008-07-31 21:27:35 +00:00
parent 5191dac459
commit 360d7412f0
7 changed files with 341 additions and 1 deletions

View File

@@ -180,6 +180,8 @@
"OpenLayers/Renderer/SVG.js", "OpenLayers/Renderer/SVG.js",
"OpenLayers/Renderer/VML.js", "OpenLayers/Renderer/VML.js",
"OpenLayers/Layer/Vector.js", "OpenLayers/Layer/Vector.js",
"OpenLayers/Strategy.js",
"OpenLayers/Protocol.js",
"OpenLayers/Layer/PointTrack.js", "OpenLayers/Layer/PointTrack.js",
"OpenLayers/Layer/GML.js", "OpenLayers/Layer/GML.js",
"OpenLayers/Style.js", "OpenLayers/Style.js",

View File

@@ -126,6 +126,18 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
*/ */
styleMap: null, styleMap: null,
/**
* Property: strategies
* {Array(<OpenLayers.Strategy>})} Optional list of strategies for the layer.
*/
strategies: null,
/**
* Property: protocol
* {<OpenLayers.Protocol>} Optional protocol for the layer.
*/
protocol: null,
/** /**
* Property: renderers * Property: renderers
* {Array(String)} List of supported Renderer classes. Add to this list to * {Array(String)} List of supported Renderer classes. Add to this list to
@@ -194,6 +206,14 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
this.features = []; this.features = [];
this.selectedFeatures = []; this.selectedFeatures = [];
// Allow for custom layer behavior
if(this.strategies){
for(var i=0, len=this.strategies.length; i<len; i++) {
this.strategies[i].setLayer(this);
}
}
}, },
/** /**
@@ -201,6 +221,16 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
* Destroy this layer * Destroy this layer
*/ */
destroy: function() { destroy: function() {
if (this.stategies) {
for(var i=0, len=this.strategies.length; i<len; i++) {
this.strategies[i].destroy();
}
this.strategies = null;
}
if (this.protocol) {
this.protocol.destroy();
this.protocol = null;
}
this.destroyFeatures(); this.destroyFeatures();
this.features = null; this.features = null;
this.selectedFeatures = null; this.selectedFeatures = null;
@@ -258,6 +288,11 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
this.renderer.map = this.map; this.renderer.map = this.map;
this.renderer.setSize(this.map.getSize()); this.renderer.setSize(this.map.getSize());
} }
if(this.strategies) {
for(var i=0, len=this.strategies.length; i<len; i++) {
this.strategies[i].activate();
}
}
}, },
/** /**

169
lib/OpenLayers/Protocol.js Executable file
View File

@@ -0,0 +1,169 @@
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* Class: OpenLayers.Protocol
* Abstract vector layer protocol class. Not to be instantiated directly. Use
* one of the protocol subclasses instead.
*/
OpenLayers.Protocol = OpenLayers.Class({
/**
* Property: format
* {<OpenLayers.Format>}
*/
format: null,
/**
* Property: options
* Any options sent to the constructor.
*/
options: null,
/**
* Constructor: OpenLayers.Protocol
* Abstract class for vector protocols. Create instances of a subclass.
*
* Parameters:
* options - {Object} Optional object whose properties will be set on the
* instance.
*/
initialize: function(options) {
OpenLayers.Util.extend(this, options);
this.options = options;
},
/**
* APIMethod: destroy
* Clean up the protocol.
*/
destroy: function() {
this.options = null;
this.format = null;
},
/**
* Method: read
* Construct a request for reading new features.
*
* Parameters:
* options - {Object} Optional object for configuring the request.
*/
read: function() {
},
/**
* Method: create
* Construct a request for writing newly created features.
*
* Parameters:
* features - {Array({<OpenLayers.Feature.Vector>})} or
* {<OpenLayers.Feature.Vector>}
* options - {Object} Optional object for configuring the request.
*/
create: function() {
},
/**
* Method: update
* Construct a request updating modified features.
*
* Parameters:
* features - {Array({<OpenLayers.Feature.Vector>})} or
* {<OpenLayers.Feature.Vector>}
* options - {Object} Optional object for configuring the request.
*/
update: function() {
},
/**
* Method: delete
* Construct a request deleting a removed feature.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>}
* options - {Object} Optional object for configuring the request.
*/
"delete": function() {
},
/**
* Method: commit
* Go over the features and for each take action
* based on the feature state. Possible actions are create,
* update and delete.
*
* Parameters:
* features - {Array({<OpenLayers.Feature.Vector>})}
* options - {Object} Map of options, the keys of the map are
* 'create', 'update', and 'delete'
*/
commit: function() {
},
CLASS_NAME: "OpenLayers.Protocol"
});
/**
* Class: OpenLayers.Protocol.Response
* Protocols return Response objects to their users.
*/
OpenLayers.Protocol.Response = new OpenLayers.Class({
/**
* Property: code
* {Integer} - OpenLayers.Protocol.Response.SUCCESS or
* OpenLayers.Protocol.Response.FAILURE
*/
code: null,
/**
* Property: features
* {Array({<OpenLayers.Feature.Vector>})} or {<OpenLayers.Feature.Vector>}
* The features returned in the response by the server.
*/
features: null,
/**
* Property: reqFeatures
* {Array({<OpenLayers.Feature.Vector>})} or {<OpenLayers.Feature.Vector>}
* The features provided by the user and placed in the request by the
* protocol.
*/
reqFeatures: null,
/**
* Property: priv
*/
priv: null,
/**
* Constructor: OpenLayers.Protocol.Response
*
* Parameters:
* options - {Object} Optional object whose properties will be set on the
* instance.
*/
initialize: function(options) {
OpenLayers.Util.extend(this, options);
},
/**
* Method: success
*
* Returns:
* {Boolean} - true on success, false otherwise
*/
success: function() {
return !!(this.code & OpenLayers.Protocol.Response.SUCCESS_MASK);
},
CLASS_NAME: "OpenLayers.Protocol.Response"
});
OpenLayers.Protocol.Response.SUCCESS_MASK = 0x000000ff;
OpenLayers.Protocol.Response.SUCCESS = 0x00000001;
OpenLayers.Protocol.Response.FAILURE_MASK = 0x0000ff00;
OpenLayers.Protocol.Response.FAILURE = 0x00000100;

72
lib/OpenLayers/Strategy.js Executable file
View File

@@ -0,0 +1,72 @@
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* Class: OpenLayers.Strategy
* Abstract vector layer strategy class. Not to be instantiated directly. Use
* one of the strategy subclasses instead.
*/
OpenLayers.Strategy = OpenLayers.Class({
/**
* Property: layer
* {<OpenLayers.Layer.Vector>}
*/
layer: null,
/**
* Property: options
* {Object} Any options sent to the constructor.
*/
options: null,
/**
* Constructor: OpenLayers.Strategy
* Abstract class for vector strategies. Create instances of a subclass.
*
* Parameters:
* options - {Object} Optional object whose properties will be set on the
* instance.
*/
initialize: function(options) {
OpenLayers.Util.extend(this, options);
this.options = options;
},
/**
* APIMethod: destroy
* Clean up the strategy.
*/
destroy: function() {
this.deactivate();
this.layer = null;
},
/**
* Method: setLayer.
*
* Parameters:
* {<OpenLayers.Layer.Vector>}
*/
setLayer: function(layer) {
this.layer = layer;
},
/**
* Method: activate
* Activate the strategy. Register any listeners, do appropriate setup.
*/
activate: function() {
},
/**
* Method: deactivate
* Deactivate the strategy. Unregister any listeners, do appropriate
* tear-down.
*/
deactivate: function() {
},
CLASS_NAME: "OpenLayers.Strategy"
});

30
tests/Protocol.html Normal file
View File

@@ -0,0 +1,30 @@
<html>
<head>
<script src="../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_initialize(t) {
t.plan(2);
var options = {};
var protocol = new OpenLayers.Protocol(options);
t.ok(protocol instanceof OpenLayers.Protocol,
"new OpenLayers.Protocol returns object" );
t.eq(protocol.options, options, "constructor sets this.options");
}
function test_destroy(t) {
t.plan(1);
var protocol = new OpenLayers.Protocol({
format: 'foo'
});
protocol.destroy();
t.eq(protocol.format, null, "destroy nullify protocol.format");
}
</script>
</head>
<body>
</body>
</html>

30
tests/Strategy.html Normal file
View File

@@ -0,0 +1,30 @@
<html>
<head>
<script src="../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_initialize(t) {
t.plan(2);
var options = {};
var strategy = new OpenLayers.Strategy(options);
t.ok(strategy instanceof OpenLayers.Strategy,
"new OpenLayers.Strategy returns object" );
t.eq(strategy.options, options, "constructor sets this.options");
}
function test_destroy(t) {
t.plan(1);
var strategy = new OpenLayers.Strategy({
layer: 'foo'
});
strategy.destroy();
t.eq(strategy.layer, null, "destroy nullify protocol.layer");
}
</script>
</head>
<body>
</body>
</html>

View File

@@ -110,6 +110,7 @@
<li>Popup/AnchoredBubble.html</li> <li>Popup/AnchoredBubble.html</li>
<li>Popup/FramedCloud.html</li> <li>Popup/FramedCloud.html</li>
<li>Projection.html</li> <li>Projection.html</li>
<li>Protocol.html</li>
<li>Renderer.html</li> <li>Renderer.html</li>
<li>Renderer/Elements.html</li> <li>Renderer/Elements.html</li>
<li>Renderer/SVG.html</li> <li>Renderer/SVG.html</li>
@@ -117,6 +118,7 @@
<li>Request.html</li> <li>Request.html</li>
<li>Request/XMLHttpRequest.html</li> <li>Request/XMLHttpRequest.html</li>
<li>Rule.html</li> <li>Rule.html</li>
<li>Strategy.html</li>
<li>Style.html</li> <li>Style.html</li>
<li>StyleMap.html</li> <li>StyleMap.html</li>
<li>Tile.html</li> <li>Tile.html</li>