]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/_base/lang.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojo / _base / lang.js
1 if(!dojo._hasResource["dojo._base.lang"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo._base.lang"] = true;
3 dojo.provide("dojo._base.lang");
4
5 // Crockford (ish) functions
6
7 dojo.isString = function(/*anything*/ it){
8         //      summary:
9         //              Return true if it is a String
10         return !!arguments.length && it != null && (typeof it == "string" || it instanceof String); // Boolean
11 }
12
13 dojo.isArray = function(/*anything*/ it){
14         //      summary:
15         //              Return true if it is an Array
16         return it && (it instanceof Array || typeof it == "array"); // Boolean
17 }
18
19 /*=====
20 dojo.isFunction = function(it){
21         // summary: Return true if it is a Function
22         // it: anything
23         //      return: Boolean
24 }
25 =====*/
26
27 dojo.isFunction = (function(){
28         var _isFunction = function(/*anything*/ it){
29                 return it && (typeof it == "function" || it instanceof Function); // Boolean
30         };
31
32         return dojo.isSafari ?
33                 // only slow this down w/ gratuitious casting in Safari since it's what's b0rken
34                 function(/*anything*/ it){
35                         if(typeof it == "function" && it == "[object NodeList]"){ return false; }
36                         return _isFunction(it); // Boolean
37                 } : _isFunction;
38 })();
39
40 dojo.isObject = function(/*anything*/ it){
41         // summary: 
42         //              Returns true if it is a JavaScript object (or an Array, a Function
43         //              or null)
44         return it !== undefined &&
45                 (it === null || typeof it == "object" || dojo.isArray(it) || dojo.isFunction(it)); // Boolean
46 }
47
48 dojo.isArrayLike = function(/*anything*/ it){
49         //      summary:
50         //              similar to dojo.isArray() but more permissive
51         //      description:
52         //              Doesn't strongly test for "arrayness".  Instead, settles for "isn't
53         //              a string or number and has a length property". Arguments objects
54         //              and DOM collections will return true when passed to
55         //              dojo.isArrayLike(), but will return false when passed to
56         //              dojo.isArray().
57         //      return:
58         //              If it walks like a duck and quicks like a duck, return `true`
59         var d = dojo;
60         return it && it !== undefined &&
61                 // keep out built-in constructors (Number, String, ...) which have length
62                 // properties
63                 !d.isString(it) && !d.isFunction(it) &&
64                 !(it.tagName && it.tagName.toLowerCase() == 'form') &&
65                 (d.isArray(it) || isFinite(it.length)); // Boolean
66 }
67
68 dojo.isAlien = function(/*anything*/ it){
69         // summary: 
70         //              Returns true if it is a built-in function or some other kind of
71         //              oddball that *should* report as a function but doesn't
72         return it && !dojo.isFunction(it) && /\{\s*\[native code\]\s*\}/.test(String(it)); // Boolean
73 }
74
75 dojo.extend = function(/*Object*/ constructor, /*Object...*/ props){
76         // summary:
77         //              Adds all properties and methods of props to constructor's
78         //              prototype, making them available to all instances created with
79         //              constructor.
80         for(var i=1, l=arguments.length; i<l; i++){
81                 dojo._mixin(constructor.prototype, arguments[i]);
82         }
83         return constructor; // Object
84 }
85
86 dojo._hitchArgs = function(scope, method /*,...*/){
87         var pre = dojo._toArray(arguments, 2);
88         var named = dojo.isString(method);
89         return function(){
90                 // arrayify arguments
91                 var args = dojo._toArray(arguments);
92                 // locate our method
93                 var f = named ? (scope||dojo.global)[method] : method;
94                 // invoke with collected args
95                 return f && f.apply(scope || this, pre.concat(args)); // mixed
96         } // Function
97 }
98
99 dojo.hitch = function(/*Object*/scope, /*Function|String*/method /*,...*/){
100         //      summary: 
101         //              Returns a function that will only ever execute in the a given scope. 
102         //              This allows for easy use of object member functions
103         //              in callbacks and other places in which the "this" keyword may
104         //              otherwise not reference the expected scope. 
105         //              Any number of default positional arguments may be passed as parameters 
106         //              beyond "method".
107         //              Each of these values will be used to "placehold" (similar to curry)
108         //              for the hitched function. 
109         //      scope: 
110         //              The scope to use when method executes. If method is a string, 
111         //              scope is also the object containing method.
112         //      method:
113         //              A function to be hitched to scope, or the name of the method in
114         //              scope to be hitched.
115         //      example:
116         //      |       dojo.hitch(foo, "bar")(); 
117         //              runs foo.bar() in the scope of foo
118         //      example:
119         //      |       dojo.hitch(foo, myFunction);
120         //              returns a function that runs myFunction in the scope of foo
121         if(arguments.length > 2){
122                 return dojo._hitchArgs.apply(dojo, arguments); // Function
123         }
124         if(!method){
125                 method = scope;
126                 scope = null;
127         }
128         if(dojo.isString(method)){
129                 scope = scope || dojo.global;
130                 if(!scope[method]){ throw(['dojo.hitch: scope["', method, '"] is null (scope="', scope, '")'].join('')); }
131                 return function(){ return scope[method].apply(scope, arguments || []); }; // Function
132         }
133         return !scope ? method : function(){ return method.apply(scope, arguments || []); }; // Function
134 }
135
136 /*=====
137 dojo.delegate = function(obj, props){
138         //      summary:
139         //              returns a new object which "looks" to obj for properties which it
140         //              does not have a value for. Optionally takes a bag of properties to
141         //              seed the returned object with initially. 
142         //      description:
143         //              This is a small implementaton of the Boodman/Crockford delegation
144         //              pattern in JavaScript. An intermediate object constructor mediates
145         //              the prototype chain for the returned object, using it to delegate
146         //              down to obj for property lookup when object-local lookup fails.
147         //              This can be thought of similarly to ES4's "wrap", save that it does
148         //              not act on types but rather on pure objects.
149         //      obj:
150         //              The object to delegate to for properties not found directly on the
151         //              return object or in props.
152         //      props:
153         //              an object containing properties to assign to the returned object
154         //      returns:
155         //              an Object of anonymous type
156         //      example:
157         //      |       var foo = { bar: "baz" };
158         //      |       var thinger = dojo.delegate(foo, { thud: "xyzzy"});
159         //      |       thinger.bar == "baz"; // delegated to foo
160         //      |       foo.thud == undefined; // by definition
161         //      |       thinger.thud == "xyzzy"; // mixed in from props
162         //      |       foo.bar = "thonk";
163         //      |       thinger.bar == "thonk"; // still delegated to foo's bar
164 }
165 =====*/
166
167
168 dojo.delegate = dojo._delegate = function(obj, props){
169
170         // boodman/crockford delegation
171         function TMP(){};
172         TMP.prototype = obj;
173         var tmp = new TMP();
174         if(props){
175                 dojo.mixin(tmp, props);
176         }
177         return tmp; // Object
178 }
179
180 dojo.partial = function(/*Function|String*/method /*, ...*/){
181         //      summary:
182         //              similar to hitch() except that the scope object is left to be
183         //              whatever the execution context eventually becomes.
184         //      description:
185         //              Calling dojo.partial is the functional equivalent of calling:
186         //              |       dojo.hitch(null, funcName, ...);
187         var arr = [ null ];
188         return dojo.hitch.apply(dojo, arr.concat(dojo._toArray(arguments))); // Function
189 }
190
191 dojo._toArray = function(/*Object*/obj, /*Number?*/offset, /*Array?*/ startWith){
192         //      summary:
193         //              Converts an array-like object (i.e. arguments, DOMCollection) to an
194         //              array. Returns a new Array with the elements of obj.
195         //      obj:
196         //              the object to "arrayify". We expect the object to have, at a
197         //              minimum, a length property which corresponds to integer-indexed
198         //              properties.
199         //      offset:
200         //              the location in obj to start iterating from. Defaults to 0.
201         //              Optional.
202         //      startWith:
203         //              An array to pack with the properties of obj. If provided,
204         //              properties in obj are appended at the end of startWith and
205         //              startWith is the returned array.
206         var arr = startWith||[];
207         for(var x = offset || 0; x < obj.length; x++){
208                 arr.push(obj[x]);
209         }
210         return arr; // Array
211 }
212
213 dojo.clone = function(/*anything*/ o){
214         // summary:
215         //              Clones objects (including DOM nodes) and all children.
216         //              Warning: do not clone cyclic structures.
217         if(!o){ return o; }
218         if(dojo.isArray(o)){
219                 var r = [];
220                 for(var i = 0; i < o.length; ++i){
221                         r.push(dojo.clone(o[i]));
222                 }
223                 return r; // Array
224         }
225         if(!dojo.isObject(o)){
226                 return o;       /*anything*/
227         }
228         if(o.nodeType && o.cloneNode){ // isNode
229                 return o.cloneNode(true); // Node
230         }
231         if(o instanceof Date){
232                 return new Date(o.getTime());   // Date
233         }
234         // Generic objects
235         var r = new o.constructor(); // specific to dojo.declare()'d classes!
236         for(var i in o){
237                 if(!(i in r) || r[i] != o[i]){
238                         r[i] = dojo.clone(o[i]);
239                 }
240         }
241         return r; // Object
242 }
243
244 dojo.trim = function(/*String*/ str){
245         // summary: 
246         //              trims whitespaces from both sides of the string
247         // description:
248         //              This version of trim() was selected for inclusion into the base due
249         //              to its compact size and relatively good performance (see Steven
250         //              Levithan's blog:
251         //              http://blog.stevenlevithan.com/archives/faster-trim-javascript).
252         //              The fastest but longest version of this function is located at
253         //              dojo.string.trim()
254         return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); // String
255 }
256
257 }