3 Mootools - My Object Oriented javascript.
9 copyright (c) 2007 Valerio Proietti, <http://mad4milk.net>
12 - Class is slightly based on Base.js <http://dean.edwards.name/weblog/2006/03/base/> (c) 2006 Dean Edwards, License <http://creativecommons.org/licenses/LGPL/2.1/>
13 - Some functions are inspired by those found in prototype.js <http://prototype.conio.net/> (c) 2005 Sam Stephenson sam [at] conio [dot] net, MIT-style license
14 - Documentation by Aaron Newton (aaron.newton [at] cnet [dot] com) and Valerio Proietti.
23 Returns true if the passed in value/object is defined, that means is not null or undefined.
26 obj - object to inspect
29 function $defined(obj){
30 return (obj != undefined);
35 Returns the type of object that matches the element passed in.
38 obj - the object to inspect.
41 >var myString = 'hello';
42 >$type(myString); //returns "string"
45 'element' - if obj is a DOM element node
46 'textnode' - if obj is a DOM text node
47 'whitespace' - if obj is a DOM whitespace node
48 'arguments' - if obj is an arguments object
49 'object' - if obj is an object
50 'string' - if obj is a string
51 'number' - if obj is a number
52 'boolean' - if obj is a boolean
53 'function' - if obj is a function
54 'regexp' - if obj is a regular expression
55 'class' - if obj is a Class. (created with new Class, or the extend of another class).
56 'arguments' - if obj is the arguments object.
57 'collection' - if obj is a native htmlelements collection, such as childNodes, getElementsByTagName .. etc.
58 false - (boolean) if the object is not defined or none of the above.
62 if (!$defined(obj)) return false;
63 if (obj.htmlElement) return 'element';
64 var type = typeof obj;
65 if (type == 'object' && obj.nodeName){
67 case 1: return 'element';
68 case 3: return /\S/.test(obj.nodeValue) ? 'textnode' : 'whitespace';
71 if (type == 'object' || type == 'function'){
72 switch(obj.constructor){
73 case Array: return 'array';
74 case RegExp: return 'regexp';
75 case Class: return 'class';
77 if (typeof obj.length == 'number'){
78 if (obj.item) return 'collection';
79 if (obj.callee) return 'arguments';
87 merges a number of objects recursively without referencing them or their sub-objects.
90 any number of objects.
93 >var mergedObj = $merge(obj1, obj2, obj3);
94 >//obj1, obj2, and obj3 are unaltered
99 for (var i = 0; i < arguments.length; i++){
100 for (var property in arguments[i]){
101 var ap = arguments[i][property];
102 var mp = mix[property];
103 if (mp && $type(ap) == 'object' && $type(mp) == 'object') mix[property] = $merge(mp, ap);
104 else mix[property] = ap;
112 Copies all the properties from the second passed object to the first passed Object.
113 If you do myWhatever.extend = $extend the first parameter will become myWhatever, and your extend function will only need one parameter.
126 $extend(firstOb, secondOb);
127 //firstOb will become:
130 'lastName': 'Dorian',
137 The first object, extended.
140 var $extend = Object.extend = function(){
141 var args = arguments;
142 if (!args[1]) args = [this, args[0]];
143 for (var property in args[1]) args[0][property] = args[1][property];
149 Will add a .extend method to the objects passed as a parameter, but the property passed in will be copied to the object's prototype only if non previously existent.
150 Its handy if you dont want the .extend method of an object to overwrite existing methods.
151 Used automatically in mootools to implement Array/String/Function/Number methods to browser that dont support them whitout manual checking.
154 a number of classes/native javascript objects
158 var $native = Object.Native = function(){
159 for (var i = 0, l = arguments.length; i < l; i++){
160 arguments[i].extend = function(props){
161 for (var prop in props){
162 if (!this.prototype[prop]) this.prototype[prop] = props[prop];
163 if (!this[prop]) this[prop] = $native.generic(prop);
169 $native.generic = function(prop){
170 return function(bind){
171 return this.prototype[prop].apply(bind, Array.prototype.slice.call(arguments, 1));
175 $native(Function, Array, String, Number);
179 Abstract class, to be used as singleton. Will add .extend to any object
185 the object with an .extend property, equivalent to <$extend>.
188 var Abstract = function(obj){
190 obj.extend = $extend;
196 var Window = new Abstract(window);
197 var Document = new Abstract(document);
198 document.head = document.getElementsByTagName('head')[0];
200 /* Section: Utility Functions */
204 Returns true if the passed in value/object exists or is 0, otherwise returns false.
205 Useful to accept zeroes.
208 obj - object to inspect
212 return !!(obj || obj === 0);
217 Returns the first object if defined, otherwise returns the second.
221 picked - the default to return
226 alert($pick(msg, 'no meessage supplied'));
231 function $pick(obj, picked){
232 return $defined(obj) ? obj : picked;
237 Returns a random integer number between the two passed in values.
240 min - integer, the minimum value (inclusive).
241 max - integer, the maximum value (inclusive).
244 a random integer between min and max.
247 function $random(min, max){
248 return Math.floor(Math.random() * (max - min + 1) + min);
253 Returns the current timestamp
260 return new Date().getTime();
265 clears a timeout or an Interval.
271 timer - the setInterval or setTimeout to clear.
274 >var myTimer = myFunction.delay(5000); //wait 5 seconds and execute my function.
275 >myTimer = $clear(myTimer); //nevermind
278 <Function.delay>, <Function.periodical>
281 function $clear(timer){
283 clearInterval(timer);
289 Some properties are attached to the window object by the browser detection.
292 window.ie - will be set to true if the current browser is internet explorer (any).
293 window.ie6 - will be set to true if the current browser is internet explorer 6.
294 window.ie7 - will be set to true if the current browser is internet explorer 7.
295 window.gecko - will be set to true if the current browser is Mozilla/Gecko.
296 window.webkit - will be set to true if the current browser is Safari/Konqueror.
297 window.webkit419 - will be set to true if the current browser is Safari2 / webkit till version 419.
298 window.webkit420 - will be set to true if the current browser is Safari3 (Webkit SVN Build) / webkit over version 419.
299 window.opera - is set to true by opera itself.
302 window.xpath = !!(document.evaluate);
303 if (window.ActiveXObject) window.ie = window[window.XMLHttpRequest ? 'ie7' : 'ie6'] = true;
304 else if (document.childNodes && !document.all && !navigator.taintEnabled) window.khtml = window.webkit = window[window.xpath ? 'webkit420' : 'webkit419'] = true;
305 else if (document.getBoxObjectFor != null) window.gecko = true;
309 if (typeof HTMLElement == 'undefined'){
310 var HTMLElement = function(){};
311 if (window.webkit) document.createElement("iframe"); //fixes safari
312 HTMLElement.prototype = (window.webkit) ? window["[[DOMElement.prototype]]"] : {};
314 HTMLElement.prototype.htmlElement = true;
316 //enables background image cache for internet explorer 6
318 if (window.ie6) try {document.execCommand("BackgroundImageCache", false, true);} catch(e){};