| 5 | | dojo.lang.mixin = function(obj, props){ |
| 6 | | var tobj = {}; |
| 7 | | for(var x in props){ |
| 8 | | if(typeof tobj[x] == "undefined" || tobj[x] != props[x]) { |
| 9 | | obj[x] = props[x]; |
| 10 | | } |
| 11 | | } |
| 12 | | // IE doesn't recognize custom toStrings in for..in |
| 13 | | if(dojo.render.html.ie && dojo.lang.isFunction(props["toString"]) && props["toString"] != obj["toString"]) { |
| 14 | | obj.toString = props.toString; |
| 15 | | } |
| 16 | | return obj; |
| 17 | | } |
| 18 | | |
| 19 | | dojo.lang.extend = function(ctor, props){ |
| 20 | | this.mixin(ctor.prototype, props); |
| 21 | | } |
| 22 | | |
| 23 | | dojo.lang.extendPrototype = function(obj, props){ |
| 24 | | this.extend(obj.constructor, props); |
| 25 | | } |
| 26 | | |
| 27 | | dojo.lang.anonCtr = 0; |
| 28 | | dojo.lang.anon = {}; |
| 29 | | dojo.lang.nameAnonFunc = function(anonFuncPtr, namespaceObj){ |
| 30 | | var nso = (namespaceObj || dojo.lang.anon); |
| 31 | | if((dj_global["djConfig"])&&(djConfig["slowAnonFuncLookups"] == true)){ |
| 32 | | for(var x in nso){ |
| 33 | | if(nso[x] === anonFuncPtr){ |
| 34 | | return x; |
| 35 | | } |
| 36 | | } |
| 37 | | } |
| 38 | | var ret = "__"+dojo.lang.anonCtr++; |
| 39 | | while(typeof nso[ret] != "undefined"){ |
| 40 | | ret = "__"+dojo.lang.anonCtr++; |
| 41 | | } |
| 42 | | nso[ret] = anonFuncPtr; |
| 43 | | return ret; |
| 44 | | } |
| 45 | | |
| 46 | | /** |
| 47 | | * Runs a function in a given scope (thisObject), can |
| 48 | | * also be used to preserve scope. |
| 49 | | * |
| 50 | | * hitch(foo, "bar"); // runs foo.bar() in the scope of foo |
| 51 | | * hitch(foo, myFunction); // runs myFunction in the scope of foo |
| 52 | | */ |
| 53 | | dojo.lang.hitch = function(thisObject, method) { |
| 54 | | if(dojo.lang.isString(method)) { |
| 55 | | var fcn = thisObject[method]; |
| 56 | | } else { |
| 57 | | var fcn = method; |
| 58 | | } |
| 59 | | |
| 60 | | return function() { |
| 61 | | return fcn.apply(thisObject, arguments); |
| 62 | | } |
| 63 | | } |
| 64 | | |
| 65 | | dojo.lang.forward = function(funcName){ |
| 66 | | // Returns a function that forwards a method call to this.func(...) |
| 67 | | return function(){ |
| 68 | | return this[funcName].apply(this, arguments); |
| 69 | | }; |
| 70 | | } |
| 71 | | |
| 72 | | dojo.lang.curry = function(ns, func /* args ... */){ |
| 73 | | var outerArgs = []; |
| 74 | | ns = ns||dj_global; |
| 75 | | if(dojo.lang.isString(func)){ |
| 76 | | func = ns[func]; |
| 77 | | } |
| 78 | | for(var x=2; x<arguments.length; x++){ |
| 79 | | outerArgs.push(arguments[x]); |
| 80 | | } |
| 81 | | var ecount = func.length - outerArgs.length; |
| 82 | | // borrowed from svend tofte |
| 83 | | function gather(nextArgs, innerArgs, expected){ |
| 84 | | var texpected = expected; |
| 85 | | var totalArgs = innerArgs.slice(0); // copy |
| 86 | | for(var x=0; x<nextArgs.length; x++){ |
| 87 | | totalArgs.push(nextArgs[x]); |
| 88 | | } |
| 89 | | // check the list of provided nextArgs to see if it, plus the |
| 90 | | // number of innerArgs already supplied, meets the total |
| 91 | | // expected. |
| 92 | | expected = expected-nextArgs.length; |
| 93 | | if(expected<=0){ |
| 94 | | var res = func.apply(ns, totalArgs); |
| 95 | | expected = texpected; |
| 96 | | return res; |
| 97 | | }else{ |
| 98 | | return function(){ |
| 99 | | return gather(arguments,// check to see if we've been run |
| 100 | | // with enough args |
| 101 | | totalArgs, // a copy |
| 102 | | expected); // how many more do we need to run?; |
| 103 | | } |
| 104 | | } |
| 105 | | } |
| 106 | | return gather([], outerArgs, ecount); |
| 107 | | } |
| 108 | | |
| 109 | | dojo.lang.curryArguments = function(ns, func, args, offset){ |
| 110 | | var targs = []; |
| 111 | | var x = offset||0; |
| 112 | | for(x=offset; x<args.length; x++){ |
| 113 | | targs.push(args[x]); // ensure that it's an arr |
| 114 | | } |
| 115 | | return dojo.lang.curry.apply(dojo.lang, [ns, func].concat(targs)); |
| 116 | | } |
| 117 | | |
| 118 | | /** |
| 119 | | * Sets a timeout in milliseconds to execute a function in a given context |
| 120 | | * with optional arguments. |
| 121 | | * |
| 122 | | * setTimeout (Object context, function func, number delay[, arg1[, ...]]); |
| 123 | | * setTimeout (function func, number delay[, arg1[, ...]]); |
| 124 | | */ |
| 125 | | dojo.lang.setTimeout = function(func, delay){ |
| 126 | | var context = window, argsStart = 2; |
| 127 | | if(!dojo.lang.isFunction(func)){ |
| 128 | | context = func; |
| 129 | | func = delay; |
| 130 | | delay = arguments[2]; |
| 131 | | argsStart++; |
| 132 | | } |
| 133 | | |
| 134 | | if(dojo.lang.isString(func)){ |
| 135 | | func = context[func]; |
| 136 | | } |
| 137 | | |
| 138 | | var args = []; |
| 139 | | for (var i = argsStart; i < arguments.length; i++) { |
| 140 | | args.push(arguments[i]); |
| 141 | | } |
| 142 | | return setTimeout(function () { func.apply(context, args); }, delay); |
| 143 | | } |
| 144 | | |
| 145 | | /** |
| 146 | | * Partial implmentation of is* functions from |
| 147 | | * http://www.crockford.com/javascript/recommend.html |
| 148 | | * NOTE: some of these may not be the best thing to use in all situations |
| 149 | | * as they aren't part of core JS and therefore can't work in every case. |
| 150 | | * See WARNING messages inline for tips. |
| 151 | | * |
| 152 | | * The following is* functions are fairly "safe" |
| 153 | | */ |
| 154 | | |
| 155 | | dojo.lang.isObject = function(wh) { |
| 156 | | return typeof wh == "object" || dojo.lang.isArray(wh) || dojo.lang.isFunction(wh); |
| 157 | | } |
| 158 | | |
| 159 | | dojo.lang.isArray = function(wh) { |
| 160 | | return (wh instanceof Array || typeof wh == "array"); |
| 161 | | } |
| 162 | | |
| 163 | | dojo.lang.isArrayLike = function(wh) { |
| 164 | | if(dojo.lang.isString(wh)){ return false; } |
| 165 | | if(dojo.lang.isFunction(wh)){ return false; } // keeps out built-in ctors (Number, String, ...) which have length properties |
| 166 | | if(dojo.lang.isArray(wh)){ return true; } |
| 167 | | if(typeof wh != "undefined" && wh |
| 168 | | && dojo.lang.isNumber(wh.length) && isFinite(wh.length)){ return true; } |
| 169 | | return false; |
| 170 | | } |
| 171 | | |
| 172 | | dojo.lang.isFunction = function(wh) { |
| 173 | | return (wh instanceof Function || typeof wh == "function"); |
| 174 | | } |
| 175 | | |
| 176 | | dojo.lang.isString = function(wh) { |
| 177 | | return (wh instanceof String || typeof wh == "string"); |
| 178 | | } |
| 179 | | |
| 180 | | dojo.lang.isAlien = function(wh) { |
| 181 | | return !dojo.lang.isFunction() && /\{\s*\[native code\]\s*\}/.test(String(wh)); |
| 182 | | } |
| 183 | | |
| 184 | | dojo.lang.isBoolean = function(wh) { |
| 185 | | return (wh instanceof Boolean || typeof wh == "boolean"); |
| 186 | | } |
| 187 | | |
| 188 | | /** |
| 189 | | * The following is***() functions are somewhat "unsafe". Fortunately, |
| 190 | | * there are workarounds the the language provides and are mentioned |
| 191 | | * in the WARNING messages. |
| 192 | | * |
| 193 | | * WARNING: In most cases, isNaN(wh) is sufficient to determine whether or not |
| 194 | | * something is a number or can be used as such. For example, a number or string |
| 195 | | * can be used interchangably when accessing array items (arr["1"] is the same as |
| 196 | | * arr[1]) and isNaN will return false for both values ("1" and 1). Should you |
| 197 | | * use isNumber("1"), that will return false, which is generally not too useful. |
| 198 | | * Also, isNumber(NaN) returns true, again, this isn't generally useful, but there |
| 199 | | * are corner cases (like when you want to make sure that two things are really |
| 200 | | * the same type of thing). That is really where isNumber "shines". |
| 201 | | * |
| 202 | | * RECOMMENDATION: Use isNaN(wh) when possible |
| 203 | | */ |
| 204 | | dojo.lang.isNumber = function(wh) { |
| 205 | | return (wh instanceof Number || typeof wh == "number"); |
| 206 | | } |
| 207 | | |
| 208 | | /** |
| 209 | | * WARNING: In some cases, isUndefined will not behave as you |
| 210 | | * might expect. If you do isUndefined(foo) and there is no earlier |
| 211 | | * reference to foo, an error will be thrown before isUndefined is |
| 212 | | * called. It behaves correctly if you scope yor object first, i.e. |
| 213 | | * isUndefined(foo.bar) where foo is an object and bar isn't a |
| 214 | | * property of the object. |
| 215 | | * |
| 216 | | * RECOMMENDATION: Use `typeof foo == "undefined"` when possible |
| 217 | | * |
| 218 | | * FIXME: Should isUndefined go away since it is error prone? |
| 219 | | */ |
| 220 | | dojo.lang.isUndefined = function(wh) { |
| 221 | | return ((wh == undefined)&&(typeof wh == "undefined")); |
| 222 | | } |
| 223 | | |
| 224 | | // end Crockford functions |
| 225 | | |
| 226 | | dojo.lang.whatAmI = function(wh) { |
| 227 | | try { |
| 228 | | if(dojo.lang.isArray(wh)) { return "array"; } |
| 229 | | if(dojo.lang.isFunction(wh)) { return "function"; } |
| 230 | | if(dojo.lang.isString(wh)) { return "string"; } |
| 231 | | if(dojo.lang.isNumber(wh)) { return "number"; } |
| 232 | | if(dojo.lang.isBoolean(wh)) { return "boolean"; } |
| 233 | | if(dojo.lang.isAlien(wh)) { return "alien"; } |
| 234 | | if(dojo.lang.isUndefined(wh)) { return "undefined"; } |
| 235 | | // FIXME: should this go first? |
| 236 | | for(var name in dojo.lang.whatAmI.custom) { |
| 237 | | if(dojo.lang.whatAmI.custom[name](wh)) { |
| 238 | | return name; |
| 239 | | } |
| 240 | | } |
| 241 | | if(dojo.lang.isObject(wh)) { return "object"; } |
| 242 | | } catch(E) {} |
| 243 | | return "unknown"; |
| 244 | | } |
| 245 | | /* |
| 246 | | * dojo.lang.whatAmI.custom[typeName] = someFunction |
| 247 | | * will return typeName is someFunction(wh) returns true |
| 248 | | */ |
| 249 | | dojo.lang.whatAmI.custom = {}; |
| 250 | | |
| 251 | | /** |
| 252 | | * Returns true for values that commonly represent numbers. |
| 253 | | * |
| 254 | | * Examples: |
| 255 | | * <pre> |
| 256 | | * dojo.lang.isNumeric(3); // returns true |
| 257 | | * dojo.lang.isNumeric("3"); // returns true |
| 258 | | * dojo.lang.isNumeric(new Number(3)); // returns true |
| 259 | | * dojo.lang.isNumeric(new String("3")); // returns true |
| 260 | | * |
| 261 | | * dojo.lang.isNumeric(3/0); // returns false |
| 262 | | * dojo.lang.isNumeric("foo"); // returns false |
| 263 | | * dojo.lang.isNumeric(new Number("foo")); // returns false |
| 264 | | * dojo.lang.isNumeric(false); // returns false |
| 265 | | * dojo.lang.isNumeric(true); // returns false |
| 266 | | * </pre> |
| 267 | | */ |
| 268 | | dojo.lang.isNumeric = function(wh){ |
| 269 | | return (!isNaN(wh) && isFinite(wh) && (wh != null) && |
| 270 | | !dojo.lang.isBoolean(wh) && !dojo.lang.isArray(wh)); |
| 271 | | } |
| 272 | | |
| 273 | | /** |
| 274 | | * Returns true for any literal, and for any object that is an |
| 275 | | * instance of a built-in type like String, Number, Boolean, |
| 276 | | * Array, Function, or Error. |
| 277 | | */ |
| 278 | | dojo.lang.isBuiltIn = function(wh){ |
| 279 | | return (dojo.lang.isArray(wh) || |
| 280 | | dojo.lang.isFunction(wh) || |
| 281 | | dojo.lang.isString(wh) || |
| 282 | | dojo.lang.isNumber(wh) || |
| 283 | | dojo.lang.isBoolean(wh) || |
| 284 | | (wh == null) || |
| 285 | | (wh instanceof Error) || |
| 286 | | (typeof wh == "error") ); |
| 287 | | } |
| 288 | | |
| 289 | | /** |
| 290 | | * Returns true for any object where the value of the |
| 291 | | * property 'constructor' is 'Object'. |
| 292 | | * |
| 293 | | * Examples: |
| 294 | | * <pre> |
| 295 | | * dojo.lang.isPureObject(new Object()); // returns true |
| 296 | | * dojo.lang.isPureObject({a: 1, b: 2}); // returns true |
| 297 | | * |
| 298 | | * dojo.lang.isPureObject(new Date()); // returns false |
| 299 | | * dojo.lang.isPureObject([11, 2, 3]); // returns false |
| 300 | | * </pre> |
| 301 | | */ |
| 302 | | dojo.lang.isPureObject = function(wh){ |
| 303 | | return ((wh != null) && dojo.lang.isObject(wh) && wh.constructor == Object); |
| 304 | | } |
| 305 | | |
| 306 | | /** |
| 307 | | * Given a value and a datatype, this method returns true if the |
| 308 | | * type of the value matches the datatype. The datatype parameter |
| 309 | | * can be an array of datatypes, in which case the method returns |
| 310 | | * true if the type of the value matches any of the datatypes. |
| 311 | | * |
| 312 | | * Examples: |
| 313 | | * <pre> |
| 314 | | * dojo.lang.isOfType("foo", String); // returns true |
| 315 | | * dojo.lang.isOfType(12345, Number); // returns true |
| 316 | | * dojo.lang.isOfType(false, Boolean); // returns true |
| 317 | | * dojo.lang.isOfType([6, 8], Array); // returns true |
| 318 | | * dojo.lang.isOfType(dojo.lang.isOfType, Function); // returns true |
| 319 | | * dojo.lang.isOfType({foo: "bar"}, Object); // returns true |
| 320 | | * dojo.lang.isOfType(new Date(), Date); // returns true |
| 321 | | * dojo.lang.isOfType(xxxxx, Date); // returns true |
| 322 | | * |
| 323 | | * dojo.lang.isOfType("foo", "string"); // returns true |
| 324 | | * dojo.lang.isOfType(12345, "number"); // returns true |
| 325 | | * dojo.lang.isOfType(false, "boolean"); // returns true |
| 326 | | * dojo.lang.isOfType([6, 8], "array"); // returns true |
| 327 | | * dojo.lang.isOfType(dojo.lang.isOfType, "function"); // returns true |
| 328 | | * dojo.lang.isOfType({foo: "bar"}, "object"); // returns true |
| 329 | | * dojo.lang.isOfType(xxxxx, "undefined"); // returns true |
| 330 | | * dojo.lang.isOfType(null, "null"); // returns true |
| 331 | | |
| 332 | | * dojo.lang.isOfType("foo", [Number, String, Boolean]); // returns true |
| 333 | | * dojo.lang.isOfType(12345, [Number, String, Boolean]); // returns true |
| 334 | | * dojo.lang.isOfType(false, [Number, String, Boolean]); // returns true |
| 335 | | * dojo.lang.isOfType(xxxxx, "undefined"); // returns true |
| 336 | | * </pre> |
| 337 | | * |
| 338 | | * @param value Any literal value or object instance. |
| 339 | | * @param type A class of object, or a literal type, or the string name of a type, or an array with a list of types. |
| 340 | | * @return Returns a boolean |
| 341 | | */ |
| 342 | | dojo.lang.isOfType = function(value, type) { |
| 343 | | if(dojo.lang.isArray(type)){ |
| 344 | | var arrayOfTypes = type; |
| 345 | | for(var i in arrayOfTypes){ |
| 346 | | var aType = arrayOfTypes[i]; |
| 347 | | if(dojo.lang.isOfType(value, aType)) { |
| 348 | | return true; |
| 349 | | } |
| 350 | | } |
| 351 | | return false; |
| 352 | | }else{ |
| 353 | | if(dojo.lang.isString(type)){ |
| 354 | | type = type.toLowerCase(); |
| 355 | | } |
| 356 | | switch (type) { |
| 357 | | case Array: |
| 358 | | case "array": |
| 359 | | return dojo.lang.isArray(value); |
| 360 | | break; |
| 361 | | case Function: |
| 362 | | case "function": |
| 363 | | return dojo.lang.isFunction(value); |
| 364 | | break; |
| 365 | | case String: |
| 366 | | case "string": |
| 367 | | return dojo.lang.isString(value); |
| 368 | | break; |
| 369 | | case Number: |
| 370 | | case "number": |
| 371 | | return dojo.lang.isNumber(value); |
| 372 | | break; |