[76] | 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
| 4 | * full text of the license. */ |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * @requires OpenLayers/Strategy.js |
---|
| 8 | * @requires OpenLayers/Filter.js |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | /** |
---|
| 12 | * Class: OpenLayers.Strategy.Filter |
---|
| 13 | * Strategy for limiting features that get added to a layer by |
---|
| 14 | * evaluating a filter. The strategy maintains a cache of |
---|
| 15 | * all features until removeFeatures is called on the layer. |
---|
| 16 | * |
---|
| 17 | * Inherits from: |
---|
| 18 | * - <OpenLayers.Strategy> |
---|
| 19 | */ |
---|
| 20 | OpenLayers.Strategy.Filter = OpenLayers.Class(OpenLayers.Strategy, { |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * APIProperty: filter |
---|
| 24 | * {<OpenLayers.Filter>} Filter for limiting features sent to the layer. |
---|
| 25 | * Use the <setFilter> method to update this filter after construction. |
---|
| 26 | */ |
---|
| 27 | filter: null, |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * Property: cache |
---|
| 31 | * {Array(<OpenLayers.Feature.Vector>)} List of currently cached |
---|
| 32 | * features. |
---|
| 33 | */ |
---|
| 34 | cache: null, |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * Property: caching |
---|
| 38 | * {Boolean} The filter is currently caching features. |
---|
| 39 | */ |
---|
| 40 | caching: false, |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * Constructor: OpenLayers.Strategy.Filter |
---|
| 44 | * Create a new filter strategy. |
---|
| 45 | * |
---|
| 46 | * Parameters: |
---|
| 47 | * options - {Object} Optional object whose properties will be set on the |
---|
| 48 | * instance. Strategy must be constructed with at least a <filter> |
---|
| 49 | * property. |
---|
| 50 | */ |
---|
| 51 | initialize: function(options) { |
---|
| 52 | OpenLayers.Strategy.prototype.initialize.apply(this, [options]); |
---|
| 53 | if (!this.filter || !(this.filter instanceof OpenLayers.Filter)) { |
---|
| 54 | throw new Error("Filter strategy must be constructed with a filter"); |
---|
| 55 | } |
---|
| 56 | }, |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * APIMethod: activate |
---|
| 60 | * Activate the strategy. Register any listeners, do appropriate setup. |
---|
| 61 | * By default, this strategy automatically activates itself when a layer |
---|
| 62 | * is added to a map. |
---|
| 63 | * |
---|
| 64 | * Returns: |
---|
| 65 | * {Boolean} True if the strategy was successfully activated or false if |
---|
| 66 | * the strategy was already active. |
---|
| 67 | */ |
---|
| 68 | activate: function() { |
---|
| 69 | var activated = OpenLayers.Strategy.prototype.activate.apply(this, arguments); |
---|
| 70 | if (activated) { |
---|
| 71 | this.cache = []; |
---|
| 72 | this.layer.events.on({ |
---|
| 73 | "beforefeaturesadded": this.handleAdd, |
---|
| 74 | "beforefeaturesremoved": this.handleRemove, |
---|
| 75 | scope: this |
---|
| 76 | }); |
---|
| 77 | } |
---|
| 78 | return activated; |
---|
| 79 | }, |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * APIMethod: deactivate |
---|
| 83 | * Deactivate the strategy. Clear the feature cache. |
---|
| 84 | * |
---|
| 85 | * Returns: |
---|
| 86 | * {Boolean} True if the strategy was successfully deactivated or false if |
---|
| 87 | * the strategy was already inactive. |
---|
| 88 | */ |
---|
| 89 | deactivate: function() { |
---|
| 90 | this.cache = null; |
---|
| 91 | if (this.layer && this.layer.events) { |
---|
| 92 | this.layer.events.un({ |
---|
| 93 | "beforefeaturesadded": this.handleAdd, |
---|
| 94 | "beforefeaturesremoved": this.handleRemove, |
---|
| 95 | scope: this |
---|
| 96 | }); |
---|
| 97 | } |
---|
| 98 | return OpenLayers.Strategy.prototype.deactivate.apply(this, arguments); |
---|
| 99 | }, |
---|
| 100 | |
---|
| 101 | /** |
---|
| 102 | * Method: handleAdd |
---|
| 103 | */ |
---|
| 104 | handleAdd: function(event) { |
---|
| 105 | if (!this.caching) { |
---|
| 106 | var features = event.features; |
---|
| 107 | event.features = []; |
---|
| 108 | var feature; |
---|
| 109 | for (var i=0, ii=features.length; i<ii; ++i) { |
---|
| 110 | feature = features[i]; |
---|
| 111 | if (this.filter.evaluate(feature)) { |
---|
| 112 | event.features.push(feature); |
---|
| 113 | } else { |
---|
| 114 | this.cache.push(feature); |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | }, |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * Method: handleRemove |
---|
| 122 | */ |
---|
| 123 | handleRemove: function(event) { |
---|
| 124 | if (!this.caching) { |
---|
| 125 | this.cache = []; |
---|
| 126 | } |
---|
| 127 | }, |
---|
| 128 | |
---|
| 129 | /** |
---|
| 130 | * APIMethod: setFilter |
---|
| 131 | * Update the filter for this strategy. This will re-evaluate |
---|
| 132 | * any features on the layer and in the cache. Only features |
---|
| 133 | * for which filter.evalute(feature) returns true will be |
---|
| 134 | * added to the layer. Others will be cached by the strategy. |
---|
| 135 | * |
---|
| 136 | * Parameters: |
---|
| 137 | * filter - <OpenLayers.Filter> A filter for evaluating features. |
---|
| 138 | */ |
---|
| 139 | setFilter: function(filter) { |
---|
| 140 | this.filter = filter; |
---|
| 141 | var previousCache = this.cache; |
---|
| 142 | this.cache = []; |
---|
| 143 | // look through layer for features to remove from layer |
---|
| 144 | this.handleAdd({features: this.layer.features}); |
---|
| 145 | // cache now contains features to remove from layer |
---|
| 146 | if (this.cache.length > 0) { |
---|
| 147 | this.caching = true; |
---|
| 148 | this.layer.removeFeatures(this.cache.slice(), {silent: true}); |
---|
| 149 | this.caching = false; |
---|
| 150 | } |
---|
| 151 | // now look through previous cache for features to add to layer |
---|
| 152 | if (previousCache.length > 0) { |
---|
| 153 | var event = {features: previousCache}; |
---|
| 154 | this.handleAdd(event); |
---|
| 155 | // event has features to add to layer |
---|
| 156 | this.caching = true; |
---|
| 157 | this.layer.addFeatures(event.features, {silent: true}); |
---|
| 158 | this.caching = false; |
---|
| 159 | } |
---|
| 160 | }, |
---|
| 161 | |
---|
| 162 | CLASS_NAME: "OpenLayers.Strategy.Filter" |
---|
| 163 | |
---|
| 164 | }); |
---|