that Andreas has been working so hard on. I think this is the single most awesome commit I've ever had the pleasure of committing. The results of this commit are described on http://trac.openlayers.org/wiki/Styles: essentially, this makes it possible to style features in all kinds of fun ways based on rules, and will also form the underlying basis for #533. Things this patch adds: * OL.Rule classes. These classes allow you to do tests against the propertie of a feature, and set a style based on these properties -- so you can compare the 'speedlimit' property of a line, and test if it is > 60, and if it is greater than 60, render it in a different color. You can also test combinations of rules using the OL.Rule.Logical class, and test featureids with the FeatureID class. * OL.Style class: The OL.Style class lets you wrap up Rules into styles that can be used with drawFeature to draw the feature in the selected style. * OL.Layer.Vector.drawFeature will check if the given style is an OL.Style object, and if so, it will draw the feature accordingly. examples/georss-flickr.html shows usage of these classes. Many, many thanks go to Andreas for all his hard work on this: this code really is very pretty, and includes unit tests for all the classes (and we know that I am a big fan of tests.) Three cheers for Andreas: Hip hip, hooray! hip hip, hooray! hip hip, hooray! git-svn-id: http://svn.openlayers.org/trunk/openlayers@5429 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
102 lines
2.8 KiB
JavaScript
102 lines
2.8 KiB
JavaScript
/* Copyright (c) 2006-2007 MetaCarta, Inc., published under the Clear BSD
|
|
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
|
|
* full text of the license. */
|
|
|
|
|
|
/**
|
|
* @requires: OpenLayers/Rule.js
|
|
*
|
|
* Class: OpenLayers.Rule.Logical
|
|
*
|
|
* This class represents ogc:And, ogc:Or and ogc:Not rules.
|
|
*
|
|
* Inherits from
|
|
* - <OpenLayers.Rule>
|
|
*/
|
|
OpenLayers.Rule.Logical = OpenLayers.Class(OpenLayers.Rule, {
|
|
|
|
/**
|
|
* APIProperty: children
|
|
* {Array(<OpenLayers.Rule>)} child rules for this rule
|
|
*/
|
|
children: null,
|
|
|
|
/**
|
|
* APIProperty: type
|
|
* {String} type of logical operator. Available types are:
|
|
* - OpenLayers.Rule.Locical.AND = "&&";
|
|
* - OpenLayers.Rule.Logical.OR = "||";
|
|
* - OpenLayers.Rule.Logical.NOT = "!";
|
|
*/
|
|
type: null,
|
|
|
|
/**
|
|
* Constructor: OpenLayers.Rule.Logical
|
|
* Creates a logical rule (And, Or, Not).
|
|
*
|
|
* Parameters:
|
|
* options - {Object} An optional object with properties to set on the
|
|
* rule
|
|
*
|
|
* Returns:
|
|
* {<OpenLayers.Rule>}
|
|
*/
|
|
initialize: function(options) {
|
|
this.children = [];
|
|
OpenLayers.Rule.prototype.initialize.apply(
|
|
this, [options]);
|
|
},
|
|
|
|
/**
|
|
* APIMethod: destroy
|
|
* nullify references to prevent circular references and memory leaks
|
|
*/
|
|
destroy: function() {
|
|
for (var i=0; i<this.children.length; i++) {
|
|
this.children[i].destroy();
|
|
}
|
|
this.children = null;
|
|
OpenLayers.Rule.prototype.destroy.apply(this, arguments);
|
|
},
|
|
|
|
/**
|
|
* APIMethod: evaluate
|
|
* evaluates this rule for a specific feature
|
|
*
|
|
* Parameters:
|
|
* feature - {<OpenLayers.Feature>} feature to apply the rule to.
|
|
*
|
|
* Returns:
|
|
* {boolean} true if the rule applies, false if it does not
|
|
*/
|
|
evaluate: function(feature) {
|
|
switch(this.type) {
|
|
case OpenLayers.Rule.Logical.AND:
|
|
for (var i=0; i<this.children.length; i++) {
|
|
if (this.children[i].evaluate(feature) == false) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
|
|
case OpenLayers.Rule.Logical.OR:
|
|
for (var i=0; i<this.children.length; i++) {
|
|
if (this.children[i].evaluate(feature) == true) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
|
|
case OpenLayers.Rule.Logical.NOT:
|
|
return (!this.children[0].evaluate(feature));
|
|
}
|
|
},
|
|
|
|
CLASS_NAME: "OpenLayers.Rule.Logical"
|
|
});
|
|
|
|
|
|
OpenLayers.Rule.Logical.AND = "&&";
|
|
OpenLayers.Rule.Logical.OR = "||";
|
|
OpenLayers.Rule.Logical.NOT = "!";
|