Merge pull request #916 from Turbo87/bounds

Added extendXY() method to Bounds class
This commit is contained in:
ahocevar
2013-03-25 05:17:49 -07:00
2 changed files with 178 additions and 124 deletions
+42 -22
View File
@@ -355,39 +355,59 @@ OpenLayers.Bounds = OpenLayers.Class({
* object. * object.
*/ */
extend:function(object) { extend:function(object) {
var bounds = null;
if (object) { if (object) {
// clear cached center location
switch(object.CLASS_NAME) { switch(object.CLASS_NAME) {
case "OpenLayers.LonLat": case "OpenLayers.LonLat":
bounds = new OpenLayers.Bounds(object.lon, object.lat, this.extendXY(object.lon, object.lat);
object.lon, object.lat);
break; break;
case "OpenLayers.Geometry.Point": case "OpenLayers.Geometry.Point":
bounds = new OpenLayers.Bounds(object.x, object.y, this.extendXY(object.x, object.y);
object.x, object.y);
break; break;
case "OpenLayers.Bounds": case "OpenLayers.Bounds":
bounds = object; // clear cached center location
this.centerLonLat = null;
if ( (this.left == null) || (object.left < this.left)) {
this.left = object.left;
}
if ( (this.bottom == null) || (object.bottom < this.bottom) ) {
this.bottom = object.bottom;
}
if ( (this.right == null) || (object.right > this.right) ) {
this.right = object.right;
}
if ( (this.top == null) || (object.top > this.top) ) {
this.top = object.top;
}
break; break;
} }
}
},
if (bounds) { /**
this.centerLonLat = null; * APIMethod: extendXY
if ( (this.left == null) || (bounds.left < this.left)) { * Extend the bounds to include the XY coordinate specified.
this.left = bounds.left; *
} * Parameters:
if ( (this.bottom == null) || (bounds.bottom < this.bottom) ) { * x - {number} The X part of the the coordinate.
this.bottom = bounds.bottom; * y - {number} The Y part of the the coordinate.
} */
if ( (this.right == null) || (bounds.right > this.right) ) { extendXY:function(x, y) {
this.right = bounds.right; // clear cached center location
} this.centerLonLat = null;
if ( (this.top == null) || (bounds.top > this.top) ) {
this.top = bounds.top; if ((this.left == null) || (x < this.left)) {
} this.left = x;
} }
if ((this.bottom == null) || (y < this.bottom)) {
this.bottom = y;
}
if ((this.right == null) || (x > this.right)) {
this.right = x;
}
if ((this.top == null) || (y > this.top)) {
this.top = y;
} }
}, },
+53 -19
View File
@@ -444,37 +444,36 @@
function test_Bounds_extend(t) { function test_Bounds_extend(t) {
t.plan( 9 ); t.plan( 9 );
// null bounds to start
var originalBounds = new OpenLayers.Bounds(); var originalBounds = new OpenLayers.Bounds();
var bounds = originalBounds.clone(); var bounds = originalBounds.clone();
//null bounds to start
bounds.extend(new OpenLayers.LonLat(4,5)); bounds.extend(new OpenLayers.LonLat(4,5));
t.ok(bounds.equals(new OpenLayers.Bounds(4,5,4,5)), "uninitialized bounds can be safely extended"); t.ok(bounds.equals(new OpenLayers.Bounds(4,5,4,5)), "uninitialized bounds can be safely extended");
// extend with null obj
originalBounds = new OpenLayers.Bounds(10,20,50,80); originalBounds = new OpenLayers.Bounds(10,20,50,80);
bounds = originalBounds.clone(); bounds = originalBounds.clone();
//null obj
bounds.extend(null); bounds.extend(null);
t.ok(bounds.equals(originalBounds), "null to extend does not crash or change original bounds"); t.ok(bounds.equals(originalBounds), "null to extend does not crash or change original bounds");
//obj with no classname // obj with no classname
var object = {}; var object = {};
bounds.extend(object); bounds.extend(object);
t.ok(bounds.equals(originalBounds), "extend() passing object with no classname does not crash or change original bounds") t.ok(bounds.equals(originalBounds), "extend() passing object with no classname does not crash or change original bounds")
//obj is bounds
//pushing all limits with bounds obj // obj is bounds
// pushing all limits with bounds obj
var testBounds = new OpenLayers.Bounds(5, 10, 60, 90); var testBounds = new OpenLayers.Bounds(5, 10, 60, 90);
object = testBounds.clone(); object = testBounds.clone();
bounds.extend(object); bounds.extend(object);
t.ok(bounds.equals(testBounds), "extend by valid bounds, pushing all limits, correctly extends bounds"); t.ok(bounds.equals(testBounds), "extend by valid bounds, pushing all limits, correctly extends bounds");
//pushing no limits with bounds obj // pushing no limits with bounds obj
bounds = originalBounds.clone(); bounds = originalBounds.clone();
testBounds = new OpenLayers.Bounds(15, 30, 40, 70); testBounds = new OpenLayers.Bounds(15, 30, 40, 70);
@@ -483,9 +482,10 @@
bounds.extend(object); bounds.extend(object);
t.ok(bounds.equals(originalBounds), "extend by valid bounds, pushing no limits, correctly does not extend bounds"); t.ok(bounds.equals(originalBounds), "extend by valid bounds, pushing no limits, correctly does not extend bounds");
// obj is lonlat
//left, bottom // obj is lonlat
// left, bottom
bounds = originalBounds.clone(); bounds = originalBounds.clone();
object = new OpenLayers.LonLat(5, 10); object = new OpenLayers.LonLat(5, 10);
@@ -495,9 +495,9 @@
t.ok( ((bounds.left == object.lon) && t.ok( ((bounds.left == object.lon) &&
(bounds.bottom == object.lat) && (bounds.bottom == object.lat) &&
(bounds.right == originalBounds.right) && (bounds.right == originalBounds.right) &&
(bounds.top == originalBounds.top)), "obj lonlat to extends correclty modifies left and bottom"); (bounds.top == originalBounds.top)), "obj lonlat to extends correctly modifies left and bottom");
//right, top // right, top
bounds = originalBounds.clone(); bounds = originalBounds.clone();
object = new OpenLayers.LonLat(60,90); object = new OpenLayers.LonLat(60,90);
@@ -507,11 +507,12 @@
t.ok( ((bounds.left == originalBounds.left) && t.ok( ((bounds.left == originalBounds.left) &&
(bounds.bottom == originalBounds.bottom) && (bounds.bottom == originalBounds.bottom) &&
(bounds.right == object.lon) && (bounds.right == object.lon) &&
(bounds.top == object.lat)), "obj lonlat to extends correclty modifies right and top"); (bounds.top == object.lat)), "obj lonlat to extends correctly modifies right and top");
// obj is point
//left, bottom // obj is point
// left, bottom
bounds = originalBounds.clone(); bounds = originalBounds.clone();
object = new OpenLayers.Geometry.Point(5, 10); object = new OpenLayers.Geometry.Point(5, 10);
@@ -521,9 +522,9 @@
t.ok( ((bounds.left == object.x) && t.ok( ((bounds.left == object.x) &&
(bounds.bottom == object.y) && (bounds.bottom == object.y) &&
(bounds.right == originalBounds.right) && (bounds.right == originalBounds.right) &&
(bounds.top == originalBounds.top)), "obj Point to extends correclty modifies left and bottom"); (bounds.top == originalBounds.top)), "obj Point to extends correctly modifies left and bottom");
//right, top // right, top
bounds = originalBounds.clone(); bounds = originalBounds.clone();
object = new OpenLayers.Geometry.Point(60,90); object = new OpenLayers.Geometry.Point(60,90);
@@ -533,11 +534,44 @@
t.ok( ((bounds.left == originalBounds.left) && t.ok( ((bounds.left == originalBounds.left) &&
(bounds.bottom == originalBounds.bottom) && (bounds.bottom == originalBounds.bottom) &&
(bounds.right == object.x) && (bounds.right == object.x) &&
(bounds.top == object.y)), "obj Point to extends correclty modifies right and top"); (bounds.top == object.y)), "obj Point to extends correctly modifies right and top");
} }
function test_Bounds_extendXY(t) {
t.plan(3);
// null bounds to start
var originalBounds = new OpenLayers.Bounds();
var bounds = originalBounds.clone();
bounds.extendXY(4, 5);
t.ok(bounds.equals(new OpenLayers.Bounds(4,5,4,5)), "uninitialized bounds can be safely extended");
// left, bottom
originalBounds = new OpenLayers.Bounds(10,20,50,80);
bounds = originalBounds.clone();
bounds.extendXY(5, 10);
t.ok( ((bounds.left == 5) &&
(bounds.bottom == 10) &&
(bounds.right == originalBounds.right) &&
(bounds.top == originalBounds.top)), "extendXY correctly modifies left and bottom");
// right, top
bounds = originalBounds.clone();
bounds.extendXY(60, 90);
t.ok( ((bounds.left == originalBounds.left) &&
(bounds.bottom == originalBounds.bottom) &&
(bounds.right == 60) &&
(bounds.top == 90)), "extendXY correctly modifies right and top");
}
function test_Bounds_wrapDateLine(t) { function test_Bounds_wrapDateLine(t) {
t.plan( 13 ); t.plan( 13 );