diff --git a/lib/OpenLayers/Ajax.js b/lib/OpenLayers/Ajax.js index 63a3651e89..d0ab1dec24 100644 --- a/lib/OpenLayers/Ajax.js +++ b/lib/OpenLayers/Ajax.js @@ -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(); diff --git a/lib/OpenLayers/BaseTypes.js b/lib/OpenLayers/BaseTypes.js index bc9f648c83..3bbcdb6f27 100644 --- a/lib/OpenLayers/BaseTypes.js +++ b/lib/OpenLayers/BaseTypes.js @@ -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); }; diff --git a/lib/OpenLayers/BaseTypes/Element.js b/lib/OpenLayers/BaseTypes/Element.js index 8b7dc2f267..de12e41448 100644 --- a/lib/OpenLayers/BaseTypes/Element.js +++ b/lib/OpenLayers/BaseTypes/Element.js @@ -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)]; } } diff --git a/lib/OpenLayers/Control/LayerSwitcher.js b/lib/OpenLayers/Control/LayerSwitcher.js index 479f60d306..eea73cd8a3 100644 --- a/lib/OpenLayers/Control/LayerSwitcher.js +++ b/lib/OpenLayers/Control/LayerSwitcher.js @@ -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); }, diff --git a/lib/OpenLayers/Control/MouseDefaults.js b/lib/OpenLayers/Control/MouseDefaults.js index d4c92d2e49..4429af89b3 100644 --- a/lib/OpenLayers/Control/MouseDefaults.js +++ b/lib/OpenLayers/Control/MouseDefaults.js @@ -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); diff --git a/lib/OpenLayers/Control/OverviewMap.js b/lib/OpenLayers/Control/OverviewMap.js index 582db53b98..9cfead4ad0 100644 --- a/lib/OpenLayers/Control/OverviewMap.js +++ b/lib/OpenLayers/Control/OverviewMap.js @@ -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']; diff --git a/lib/OpenLayers/Control/PanZoom.js b/lib/OpenLayers/Control/PanZoom.js index 945ee0fd8d..6beeb28e8a 100644 --- a/lib/OpenLayers/Control/PanZoom.js +++ b/lib/OpenLayers/Control/PanZoom.js @@ -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; diff --git a/lib/OpenLayers/Control/Panel.js b/lib/OpenLayers/Control/Panel.js index 5b3e2d0cd2..d62599636a 100644 --- a/lib/OpenLayers/Control/Panel.js +++ b/lib/OpenLayers/Control/Panel.js @@ -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 diff --git a/lib/OpenLayers/Events.js b/lib/OpenLayers/Events.js index e293bcdef0..d4c4a620da 100644 --- a/lib/OpenLayers/Events.js +++ b/lib/OpenLayers/Events.js @@ -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. diff --git a/lib/OpenLayers/Format/WKT.js b/lib/OpenLayers/Format/WKT.js index 18f2925726..2e7979357e 100644 --- a/lib/OpenLayers/Format/WKT.js +++ b/lib/OpenLayers/Format/WKT.js @@ -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