Skip to content

Commit 6edb290

Browse files
committed
Merge bugfixes and test improvements for rc4 release.
git-svn-id: http://svn.openlayers.org/branches/openlayers/2.1@1513 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
1 parent c7c21ea commit 6edb290

10 files changed

+163
-12
lines changed

lib/OpenLayers/BaseTypes.js

+24
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,30 @@ OpenLayers.Bounds.prototype = {
424424
this.right + x, this.top + y);
425425
},
426426

427+
/**
428+
* @param {OpenLayers.LonLat} ll
429+
* @param {Boolean} inclusive Whether or not to include the border.
430+
* Default is true
431+
*
432+
* @return Whether or not the passed-in lonlat is within this bounds
433+
* @type Boolean
434+
*/
435+
containsLonLat:function(ll, inclusive) {
436+
return this.contains(ll.lon, ll.lat, inclusive);
437+
},
438+
439+
/**
440+
* @param {OpenLayers.Pixel} px
441+
* @param {Boolean} inclusive Whether or not to include the border.
442+
* Default is true
443+
*
444+
* @return Whether or not the passed-in pixel is within this bounds
445+
* @type Boolean
446+
*/
447+
containsPixel:function(px, inclusive) {
448+
return this.contains(px.x, px.y, inclusive);
449+
},
450+
427451
/**
428452
* @param {float} x
429453
* @param {float} y

lib/OpenLayers/Feature.js

+40-2
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,34 @@ OpenLayers.Feature.prototype= {
6767
this.marker = null;
6868
}
6969
if (this.popup != null) {
70-
this.popup.destroy();
70+
this.destroyPopup(this.popup);
7171
this.popup = null;
7272
}
7373
},
7474

75+
/**
76+
* @returns Whether or not the feature is currently visible on screen
77+
* (based on its 'lonlat' property)
78+
* @type Boolean
79+
*/
80+
onScreen:function() {
81+
82+
var onScreen = false;
83+
if ((this.layer != null) && (this.layer.map != null)) {
84+
var screenBounds = this.layer.map.getExtent();
85+
onScreen = screenBounds.containsLonLat(this.lonlat);
86+
}
87+
return onScreen;
88+
},
89+
7590

