- uploading 1.1 in tags
[mootools.git] / Core / Core.js
blob618d983875a1950d1cff9a3371bfa53a89c6e20f
1 /*
2 Script: Core.js
3         Mootools - My Object Oriented javascript.
5 License:
6         MIT-style license.
8 MooTools Copyright:
9         copyright (c) 2007 Valerio Proietti, <http://mad4milk.net>
11 MooTools Credits:
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.
17 var MooTools = {
18         'version': 1.1
22 Function: $defined
23         Returns true if the passed in value/object is defined, that means is not null or undefined.
25 Arguments:
26         obj - object to inspect
29 function $defined(obj){
30         return (obj != undefined);
34 Function: $type
35         Returns the type of object that matches the element passed in.
37 Arguments:
38         obj - the object to inspect.
40 Example:
41         >var myString = 'hello';
42         >$type(myString); //returns "string"
44 Returns:
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.
61 function $type(obj){
62         if (!$defined(obj)) return false;
63         if (obj.htmlElement) return 'element';
64         var type = typeof obj;
65         if (type == 'object' && obj.nodeName){
66                 switch(obj.nodeType){
67                         case 1: return 'element';
68                         case 3: return /\S/.test(obj.nodeValue) ? 'textnode' : 'whitespace';
69                 }
70         }
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';
76                 }
77                 if (typeof obj.length == 'number'){
78                         if (obj.item) return 'collection';
79                         if (obj.callee) return 'arguments';
80                 }
81         }
82         return type;
86 Function: $merge
87         merges a number of objects recursively without referencing them or their sub-objects.
89 Arguments:
90         any number of objects.
92 Example:
93         >var mergedObj = $merge(obj1, obj2, obj3);
94         >//obj1, obj2, and obj3 are unaltered
97 function $merge(){
98         var mix = {};
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;
105                 }
106         }
107         return mix;
111 Function: $extend
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.
115 Example:
116         (start code)
117         var firstOb = {
118                 'name': 'John',
119                 'lastName': 'Doe'
120         };
121         var secondOb = {
122                 'age': '20',
123                 'sex': 'male',
124                 'lastName': 'Dorian'
125         };
126         $extend(firstOb, secondOb);
127         //firstOb will become:
128         {
129                 'name': 'John',
130                 'lastName': 'Dorian',
131                 'age': '20',
132                 'sex': 'male'
133         };
134         (end)
136 Returns:
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];
144         return args[0];
148 Function: $native
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.
153 Arguments:
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);
164                         }
165                 };
166         }
169 $native.generic = function(prop){
170         return function(bind){
171                 return this.prototype[prop].apply(bind, Array.prototype.slice.call(arguments, 1));
172         };
175 $native(Function, Array, String, Number);
178 Class: Abstract
179         Abstract class, to be used as singleton. Will add .extend to any object
181 Arguments:
182         an object
184 Returns:
185         the object with an .extend property, equivalent to <$extend>.
188 var Abstract = function(obj){
189         obj = obj || {};
190         obj.extend = $extend;
191         return obj;
194 //window, document
196 var Window = new Abstract(window);
197 var Document = new Abstract(document);
198 document.head = document.getElementsByTagName('head')[0];
200 /* Section: Utility Functions */
203 Function: $chk
204         Returns true if the passed in value/object exists or is 0, otherwise returns false.
205         Useful to accept zeroes.
207 Arguments:
208         obj - object to inspect
211 function $chk(obj){
212         return !!(obj || obj === 0);
216 Function: $pick
217         Returns the first object if defined, otherwise returns the second.
219 Arguments:
220         obj - object to test
221         picked - the default to return
223 Example:
224         (start code)
225                 function say(msg){
226                         alert($pick(msg, 'no meessage supplied'));
227                 }
228         (end)
231 function $pick(obj, picked){
232         return $defined(obj) ? obj : picked;
236 Function: $random
237         Returns a random integer number between the two passed in values.
239 Arguments:
240         min - integer, the minimum value (inclusive).
241         max - integer, the maximum value (inclusive).
243 Returns:
244         a random integer between min and max.
247 function $random(min, max){
248         return Math.floor(Math.random() * (max - min + 1) + min);
252 Function: $time
253         Returns the current timestamp
255 Returns:
256         a timestamp integer.
259 function $time(){
260         return new Date().getTime();
264 Function: $clear
265         clears a timeout or an Interval.
267 Returns:
268         null
270 Arguments:
271         timer - the setInterval or setTimeout to clear.
273 Example:
274         >var myTimer = myFunction.delay(5000); //wait 5 seconds and execute my function.
275         >myTimer = $clear(myTimer); //nevermind
277 See also:
278         <Function.delay>, <Function.periodical>
281 function $clear(timer){
282         clearTimeout(timer);
283         clearInterval(timer);
284         return null;
288 Class: window
289         Some properties are attached to the window object by the browser detection.
291 Properties:
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;
307 //htmlelement
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){};