Deprecating all prototype extensions. This puts all OpenLayers functionality in the OpenLayers namespace. If you are using any of the Function, String, or Number prototype extensions, start using the functional equivalents in the OpenLayers namespace - the prototype extensions will be gone in 3.0 (closes #712).

git-svn-id: http://svn.openlayers.org/trunk/openlayers@4302 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2007-09-14 20:08:47 +00:00
parent af72722ded
commit 2478677985
26 changed files with 341 additions and 217 deletions

View File

@@ -53,14 +53,14 @@ OpenLayers.nullHandler = function(request) {
OpenLayers.loadURL = function(uri, params, caller,
onComplete, onFailure) {
if (OpenLayers.ProxyHost && uri.startsWith("http")) {
if (OpenLayers.ProxyHost && OpenLayers.String.startsWith(uri, "http")) {
uri = OpenLayers.ProxyHost + escape(uri);
}
var success = (onComplete) ? onComplete.bind(caller)
var success = (onComplete) ? OpenLayers.Function.bind(onComplete, caller)
: OpenLayers.nullHandler;
var failure = (onFailure) ? onFailure.bind(caller)
var failure = (onFailure) ? OpenLayers.Function.bind(onFailure, caller)
: OpenLayers.nullHandler;
// from prototype.js
@@ -307,11 +307,11 @@ OpenLayers.Ajax.Request = OpenLayers.Class(OpenLayers.Ajax.Base, {
if (this.options.asynchronous) {
this.transport.onreadystatechange =
this.onStateChange.bind(this);
OpenLayers.Function.bind(this.onStateChange, this);
setTimeout((function() {
this.respondToReadyState(1)
}).bind(this), 10);
setTimeout(OpenLayers.Function.bind(
(function() {this.respondToReadyState(1)}),this), 10
);
}
this.setRequestHeaders();

View File

@@ -4,7 +4,7 @@
/**
* Header: OpenLayers Base Types
* Modifications to standard JavaScript types are described here.
* OpenLayers custom string, number and function functions are described here.
*/
/*********************
@@ -13,10 +13,79 @@
* *
*********************/
OpenLayers.String = {
/**
* APIMethod: OpenLayers.String.startsWith
* Whether or not a string starts with another string.
*
* Parameters:
* str - {String} The string to test.
* sub - {Sring} The substring to look for.
*
* Returns:
* {Boolean} The first string starts with the second.
*/
startsWith: function(str, sub) {
return (str.indexOf(sub) == 0);
},
/**
* APIMethod: OpenLayers.String.contains
* Whether or not a string contains another string.
*
* Parameters:
* str - {String} The string to test.
* sub - {String} The substring to look for.
*
* Returns:
* {Boolean} The first string contains the second.
*/
contains: function(str, sub) {
return (str.indexOf(sub) != -1);
},
/**
* APIMethod: OpenLayers.String.trim
* Removes leading and trailing whitespace characters from a string.
*
* Parameters:
* str - {String} The (potentially) space padded string. This string is not
* modified.
*
* Returns:
* {String} A trimmed version of the string - all leading and
* trailing spaces removed.
*/
trim: function(str) {
return str.replace(/^\s*(.*?)\s*$/, "$1");
},
/**
* APIMethod: OpenLayers.String.camelize
* Camel-case a hyphenated string.
* Ex. "chicken-head" becomes "chickenHead", and
* "-chicken-head" becomes "ChickenHead".
*
* Parameters:
* str - {String} The string to be camelized. The original is not modified.
*
* Returns:
* {String} The string, camelized
*/
camelize: function(str) {
var oStringList = str.split('-');
var camelizedString = oStringList[0];
for (var i = 1; i < oStringList.length; i++) {
var s = oStringList[i];
camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
}
return camelizedString;
}
};
/**
* APIMethod: String.startsWith
* Whether or not a string starts with another string.
* Deprecated. Whether or not a string starts with another string.
*
* Parameters:
* sStart - {Sring} The string we're testing for.
@@ -25,12 +94,16 @@
* {Boolean} Whether or not this string starts with the string passed in.
*/
String.prototype.startsWith = function(sStart) {
return (this.substr(0,sStart.length) == sStart);
OpenLayers.Console.warn(
"This method has been deprecated and will be removed in 3.0. " +
"Please use OpenLayers.String.startsWith instead"
);
return OpenLayers.String.startsWith(this, sStart);
};
/**
* APIMethod: String.contains
* Whether or not a string contains another string.
* Deprecated. Whether or not a string contains another string.
*
* Parameters:
* str - {String} The string that we're testing for.
@@ -39,24 +112,32 @@ String.prototype.startsWith = function(sStart) {
* {Boolean} Whether or not this string contains with the string passed in.
*/
String.prototype.contains = function(str) {
return (this.indexOf(str) != -1);
OpenLayers.Console.warn(
"This method has been deprecated and will be removed in 3.0. " +
"Please use OpenLayers.String.contains instead"
);
return OpenLayers.String.contains(this, str);
};
/**
* APIMethod: String.trim
* Removes leading and trailing whitespace characters from a string.
* Deprecated. Removes leading and trailing whitespace characters from a string.
*
* Returns:
* {String} A trimmed version of the string - all leading and
* trailing spaces removed
*/
String.prototype.trim = function() {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
OpenLayers.Console.warn(
"This method has been deprecated and will be removed in 3.0. " +
"Please use OpenLayers.String.trim instead"
);
return OpenLayers.String.trim(this);
};
/**
* APIMethod: camelize
* Camel-case a hyphenated string.
* Deprecated. Camel-case a hyphenated string.
* Ex. "chicken-head" becomes "chickenHead", and
* "-chicken-head" becomes "ChickenHead".
*
@@ -64,13 +145,11 @@ String.prototype.trim = function() {
* {String} The string, camelized
*/
String.prototype.camelize = function() {
var oStringList = this.split('-');
var camelizedString = oStringList[0];
for (var i = 1; i < oStringList.length; i++) {
var s = oStringList[i];
camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
}
return camelizedString;
OpenLayers.Console.warn(
"This method has been deprecated and will be removed in 3.0. " +
"Please use OpenLayers.String.camelize instead"
);
return OpenLayers.String.camelize(this);
};
@@ -80,10 +159,34 @@ String.prototype.camelize = function() {
* *
*********************/
OpenLayers.Number = {
/**
* APIMethod: OpenLayers.Number.limitSigDigs
* Limit the number of significant digits on an integer.
*
* Parameters:
* num - {Integer}
* sig - {Integer}
*
* Returns:
* {Integer} The number, rounded to the specified number of significant
* digits.
*/
limitSigDigs: function(num, sig) {
var fig;
if(sig > 0) {
fig = parseFloat(num.toPrecision(sig));
} else {
fig = 0;
}
return fig;
}
};
/**
* APIMethod: Number.limitSigDigs
* Limit the number of significant digits on an integer. Does *not* work
* with floats!
* Deprecated. Limit the number of significant digits on an integer. Does *not*
* work with floats!
*
* Parameters:
* sig - {Integer}
@@ -93,17 +196,11 @@ String.prototype.camelize = function() {
* If null, 0, or negative value passed in, returns 0
*/
Number.prototype.limitSigDigs = function(sig) {
var numStr = (sig > 0) ? this.toString() : "0";
if (numStr.contains(".")) {
var msg = "limitSigDig can not be called on a floating point number";
OpenLayers.Console.error(msg);
return null;
}
if ( (sig > 0) && (sig < numStr.length) ) {
var exp = numStr.length - sig;
numStr = Math.round( this / Math.pow(10, exp)) * Math.pow(10, exp);
}
return parseInt(numStr);
OpenLayers.Console.warn(
"This method has been deprecated and will be removed in 3.0. " +
"Please use OpenLayers.Number.limitSigDigs instead"
);
return OpenLayers.Number.limitSigDigs(this, sig);
};
@@ -113,9 +210,54 @@ Number.prototype.limitSigDigs = function(sig) {
* *
*********************/
OpenLayers.Function = {
/**
* APIMethod: OpenLayers.Function.bind
* Bind a function to an object. Method to easily create closures with
* 'this' altered.
*
* Parameters:
* func - {Function} Input function.
* object - {Object} The object to bind to the input function (as this).
*
* Returns:
* {Function} A closure with 'this' set to the passed in object.
*/
bind: function(func, object) {
// create a reference to all arguments past the second one
var args = Array.prototype.slice.apply(arguments, [2]);
return function() {
// Push on any additional arguments from the actual function call.
// These will come after those sent to the bind call.
var newArgs = args.concat(
Array.prototype.slice.apply(arguments, [0])
);
return func.apply(object, newArgs);
};
},
/**
* APIMethod: OpenLayers.Function.bindAsEventListener
* Bind a function to an object, and configure it to receive the event
* object as first parameter when called.
*
* Parameters:
* func - {Function} Input function to serve as an event listener.
* object - {Object} A reference to this.
*
* Returns:
* {Function}
*/
bindAsEventListener: function(func, object) {
return function(event) {
return func.call(object, event || window.event);
};
}
};
/**
* APIMethod: Function.bind
* Bind a function to an object.
* Deprecated. Bind a function to an object.
* Method to easily create closures with 'this' altered.
*
* Parameters:
@@ -126,31 +268,19 @@ Number.prototype.limitSigDigs = function(sig) {
* argument.
*/
Function.prototype.bind = function() {
var __method = this;
var args = [];
var object = arguments[0];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
return function(moreargs) {
var i;
var newArgs = [];
for (i = 0; i < args.length; i++) {
newArgs.push(args[i]);
}
for (i = 0; i < arguments.length; i++) {
newArgs.push(arguments[i]);
}
return __method.apply(object, newArgs);
};
OpenLayers.Console.warn(
"This method has been deprecated and will be removed in 3.0. " +
"Please use OpenLayers.Function.bind instead"
);
// new function takes the same arguments with this function up front
Array.prototype.unshift.apply(arguments, [this]);
return OpenLayers.Function.bind.apply(null, arguments);
};
/**
* APIMethod: Function.bindAsEventListener
* Bind a function to an object, and configure it to receive the event object
* as first parameter when called.
* Deprecated. Bind a function to an object, and configure it to receive the
* event object as first parameter when called.
*
* Parameters:
* object - {Object} A reference to this.
@@ -159,8 +289,9 @@ Function.prototype.bind = function() {
* {Function}
*/
Function.prototype.bindAsEventListener = function(object) {
var __method = this;
return function(event) {
return __method.call(object, event || window.event);
};
OpenLayers.Console.warn(
"This method has been deprecated and will be removed in 3.0. " +
"Please use OpenLayers.Function.bindAsEventListener instead"
);
return OpenLayers.Function.bindAsEventListener(this, object);
};

View File

@@ -135,7 +135,7 @@ OpenLayers.Element = {
*/
getStyle: function(element, style) {
element = OpenLayers.Util.getElement(element);
var value = element.style[style.camelize()];
var value = element.style[OpenLayers.String.camelize(style)];
if (!value) {
if (document.defaultView &&
document.defaultView.getComputedStyle) {
@@ -143,7 +143,7 @@ OpenLayers.Element = {
var css = document.defaultView.getComputedStyle(element, null);
value = css ? css.getPropertyValue(style) : null;
} else if (element.currentStyle) {
value = element.currentStyle[style.camelize()];
value = element.currentStyle[OpenLayers.String.camelize(style)];
}
}

View File

@@ -279,7 +279,9 @@ OpenLayers.Control.LayerSwitcher =
'layerSwitcher': this
}
OpenLayers.Event.observe(inputElem, "mouseup",
this.onInputClick.bindAsEventListener(context));
OpenLayers.Function.bindAsEventListener(this.onInputClick,
context)
);
// create span
var labelSpan = document.createElement("span");
@@ -290,7 +292,9 @@ OpenLayers.Control.LayerSwitcher =
labelSpan.style.verticalAlign = (baseLayer) ? "bottom"
: "baseline";
OpenLayers.Event.observe(labelSpan, "click",
this.onInputClick.bindAsEventListener(context));
OpenLayers.Function.bindAsEventListener(this.onInputClick,
context)
);
// create line break
var br = document.createElement("br");
@@ -463,11 +467,11 @@ OpenLayers.Control.LayerSwitcher =
this.div.style.backgroundColor = "transparent";
OpenLayers.Event.observe(this.div, "mouseup",
this.mouseUp.bindAsEventListener(this));
OpenLayers.Function.bindAsEventListener(this.mouseUp, this));
OpenLayers.Event.observe(this.div, "click",
this.ignoreEvent);
OpenLayers.Event.observe(this.div, "mousedown",
this.mouseDown.bindAsEventListener(this));
OpenLayers.Function.bindAsEventListener(this.mouseDown, this));
OpenLayers.Event.observe(this.div, "dblclick", this.ignoreEvent);
@@ -496,7 +500,7 @@ OpenLayers.Control.LayerSwitcher =
this.baseLayersDiv = document.createElement("div");
this.baseLayersDiv.style.paddingLeft = "10px";
/*OpenLayers.Event.observe(this.baseLayersDiv, "click",
this.onLayerClick.bindAsEventListener(this));
OpenLayers.Function.bindAsEventListener(this.onLayerClick, this));
*/
@@ -545,9 +549,9 @@ OpenLayers.Control.LayerSwitcher =
this.maximizeDiv.style.right = "0px";
this.maximizeDiv.style.left = "";
this.maximizeDiv.style.display = "none";
OpenLayers.Event.observe(this.maximizeDiv,
"click",
this.maximizeControl.bindAsEventListener(this));
OpenLayers.Event.observe(this.maximizeDiv, "click",
OpenLayers.Function.bindAsEventListener(this.maximizeControl, this)
);
this.div.appendChild(this.maximizeDiv);
@@ -564,9 +568,9 @@ OpenLayers.Control.LayerSwitcher =
this.minimizeDiv.style.right = "0px";
this.minimizeDiv.style.left = "";
this.minimizeDiv.style.display = "none";
OpenLayers.Event.observe(this.minimizeDiv,
"click",
this.minimizeControl.bindAsEventListener(this));
OpenLayers.Event.observe(this.minimizeDiv, "click",
OpenLayers.Function.bindAsEventListener(this.minimizeControl, this)
);
this.div.appendChild(this.minimizeDiv);
},

View File

@@ -84,7 +84,9 @@ OpenLayers.Control.MouseDefaults = OpenLayers.Class(OpenLayers.Control, {
*/
registerWheelEvents: function() {
this.wheelObserver = this.onWheelEvent.bindAsEventListener(this);
this.wheelObserver = OpenLayers.Function.bindAsEventListener(
this.onWheelEvent, this
);
//register mousewheel events specifically on the window and document
OpenLayers.Event.observe(window, "DOMMouseScroll", this.wheelObserver);

View File

@@ -218,9 +218,10 @@ OpenLayers.Control.OverviewMap = OpenLayers.Class(OpenLayers.Control, {
'absolute');
this.maximizeDiv.style.display = 'none';
this.maximizeDiv.className = this.displayClass + 'MaximizeButton';
OpenLayers.Event.observe(this.maximizeDiv,
'click',
this.maximizeControl.bindAsEventListener(this));
OpenLayers.Event.observe(this.maximizeDiv, 'click',
OpenLayers.Function.bindAsEventListener(this.maximizeControl,
this)
);
this.div.appendChild(this.maximizeDiv);
// minimize button div
@@ -233,9 +234,10 @@ OpenLayers.Control.OverviewMap = OpenLayers.Class(OpenLayers.Control, {
'absolute');
this.minimizeDiv.style.display = 'none';
this.minimizeDiv.className = this.displayClass + 'MinimizeButton';
OpenLayers.Event.observe(this.minimizeDiv,
'click',
this.minimizeControl.bindAsEventListener(this));
OpenLayers.Event.observe(this.minimizeDiv, 'click',
OpenLayers.Function.bindAsEventListener(this.minimizeControl,
this)
);
this.div.appendChild(this.minimizeDiv);
var eventsToStop = ['dblclick','mousedown'];

View File

@@ -112,11 +112,11 @@ OpenLayers.Control.PanZoom = OpenLayers.Class(OpenLayers.Control, {
this.div.appendChild(btn);
OpenLayers.Event.observe(btn, "mousedown",
this.buttonDown.bindAsEventListener(btn));
OpenLayers.Function.bindAsEventListener(this.buttonDown, btn));
OpenLayers.Event.observe(btn, "dblclick",
this.doubleClick.bindAsEventListener(btn));
OpenLayers.Function.bindAsEventListener(this.doubleClick, btn));
OpenLayers.Event.observe(btn, "click",
this.doubleClick.bindAsEventListener(btn));
OpenLayers.Function.bindAsEventListener(this.doubleClick, btn));
btn.action = id;
btn.map = this.map;
btn.slideFactor = this.slideFactor;

View File

@@ -170,9 +170,9 @@ OpenLayers.Control.Panel = OpenLayers.Class(OpenLayers.Control, {
var textNode = document.createTextNode(" ");
controls[i].panel_div = element;
OpenLayers.Event.observe(controls[i].panel_div, "click",
this.onClick.bind(this, controls[i]));
OpenLayers.Function.bind(this.onClick, this, controls[i]));
OpenLayers.Event.observe(controls[i].panel_div, "mousedown",
OpenLayers.Event.stop.bindAsEventListener());
OpenLayers.Function.bindAsEventListener(OpenLayers.Event.stop));
}
if (this.map) { // map.addControl() has already been called on the panel

View File

@@ -404,7 +404,9 @@ OpenLayers.Events = OpenLayers.Class({
// keep a bound copy of handleBrowserEvent() so that we can
// pass the same function to both Event.observe() and .stopObserving()
this.eventHandler = this.handleBrowserEvent.bindAsEventListener(this);
this.eventHandler = OpenLayers.Function.bindAsEventListener(
this.handleBrowserEvent, this
);
// if eventTypes is specified, create a listeners list for each
// custom application event.

View File

@@ -208,7 +208,7 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, {
* @private
*/
'point': function(str) {
var coords = str.trim().split(this.regExes.spaces);
var coords = OpenLayers.String.trim(str).split(this.regExes.spaces);
return new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(coords[0], coords[1])
);
@@ -221,7 +221,7 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, {
* @private
*/
'multipoint': function(str) {
var points = str.trim().split(',');
var points = OpenLayers.String.trim(str).split(',');
var components = [];
for(var i=0; i<points.length; ++i) {
components.push(this.parse.point.apply(this, [points[i]]).geometry);
@@ -238,7 +238,7 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, {
* @private
*/
'linestring': function(str) {
var points = str.trim().split(',');
var points = OpenLayers.String.trim(str).split(',');
var components = [];
for(var i=0; i<points.length; ++i) {
components.push(this.parse.point.apply(this, [points[i]]).geometry);
@@ -256,7 +256,7 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, {
*/
'multilinestring': function(str) {
var line;
var lines = str.trim().split(this.regExes.parenComma);
var lines = OpenLayers.String.trim(str).split(this.regExes.parenComma);
var components = [];
for(var i=0; i<lines.length; ++i) {
line = lines[i].replace(this.regExes.trimParens, '$1');
@@ -275,7 +275,7 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, {
*/
'polygon': function(str) {
var ring, linestring, linearring;
var rings = str.trim().split(this.regExes.parenComma);
var rings = OpenLayers.String.trim(str).split(this.regExes.parenComma);
var components = [];
for(var i=0; i<rings.length; ++i) {
ring = rings[i].replace(this.regExes.trimParens, '$1');
@@ -296,7 +296,7 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, {
*/
'multipolygon': function(str) {
var polygon;
var polygons = str.trim().split(this.regExes.doubleParenComma);
var polygons = OpenLayers.String.trim(str).split(this.regExes.doubleParenComma);
var components = [];
for(var i=0; i<polygons.length; ++i) {
polygon = polygons[i].replace(this.regExes.trimParens, '$1');
@@ -316,7 +316,7 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, {
'geometrycollection': function(str) {
// separate components of the collection with |
str = str.replace(/,\s*([A-Za-z])/g, '|$1');
var wktArray = str.trim().split('|');
var wktArray = OpenLayers.String.trim(str).split('|');
var components = [];
for(var i=0; i<wktArray.length; ++i) {
components.push(OpenLayers.Format.WKT.prototype.read.apply(this,[wktArray[i]]));

View File

@@ -60,7 +60,8 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, {
text = text.substring(index);
}
var node = OpenLayers.Util.Try(
(function() {
OpenLayers.Function.bind((
function() {
var xmldom;
/**
* Since we want to be able to call this method on the prototype
@@ -74,7 +75,8 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, {
}
xmldom.loadXML(text);
return xmldom;
}).bind(this),
}
), this),
function() {
return new DOMParser().parseFromString(text, 'text/xml');
},

View File

@@ -49,7 +49,9 @@ OpenLayers.Handler.Keyboard = OpenLayers.Class(OpenLayers.Handler, {
initialize: function(control, callbacks, options) {
OpenLayers.Handler.prototype.initialize.apply(this, arguments);
// cache the bound event listener method so it can be unobserved later
this.eventListener = this.handleKeyEvent.bindAsEventListener(this);
this.eventListener = OpenLayers.Function.bindAsEventListener(
this.handleKeyEvent, this
);
},
/**

View File

@@ -39,7 +39,9 @@ OpenLayers.Handler.MouseWheel = OpenLayers.Class(OpenLayers.Handler, {
*/
initialize: function(control, callbacks, options) {
OpenLayers.Handler.prototype.initialize.apply(this, arguments);
this.wheelListener = this.onWheelEvent.bindAsEventListener(this);
this.wheelListener = OpenLayers.Function.bindAsEventListener(
this.onWheelEvent, this
);
},
/**

View File

@@ -228,11 +228,12 @@ OpenLayers.Layer.GeoRSS = OpenLayers.Class(OpenLayers.Layer.Markers, {
if (!sameMarkerClicked) {
var popup = this.createPopup();
OpenLayers.Event.observe(popup.div, "click",
function() {
OpenLayers.Function.bind(function() {
for(var i=0; i < this.layer.map.popups.length; i++) {
this.layer.map.removePopup(this.layer.map.popups[i]);
}
}.bind(this));
}, this)
);
this.layer.map.addPopup(popup);
}
OpenLayers.Event.stop(evt);

View File

@@ -393,13 +393,14 @@ OpenLayers.Layer.WFS = OpenLayers.Class(
var data = this.writer.write(this.features);
var url = this.url;
if (OpenLayers.ProxyHost && this.url.startsWith("http")) {
if (OpenLayers.ProxyHost &&
OpenLayers.String.startsWith(this.url, "http")) {
url = OpenLayers.ProxyHost + escape(this.url);
}
var success = this.commitSuccess.bind(this);
var success = OpenLayers.Function.bind(this.commitSuccess, this);
var failure = this.commitFailure.bind(this);
var failure = OpenLayers.Function.bind(this.commitFailure, this);
data = OpenLayers.Ajax.serializeXMLToString(data);

View File

@@ -301,7 +301,7 @@ OpenLayers.Map = OpenLayers.Class({
// Because Mozilla does not support the "resize" event for elements
// other than "window", we need to put a hack here.
if (navigator.appName.contains("Microsoft")) {
if (OpenLayers.String.contains(navigator.appName, "Microsoft")) {
// If IE, register the resize on the div
this.events.register("resize", this, this.updateSize);
} else {
@@ -309,7 +309,7 @@ OpenLayers.Map = OpenLayers.Class({
// Note that this is ok, as updateSize() does nothing if the
// map's size has not actually changed.
OpenLayers.Event.observe(window, 'resize',
this.updateSize.bind(this));
OpenLayers.Function.bind(this.updateSize, this));
}
// only append link stylesheet if the theme property is set
@@ -354,7 +354,7 @@ OpenLayers.Map = OpenLayers.Class({
this.popups = [];
this.unloadDestroy = this.destroy.bind(this);
this.unloadDestroy = OpenLayers.Function.bind(this.destroy, this);
// always call map.destroy()

View File

@@ -170,7 +170,7 @@ OpenLayers.Popup = OpenLayers.Class({
OpenLayers.Event.stop(e);
}
OpenLayers.Event.observe(closeImg, "click",
closePopup.bindAsEventListener(this));
OpenLayers.Function.bindAsEventListener(closePopup, this));
}

View File

@@ -177,7 +177,7 @@ OpenLayers.Tile.Image = OpenLayers.Class(OpenLayers.Tile, {
http://openlayers.org/pipermail/dev/2007-January/000205.html
OpenLayers.Event.observe( this.imgDiv, "load",
this.checkImgURL.bind(this) );
OpenLayers.Function.bind(this.checkImgURL, this) );
*/
this.frame.appendChild(this.imgDiv);
this.layer.div.appendChild(this.frame);
@@ -206,7 +206,8 @@ OpenLayers.Tile.Image = OpenLayers.Class(OpenLayers.Tile, {
this.events.triggerEvent("loadend");
}
}
OpenLayers.Event.observe(this.imgDiv, 'load', onload.bind(this));
OpenLayers.Event.observe(this.imgDiv, 'load',
OpenLayers.Function.bind(onload, this));
},

View File

@@ -255,9 +255,9 @@ OpenLayers.Util.createImage = function(id, px, sz, imgURL, position, border,
if(delayDisplay) {
image.style.display = "none";
OpenLayers.Event.observe(image, "load",
OpenLayers.Util.onImageLoad.bind(image));
OpenLayers.Function.bind(OpenLayers.Util.onImageLoad, image));
OpenLayers.Event.observe(image, "error",
OpenLayers.Util.onImageLoadError.bind(image));
OpenLayers.Function.bind(OpenLayers.Util.onImageLoadError, image));
}
@@ -453,9 +453,9 @@ OpenLayers.Util.createAlphaImageDiv = function(id, px, sz, imgURL,
if (delayDisplay) {
img.style.display = "none";
OpenLayers.Event.observe(img, "load",
OpenLayers.Util.onImageLoad.bind(div));
OpenLayers.Function.bind(OpenLayers.Util.onImageLoad, div));
OpenLayers.Event.observe(img, "error",
OpenLayers.Util.onImageLoadError.bind(div));
OpenLayers.Function.bind(OpenLayers.Util.onImageLoadError, div));
}
OpenLayers.Util.modifyAlphaImageDiv(div, id, px, sz, imgURL, position,
@@ -791,9 +791,10 @@ OpenLayers.Util.getParameters = function(url) {
//parse out parameters portion of url string
var paramsString = "";
if (url.contains('?')) {
if (OpenLayers.String.contains(url, '?')) {
var start = url.indexOf('?') + 1;
var end = url.contains("#") ? url.indexOf('#') : url.length;
var end = OpenLayers.String.contains(url, "#") ?
url.indexOf('#') : url.length;
paramsString = url.substring(start, end);
}

View File

@@ -9,7 +9,8 @@
var g = new OpenLayers.Geometry.Surface();
t.eq(g.CLASS_NAME, "OpenLayers.Geometry.Surface", "correct CLASS_NAME")
t.ok(g.id.startsWith("OpenLayers.Geometry.Surface_"), "id correctly set");
t.ok(OpenLayers.String.startsWith(g.id, "OpenLayers.Geometry.Surface_"),
"id correctly set");
}

View File

@@ -11,9 +11,12 @@
var test1 = "chicken";
var test2 = "beet";
t.ok(str.startsWith("chicken"), " 'chickenHead' starts with 'chicken'");
t.ok(!str.startsWith("Head"), " 'chickenHead' contains 'Head' but does *not* start with it");
t.ok(!str.startsWith("beet"), "'chickenHead' doesnt start with 'beet'");
t.ok(OpenLayers.String.startsWith(str, "chicken"),
"'chickenHead' starts with 'chicken'");
t.ok(!OpenLayers.String.startsWith(str, "Head"),
"'chickenHead' does not start with 'Head'");
t.ok(!OpenLayers.String.startsWith(str, "beet"),
"'chickenHead' doesnt start with 'beet'");
}
function test_02_String_contains(t) {
@@ -21,81 +24,84 @@
var str = "chickenHead";
t.ok(str.contains("chicken"), "(beginning) 'chickenHead' contains with 'chicken'");
t.ok(str.contains("ick"), "(middle) 'chickenHead' contains with 'ick'");
t.ok(str.contains("Head"), "(end) 'chickenHead' contains with 'Head'");
t.ok(!str.startsWith("beet"), "'chickenHead' doesnt start with 'beet'");
t.ok(OpenLayers.String.contains(str, "chicken"),
"(beginning) 'chickenHead' contains with 'chicken'");
t.ok(OpenLayers.String.contains(str, "ick"),
"(middle) 'chickenHead' contains with 'ick'");
t.ok(OpenLayers.String.contains(str, "Head"),
"(end) 'chickenHead' contains with 'Head'");
t.ok(!OpenLayers.String.startsWith(str, "beet"),
"'chickenHead' doesnt start with 'beet'");
}
function test_03_String_trim(t) {
t.plan(5);
var str = "chickenHead";
t.eq(str.trim(), "chickenHead", "string with no extra whitespace is left alone");
t.eq(OpenLayers.String.trim(str),
"chickenHead", "string with no extra whitespace is left alone");
str = " chickenHead";
t.eq(str.trim(), "chickenHead", "string with extra whitespace at beginning is trimmed correctly");
t.eq(OpenLayers.String.trim(str),
"chickenHead", "string with extra whitespace at beginning is trimmed correctly");
str = "chickenHead ";
t.eq(str.trim(), "chickenHead", "string with extra whitespace at end is trimmed correctly");
t.eq(OpenLayers.String.trim(str),
"chickenHead", "string with extra whitespace at end is trimmed correctly");
str = " chickenHead ";
t.eq(str.trim(), "chickenHead", "string with extra whitespace at beginning and end is trimmed correctly");
t.eq(OpenLayers.String.trim(str),
"chickenHead", "string with extra whitespace at beginning and end is trimmed correctly");
str = " ";
t.eq(str.trim(), "", "whitespace string is trimmed correctly");
t.eq(OpenLayers.String.trim(str), "", "whitespace string is trimmed correctly");
}
function test_05_String_camelize(t) {
t.plan(7);
var str = "chickenhead";
t.eq(str.camelize(), "chickenhead", "string with no hyphens is left alone");
t.eq(OpenLayers.String.camelize(str), "chickenhead", "string with no hyphens is left alone");
str = "chicken-head";
t.eq(str.camelize(), "chickenHead", "string with one middle hyphen is camelized correctly");
t.eq(OpenLayers.String.camelize(str), "chickenHead", "string with one middle hyphen is camelized correctly");
str = "chicken-head-man";
t.eq(str.camelize(), "chickenHeadMan", "string with multiple middle hyphens is camelized correctly");
t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens is camelized correctly");
str = "-chickenhead";
t.eq(str.camelize(), "Chickenhead", "string with starting hyphen is camelized correctly (capitalized)");
t.eq(OpenLayers.String.camelize(str), "Chickenhead", "string with starting hyphen is camelized correctly (capitalized)");
str = "-chicken-head-man";
t.eq(str.camelize(), "ChickenHeadMan", "string with starting hypen and multiple middle hyphens is camelized correctly");
t.eq(OpenLayers.String.camelize(str), "ChickenHeadMan", "string with starting hypen and multiple middle hyphens is camelized correctly");
str = "chicken-";
t.eq(str.camelize(), "chicken", "string ending in hyphen is camelized correctly (hyphen dropped)");
t.eq(OpenLayers.String.camelize(str), "chicken", "string ending in hyphen is camelized correctly (hyphen dropped)");
str = "chicken-head-man-";
t.eq(str.camelize(), "chickenHeadMan", "string with multiple middle hyphens and end hyphen is camelized correctly (end hyphen dropped)");
t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens and end hyphen is camelized correctly (end hyphen dropped)");
}
function test_06_Number_limitSigDigs(t) {
t.plan(10);
t.plan(9);
var num = 123456789;
t.eq(num.limitSigDigs(), 0, "passing 'null' as sig returns 0");
t.eq(num.limitSigDigs(-1), 0, "passing -1 as sig returns 0");
t.eq(num.limitSigDigs(0), 0, "passing 0 as sig returns 0");
t.eq(OpenLayers.Number.limitSigDigs(num), 0, "passing 'null' as sig returns 0");
t.eq(OpenLayers.Number.limitSigDigs(num, -1), 0, "passing -1 as sig returns 0");
t.eq(OpenLayers.Number.limitSigDigs(num, 0), 0, "passing 0 as sig returns 0");
t.eq(num.limitSigDigs(15), 123456789, "passing sig greater than num digits in number returns number unmodified");
t.eq(OpenLayers.Number.limitSigDigs(num, 15), 123456789, "passing sig greater than num digits in number returns number unmodified");
t.eq(num.limitSigDigs(1), 100000000, "passing sig 1 works");
t.eq(num.limitSigDigs(3), 123000000, "passing middle sig works (rounds down)");
t.eq(num.limitSigDigs(5), 123460000, "passing middle sig works (rounds up)");
t.eq(num.limitSigDigs(9), 123456789, "passing sig equal to num digits in number works");
t.eq(OpenLayers.Number.limitSigDigs(num, 1), 100000000, "passing sig 1 works");
t.eq(OpenLayers.Number.limitSigDigs(num, 3), 123000000, "passing middle sig works (rounds down)");
t.eq(OpenLayers.Number.limitSigDigs(num, 5), 123460000, "passing middle sig works (rounds up)");
t.eq(OpenLayers.Number.limitSigDigs(num, 9), 123456789, "passing sig equal to num digits in number works");
var temp = OpenLayers.Console.error;
OpenLayers.Console.error = function() {
t.ok(true, "error reported when run on floating point number")
}
num = 1234.56789;
t.ok(num.limitSigDigs(5) == null, "running limSigDig() on a floating point number returns null");
t.eq(OpenLayers.Number.limitSigDigs(num, 5), 1234.6, "running limSigDig() on a floating point number works fine");
OpenLayers.Console.error = temp;
}
function test_07_Function_bind(t) {
@@ -115,7 +121,7 @@
t.eq(arguments.length, 4, "correct number of arguments ((regression test for #876))");
};
var newFoo = foo.bind(g_obj, g_Arg1, g_Arg2);
var newFoo = OpenLayers.Function.bind(foo, g_obj, g_Arg1, g_Arg2);
newFoo(g_Arg3, g_Arg4);
@@ -135,7 +141,7 @@
g_X = x;
};
var newFoo = foo.bindAsEventListener(g_obj);
var newFoo = OpenLayers.Function.bindAsEventListener(foo, g_obj);
g_X = null;

View File

@@ -21,7 +21,8 @@
t.ok( feature instanceof OpenLayers.Feature, "new OpenLayers.Feature returns Feature object" );
t.eq( feature.layer, layer, "feature.layer set correctly" );
t.ok( feature.id.startsWith("OpenLayers.Feature_"), "feature.id set correctly" );
t.ok(OpenLayers.String.startsWith(feature.id, "OpenLayers.Feature_"),
"feature.id set correctly");
t.ok( feature.lonlat.equals(lonlat), "feature.lonlat set correctly" );
t.eq( feature.data.iconURL, iconURL, "feature.data.iconURL set correctly" );
t.ok( feature.data.iconSize.equals(iconSize), "feature.data.iconSize set correctly" );

View File

@@ -10,7 +10,8 @@
var g = new OpenLayers.Geometry();
t.eq(g.CLASS_NAME, "OpenLayers.Geometry", "correct CLASS_NAME")
t.ok(g.id.startsWith("OpenLayers.Geometry_"), "id correctly set");
t.ok(OpenLayers.String.startsWith(g.id, "OpenLayers.Geometry_"),
"id correctly set");
}
@@ -20,7 +21,8 @@
var clone = geometry.clone();
t.eq(clone.CLASS_NAME, "OpenLayers.Geometry", "correct CLASS_NAME")
t.ok(clone.id.startsWith("OpenLayers.Geometry_"), "id correctly set");
t.ok(OpenLayers.String.startsWith(clone.id, "OpenLayers.Geometry_"),
"id correctly set");
}
function test_02_Geometry_setBounds(t) {

View File

@@ -13,7 +13,8 @@
popup = new OpenLayers.Popup();
t.ok( popup instanceof OpenLayers.Popup, "new OpenLayers.Popup returns Popup object" );
t.ok(popup.id.startsWith("OpenLayers.Popup"), "valid default popupid");
t.ok(OpenLayers.String.startsWith(popup.id, "OpenLayers.Popup"),
"valid default popupid");
var firstID = popup.id;
t.ok(popup.size.equals(size), "good default popup.size");
t.eq(popup.contentHTML, "", "good default popup.contentHTML");

View File

@@ -23,7 +23,8 @@
t.ok( tile.size.equals(size), "tile.size is set correctly" );
t.ok( tile.id != null, "tile is given an id");
t.ok( tile.id.startsWith("Tile_"), "tile's id starts correctly");
t.ok(OpenLayers.String.startsWith(tile.id, "Tile_"),
"tile's id starts correctly");
t.ok( tile.events != null, "tile's events intitialized");
}

View File

@@ -11,25 +11,6 @@
"getImagesLocation()" );
}
function test_02_Util_Strings(t) {
t.plan(5);
var str = " chicken pox ";
t.ok(str.contains("chicken"), "contains() function correctly finds an embedded string");
t.ok(!str.contains("marsupial"), "contains() function correctly does not finds an random string");
var trimmedStr = str.trim();
t.eq(trimmedStr, "chicken pox", "String.trim works correctly");
t.eq(trimmedStr.startsWith("chicken"), true, "String.startsWith correctly finds chicken");
t.eq(trimmedStr.startsWith("dolphin"), false, "String.startsWith correctly does not find turkey");
}
function test_03_Util_Array(t) {
t.plan( 1 );
@@ -423,32 +404,12 @@
t.plan(2);
var id = OpenLayers.Util.createUniqueID();
t.ok( id.startsWith("id_"), "default OpenLayers.Util.createUniqueID starts id correctly");
t.ok(OpenLayers.String.startsWith(id, "id_"),
"default OpenLayers.Util.createUniqueID starts id correctly");
var id = OpenLayers.Util.createUniqueID("chicken");
t.ok( id.startsWith("chicken"), "OpenLayers.Util.createUniqueID starts id correctly");
}
function test_12_Util_limitSigDigs(t) {
t.plan(7);
var x;
x = 123456;
t.eq(x.limitSigDigs(3), 123000, "correctly rounds down");
x = 555555;
t.eq(x.limitSigDigs(3), 556000, "correctly rounds up");
x = 66;
t.eq(x.limitSigDigs(3), 66, "correctly handles number smaller than sigdig");
t.eq(x.limitSigDigs(null), 0, "correctly handles null sigdig");
t.eq(x.limitSigDigs(0), 0, "correctly handles 0 sigdig");
t.eq(x.limitSigDigs(-1), 0, "correctly handles negative sigdig");
x = 0;
t.eq(x.limitSigDigs(2), 0, "correctly handles 0 number");
t.ok(OpenLayers.String.startsWith(id, "chicken"),
"OpenLayers.Util.createUniqueID starts id correctly");
}
function test_13_Util_normalizeScale(t) {