Ticket #328: layerclone.patch
| File layerclone.patch, 10.5 kB (added by euzuro, 2 years ago) |
|---|
-
tests/test_Layer.html
old new 33 33 } 34 34 35 35 function test_02_Layer_clone (t) { 36 t.plan( 6);36 t.plan( 8 ); 37 37 38 var map = new OpenLayers.Map('map');39 var options = { chicken: 151, foo: "bar" };38 var mapone = new OpenLayers.Map('map'); 39 var options = { chicken: 151, foo: "bar", maxResolution: "auto" }; 40 40 var layer = new OpenLayers.Layer('Test Layer', options); 41 map .addLayer(layer);41 mapone.addLayer(layer); 42 42 43 43 // randomly assigned property 44 44 layer.chocolate = 5; 45 45 46 46 var clone = layer.clone(); 47 47 48 t.ok( clone.map == null, "cloned layer has map property set to null") 49 50 var maptwo = new OpenLayers.Map('map2'); 51 maptwo.addLayer(clone); 52 48 53 t.ok( clone instanceof OpenLayers.Layer, "new OpenLayers.Layer returns object" ); 49 54 t.eq( clone.name, "Test Layer", "default clone.name is correct" ); 50 55 t.ok( ((clone.options["chicken"] == 151) && (clone.options["foo"] == "bar")), "clone.options correctly set" ); … … 53 58 layer.addOptions({chicken:152}); 54 59 t.eq(clone.options["chicken"], 151, "made a clean copy of options"); 55 60 61 t.ok( (layer.maxResolution != clone.maxResolution), "maxresolution of clone reset to new map div"); 62 t.ok( (layer.minResolution != clone.minResolution), "minresolution of clone reset to new map div"); 56 63 57 t.ok( clone.map == null, "cloned layer has map property set to null")58 59 64 } 60 65 61 66 function test_03_Layer_setName (t) { … … 174 179 </script> 175 180 </head> 176 181 <body> 177 <div id="map"></div> 182 <div id="map" style="width:500px;height:500px"></div> 183 <div id="map2" style="width:100px;height:100px"></div> 178 184 </body> 179 185 </html> -
lib/OpenLayers/Layer.js
old new 206 206 setMap: function(map) { 207 207 this.map = map; 208 208 209 var properties = new Array(210 'projection', 'units',211 'scales', 'resolutions',212 'maxScale', 'minScale',213 'maxResolution', 'minResolution',214 'minExtent', 'maxExtent',215 'numZoomLevels'216 );217 if (this.map.maxZoomLevel && !this.numZoomLevels) {218 this.numZoomLevels = this.map.maxZoomLevel + 1;219 }220 for(var i=0; i < properties.length; i++) {221 if (this[properties[i]] == null) {222 this[properties[i]] = this.map[properties[i]];223 }224 }225 209 this.initResolutions(); 226 210 227 211 this.inRange = this.calculateInRange(); … … 322 306 * @private 323 307 */ 324 308 initResolutions: function() { 325 326 if ((this.scales != null) || (this.resolutions != null)) { 309 310 // These are the relevant options which are used for calculating 311 // resolutions information. 312 // 313 var props = new Array( 314 'projection', 'units', 315 'scales', 'resolutions', 316 'maxScale', 'minScale', 317 'maxResolution', 'minResolution', 318 'minExtent', 'maxExtent', 319 'numZoomLevels', 'maxZoomLevel' 320 ); 321 322 // First we create a new object where we will store all of the 323 // resolution-related properties that we find in either the layer's 324 // 'options' array or from the map. 325 // 326 var confProps = new Object(); 327 for(var i=0; i < props.length; i++) { 328 var property = props[i]; 329 confProps[property] = this.options[property] || this.map[property]; 330 } 331 332 // If numZoomLevels hasn't been set and the maxZoomLevel *has*, 333 // then use maxZoomLevel to calculate numZoomLevels 334 // 335 if ( (!confProps.numZoomLevels) && (confProps.maxZoomLevel) ) { 336 confProps.numZoomLevels = confProps.maxZoomLevel + 1; 337 } 338 339 // First off, we take whatever hodge-podge of values we have and 340 // calculate/distill them down into a resolutions[] array 341 // 342 if ((confProps.scales != null) || (confProps.resolutions != null)) { 327 343 //preset levels 328 if (this.scales != null) { 329 this.resolutions = new Array(); 330 for(var i = 0; i < this.scales.length; i++) { 331 this.resolutions[i] = 332 OpenLayers.Util.getResolutionFromScale(this.scales[i], 333 this.units); 344 if (confProps.scales != null) { 345 confProps.resolutions = new Array(); 346 for(var i = 0; i < confProps.scales.length; i++) { 347 var scale = confProps.scales[i]; 348 confProps.resolutions[i] = 349 OpenLayers.Util.getResolutionFromScale(scale, 350 confProps.units); 334 351 } 335 352 } 336 this.numZoomLevels = this.resolutions.length;353 confProps.numZoomLevels = confProps.resolutions.length; 337 354 338 355 } else { 339 //maxResolution and numZoomLevels 356 //maxResolution and numZoomLevels based calculation 340 357 341 this.resolutions = new Array();358 confProps.resolutions = new Array(); 342 359 343 360 // determine maxResolution 344 if ( this.minScale) {345 this.maxResolution =346 OpenLayers.Util.getResolutionFromScale( this.minScale,347 this.units);348 } else if ( this.maxResolution == "auto") {361 if (confProps.minScale) { 362 confProps.maxResolution = 363 OpenLayers.Util.getResolutionFromScale(confProps.minScale, 364 confProps.units); 365 } else if (confProps.maxResolution == "auto") { 349 366 var viewSize = this.map.getSize(); 350 var wRes = this.maxExtent.getWidth() / viewSize.w;351 var hRes = this.maxExtent.getHeight()/ viewSize.h;352 this.maxResolution = Math.max(wRes, hRes);367 var wRes = confProps.maxExtent.getWidth() / viewSize.w; 368 var hRes = confProps.maxExtent.getHeight()/ viewSize.h; 369 confProps.maxResolution = Math.max(wRes, hRes); 353 370 } 354 371 355 372 // determine minResolution 356 if ( this.maxScale != null) {357 this.minResolution =358 OpenLayers.Util.getResolutionFromScale( this.maxScale);359 } else if ( (this.minResolution == "auto") &&360 (this.minExtent != null)){373 if (confProps.maxScale != null) { 374 confProps.minResolution = 375 OpenLayers.Util.getResolutionFromScale(confProps.maxScale); 376 } else if ( (confProps.minResolution == "auto") && 377 (confProps.minExtent != null) ) { 361 378 var viewSize = this.map.getSize(); 362 var wRes = this.minExtent.getWidth() / viewSize.w;363 var hRes = this.minExtent.getHeight()/ viewSize.h;364 this.minResolution = Math.max(wRes, hRes);379 var wRes = confProps.minExtent.getWidth() / viewSize.w; 380 var hRes = confProps.minExtent.getHeight()/ viewSize.h; 381 confProps.minResolution = Math.max(wRes, hRes); 365 382 } 366 383 367 384 // determine numZoomLevels 368 if ( this.minResolution != null) {369 var ratio = this.maxResolution / this.minResolution;370 this.numZoomLevels =385 if (confProps.minResolution != null) { 386 var ratio = confProps.maxResolution / confProps.minResolution; 387 confProps.numZoomLevels = 371 388 Math.floor(Math.log(ratio) / Math.log(2)) + 1; 372 389 } 373 390 374 391 // now we have numZoomLevels and maxResolution, 375 392 // we can populate the resolutions array 376 for (var i=0; i < this.numZoomLevels; i++) { 377 this.resolutions.push(this.maxResolution / Math.pow(2, i)); 393 for (var i=0; i < confProps.numZoomLevels; i++) { 394 var res = confProps.maxResolution / Math.pow(2, i) 395 confProps.resolutions.push(res); 378 396 } 379 397 } 398 399 //sort resolutions array ascendingly 400 // 401 confProps.resolutions.sort( function(a, b) { return(b-a); } ); 380 402 381 this.resolutions.sort( function(a,b) { 382 return(b-a); 383 }); 403 // now set our newly calculated values back to the layer 404 // Note: We specifically do *not* set them to layer.options, which we 405 // will preserve as it was when we added this layer to the map. 406 // this way cloned layers reset themselves to new map div 407 // dimensions) 408 // 409 this.projection = confProps.projection; 410 this.units = confProps.units; 384 411 385 this.minResolution = this.resolutions[this.resolutions.length - 1]; 386 this.maxResolution = this.resolutions[0]; 412 this.resolutions = confProps.resolutions; 413 this.maxResolution = confProps.resolutions[0]; 414 var lastIndex = confProps.resolutions.length - 1; 415 this.minResolution = confProps.resolutions[lastIndex]; 387 416 388 this.minScale = 389 OpenLayers.Util.getScaleFromResolution(this.maxResolution, 390 this.units); 391 this.maxScale = 392 OpenLayers.Util.getScaleFromResolution(this.minResolution, 393 this.units); 417 this.scales = new Array(); 418 for(var i = 0; i < confProps.resolutions.length; i++) { 419 this.scales[i] = 420 OpenLayers.Util.getScaleFromResolution(confProps.resolutions[i], 421 confProps.units); 422 } 423 this.minScale = this.scales[0]; 424 this.maxScale = this.scales[this.scales.length - 1]; 425 426 this.minExtent = confProps.minExtent; 427 this.maxExtent = confProps.maxExtent; 428 429 this.numZoomLevels = confProps.numZoomLevels; 394 430 }, 395 431 396 432 /**