7691
/**
7792
* @returns A Marker Object created from the 'lonlat' and 'icon' properties
7893
* set in this.data. If no 'lonlat' is set, returns null. If no
79-
* 'icon' is set, OpenLayers.Marker() will load the default image
94+
* 'icon' is set, OpenLayers.Marker() will load the default image.
95+
*
96+
* Note: this.marker is set to return value
97+
*
8098
* @type OpenLayers.Marker
8199
*/
82100
createMarker: function() {
@@ -89,12 +107,24 @@ OpenLayers.Feature.prototype= {
89107
return this.marker;
90108
},
91109

110+
/** If user overrides the createMarker() function, s/he should be able
111+
* to also specify an alternative function for destroying it
112+
*/
92113
destroyMarker: function() {
93114
this.marker.destroy();
94115
},
95116

96117
/**
118+
* @returns A Popup Object created from the 'lonlat', 'popupSize',
119+
* and 'popupContentHTML' properties set in this.data. It uses
120+
* this.marker.icon as default anchor.
121+
*
122+
* If no 'lonlat' is set, returns null.
123+
* If no this.marker has been created, no anchor is sent.
124+
*
125+
* Note: this.popup is set to return value
97126
*
127+
* @type OpenLayers.Popup.AnchoredBubble
98128
*/
99129
createPopup: function() {
100130

@@ -112,5 +142,13 @@ OpenLayers.Feature.prototype= {
112142
return this.popup;
113143
},
114144

145+
146+
/** As with the marker, if user overrides the createPopup() function, s/he
147+
* should also be able to override the destruction
148+
*/
149+
destroyPopup: function() {
150+
this.popup.destroy()
151+
},
152+
115153
CLASS_NAME: "OpenLayers.Feature"
116154
};

lib/OpenLayers/Layer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ OpenLayers.Layer.prototype = {
393393
*/
394394
getZoomForResolution: function(resolution) {
395395

396-
for(var i=1; i <= this.resolutions.length; i++) {
396+
for(var i=1; i < this.resolutions.length; i++) {
397397
if ( this.resolutions[i] < resolution) {
398398
break;
399399
}

lib/OpenLayers/Map.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,7 @@ OpenLayers.Map.prototype = {
354354
if (newBaseLayer != this.baseLayer) {
355355

356356
// is newBaseLayer an already loaded layer?
357-
var foundLayer = (this.layers.indexOf(newBaseLayer) != -1);
358-
if (foundLayer) {
357+
if (this.layers.indexOf(newBaseLayer) != -1) {
359358

360359
// make the old base layer invisible
361360
if (this.baseLayer != null) {
@@ -455,10 +454,20 @@ OpenLayers.Map.prototype = {
455454
/********************************************************/
456455

457456
/**
458-
* @returns {OpenLayers.Size}
457+
* @returns An OpenLayers.Size object that represents the size, in pixels,
458+
* of the div into which OpenLayers has been loaded.
459+
*
460+
* Note: A clone() of this locally cached variable is returned, so
461+
* as not to allow users to modify it.
462+
*
463+
* @type OpenLayers.Size
459464
*/
460465
getSize: function () {
461-
return this.size;
466+
var size = null;
467+
if (this.size != null) {
468+
size = this.size.clone();
469+
}
470+
return size;
462471
},
463472

464473
/**
@@ -669,7 +678,7 @@ OpenLayers.Map.prototype = {
669678
var valid = false;
670679
if (lonlat != null) {
671680
var maxExtent = this.getMaxExtent();
672-
valid = maxExtent.contains(lonlat.lon, lonlat.lat);
681+
valid = maxExtent.containsLonLat(lonlat);
673682
}
674683
return valid;
675684
},

lib/OpenLayers/Marker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ OpenLayers.Marker.prototype = {
8282
var onScreen = false;
8383
if (this.map) {
8484
var screenBounds = this.map.getExtent();
85-
onScreen = screenBounds.contains(this.lonlat.lon, this.lonlat.lat);
85+
onScreen = screenBounds.containsLonLat(this.lonlat);
8686
}
8787
return onScreen;
8888
},

tests/test_Bounds.html

+8-1
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,19 @@
5353
}
5454

5555
function test_04_Bounds_contains(t) {
56-
t.plan( 4 );
56+
t.plan( 6 );
5757
bounds = new OpenLayers.Bounds(10,10,40,40);
5858
t.eq( bounds.contains(20,20), true, "bounds(10,10,40,40) correctly contains LonLat(20,20)" );
5959
t.eq( bounds.contains(0,0), false, "bounds(10,10,40,40) correctly does not contain LonLat(0,0)" );
6060
t.eq( bounds.contains(40,40), true, "bounds(10,10,40,40) correctly contains LonLat(40,40) with inclusive set to true" );
6161
t.eq( bounds.contains(40,40, false), false, "bounds(10,10,40,40) correctly does not contain LonLat(40,40) with inclusive set to false" );
62+
63+
var px = new OpenLayers.Pixel(15,30);
64+
t.eq( bounds.containsPixel(px), bounds.contains(px.x, px.y), "containsPixel works");
65+
66+
var ll = new OpenLayers.LonLat(15,30);
67+
t.eq( bounds.containsLonLat(ll), bounds.contains(ll.lon, ll.lat), "containsLonLat works");
68+
6269
}
6370

6471
function test_05_Bounds_fromString(t) {

tests/test_Feature.html

+26
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,32 @@
7676
"Layer div img contains correct url" );
7777
*/
7878
}
79+
80+
function test_03_Feature_onScreen(t) {
81+
t.plan( 2 );
82+
83+
var map = new OpenLayers.Map("map");
84+
85+
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
86+
var wms = new OpenLayers.Layer.WMS(name, url);
87+
88+
map.addLayer(wms);
89+
90+
var layer = new OpenLayers.Layer("foo");
91+
map.addLayer(layer);
92+
93+
map.zoomToExtent(new OpenLayers.Bounds(-50,-50,50,50));
94+
95+
//onscreen feature
96+
var feature1 = new OpenLayers.Feature(layer,
97+
new OpenLayers.LonLat(0,0));
98+
t.ok( feature1.onScreen(), "feature knows it's onscreen" );
99+
100+
//onscreen feature
101+
var feature2 = new OpenLayers.Feature(layer,
102+
new OpenLayers.LonLat(100,100));
103+
t.ok( !feature2.onScreen(), "feature knows it's offscreen" );
104+
}
79105

80106
// -->
81107
</script>

tests/test_Layer.html

+17
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@
126126
}
127127

128128

129+
function test_06_Layer_getZoomForResolution(t) {
130+
131+
t.plan(4);
132+
133+
var layer = new OpenLayers.Layer('Test Layer');
134+
135+
//make some dummy resolutions
136+
layer.resolutions = [128, 64, 32, 16, 8, 4, 2];
137+
138+
t.eq(layer.getZoomForResolution(200), 0, "zoom all the way out");
139+
t.eq(layer.getZoomForResolution(25), 2, "zoom in middle");
140+
t.eq(layer.getZoomForResolution(3), 5, "zoom allmost all the way in");
141+
t.eq(layer.getZoomForResolution(1), 6, "zoom all the way in");
142+
143+
}
144+
145+
129146
/******
130147
*
131148
*

tests/test_Layer_WMS.html

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
function test_01_Layer_WMS_constructor (t) {
1515
t.plan( 4 );
1616

17-
layer = new OpenLayers.Layer.WMS();
18-
1917
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
2018
layer = new OpenLayers.Layer.WMS(name, url, params);
2119
t.ok( layer instanceof OpenLayers.Layer.WMS, "new OpenLayers.Layer.WMS returns object" );

tests/test_Marker.html

+32
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,42 @@
1414
t.ok( marker.lonlat.equals(ll), "marker.lonlat returns correct" );
1515
}
1616

17+
function test_02_Marker_onScreen(t) {
18+
t.plan( 2 );
19+
20+
var map = new OpenLayers.Map("map");
21+
22+
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
23+
layer = new OpenLayers.Layer.WMS(name, url);
24+
25+
map.addLayer(layer);
26+
27+
mlayer = new OpenLayers.Layer.Markers('Test Layer');
28+
map.addLayer(mlayer);
29+
30+
map.zoomToExtent(new OpenLayers.Bounds(-50,-50,50,50));
31+
32+
//onscreen marker
33+
var ll = new OpenLayers.LonLat(0,0);
34+
var marker = new OpenLayers.Marker(ll);
35+
mlayer.addMarker(marker);
36+
37+
t.ok( marker.onScreen(), "marker knows it's onscreen" );
38+
39+
//offscreen marker
40+
var ll = new OpenLayers.LonLat(100,100);
41+
var marker2 = new OpenLayers.Marker(ll);
42+
mlayer.addMarker(marker2);
43+
44+
t.ok( !marker2.onScreen(), "marker knows it's offscreen" );
45+
46+
}
47+
1748

1849
// -->
1950
</script>
2051
</head>
2152
<body>
53+
<div id="map" style="width:500px;height:550px"></div>
2254
</body>
2355
</html>

0 commit comments

Comments
 (0)