3 MooTools - My Object Oriented JavaScript Tools.
9 Copyright (c) 2006-2008 [Valerio Proietti](http://mad4milk.net/).
12 [The MooTools production team](http://mootools.net/developers/).
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)
20 'version': '1.2.3dev',
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;
42 var family = name.toLowerCase();
43 object.prototype.$family = {name: family};
44 Native.typize(object, family);
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);
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);
59 for (var a in a1) this.alias(a, a1[a], a2);
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);
69 if (methods) object.implement(methods);
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);
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);
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);
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"]
102 for (var g in generics){
103 for (var i = generics[g].length; i--;) Native.genericize(natives[g], generics[g][i], true);
107 var Hash = new Native({
111 initialize: function(object){
112 if ($type(object) == 'hash') object = $unlink(object.getClean());
113 for (var key in object) this[key] = object[key];
121 forEach: function(fn, bind){
122 for (var key in this){
123 if (this.hasOwnProperty(key)) fn.call(bind, this[key], key, this);
127 getClean: function(){
129 for (var key in this){
130 if (this.hasOwnProperty(key)) clean[key] = this[key];
135 getLength: function(){
137 for (var key in this){
138 if (this.hasOwnProperty(key)) length++;
145 Hash.alias('forEach', 'each');
149 forEach: function(fn, bind){
150 for (var i = 0, l = this.length; i < l; i++) fn.call(bind, this[i], i, this);
155 Array.alias('forEach', 'each');
157 function $A(iterable){
159 var l = iterable.length, array = new Array(l);
160 while (l--) array[l] = iterable[l];
163 return Array.prototype.slice.call(iterable);
166 function $arguments(i){
173 return !!(obj || obj === 0);
176 function $clear(timer){
178 clearInterval(timer);
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);
193 function $extend(original, extended){
194 for (var key in (extended || {})) original[key] = extended[key];
199 return new Hash(object);
202 function $lambda(value){
203 return ($type(value) == 'function') ? value : function(){
209 var args = Array.slice(arguments);
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);
227 for (var i = 0, l = arguments.length; i < l; i++){
228 if (arguments[i] != undefined) return arguments[i];
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(){
247 for (var i = 0, l = arguments.length; i < l; i++){
249 return arguments[i]();
256 if (obj == undefined) return false;
257 if (obj.$family) return (obj.$family.name == 'number' && !isFinite(obj)) ? false : obj.$family.name;
259 switch (obj.nodeType){
260 case 1: return 'element';
261 case 3: return (/\S/).test(obj.nodeValue) ? 'textnode' : 'whitespace';
263 } else if (typeof obj.length == 'number'){
264 if (obj.callee) return 'arguments';
265 else if (obj.item) return 'collection';
270 function $unlink(object){
272 switch ($type(object)){
275 for (var p in object) unlinked[p] = $unlink(object[p]);
278 unlinked = new Hash(object);
282 for (var i = 0, l = object.length; i < l; i++) unlinked[i] = $unlink(object[i]);
284 default: return object;