allow an uninitialized Bounds object to be functional (and extendable). thanks for the extra docs cr5 (Closes #929)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@4049 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
euzuro
2007-08-27 13:00:54 +00:00
parent c79f1a56c8
commit 1cf74313ab
2 changed files with 58 additions and 21 deletions

View File

@@ -5,7 +5,14 @@
/** /**
* Class: OpenLayers.Bounds * Class: OpenLayers.Bounds
* Instances of this class represent bounding boxes. Data stored as left, * Instances of this class represent bounding boxes. Data stored as left,
* bottom, right, top floats * bottom, right, top floats. All values are initialized to null, however,
* you should make sure you set them before using the bounds for anything.
* Possible use case:
*
* > bounds = new OpenLayers.Bounds();
* > bounds.extend(new OpenLayers.LonLat(4,5));
* > bounds.extend(new OpenLayers.LonLat(5,6));
* > bounds.toBBOX() // returns 4,5,5,6
*/ */
OpenLayers.Bounds = OpenLayers.Class({ OpenLayers.Bounds = OpenLayers.Class({
@@ -13,25 +20,25 @@ OpenLayers.Bounds = OpenLayers.Class({
* Property: left * Property: left
* {Number} * {Number}
*/ */
left: 0.0, left: null,
/** /**
* Property: bottom * Property: bottom
* {Number} * {Number}
*/ */
bottom: 0.0, bottom: null,
/** /**
* Property: right * Property: right
* {Number} * {Number}
*/ */
right: 0.0, right: null,
/** /**
* Property: top * Property: top
* {Number} * {Number}
*/ */
top: 0.0, top: null,
/** /**
* Constructor: OpenLayers.Bounds * Constructor: OpenLayers.Bounds
@@ -46,10 +53,18 @@ OpenLayers.Bounds = OpenLayers.Class({
* top - {Number} The top bounds. * top - {Number} The top bounds.
*/ */
initialize: function(left, bottom, right, top) { initialize: function(left, bottom, right, top) {
this.left = parseFloat(left); if (left != null) {
this.bottom = parseFloat(bottom); this.left = parseFloat(left);
this.right = parseFloat(right); }
this.top = parseFloat(top); if (bottom != null) {
this.bottom = parseFloat(bottom);
}
if (right != null) {
this.right = parseFloat(right);
}
if (top != null) {
this.top = parseFloat(top);
}
}, },
/** /**
@@ -237,14 +252,18 @@ OpenLayers.Bounds = OpenLayers.Class({
} }
if (bounds) { if (bounds) {
this.left = (bounds.left < this.left) ? bounds.left if ( (this.left == null) || (bounds.left < this.left)) {
: this.left; this.left = bounds.left;
this.bottom = (bounds.bottom < this.bottom) ? bounds.bottom }
: this.bottom; if ( (this.bottom == null) || (bounds.bottom < this.bottom) ) {
this.right = (bounds.right > this.right) ? bounds.right this.bottom = bounds.bottom;
: this.right; }
this.top = (bounds.top > this.top) ? bounds.top if ( (this.right == null) || (bounds.right > this.right) ) {
: this.top; this.right = bounds.right;
}
if ( (this.top == null) || (bounds.top > this.top) ) {
this.top = bounds.top;
}
} }
} }
}, },

View File

@@ -4,7 +4,17 @@
<script type="text/javascript"><!-- <script type="text/javascript"><!--
var bounds; var bounds;
function test_01_Bounds_constructor (t) { function test_01_Bounds_constructor (t) {
t.plan( 11 ); t.plan( 17 );
bounds = new OpenLayers.Bounds();
t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
t.eq( bounds.CLASS_NAME, "OpenLayers.Bounds", "bounds.CLASS_NAME is set correctly" );
t.eq( bounds.left, null, "bounds.left is initialized to null" );
t.eq( bounds.bottom, null, "bounds.bottom is initialized to null" );
t.eq( bounds.right, null, "bounds.right is initialized to null" );
t.eq( bounds.top, null, "bounds.top is initialized to null" );
bounds = new OpenLayers.Bounds(0,2,10,4); bounds = new OpenLayers.Bounds(0,2,10,4);
t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" ); t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
t.eq( bounds.CLASS_NAME, "OpenLayers.Bounds", "bounds.CLASS_NAME is set correctly" ); t.eq( bounds.CLASS_NAME, "OpenLayers.Bounds", "bounds.CLASS_NAME is set correctly" );
@@ -298,11 +308,19 @@
function test_16_Bounds_extend(t) { function test_16_Bounds_extend(t) {
t.plan( 8 ); t.plan( 9 );
var originalBounds = new OpenLayers.Bounds(10,20,50,80);
var originalBounds = new OpenLayers.Bounds();
var bounds = originalBounds.clone(); var bounds = originalBounds.clone();
//null bounds to start
bounds.extend(new OpenLayers.LonLat(4,5));
t.ok(bounds.equals(new OpenLayers.Bounds(4,5,4,5)), "uninitialized bounds can be safely extended");
originalBounds = new OpenLayers.Bounds(10,20,50,80);
bounds = originalBounds.clone();
//null obj //null obj
bounds.extend(null); bounds.extend(null);