百度首页 | 百度空间
 
查看文章
 
moolite for Extjs 2.0
2007-12-10 21:12

function $defined(obj){
   return (obj != undefined);
};

function $lambda(value){
   return (typeof value == 'function') ? value : function(){
       return value;
   };
};

function $type(obj){
   if (obj == undefined) return false;
   if (obj.$family) return ( obj.$family == 'number' && !isFinite(obj)) ? false : obj.$family;
   if (obj.nodeName){
       switch (obj.nodeType){
           case 1: return 'element';
           case 3: return (/\S/).test(obj.nodeValue) ? 'textnode' : 'whitespace';
       }
   } else if (typeof obj.length == 'number'){
       return (obj.callee) ? 'arguments' : 'collection';
   }
   return typeof obj;
};

function $extend(original, extended){
   for (var key in (extended || {})) original[key] = extended[key];
   return original;
};

function $unlink(object){
   if ($type(object) != 'object') return object;
   var unlinked = {};
   for (var p in object) unlinked[p] = $unlink(object[p]);
   return unlinked;
};

function $merge(){
   var mix = {};
   for (var i = 0, l = arguments.length; i < l; i++){
       var object = arguments[i];
       if ($type(object) != 'object') continue;
       for (var key in object){
           var op = object[key], mp = mix[key];
           mix[key] = (mp && $type(op) == 'object' && $type(mp) == 'object') ? $merge(mp, op) : $unlink(op);
       }
   }
   return mix;
};

var Native = function(name, object){
   object.constructor = Native;
   object.prototype.constructor = object;
   object.prototype.$family   = name.toLowerCase();
   object.implement = function(properties){
       for (var property in properties) {
           if( !object.prototype[property] ){
               object.prototype[property]   = properties[property];
               Native.genericize(object,property, true );
           }          
       }
       return this;
   };

   return object;
};


Native.genericize = function(object, property, check){
   if ((!check || !object[property]) && typeof object.prototype[property] == 'function') object[property] = function(){
       var args = Array.prototype.slice.call(arguments);
       return object.prototype[property].apply(args.shift(), args);
   };
};

(function(objects){
   for (var name in objects) {
       new Native(name, objects[name]);
   }
})({'String': String, 'Function': Function, 'Number': Number, 'Array': Array, 'RegExp': RegExp, 'Date': Date});

(function(object, methods){
   for (var i = 0, l = methods.length; i < l; i++) Native.genericize(object, methods[i], true);
   return arguments.callee;
})
(Array, ['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift', 'concat', 'join', 'slice', 'toString', 'valueOf', 'indexOf', 'lastIndexOf'])
(String, ['charAt', 'charCodeAt', 'concat', 'indexOf', 'lastIndexOf', 'match', 'replace', 'search', 'slice', 'split', 'substr', 'substring', 'toLowerCase', 'toUpperCase', 'valueOf']);


var Hash = new Native( 'Hash', function(object){
       if( typeof(object) != 'object' ) object = {};
       if ( object.constructor === Hash ) return object;
       for (var key in object){
           if (!this[key]) this[key] = object[key];
       }
       return this;
});


Hash.implement({

   getLength: function(){
       var length = 0;
       for (var key in this){
           if (this.hasOwnProperty(key)) length++;
       }
       return length;
   },

   each: function(fn, bind){
       for (var key in this){
           if (this.hasOwnProperty(key)) fn.call(bind, this[key], key, this);
       }
   },

  
   has: Object.prototype.hasOwnProperty,

   keyOf: function(value){
       for (var key in this){
           if (this.hasOwnProperty(key) && this[key] === value) return key;
       }
       return null;
   },

   hasValue: function(value){
       return (Hash.keyOf(this, value) !== null);
   },

   extend: function(properties){
       Hash.each(properties, function(value, key){
           Hash.set(this, key, value);
       }, this);
       return this;
   },

   merge: function(properties){
       Hash.each(properties, function(value, key){
           Hash.include(this, key, value);
       }, this);
       return this;
   },

   remove: function(key){
       if (this.hasOwnProperty(key)) delete this[key];
       return this;
   },

   get: function(key){
       return (this.hasOwnProperty(key)) ? this[key] : null;
   },

   set: function(key, value){
       if (!this[key] || this.hasOwnProperty(key)) this[key] = value;
       return this;
   },

   empty: function(){
       Hash.each(this, function(value, key){
           delete this[key];
       }, this);
       return this;
   },

   include: function(key, value){
       var k = this[key];
       if (!$defined(k)) this[key] = value;
       return this;
   },

   map: function(fn, bind){
       var results = new Hash;
       Hash.each(this, function(value, key){
           results.set(key, fn.call(bind, value, key, this));
       }, this);
       return results;
   },

   filter: function(fn, bind){
       var results = new Hash;
       Hash.each(this, function(value, key){
           if (fn.call(bind, value, key, this)) results.set(key, value);
       }, this);
       return results;
   },

   every: function(fn, bind){
       for (var key in this){
           if (this.hasOwnProperty(key) && !fn.call(bind, this[key], key)) return false;
       }
       return true;
   },

   some: function(fn, bind){
       for (var key in this){
           if (this.hasOwnProperty(key) && fn.call(bind, this[key], key)) return true;
       }
       return false;
   },

   getKeys: function(){
       var keys = [];
       Hash.each(this, function(value, key){
           keys.push(key);
       });
       return keys;
   },

   getValues: function(){
       var values = [];
       Hash.each(this, function(value){
           values.push(value);
       });
       return values;
   },

   toString: function(){
       return Json.encode(this);
   }

});



