Ticket #1071: selectfeature.patch
| File selectfeature.patch, 7.8 kB (added by crschmidt, 1 year ago) |
|---|
-
tests/Control/test_SelectFeature.html
old new 23 23 var layer = new OpenLayers.Layer.Vector(); 24 24 map.addLayer(layer); 25 25 var control = new OpenLayers.Control.SelectFeature(layer); 26 control. handler.destroy = function() {26 control.featureHandler.destroy = function() { 27 27 t.ok(true, 28 28 "control.destroy calls destroy on feature handler"); 29 29 } … … 47 47 t.eq(feature.style.strokeColor, OpenLayers.Feature.Vector.style['default'].strokeColor, "feature style is set back to old style"); 48 48 } 49 49 50 function test_Control_SelectFeature_multiple(t) { 51 t.plan(5); 52 var map = new OpenLayers.Map("map"); 53 var layer = new OpenLayers.Layer.Vector(); 54 map.addLayer(layer); 55 var control = new OpenLayers.Control.SelectFeature(layer, {'multiple': true, inclusiveBox: false }); 56 var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0)); 57 var feature2 = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(1,1)); 58 layer.addFeatures([feature,feature2]); 59 layer.drawFeature = function() { } 60 control.setMap(map); 61 control.clickFeature(feature); 62 t.eq(feature.style.strokeColor, OpenLayers.Feature.Vector.style['select'].strokeColor, "feature style is set to select style"); 63 control.clickFeature(feature); 64 t.eq(feature.style.strokeColor, OpenLayers.Feature.Vector.style['default'].strokeColor, "feature style is set back to old style"); 65 map.getLonLatFromPixel = function(arg) { 66 return new OpenLayers.LonLat(arg.x, arg.y); 67 } 68 control.selectBox(new OpenLayers.Bounds(-1, -1, 2, 2)); 69 t.eq(layer.selectedFeatures.length, 2, "box around all features selects all features.") 70 control.selectBox(new OpenLayers.Bounds(-1, -1, .5, .5)); 71 t.eq(layer.selectedFeatures.length, 1, "box around one feature turns that feature off with inclusiveBox false") 72 control.inclusiveBox = true; 73 control.selectBox(new OpenLayers.Bounds(-1, -1, 2, 2)); 74 t.eq(layer.selectedFeatures.length, 2, "inclusiveBox lets all features turn on even when one is on already.") 75 76 } 77 50 78 function test_Control_SelectFeature_activate(t) { 51 79 t.plan(2); 52 80 var map = new OpenLayers.Map("map"); … … 54 82 map.addLayer(layer); 55 83 var control = new OpenLayers.Control.SelectFeature(layer); 56 84 map.addControl(control); 57 t.ok(!control. handler.active,85 t.ok(!control.featureHandler.active, 58 86 "feature handler is not active prior to activating control"); 59 87 control.activate(); 60 t.ok(control. handler.active,88 t.ok(control.featureHandler.active, 61 89 "feature handler is active after activating control"); 62 90 } 63 91 … … 70 98 map.addControl(control); 71 99 72 100 control.activate(); 73 control. handler.deactivate = function() {101 control.featureHandler.deactivate = function() { 74 102 t.ok(true, 75 103 "control.deactivate calls deactivate on feature handler"); 76 104 } -
lib/OpenLayers/Control/SelectFeature.js
old new 20 20 * {Boolean} Allow selection of multiple geometries 21 21 */ 22 22 multiple: false, 23 24 /** 25 * APIProperty: inclusiveBox 26 * {Boolean} If this is true, then when a box is drawn (in multiple feature 27 * selection mode), all intersected features will be selected, even if they 28 * were already selected. Otherwise, the box will switch the state of any 29 * features. Default is true. 30 */ 31 inclusiveBox: true, 23 32 33 24 34 /** 25 35 * APIProperty: hover 26 36 * {Boolean} Select on mouse over and deselect on mouse out. If true, this … … 89 99 out: this.outFeature 90 100 }, this.callbacks); 91 101 var handlerOptions = {geometryTypes: this.geometryTypes}; 92 this. handler = new OpenLayers.Handler.Feature(this, layer,102 this.featureHandler = new OpenLayers.Handler.Feature(this, layer, 93 103 this.callbacks, 94 104 handlerOptions); 105 if (this.multiple) { 106 this.boxHandler = new OpenLayers.Handler.Box( this, 107 {done: this.selectBox}, { 108 keyMask: OpenLayers.Handler.MOD_SHIFT, 109 boxDivClassName: "olControlSelectFeatureSelectBox" 110 } ); 111 } 95 112 }, 113 114 /** 115 * Method: activate 116 */ 117 activate: function() { 118 this.featureHandler.activate(); 119 if (this.boxHandler) { 120 this.boxHandler.activate(); 121 } 122 return OpenLayers.Control.prototype.activate.apply(this,arguments); 123 }, 96 124 97 125 /** 126 * Method: deactivate 127 */ 128 deactivate: function() { 129 if (this.boxHandler) { 130 this.boxHandler.deactivate(); 131 } 132 this.featureHandler.deactivate(); 133 return OpenLayers.Control.prototype.deactivate.apply(this,arguments); 134 }, 135 136 /** 137 * Method: destroy 138 */ 139 destroy: function() { 140 this.featureHandler.destroy(); 141 if (this.boxHandler) { 142 this.boxHandler.destroy(); 143 } 144 return OpenLayers.Control.prototype.destroy.apply(this,arguments); 145 }, 146 147 /** 148 * Method: selectBox 149 * Callback from the boxHandler set up when multiple feature selection is on. 150 * depending on the inclusiveBox setting, will either select all features which 151 * intersect the given bounds, or toggle the feature selection status. 152 * If passed a bounds (shift-click) has no effect. 153 * 154 * Parameters: 155 * feature - {<OpenLayers.Bounds> || <OpenLayers.LonLat> } 156 */ 157 selectBox: function (position) { 158 if (position instanceof OpenLayers.Bounds) { 159 var minXY = this.map.getLonLatFromPixel( 160 new OpenLayers.Pixel(position.left, position.bottom)); 161 var maxXY = this.map.getLonLatFromPixel( 162 new OpenLayers.Pixel(position.right, position.top)); 163 var bounds = new OpenLayers.Bounds(minXY.lon, minXY.lat, 164 maxXY.lon, maxXY.lat); 165 for(var i = 0; i < this.layer.features.length; i++) { 166 var feature = this.layer.features[i]; 167 if (bounds.intersectsBounds(feature.geometry.getBounds())) { 168 if (this.inclusiveBox) { 169 if(OpenLayers.Util.indexOf(this.layer.selectedFeatures, feature) == -1) { 170 this.select(feature); 171 } 172 } else { 173 this.clickFeature(feature) 174 } 175 } 176 } 177 } 178 }, 179 180 /** 98 181 * Method: clickFeature 99 182 * Called when the feature handler detects a click on a feature 100 183 * … … 202 285 * map - {<OpenLayers.Map>} 203 286 */ 204 287 setMap: function(map) { 205 this.handler.setMap(map); 288 this.featureHandler.setMap(map); 289 if (this.multiple) { 290 this.boxHandler.setMap(map); 291 } 206 292 OpenLayers.Control.prototype.setMap.apply(this, arguments); 207 293 }, 208 294
