More trip planner testing with colors
[busui.git] / labs / openlayers / lib / OpenLayers / BaseTypes.js
blob:a/labs/openlayers/lib/OpenLayers/BaseTypes.js -> blob:b/labs/openlayers/lib/OpenLayers/BaseTypes.js
<
  /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for
  * full list of contributors). Published under the Clear BSD license.
  * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
  * full text of the license. */
   
  /**
  * @requires OpenLayers/BaseTypes/Class.js
  * @requires OpenLayers/BaseTypes/LonLat.js
  * @requires OpenLayers/BaseTypes/Size.js
  * @requires OpenLayers/BaseTypes/Pixel.js
  * @requires OpenLayers/BaseTypes/Bounds.js
  * @requires OpenLayers/BaseTypes/Element.js
  * @requires OpenLayers/Lang/en.js
  * @requires OpenLayers/Console.js
  */
   
  /**
  * Header: OpenLayers Base Types
  * OpenLayers custom string, number and function functions are described here.
  */
   
  /**
  * Namespace: OpenLayers.String
  * Contains convenience functions for string manipulation.
  */
  OpenLayers.String = {
   
  /**
  * APIFunction: startsWith
  * Test whether 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);
  },
   
  /**
  * APIFunction: contains
  * Test whether 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);
  },
   
  /**
  * APIFunction: 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 with all leading and
  * trailing spaces removed.
  */
  trim: function(str) {
  return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  },
   
  /**
  * APIFunction: 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, len=oStringList.length; i<len; i++) {
  var s = oStringList[i];
  camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
  }
  return camelizedString;
  },
   
  /**
  * APIFunction: format
  * Given a string with tokens in the form ${token}, return a string
  * with tokens replaced with properties from the given context
  * object. Represent a literal "${" by doubling it, e.g. "${${".
  *
  * Parameters:
  * template - {String} A string with tokens to be replaced. A template
  * has the form "literal ${token}" where the token will be replaced
  * by the value of context["token"].
  * context - {Object} An optional object with properties corresponding
  * to the tokens in the format string. If no context is sent, the
  * window object will be used.
  * args - {Array} Optional arguments to pass to any functions found in
  * the context. If a context property is a function, the token
  * will be replaced by the return from the function called with
  * these arguments.
  *
  * Returns:
  * {String} A string with tokens replaced from the context object.
  */
  format: function(template, context, args) {
  if(!context) {
  context = window;
  }
   
  // Example matching:
  // str = ${foo.bar}
  // match = foo.bar
  var replacer = function(str, match) {
  var replacement;
   
  // Loop through all subs. Example: ${a.b.c}
  // 0 -> replacement = context[a];
  // 1 -> replacement = context[a][b];
  // 2 -> replacement = context[a][b][c];
  var subs = match.split(/\.+/);
  for (var i=0; i< subs.length; i++) {
  if (i == 0) {
  replacement = context;
  }
   
  replacement = replacement[subs[i]];
  }
   
  if(typeof replacement == "function") {
  replacement = args ?
  replacement.apply(null, args) :
  replacement();
  }
   
  // If replacement is undefined, return the string 'undefined'.
  // This is a workaround for a bugs in browsers not properly
  // dealing with non-participating groups in regular expressions:
  // http://blog.stevenlevithan.com/archives/npcg-javascript
  if (typeof replacement == 'undefined') {
  return 'undefined';
  } else {
  return replacement;
  }
  };
   
  return template.replace(OpenLayers.String.tokenRegEx, replacer);
  },
   
  /**
  * Property: OpenLayers.String.tokenRegEx
  * Used to find tokens in a string.
  * Examples: ${a}, ${a.b.c}, ${a-b}, ${5}
  */
  tokenRegEx: /\$\{([\w.]+?)\}/g,
   
  /**
  * Property: OpenLayers.String.numberRegEx
  * Used to test strings as numbers.
  */
  numberRegEx: /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/,
   
  /**
  * APIFunction: OpenLayers.String.isNumeric
  * Determine whether a string contains only a numeric value.
  *
  * Examples:
  * (code)
  * OpenLayers.String.isNumeric("6.02e23") // true
  * OpenLayers.String.isNumeric("12 dozen") // false
  * OpenLayers.String.isNumeric("4") // true
  * OpenLayers.String.isNumeric(" 4 ") // false
  * (end)
  *
  * Returns:
  * {Boolean} String contains only a number.
  */
  isNumeric: function(value) {
  return OpenLayers.String.numberRegEx.test(value);
  },
   
  /**
  * APIFunction: numericIf
  * Converts a string that appears to be a numeric value into a number.
  *
  * Returns
  * {Number|String} a Number if the passed value is a number, a String
  * otherwise.
  */
  numericIf: function(value) {
  return OpenLayers.String.isNumeric(value) ? parseFloat(value) : value;
  }
   
  };
   
  if (!String.prototype.startsWith) {
  /**
  * APIMethod: String.startsWith
  * *Deprecated*. Whether or not a string starts with another string.
  *
  * Parameters:
  * sStart - {Sring} The string we're testing for.
  *
  * Returns:
  * {Boolean} Whether or not this string starts with the string passed in.
  */
  String.prototype.startsWith = function(sStart) {
  OpenLayers.Console.warn(OpenLayers.i18n("methodDeprecated",
  {'newMethod':'OpenLayers.String.startsWith'}));
  return OpenLayers.String.startsWith(this, sStart);
  };
  }
   
  if (!String.prototype.contains) {
  /**
  * APIMethod: String.contains
  * *Deprecated*. Whether or not a string contains another string.
  *
  * Parameters:
  * str - {String} The string that we're testing for.
  *
  * Returns:
  * {Boolean} Whether or not this string contains with the string passed in.
  */
  String.prototype.contains = function(str) {
  OpenLayers.Console.warn(OpenLayers.i18n("methodDeprecated",
  {'newMethod':'OpenLayers.String.contains'}));
  return OpenLayers.String.contains(this, str);
  };
  }
   
  if (!String.prototype.trim) {
  /**
  * APIMethod: String.trim
  * *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() {
  OpenLayers.Console.warn(OpenLayers.i18n("methodDeprecated",
  {'newMethod':'OpenLayers.String.trim'}));
  return OpenLayers.String.trim(this);
  };
  }
   
  if (!String.prototype.camelize) {
  /**
  * APIMethod: String.camelize
  * *Deprecated*. Camel-case a hyphenated string.
  * Ex. "chicken-head" becomes "chickenHead", and
  * "-chicken-head" becomes "ChickenHead".
  *
  * Returns:
  * {String} The string, camelized
  */
  String.prototype.camelize = function() {
  OpenLayers.Console.warn(OpenLayers.i18n("methodDeprecated",
  {'newMethod':'OpenLayers.String.camelize'}));
  return OpenLayers.String.camelize(this);
  };
  }
   
  /**
  * Namespace: OpenLayers.Number
  * Contains convenience functions for manipulating numbers.
  */
  OpenLayers.Number = {
   
  /**
  * Property: decimalSeparator
  * Decimal separator to use when formatting numbers.
  */
  decimalSeparator: ".",
   
  /**
  * Property: thousandsSeparator
  * Thousands separator to use when formatting numbers.
  */
  thousandsSeparator: ",",
   
  /**
  * APIFunction: limitSigDigs
  * Limit the number of significant digits on a float.
  *
  * Parameters:
  * num - {Float}
  * sig - {Integer}
  *
  * Returns:
  * {Float} The number, rounded to the specified number of significant
  * digits.
  */
  limitSigDigs: function(num, sig) {
  var fig = 0;
  if (sig > 0) {
  fig = parseFloat(num.toPrecision(sig));
  }
  return fig;
  },
   
  /**
  * APIFunction: format
  * Formats a number for output.
  *
  * Parameters:
  * num - {Float}
  * dec - {Integer} Number of decimal places to round to.
  * Defaults to 0. Set to null to leave decimal places unchanged.
  * tsep - {String} Thousands separator.
  * Default is ",".
  * dsep - {String} Decimal separator.
  * Default is ".".
  *
  * Returns:
  * {String} A string representing the formatted number.
  */
  format: function(num, dec, tsep, dsep) {
  dec = (typeof dec != "undefined") ? dec : 0;
  tsep = (typeof tsep != "undefined") ? tsep :
  OpenLayers.Number.thousandsSeparator;
  dsep = (typeof dsep != "undefined") ? dsep :
  OpenLayers.Number.decimalSeparator;
   
  if (dec != null) {
  num = parseFloat(num.toFixed(dec));
  }
   
  var parts = num.toString().split(".");
  if (parts.length == 1 && dec == null) {
  // integer where we do not want to touch the decimals
  dec = 0;
  }
   
  var integer = parts[0];
  if (tsep) {
  var thousands = /(-?[0-9]+)([0-9]{3})/;
  while(thousands.test(integer)) {
  integer = integer.replace(thousands, "$1" + tsep + "$2");
  }
  }
   
  var str;
  if (dec == 0) {
  str = integer;
  } else {
  var rem = parts.length > 1 ? parts[1] : "0";
  if (dec != null) {
  rem = rem + new Array(dec - rem.length + 1).join("0");
  }
  str = integer + dsep + rem;
  }
  return str;
  }
  };
   
  if (!Number.prototype.limitSigDigs) {
  /**
  * APIMethod: Number.limitSigDigs
  * *Deprecated*. Limit the number of significant digits on an integer. Does *not*
  * work with floats!
  *
  * Parameters:
  * sig - {Integer}
  *
  * Returns:
  * {Integer} The number, rounded to the specified number of significant digits.
  * If null, 0, or negative value passed in, returns 0