- got rid of a strict warning.
[mootools/dkf.git] / Source / Core / Core.js
blob86355fe9aec18077a32f38cb59a67926f23727ae
1 /*
2 Script: Core.js
3         MooTools - My Object Oriented JavaScript Tools.
5 License:
6         MIT-style license.
8 Copyright:
9         Copyright (c) 2006-2008 [Valerio Proietti](http://mad4milk.net/).
11 Code & Documentation:
12         [The MooTools production team](http://mootools.net/developers/).
14 Inspiration:
15         - Class implementation inspired by [Base.js](http://dean.edwards.name/weblog/2006/03/base/) Copyright (c) 2006 Dean Edwards, [GNU Lesser General Public License](http://opensource.org/licenses/lgpl-license.php)
16         - Some functionality inspired by [Prototype.js](http://prototypejs.org) Copyright (c) 2005-2007 Sam Stephenson, [MIT License](http://opensource.org/licenses/mit-license.php)
19 var MooTools = {
20         'version': '1.2.3dev',
21         'build': '%build%'
24 var Native = function(options){
25         options = options || {};
26         var name = options.name;
27         var legacy = options.legacy;
28         var protect = options.protect;
29         var methods = options.implement;
30         var generics = options.generics;
31         var initialize = options.initialize;
32         var afterImplement = options.afterImplement || function(){};
33         var object = initialize || legacy;
34         generics = generics !== false;
36         object.constructor = Native;
37         object.$family = {name: 'native'};
38         if (legacy && initialize) object.prototype = legacy.prototype;
39         object.prototype.constructor = object;
41         if (name){
42                 var family = name.toLowerCase();
43                 object.prototype.$family = {name: family};
44                 Native.typize(object, family);
45         }
47         var add = function(obj, name, method, force){
48                 if (!protect || force || !obj.prototype[name]) obj.prototype[name] = method;
49                 if (generics) Native.genericize(obj, name, protect);
50                 afterImplement.call(obj, name, method);
51                 return obj;
52         };
54         object.alias = function(a1, a2, a3){
55                 if (typeof a1 == 'string'){
56                         var pa1 = this.prototype[a1];
57                         if ((a1 = pa1)) return add(this, a2, a1, a3);
58                 }
59                 for (var a in a1) this.alias(a, a1[a], a2);
60                 return this;
61         };
63         object.implement = function(a1, a2, a3){
64                 if (typeof a1 == 'string') return add(this, a1, a2, a3);
65                 for (var p in a1) add(this, p, a1[p], a2);
66                 return this;
67         };
69         if (methods) object.implement(methods);
71         return object;
74 Native.genericize = function(object, property, check){
75         if ((!check || !object[property]) && typeof object.prototype[property] == 'function') object[property] = function(){
76                 var args = Array.prototype.slice.call(arguments);
77                 return object.prototype[property].apply(args.shift(), args);
78         };
81 Native.implement = function(objects, properties){
82         for (var i = 0, l = objects.length; i < l; i++) objects[i].implement(properties);
85 Native.typize = function(object, family){
86         if (!object.type) object.type = function(item){
87                 return ($type(item) === family);
88         };
91 (function(){
92         var natives = {'Array': Array, 'Date': Date, 'Function': Function, 'Number': Number, 'RegExp': RegExp, 'String': String};
93         for (var n in natives) new Native({name: n, initialize: natives[n], protect: true});
95         var types = {'boolean': Boolean, 'native': Native, 'object': Object};
96         for (var t in types) Native.typize(types[t], t);
98         var generics = {
99                 'Array': ["concat", "indexOf", "join", "lastIndexOf", "pop", "push", "reverse", "shift", "slice", "sort", "splice", "toString", "unshift", "valueOf"],
100                 'String': ["charAt", "charCodeAt", "concat", "indexOf", "lastIndexOf", "match", "replace", "search", "slice", "split", "substr", "substring", "toLowerCase", "toUpperCase", "valueOf"]
101         };
102         for (var g in generics){
103                 for (var i = generics[g].length; i--;) Native.genericize(natives[g], generics[g][i], true);
104         }
105 })();
107 var Hash = new Native({
109         name: 'Hash',
111         initialize: function(object){
112                 if ($type(object) == 'hash') object = $unlink(object.getClean());
113                 for (var key in object) this[key] = object[key];
114                 return this;
115         }
119 Hash.implement({
121         forEach: function(fn, bind){
122                 for (var key in this){
123                         if (this.hasOwnProperty(key)) fn.call(bind, this[key], key, this);
124                 }
125         },
127         getClean: function(){
128                 var clean = {};
129                 for (var key in this){
130                         if (this.hasOwnProperty(key)) clean[key] = this[key];
131                 }
132                 return clean;
133         },
135         getLength: function(){
136                 var length = 0;
137                 for (var key in this){
138                         if (this.hasOwnProperty(key)) length++;
139                 }
140                 return length;
141         }
145 Hash.alias('forEach', 'each');
147 Array.implement({
149         forEach: function(fn, bind){
150                 for (var i = 0, l = this.length; i < l; i++) fn.call(bind, this[i], i, this);
151         }
155 Array.alias('forEach', 'each');
157 function $A(iterable){
158         if (iterable.item){
159                 var l = iterable.length, array = new Array(l);
160                 while (l--) array[l] = iterable[l];
161                 return array;
162         }
163         return Array.prototype.slice.call(iterable);
166 function $arguments(i){
167         return function(){
168                 return arguments[i];
169         };
172 function $chk(obj){
173         return !!(obj || obj === 0);
176 function $clear(timer){
177         clearTimeout(timer);
178         clearInterval(timer);
179         return null;
182 function $defined(obj){
183         return (obj != undefined);
186 function $each(iterable, fn, bind){
187         var type = $type(iterable);
188         ((type == 'arguments' || type == 'collection' || type == 'array') ? Array : Hash).each(iterable, fn, bind);
191 function $empty(){};
193 function $extend(original, extended){
194         for (var key in (extended || {})) original[key] = extended[key];
195         return original;
198 function $H(object){
199         return new Hash(object);
202 function $lambda(value){
203         return ($type(value) == 'function') ? value : function(){
204                 return value;
205         };
208 function $merge(){
209         var args = Array.slice(arguments);
210         args.unshift({});
211         return $mixin.apply(null, args);
214 function $mixin(mix){
215         for (var i = 1, l = arguments.length; i < l; i++){
216                 var object = arguments[i];
217                 if ($type(object) != 'object') continue;
218                 for (var key in object){
219                         var op = object[key], mp = mix[key];
220                         mix[key] = (mp && $type(op) == 'object' && $type(mp) == 'object') ? $mixin(mp, op) : $unlink(op);
221                 }
222         }
223         return mix;
226 function $pick(){
227         for (var i = 0, l = arguments.length; i < l; i++){
228                 if (arguments[i] != undefined) return arguments[i];
229         }
230         return null;
233 function $random(min, max){
234         return Math.floor(Math.random() * (max - min + 1) + min);
237 function $splat(obj){
238         var type = $type(obj);
239         return (type) ? ((type != 'array' && type != 'arguments') ? [obj] : obj) : [];
242 var $time = Date.now || function(){
243         return +new Date;
246 function $try(){
247         for (var i = 0, l = arguments.length; i < l; i++){
248                 try {
249                         return arguments[i]();
250                 } catch(e){}
251         }
252         return null;
255 function $type(obj){
256         if (obj == undefined) return false;
257         if (obj.$family) return (obj.$family.name == 'number' && !isFinite(obj)) ? false : obj.$family.name;
258         if (obj.nodeName){
259                 switch (obj.nodeType){
260                         case 1: return 'element';
261                         case 3: return (/\S/).test(obj.nodeValue) ? 'textnode' : 'whitespace';
262                 }
263         } else if (typeof obj.length == 'number'){
264                 if (obj.callee) return 'arguments';
265                 else if (obj.item) return 'collection';
266         }
267         return typeof obj;
270 function $unlink(object){
271         var unlinked;
272         switch ($type(object)){
273                 case 'object':
274                         unlinked = {};
275                         for (var p in object) unlinked[p] = $unlink(object[p]);
276                 break;
277                 case 'hash':
278                         unlinked = new Hash(object);
279                 break;
280                 case 'array':
281                         unlinked = [];
282                         for (var i = 0, l = object.length; i < l; i++) unlinked[i] = $unlink(object[i]);
283                 break;
284                 default: return object;
285         }
286         return unlinked;