Array.implement({

   forEach: function(fn, bind){
       for (var i = 0, l = this.length; i < l; i++) fn.call(bind, this[i], i, this);
   },

   every: function(fn, bind){
       for (var i = 0, l = this.length; i < l; i++){
           if (!fn.call(bind, this[i], i, this)) return false;
       }
       return true;
   },

   filter: function(fn, bind){
       var results = [];
       for (var i = 0, l = this.length; i < l; i++){
           if (fn.call(bind, this[i], i, this)) results.push(this[i]);
       }
       return results;
   },

   indexOf: function(item, from){
       var len = this.length;
       for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++){
           if (this[i] === item) return i;
       }
       return -1;
   },

   map: function(fn, bind){
       var results = [];
       for (var i = 0, l = this.length; i < l; i++) results[i] = fn.call(bind, this[i], i, this);
       return results;
   },

   some: function(fn, bind){
       for (var i = 0, l = this.length; i < l; i++){
           if (fn.call(bind, this[i], i, this)) return true;
       }
       return false;
   },

   associate: function(keys){
       var obj = {}, length = Math.min(this.length, keys.length);
       for (var i = 0; i < length; i++) obj[keys[i]] = this[i];
       return obj;
   },

   link: function(object){
       var result = {};
       for (var i = 0, l = this.length; i < l; i++){
           for (var key in object){
               if ( object[key] === $type(this[i]) ){
                   result[key] = this[i];
                   delete object[key];
                   break;
               }
           }
       }
       return result;
   },

   contains: function(item, from){
       return this.indexOf(item, from) != -1;
   },

   getLast: function(){
       return (this.length) ? this[this.length - 1] : null;
   },

   include: function(item){
       if (!this.contains(item)) this.push(item);
       return this;
   },

   extend: function(array){
       for (var i = 0, j = array.length; i < j; i++) this.push(array[i]);
       return this;
   },

   merge: function(array){
       for (var i = 0, l = array.length; i < l; i++) this.include(array[i]);
       return this;
   },

   remove: function(item){
       for (var i = this.length; i--; i){
           if (this[i] === item) this.splice(i, 1);
       }
       return this;
   },

   empty: function(){
       this.length = 0;
       return this;
   }

});

String.implement({
   escapeRegExp: function(){
       return this.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1');
   },
   trim: function(){
       return this.replace(/^\s+|\s+$/g, '');
   }
});

var Json = new Hash({

   encode: function(obj){
       switch ($type(obj)){
           case 'string':
               return '"' + obj.replace(/[\x00-\x1f\\"]/g, Json.$replaceChars) + '"';
           case 'array':
               return '[' + String(obj.map(Json.encode).filter($defined)) + ']';
           case 'object': case 'hash':
               var string = [];
               Hash.each(obj, function(value, key){
                   var json = Json.encode(value);
                   if (json) string.push(Json.encode(key) + ':' + json);
               });
               return '{' + String(string) + '}';
           case 'number': case 'boolean': return String(obj);
           case false: return 'null';
       }
       return null;
   },

   $specialChars: {'\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\'},

   $replaceChars: function(chr){
       return Json.$specialChars[chr] || '\\u00' + Math.floor(chr.charCodeAt() / 16).toString(16) + (chr.charCodeAt() % 16).toString(16);
   },

   decode: function(string, secure){
       if ($type(string) != 'string' || !string.length) return null;
       if (secure && !(/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(string.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''))) return null;
       return eval('(' + string + ')');
   }

});



var Cookie = {

   options: {
       path: false,
       domain: false,
       duration: false,
       secure: false
   },

   set: function(key, value, options){
       options = $merge(this.options, options);
       value = encodeURIComponent(value);
       if (options.domain) value += '; domain=' + options.domain;
       if (options.path) value += '; path=' + options.path;
       if (options.duration){
           var date = new Date();
           date.setTime(date.getTime() + options.duration * 24 * 60 * 60 * 1000);
           value += '; expires=' + date.toGMTString();
       }
       if (options.secure) value += '; secure';
       document.cookie = key + '=' + value;
       return $extend(options, {'key': key, 'value': value});
   },

   get: function(key){
       var value = document.cookie.match('(?:^|;)\\s*' + key.escapeRegExp() + '=([^;]*)');
       return value ? decodeURIComponent(value[1]) : false;
   },

   remove: function(cookie, options){
       if ($type(cookie) == 'object') this.set(cookie.key, '', $merge(cookie, {duration: -1}));
       else this.set(cookie, '', $merge(options, {duration: -1}));
   }

};

Hookie = new Native('hookie', function(name, autoSave){
   this.name   = name;
   this.autoSave   = $defined(autoSave);
   this.load();
});

Hookie.implement({
   save: function(){
       var str = Json.encode(this.hash);
       if (str.length > 4096) return false;
       if (str.length == 2) Cookie.remove(this.name, this.options);
       else Cookie.set(this.name, str, this.options);
       return true;
   },

   load: function(){
       this.hash = new Hash(   Json.decode(Cookie.get(this.name)) || {} );
       return this;
   }

});


(function(){
   var methods = {};
   Hash.getKeys(Hash.prototype).forEach(function(method){
       methods[method] = function(){
           var value = Hash.prototype[method].apply(this.hash, arguments);
           if (this.autoSave) this.save();
           return value;
       };
   });
   Hookie.implement(methods);
})();

类别:Web | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码:
 

     

©2008 Baidu