vitro/webapp/web/src/lang/common.js

190 lines
5.6 KiB
JavaScript

/*
Copyright (c) 2004-2006, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.provide("dojo.lang.common");
dojo.require("dojo.lang");
/*
* Adds the given properties/methods to the specified object
*/
dojo.lang._mixin = function(obj, props){
var tobj = {};
for(var x in props){
// the "tobj" condition avoid copying properties in "props"
// inherited from Object.prototype. For example, if obj has a custom
// toString() method, don't overwrite it with the toString() method
// that props inherited from Object.protoype
if(typeof tobj[x] == "undefined" || tobj[x] != props[x]) {
obj[x] = props[x];
}
}
// IE doesn't recognize custom toStrings in for..in
if(dojo.render.html.ie && dojo.lang.isFunction(props["toString"]) && props["toString"] != obj["toString"]) {
obj.toString = props.toString;
}
return obj;
}
/*
* Adds the properties/methods of argument Objects to obj
*/
dojo.lang.mixin = function(obj, props /*, props, ..., props */){
for(var i=1, l=arguments.length; i<l; i++){
dojo.lang._mixin(obj, arguments[i]);
}
return obj;
}
/*
* Adds the properties/methods of argument Objects to ctor's prototype
*/
dojo.lang.extend = function(ctor /*function*/, props /*, props, ..., props */){
for(var i=1, l=arguments.length; i<l; i++){
dojo.lang._mixin(ctor.prototype, arguments[i]);
}
return ctor;
}
/**
* See if val is in arr. Call signatures:
* find(array, value, identity) // recommended
* find(value, array, identity)
**/
dojo.lang.find = function( /*Array*/ arr,
/*Object*/ val,
/*boolean*/ identity,
/*boolean*/ findLast){
// support both (arr, val) and (val, arr)
if(!dojo.lang.isArrayLike(arr) && dojo.lang.isArrayLike(val)) {
var a = arr;
arr = val;
val = a;
}
var isString = dojo.lang.isString(arr);
if(isString) { arr = arr.split(""); }
if(findLast) {
var step = -1;
var i = arr.length - 1;
var end = -1;
} else {
var step = 1;
var i = 0;
var end = arr.length;
}
if(identity){
while(i != end) {
if(arr[i] === val){ return i; }
i += step;
}
}else{
while(i != end) {
if(arr[i] == val){ return i; }
i += step;
}
}
return -1;
}
dojo.lang.indexOf = dojo.lang.find;
dojo.lang.findLast = function(/*Array*/ arr, /*Object*/ val, /*boolean*/ identity){
return dojo.lang.find(arr, val, identity, true);
}
dojo.lang.lastIndexOf = dojo.lang.findLast;
dojo.lang.inArray = function(arr /*Array*/, val /*Object*/){
return dojo.lang.find(arr, val) > -1; // return: boolean
}
/**
* Partial implmentation of is* functions from
* http://www.crockford.com/javascript/recommend.html
* NOTE: some of these may not be the best thing to use in all situations
* as they aren't part of core JS and therefore can't work in every case.
* See WARNING messages inline for tips.
*
* The following is* functions are fairly "safe"
*/
dojo.lang.isObject = function(wh){
if(typeof wh == "undefined"){ return false; }
return (typeof wh == "object" || wh === null || dojo.lang.isArray(wh) || dojo.lang.isFunction(wh));
}
dojo.lang.isArray = function(wh){
return (wh instanceof Array || typeof wh == "array");
}
dojo.lang.isArrayLike = function(wh){
if(dojo.lang.isString(wh)){ return false; }
if(dojo.lang.isFunction(wh)){ return false; } // keeps out built-in ctors (Number, String, ...) which have length properties
if(dojo.lang.isArray(wh)){ return true; }
if(typeof wh != "undefined" && wh
&& dojo.lang.isNumber(wh.length) && isFinite(wh.length)){ return true; }
return false;
}
dojo.lang.isFunction = function(wh){
if(!wh){ return false; }
return (wh instanceof Function || typeof wh == "function");
}
dojo.lang.isString = function(wh){
return (wh instanceof String || typeof wh == "string");
}
dojo.lang.isAlien = function(wh){
if(!wh){ return false; }
return !dojo.lang.isFunction() && /\{\s*\[native code\]\s*\}/.test(String(wh));
}
dojo.lang.isBoolean = function(wh){
return (wh instanceof Boolean || typeof wh == "boolean");
}
/**
* The following is***() functions are somewhat "unsafe". Fortunately,
* there are workarounds the the language provides and are mentioned
* in the WARNING messages.
*
* WARNING: In most cases, isNaN(wh) is sufficient to determine whether or not
* something is a number or can be used as such. For example, a number or string
* can be used interchangably when accessing array items (arr["1"] is the same as
* arr[1]) and isNaN will return false for both values ("1" and 1). Should you
* use isNumber("1"), that will return false, which is generally not too useful.
* Also, isNumber(NaN) returns true, again, this isn't generally useful, but there
* are corner cases (like when you want to make sure that two things are really
* the same type of thing). That is really where isNumber "shines".
*
* RECOMMENDATION: Use isNaN(wh) when possible
*/
dojo.lang.isNumber = function(wh){
return (wh instanceof Number || typeof wh == "number");
}
/**
* WARNING: In some cases, isUndefined will not behave as you
* might expect. If you do isUndefined(foo) and there is no earlier
* reference to foo, an error will be thrown before isUndefined is
* called. It behaves correctly if you scope yor object first, i.e.
* isUndefined(foo.bar) where foo is an object and bar isn't a
* property of the object.
*
* RECOMMENDATION: Use `typeof foo == "undefined"` when possible
*
* FIXME: Should isUndefined go away since it is error prone?
*/
dojo.lang.isUndefined = function(wh){
return ((wh == undefined)&&(typeof wh == "undefined"));
}
// end Crockford functions