| /* |
| * |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| * |
| */ |
| |
| var utils = exports; |
| |
| /** |
| * Defines a property getter for obj[key]. |
| */ |
| utils.defineGetter = function(obj, key, func) { |
| if (Object.defineProperty) { |
| Object.defineProperty(obj, key, { get: func }); |
| } else { |
| obj.__defineGetter__(key, func); |
| } |
| }; |
| |
| utils.arrayIndexOf = function(a, item) { |
| if (a.indexOf) { |
| return a.indexOf(item); |
| } |
| var len = a.length; |
| for (var i = 0; i < len; ++i) { |
| if (a[i] == item) { |
| return i; |
| } |
| } |
| return -1; |
| }; |
| |
| /** |
| * Returns whether the item was found in the array. |
| */ |
| utils.arrayRemove = function(a, item) { |
| var index = utils.arrayIndexOf(a, item); |
| if (index != -1) { |
| a.splice(index, 1); |
| } |
| return index != -1; |
| }; |
| |
| /** |
| * Returns an indication of whether the argument is an array or not |
| */ |
| utils.isArray = function(a) { |
| return Object.prototype.toString.call(a) == '[object Array]'; |
| }; |
| |
| /** |
| * Returns an indication of whether the argument is a Date or not |
| */ |
| utils.isDate = function(d) { |
| return Object.prototype.toString.call(d) == '[object Date]'; |
| }; |
| |
| /** |
| * Does a deep clone of the object. |
| */ |
| utils.clone = function(obj) { |
| if(!obj || typeof obj == 'function' || utils.isDate(obj) || typeof obj != 'object') { |
| return obj; |
| } |
| |
| var retVal, i; |
| |
| if(utils.isArray(obj)){ |
| retVal = []; |
| for(i = 0; i < obj.length; ++i){ |
| retVal.push(utils.clone(obj[i])); |
| } |
| return retVal; |
| } |
| |
| retVal = {}; |
| for(i in obj){ |
| if(!(i in retVal) || retVal[i] != obj[i]) { |
| retVal[i] = utils.clone(obj[i]); |
| } |
| } |
| return retVal; |
| }; |
| |
| /** |
| * Returns a wrapped version of the function |
| */ |
| utils.close = function(context, func, params) { |
| if (typeof params == 'undefined') { |
| return function() { |
| return func.apply(context, arguments); |
| }; |
| } else { |
| return function() { |
| return func.apply(context, params); |
| }; |
| } |
| }; |
| |
| /** |
| * Create a UUID |
| */ |
| utils.createUUID = function() { |
| return UUIDcreatePart(4) + '-' + |
| UUIDcreatePart(2) + '-' + |
| UUIDcreatePart(2) + '-' + |
| UUIDcreatePart(2) + '-' + |
| UUIDcreatePart(6); |
| }; |
| |
| /** |
| * Extends a child object from a parent object using classical inheritance |
| * pattern. |
| */ |
| utils.extend = (function() { |
| // proxy used to establish prototype chain |
| var F = function() {}; |
| // extend Child from Parent |
| return function(Child, Parent) { |
| F.prototype = Parent.prototype; |
| Child.prototype = new F(); |
| Child.__super__ = Parent.prototype; |
| Child.prototype.constructor = Child; |
| }; |
| }()); |
| |
| /** |
| * Alerts a message in any available way: alert or console.log. |
| */ |
| utils.alert = function(msg) { |
| if (window.alert) { |
| window.alert(msg); |
| } else if (console && console.log) { |
| console.log(msg); |
| } |
| }; |
| |
| /** |
| * Formats a string and arguments following it ala sprintf() |
| * |
| * see utils.vformat() for more information |
| */ |
| utils.format = function(formatString /* ,... */) { |
| var args = [].slice.call(arguments, 1); |
| return utils.vformat(formatString, args); |
| }; |
| |
| /** |
| * Formats a string and arguments following it ala vsprintf() |
| * |
| * format chars: |
| * %j - format arg as JSON |
| * %o - format arg as JSON |
| * %c - format arg as '' |
| * %% - replace with '%' |
| * any other char following % will format it's |
| * arg via toString(). |
| * |
| * for rationale, see FireBug's Console API: |
| * http://getfirebug.com/wiki/index.php/Console_API |
| */ |
| utils.vformat = function(formatString, args) { |
| if (formatString === null || formatString === undefined) return ""; |
| if (arguments.length == 1) return formatString.toString(); |
| if (typeof formatString != "string") return formatString.toString(); |
| |
| var pattern = /(.*?)%(.)(.*)/; |
| var rest = formatString; |
| var result = []; |
| |
| while (args.length) { |
| var arg = args.shift(); |
| var match = pattern.exec(rest); |
| |
| if (!match) break; |
| |
| rest = match[3]; |
| |
| result.push(match[1]); |
| |
| if (match[2] == '%') { |
| result.push('%'); |
| args.unshift(arg); |
| continue; |
| } |
| |
| result.push(formatted(arg, match[2])); |
| } |
| |
| result.push(rest); |
| |
| return result.join(''); |
| }; |
| |
| //------------------------------------------------------------------------------ |
| function UUIDcreatePart(length) { |
| var uuidpart = ""; |
| for (var i=0; i<length; i++) { |
| var uuidchar = parseInt((Math.random() * 256), 10).toString(16); |
| if (uuidchar.length == 1) { |
| uuidchar = "0" + uuidchar; |
| } |
| uuidpart += uuidchar; |
| } |
| return uuidpart; |
| } |
| |
| //------------------------------------------------------------------------------ |
| function formatted(object, formatChar) { |
| |
| try { |
| switch(formatChar) { |
| case 'j': |
| case 'o': return JSON.stringify(object); |
| case 'c': return ''; |
| } |
| } |
| catch (e) { |
| return "error JSON.stringify()ing argument: " + e; |
| } |
| |
| if ((object === null) || (object === undefined)) { |
| return Object.prototype.toString.call(object); |
| } |
| |
| return object.toString(); |
| } |