Roxen.git / server / modules / js-support / scripts / roxen / lib / lib.js

version» Context lines:

Roxen.git/server/modules/js-support/scripts/roxen/lib/lib.js:12:    */    array2object: function (a) {    var res = {};    for (var i = 0; i < a.length; i++) {    res[a[i]] = true;    }    return res;    },       /** +  * Returns array with unique entries. +  * @method arrayUnique +  * @param {array} a The array. +  * @return {array} A new array with duplicates removed. +  */ +  arrayUnique: function(a) { +  // Requires the JavaScript 1.6/ECMAScript 5 filter function. +  // Credit to <http://stackoverflow.com/a/14438954>. +  return a.filter(function(value, index) { +  return a.indexOf(value) === index; +  }); +  }, +  +  /** +  * Returns the object keys as an array. +  * @method indices +  * @param {Object} o The array. +  * @return {Array} The result. +  */ +  indices: function(o) { +  var res = [ ]; +  for (var key in o) +  if (o.hasOwnProperty(key)) +  res.push(key); +  return res; +  }, +  +  /**    * Binds a function to a scope.    * @method bind    * @param {Object} o Scope.    * @param {Function} f Function.    * @return {Function} The scoped function.    */    bind: function (o, f) {    return function() {    return f.apply(o, arguments);    };
Roxen.git/server/modules/js-support/scripts/roxen/lib/lib.js:36:    * @method clone    * @param {Object} o Object to clone.    * @return {Object} The cloned object.    */    clone: function (o) {    function F() {}    F.prototype = o;    return new F();    },    +  /* +  * Performs a recursive value comparison of JS values. +  * @method deepCompare +  * @param {any} x The first value. +  * @param {any} y The second value. +  * @return {Boolean} Equality result. +  */ +  deepCompare: function(x, y) { +  // Adopted from Jean Vincent's answer at +  // stackoverflow.com/questions/1068834/object-comparison-in-javascript +  // +  // NOTE: Does not handle NaN, cyclical structures or +/-0. +  +  // If both x and y are null or undefined and exactly the same +  if (x === y) return true; +  +  // If they are not strictly equal, they both need to be Objects +  if (!(x instanceof Object) || !(y instanceof Object)) return false; +  +  // They must have the exact same prototype chain, the closest we can do +  // is test there constructor. +  if (x.constructor !== y.constructor) return false; +  +  for (var p in x) { +  // Other properties were tested using x.constructor === y.constructor +  if (!x.hasOwnProperty(p)) continue; +  +  // Allows to compare x[p] and y[p] when set to undefined +  if (!y.hasOwnProperty(p)) return false; +  +  // If they have the same strict value or identity then they are equal +  if (x[p] === y[p]) continue; +  +  // Numbers, Strings, Functions, Booleans must be strictly equal +  if (typeof(x[p]) !== "object") return false; +  +  // Objects and Arrays must be tested recursively +  if (!deepCompare(x[p], y[p])) return false; +  } +  +  for (p in y) { +  // Allows x[p] to be set to undefined +  if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) return false; +  } +  +  return true; +  }, +     /**    * Performs a deep copy of an object or array.    * @method deepCopy    * @param {Object} obj The array/object to deep copy.    * @return {Object} The copy.    */    deepCopy: function(obj) {    // Borrowed from    // http://james.padolsey.com/javascript/deep-copying-of-objects-and-arrays/    //    // FIXME: Replace with clone() in YUI3?    var out, i, len;    if (Object.prototype.toString.call(obj) === "[object Array]") {    out = [ ];    len = obj.length;    for (i = 0 ; i < len; i++)    out[i] = arguments.callee(obj[i]);    return out; -  +  } else if (obj === null) { +  return null;    } else if (typeof obj === "object") {    out = { };    for (i in obj)    out[i] = arguments.callee(obj[i]);    return out;    }    return obj;    },       /**
Roxen.git/server/modules/js-support/scripts/roxen/lib/lib.js:126:       /**    * Outputs string to Roxen Webserver debug log.    * @method debugLog    * @param {String} s The string.    */    debugLog: function (s) {    ROXEN.AFS.call("debug-log", {message: s});    },    +  escape: function (s) { +  // We would love to use the "encodeURIComponent" function, but it +  // encodes utf8 without using the %uXXXX standard encoding, which +  // makes it unsuitable for reception on the server side. This +  // implementation of escape uses %uXXXX, but we need to post process +  // the '+' character since it's not handled at all by escape. +  return escape(s).replace(/\+/g, "%2B"); +  }, +     /**    * Escapes a string for use as an URI component. Like    * encodeURIComponent, but also encodes ! ' ( ) * which are part    * of the reserved set in RFC 3987.    *    * @method escape    * @param {String} s String to encode.    * @return {String} The result.    */    escapeURIComponent: function (s) {
Roxen.git/server/modules/js-support/scripts/roxen/lib/lib.js:174:    },       /**    * Filter an array by executing a filter function on each array element    * and return the result.    * @method filter    * @param {Array} a Array of elements.    * @param {Function} f Applied function to each element.    * f's argument list is (element, index, array).    * @param {Object} o Scope correction. -  * @retrun {Array} The result. +  * @return {Array} The result.    */    filter: function (a, f, o) {    var result = [];    for (var i = 0; i < a.length; i++) {    var r = f.call(o, a[i], i, a);    if (r) {    result.push(a[i]);    }    }    return result;    },       /** -  +  * Returns the parent path for a full path by stripping off the trailing +  * segment. The result will always end in "/". +  * +  * @method dirname +  * @param {String} path The input path. +  * @return {string} The result. +  */ +  dirname: function(path) { +  if (!path || path === "") +  return "/"; +  if (path.charAt(path.length - 1) === "/") +  return path; +  +  // Strip the trailing file name from a path and return the directory +  var segments = path.split("/"); +  segments.pop(); +  return segments.join("/") + "/"; +  }, +  +  /** +  * Returns the name excluding parent levels in full path by stripping off +  * preceding segments separated by "/". +  * +  * @method basename +  * @param {String} path The input path. +  * @return {string} The result. +  */ +  basename: function(path) { +  if (!path || path === "") +  return ""; +  var slash_pos = path.lastIndexOf("/"); +  if (slash_pos >= 0) +  return path.substr(slash_pos + 1); +  return path; +  }, +  +  /**    * Decode string from UTF8.    * @method fromUTF8    * @param {String} s String to decode.    * @return {String} The result.    */    fromUTF8: function (s) {    return decodeURIComponent(escape(s));    },       /**
Roxen.git/server/modules/js-support/scripts/roxen/lib/lib.js:264:    }    return false;    },       /**    * Outputs all arguments to client log.    * @method log    */    log: function () {    if (typeof console !== "undefined" && console.log) { -  var args = [ ROXEN.getISOTimeString() + ": " ]. -  concat(Array.prototype.slice.call(arguments)); -  console.log.apply(console, args); +  console.log.apply(console, ROXEN.formatLog(arguments));    }    }, -  +  info: function () { +  if (typeof console !== "undefined" && console.info) { +  console.info.apply(console, ROXEN.formatLog(arguments)); +  } +  }, +  warn: function () { +  if (typeof console !== "undefined" && console.warn) { +  console.warn.apply(console, ROXEN.formatLog(arguments)); +  } +  }, +  error: function () { +  if (typeof console !== "undefined" && console.error) { +  console.error.apply(console, ROXEN.formatLog(arguments)); +  } +  },    -  +  formatLog: function (args) { +  return [ ROXEN.getISOTimeString() + " test (UTC): " ]. +  concat(Array.prototype.slice.call(args)); +  }, +     /**    * Determines wheather or not the provided object is an array.    * This an alias for YAHOO.lang.isArray().    * @method isArray    * @param {any} o The object being testing    * @return {boolean} the result    */    isArray: YAHOO.lang.isArray,       /**
Roxen.git/server/modules/js-support/scripts/roxen/lib/lib.js:426:    a.push(b.join(","));    }    else {    a.push(ROXEN.escapeURIComponent(args[i]));    }    }    }    return a.join("");    },    +  getQueryVariable: function (uri, variable) { +  var query = uri.split("?"); +  if (query.length < 2) return false; +  query = query[1]; +  var vars = query.split("&"); +  for (var i = 0; i < vars.length; i++) { +  var pair = vars[i].split("="); +  if(pair[0] === variable) { +  return pair[1]; +  } +  } +  return false; +  }, +     /**    * Trims whitespaces from the end of a given string.    * @method rtrim    * @param {String} s The string to trim.    * @return {String} The result.    */    rtrim: function (s) {    return s.replace(/\s+$/g, "");    },       /**    * Encode string to UTF8.    * @method toUTF8    * @param {String} s String to encode.    * @return {String} The result.    */    toUTF8: function (s) {    return unescape(encodeURIComponent(s));    },    -  +  /* +  * Given a count number it returns either the singluar or plural phrase, +  * or optionally the zero count phrase. +  * @method count_inflection +  * @param {Number} num The count number to base the decision on. +  * @param {String} singular The singular phrase if count is 1. +  * @param {String} plural The plural phrase if count is not 1 (including +  * zero if opt_zero isn't available). +  * @param {String} opt_zero The optional phrase to return for zero count. +  */ +  count_inflection: function(num, singular, plural, opt_zero) +  { +  if ((num === 0) && opt_zero) +  return opt_zero; +  return (num == 1) ? singular : plural; +  }, +     /**    * Trims whitespaces from both the beginning and end of a given string.    * @method trim    * @param {String} s The string to trim.    * @return {String} The result.    */    trim: function (s) {    return s.replace(/^\s+|\s+$/g, "");    },   
Roxen.git/server/modules/js-support/scripts/roxen/lib/lib.js:537:    toggle_logger: function () {    if (this.logger_visible) {    this.logger.hide();    this.logger_visible = false;    } else {    this.logger.show();    this.logger_visible = true;    }    },    +  weekStartsOnSunday: function() { +  // Date.getDay() returns locally-unaware 0-6 for Sun-Sat for a given +  // date. We can therefore only make a guess based on browser locale +  // extracted from the language setting. +  // +  // C.f. <http://en.wikipedia.org/wiki/Seven-day_week#mediaviewer/File:First_Day_of_Week_World_Map.svg>. +  // (We don't list every single tiny country on the map below.) +  var week_starts_on_sun = +  [ "us", "cn", "jp", "ca", // US, China, Japan, Canada, +  "za", "zw", "ke", // South Africa, Zimbabwe, Kenya, +  "ph", "tw", "hk", // Philippines, Taiwan, Hong Kong +  "mx", "gt", "sv", // Mexico, Guatemala, El Salvador +  "ni", "cr", "pa", // Nicaragua, Costa Rica, Panama +  "co", "ve", "ec", // Colombia, Venezuela, Ecuador +  "pe", "br", "bo", // Peru, Brazil, Bolivia +  "cl", "ar", "il" ]; // Chile, Argentina, Israel +  var lang = navigator.language.toLowerCase(); +  if (lang.length >= 5) { +  var country = lang.substr(3, 2); +  if (week_starts_on_sun.indexOf(country) >= 0) +  return true; +  } +  return false; +  }, +  +  shortDate: function(unixtime) { +  var tm = new Date(unixtime * 1000); +  var locale = "en-US"; +  var date_fmt = "%b %e, %Y"; +  return YAHOO.util.Date.format(tm, { format: date_fmt }, +  locale).replace(" ", " "); +  }, +     /**    * Return a short readable date and time string similar to    * Sitebuilder.mtime_to_str().    * @method shortDateTime    * @param timestamp {Int or Date}    */    shortDateTime: function (timestamp, force_include_time, am_pm) {    var tm;    if (typeof (timestamp) === "number") tm = new Date(timestamp*1000);    else tm = timestamp;