| 1 | dojo.provide("dojo.lang.func"); |
|---|
| 2 | |
|---|
| 3 | dojo.require("dojo.lang.common"); |
|---|
| 4 | |
|---|
| 5 | /** |
|---|
| 6 | * Runs a function in a given scope (thisObject), can |
|---|
| 7 | * also be used to preserve scope. |
|---|
| 8 | * |
|---|
| 9 | * hitch(foo, "bar"); // runs foo.bar() in the scope of foo |
|---|
| 10 | * hitch(foo, myFunction); // runs myFunction in the scope of foo |
|---|
| 11 | */ |
|---|
| 12 | dojo.lang.hitch = function(thisObject, method){ |
|---|
| 13 | var fcn = (dojo.lang.isString(method) ? thisObject[method] : method) || function(){}; |
|---|
| 14 | |
|---|
| 15 | return function() { |
|---|
| 16 | return fcn.apply(thisObject, arguments); |
|---|
| 17 | }; |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | dojo.lang.anonCtr = 0; |
|---|
| 21 | dojo.lang.anon = {}; |
|---|
| 22 | dojo.lang.nameAnonFunc = function(anonFuncPtr, namespaceObj, searchForNames){ |
|---|
| 23 | var nso = (namespaceObj || dojo.lang.anon); |
|---|
| 24 | if( (searchForNames) || |
|---|
| 25 | ((dj_global["djConfig"])&&(djConfig["slowAnonFuncLookups"] == true)) ){ |
|---|
| 26 | for(var x in nso){ |
|---|
| 27 | try{ |
|---|
| 28 | if(nso[x] === anonFuncPtr){ |
|---|
| 29 | return x; |
|---|
| 30 | } |
|---|
| 31 | }catch(e){} // window.external fails in IE embedded in Eclipse (Eclipse bug #151165) |
|---|
| 32 | } |
|---|
| 33 | } |
|---|
| 34 | var ret = "__"+dojo.lang.anonCtr++; |
|---|
| 35 | while(typeof nso[ret] != "undefined"){ |
|---|
| 36 | ret = "__"+dojo.lang.anonCtr++; |
|---|
| 37 | } |
|---|
| 38 | nso[ret] = anonFuncPtr; |
|---|
| 39 | return ret; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | dojo.lang.forward = function(funcName){ |
|---|
| 43 | // Returns a function that forwards a method call to this.func(...) |
|---|
| 44 | return function(){ |
|---|
| 45 | return this[funcName].apply(this, arguments); |
|---|
| 46 | }; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | dojo.lang.curry = function(ns, func /* args ... */){ |
|---|
| 50 | var outerArgs = []; |
|---|
| 51 | ns = ns||dj_global; |
|---|
| 52 | if(dojo.lang.isString(func)){ |
|---|
| 53 | func = ns[func]; |
|---|
| 54 | } |
|---|
| 55 | for(var x=2; x<arguments.length; x++){ |
|---|
| 56 | outerArgs.push(arguments[x]); |
|---|
| 57 | } |
|---|
| 58 | // since the event system replaces the original function with a new |
|---|
| 59 | // join-point runner with an arity of 0, we check to see if it's left us |
|---|
| 60 | // any clues about the original arity in lieu of the function's actual |
|---|
| 61 | // length property |
|---|
| 62 | var ecount = (func["__preJoinArity"]||func.length) - outerArgs.length; |
|---|
| 63 | // borrowed from svend tofte |
|---|
| 64 | function gather(nextArgs, innerArgs, expected){ |
|---|
| 65 | var texpected = expected; |
|---|
| 66 | var totalArgs = innerArgs.slice(0); // copy |
|---|
| 67 | for(var x=0; x<nextArgs.length; x++){ |
|---|
| 68 | totalArgs.push(nextArgs[x]); |
|---|
| 69 | } |
|---|
| 70 | // check the list of provided nextArgs to see if it, plus the |
|---|
| 71 | // number of innerArgs already supplied, meets the total |
|---|
| 72 | // expected. |
|---|
| 73 | expected = expected-nextArgs.length; |
|---|
| 74 | if(expected<=0){ |
|---|
| 75 | var res = func.apply(ns, totalArgs); |
|---|
| 76 | expected = texpected; |
|---|
| 77 | return res; |
|---|
| 78 | }else{ |
|---|
| 79 | return function(){ |
|---|
| 80 | return gather(arguments,// check to see if we've been run |
|---|
| 81 | // with enough args |
|---|
| 82 | totalArgs, // a copy |
|---|
| 83 | expected); // how many more do we need to run?; |
|---|
| 84 | }; |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | return gather([], outerArgs, ecount); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | dojo.lang.curryArguments = function(ns, func, args, offset){ |
|---|
| 91 | var targs = []; |
|---|
| 92 | var x = offset||0; |
|---|
| 93 | for(x=offset; x<args.length; x++){ |
|---|
| 94 | targs.push(args[x]); // ensure that it's an arr |
|---|
| 95 | } |
|---|
| 96 | return dojo.lang.curry.apply(dojo.lang, [ns, func].concat(targs)); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | dojo.lang.tryThese = function(){ |
|---|
| 100 | for(var x=0; x<arguments.length; x++){ |
|---|
| 101 | try{ |
|---|
| 102 | if(typeof arguments[x] == "function"){ |
|---|
| 103 | var ret = (arguments[x]()); |
|---|
| 104 | if(ret){ |
|---|
| 105 | return ret; |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | }catch(e){ |
|---|
| 109 | dojo.debug(e); |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | dojo.lang.delayThese = function(farr, cb, delay, onend){ |
|---|
| 115 | /** |
|---|
| 116 | * alternate: (array funcArray, function callback, function onend) |
|---|
| 117 | * alternate: (array funcArray, function callback) |
|---|
| 118 | * alternate: (array funcArray) |
|---|
| 119 | */ |
|---|
| 120 | if(!farr.length){ |
|---|
| 121 | if(typeof onend == "function"){ |
|---|
| 122 | onend(); |
|---|
| 123 | } |
|---|
| 124 | return; |
|---|
| 125 | } |
|---|
| 126 | if((typeof delay == "undefined")&&(typeof cb == "number")){ |
|---|
| 127 | delay = cb; |
|---|
| 128 | cb = function(){}; |
|---|
| 129 | }else if(!cb){ |
|---|
| 130 | cb = function(){}; |
|---|
| 131 | if(!delay){ delay = 0; } |
|---|
| 132 | } |
|---|
| 133 | setTimeout(function(){ |
|---|
| 134 | (farr.shift())(); |
|---|
| 135 | cb(); |
|---|
| 136 | dojo.lang.delayThese(farr, cb, delay, onend); |
|---|
| 137 | }, delay); |
|---|
| 138 | } |
|---|