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