Removed modifications to Array.prototype and moved them into OL.Util instead. All tests pass.

git-svn-id: http://svn.openlayers.org/trunk/openlayers@1590 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Schuyler Erle
2006-10-05 15:18:02 +00:00
parent efa9499d57
commit 4739147a2f
12 changed files with 55 additions and 85 deletions
+15 -64
View File
@@ -367,24 +367,21 @@ OpenLayers.Bounds.prototype = {
}, },
/** /**
* @param {int} decimal How many significant digits in the bbox coords? * @return Simple String representation of OpenLayers.Bounds object.
* Default is 6 * (ex. <i>"5,42,10,45"</i>)
* * @type String
* @returns Simple String representation of OpenLayers.Bounds object. */
* (ex. <i>"5,42,10,45"</i>) toBBOX:function(power) {
* @type String var mult;
*/ if (power) {
toBBOX:function(decimal) { mult = Math.pow(10,power);
if (decimal== null) { } else {
decimal = 6; mult = Math.pow(10,6);
} }
var mult = Math.pow(10, decimal); return (Math.round(this.left*mult)/mult + "," +
var bbox = Math.round(this.left*mult)/mult + "," + Math.round(this.bottom*mult)/mult + "," +
Math.round(this.bottom*mult)/mult + "," + Math.round(this.right*mult)/mult + "," +
Math.round(this.right*mult)/mult + "," + Math.round(this.top*mult)/mult);
Math.round(this.top*mult)/mult;
return bbox;
}, },
/** /**
@@ -812,52 +809,6 @@ String.prototype.camelize = function() {
}; };
/*********************
* *
* ARRAY *
* *
*********************/
/** Remove an object from an array. Iterates through the array
* to find the item, then removes it.
*
* @param {Object} item
*
* @returns A reference to the array
* @type Array
*/
Array.prototype.remove = function(item) {
for(var i=0; i < this.length; i++) {
if(this[i] == item) {
this.splice(i,1);
//break;more than once??
}
}
return this;
}
/**
* @returns A fresh copy of the array
* @type Array
*/
Array.prototype.clone = function() {
var clone = new Array();
for (var i = 0; i < this.length; i++) {
clone[i] = this[i];
}
return clone;
};
/**
*/
Array.prototype.clear = function() {
this.length = 0;
};
/********************* /*********************
* * * *
* NUMBER * * NUMBER *
+1 -1
View File
@@ -148,7 +148,7 @@ OpenLayers.Layer.GeoRSS.prototype =
if (this.features != null) { if (this.features != null) {
while(this.features.length > 0) { while(this.features.length > 0) {
var feature = this.features[0]; var feature = this.features[0];
this.features.remove(feature); OpenLayers.Util.removeItem(this.features, feature);
feature.destroy(); feature.destroy();
} }
} }
+1 -1
View File
@@ -324,7 +324,7 @@ OpenLayers.Layer.Grid.prototype =
for(var iRow=0; iRow < this.grid.length; iRow++) { for(var iRow=0; iRow < this.grid.length; iRow++) {
var row = this.grid[iRow]; var row = this.grid[iRow];
for(var iCol=0; iCol < row.length; iCol++) { for(var iCol=0; iCol < row.length; iCol++) {
row[iCol].clear(); OpenLayers.Util.clearArray(row[iCol]);
} }
} }
} }
+1 -1
View File
@@ -70,7 +70,7 @@ OpenLayers.Layer.Markers.prototype =
* @param {OpenLayers.Marker} marker * @param {OpenLayers.Marker} marker
*/ */
removeMarker: function(marker) { removeMarker: function(marker) {
this.markers.remove(marker); OpenLayers.Util.removeItem(this.markers, marker);
if ((marker.icon != null) && (marker.icon.imageDiv != null) && if ((marker.icon != null) && (marker.icon.imageDiv != null) &&
(marker.icon.imageDiv.parentNode == this.div) ) { (marker.icon.imageDiv.parentNode == this.div) ) {
this.div.removeChild(marker.icon.imageDiv); this.div.removeChild(marker.icon.imageDiv);
+1 -1
View File
@@ -155,7 +155,7 @@ OpenLayers.Layer.Text.prototype =
if (this.features != null) { if (this.features != null) {
while(this.features.length > 0) { while(this.features.length > 0) {
var feature = this.features[0]; var feature = this.features[0];
this.features.remove(feature); OpenLayers.Util.removeItem(this.features, feature);
feature.destroy(); feature.destroy();
} }
} }
+1 -1
View File
@@ -124,7 +124,7 @@ OpenLayers.Layer.WMS.Untiled.prototype =
//clear out the old tile //clear out the old tile
if (this.tile) { if (this.tile) {
this.tile.clear(); OpenLayers.Util.clearArray(this.tile);
} }
//determine new tile bounds //determine new tile bounds
+2 -2
View File
@@ -327,7 +327,7 @@ OpenLayers.Map.prototype = {
this.layerContainerDiv.removeChild(layer.div); this.layerContainerDiv.removeChild(layer.div);
} }
layer.map = null; layer.map = null;
this.layers.remove(layer); OpenLayers.Util.removeItem(this.layers, layer);
// if we removed the base layer, need to set a new one // if we removed the base layer, need to set a new one
if (this.baseLayer == layer) { if (this.baseLayer == layer) {
@@ -432,7 +432,7 @@ OpenLayers.Map.prototype = {
* @param {OpenLayers.Popup} popup * @param {OpenLayers.Popup} popup
*/ */
removePopup: function(popup) { removePopup: function(popup) {
this.popups.remove(popup); OpenLayers.Util.removeItem(this.popups, popup);
if (popup.div) { if (popup.div) {
try { this.layerContainerDiv.removeChild(popup.div); } try { this.layerContainerDiv.removeChild(popup.div); }
catch (e) { } // Popups sometimes apparently get disconnected catch (e) { } // Popups sometimes apparently get disconnected
+1 -1
View File
@@ -175,7 +175,7 @@ OpenLayers.Popup.AnchoredBubble.prototype =
//we want to round all the corners _except_ the opposite one. //we want to round all the corners _except_ the opposite one.
var corner = OpenLayers.Bounds.oppositeQuadrant(this.relativePosition); var corner = OpenLayers.Bounds.oppositeQuadrant(this.relativePosition);
corners.remove(corner); OpenLayers.Util.removeItem(corners, corner);
return corners.join(" "); return corners.join(" ");
}, },
+1 -1
View File
@@ -84,7 +84,7 @@ OpenLayers.Tile.prototype = {
redraw = true; redraw = true;
} }
this.clear(); OpenLayers.Util.clearArray(this);
this.bounds = bounds.clone(); this.bounds = bounds.clone();
this.position = position.clone(); this.position = position.clone();
if (redraw) { if (redraw) {
+1 -1
View File
@@ -60,7 +60,7 @@ OpenLayers.Tile.WFS.prototype =
*/ */
draw:function() { draw:function() {
if (this.drawn) { if (this.drawn) {
this.clear(); OpenLayers.Util.clearArray(this);
} }
OpenLayers.Tile.prototype.draw.apply(this, arguments); OpenLayers.Tile.prototype.draw.apply(this, arguments);
if (this.layer.displayOutsideMaxExtent || (this.layer.maxExtent && if (this.layer.displayOutsideMaxExtent || (this.layer.maxExtent &&
+25
View File
@@ -35,6 +35,31 @@ OpenLayers.Util.extend = function(destination, source) {
return destination; return destination;
}; };
/** Remove an object from an array. Iterates through the array
* to find the item, then removes it.
*
* @param {Object} item
*
* @returns A reference to the array
* @type Array
*/
OpenLayers.Util.removeItem = function(array, item) {
for(var i=0; i < array.length; i++) {
if(array[i] == item) {
array.splice(i,1);
//break;more than once??
}
}
return array;
};
/**
*/
OpenLayers.Util.clearArray = function(array) {
array.length = 0;
};
/** /**
* @param {String} id * @param {String} id
* @param {OpenLayers.Pixel} px * @param {OpenLayers.Pixel} px
+5 -11
View File
@@ -30,21 +30,15 @@
} }
function test_03_Util_Array(t) { function test_03_Util_Array(t) {
t.plan( 5 ); t.plan( 2 );
var array = new Array(1,2,3,4,5); var array = new Array(1,2,3,4,5);
array.remove(3); OpenLayers.Util.removeItem(array, 3);
t.eq( array.toString(), "1,2,4,5", "array.remove works"); t.eq( array.toString(), "1,2,4,5", "Util.removeItem works");
copy = array.clone(); OpenLayers.Util.clearArray(array);
t.eq( copy.toString(), "1,2,4,5", "array.clone() works"); t.eq( array.toString(), "", "Util.clearArray works");
array.push(7);
t.eq( copy.toString(), "1,2,4,5", "changing a value in the copied array doesnt affect the new array");
t.eq( copy.indexOf(5), 3, "indexOf function returns index of value in an array");
t.eq( copy.indexOf(75), -1, "indexOf function returns -1 when element not found in array");
} }