📄 prototype.js
字号:
}});
}
String.prototype.gsub.prepareReplacement=function(replacement){
if(Object.isFunction(replacement)){
return replacement;
}
var template=new Template(replacement);
return function(match){
return template.evaluate(match);
};
};
String.prototype.parseQuery=String.prototype.toQueryParams;
Object.extend(String.prototype.escapeHTML,{div:document.createElement("div"),text:document.createTextNode("")});
with(String.prototype.escapeHTML){
div.appendChild(text);
}
var Template=Class.create({initialize:function(template,pattern){
this.template=template.toString();
this.pattern=pattern||Template.Pattern;
},evaluate:function(object){
if(Object.isFunction(object.toTemplateReplacements)){
object=object.toTemplateReplacements();
}
return this.template.gsub(this.pattern,function(match){
if(object==null){
return "";
}
var before=match[1]||"";
if(before=="\\"){
return match[2];
}
var ctx=object,expr=match[3];
var pattern=/^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/,match=pattern.exec(expr);
if(match==null){
return before;
}
while(match!=null){
var comp=match[1].startsWith("[")?match[2].gsub("\\\\]","]"):match[1];
ctx=ctx[comp];
if(null==ctx||""==match[3]){
break ;
}
expr=expr.substring("["==match[3]?match[1].length:match[0].length);
match=pattern.exec(expr);
}
return before+String.interpret(ctx);
}.bind(this));
}});
Template.Pattern=/(^|.|\r|\n)(#\{(.*?)\})/;
var $break={};
var Enumerable={each:function(iterator,context){
var index=0;
iterator=iterator.bind(context);
try{
this._each(function(value){
iterator(value,index++);
});
}
catch(e){
}
return this;
},eachSlice:function(number,iterator,context){
iterator=iterator?iterator.bind(context):Prototype.K;
var index=-number,slices=[],array=this.toArray();
while((index+=number)<array.length){
slices.push(array.slice(index,index+number));
}
return slices.collect(iterator,context);
},all:function(iterator,context){
iterator=iterator?iterator.bind(context):Prototype.K;
var result=true;
this.each(function(value,index){
result=result&&!!iterator(value,index);
if(!result){
throw $break;
}
});
return result;
},any:function(iterator,context){
iterator=iterator?iterator.bind(context):Prototype.K;
var result=false;
this.each(function(value,index){
if(result=!!iterator(value,index)){
throw $break;
}
});
return result;
},collect:function(iterator,context){
iterator=iterator?iterator.bind(context):Prototype.K;
var results=[];
this.each(function(value,index){
results.push(iterator(value,index));
});
return results;
},detect:function(iterator,context){
iterator=iterator.bind(context);
var result;
this.each(function(value,index){
if(iterator(value,index)){
result=value;
throw $break;
}
});
return result;
},findAll:function(iterator,context){
iterator=iterator.bind(context);
var results=[];
this.each(function(value,index){
if(iterator(value,index)){
results.push(value);
}
});
return results;
},grep:function(filter,iterator,context){
iterator=iterator?iterator.bind(context):Prototype.K;
var results=[];
if(Object.isString(filter)){
filter=new RegExp(filter);
}
this.each(function(value,index){
if(filter.match(value)){
results.push(iterator(value,index));
}
});
return results;
},include:function(object){
if(Object.isFunction(this.indexOf)){
if(this.indexOf(object)!=-1){
return true;
}
}
var found=false;
this.each(function(value){
if(value==object){
found=true;
throw $break;
}
});
return found;
},inGroupsOf:function(number,fillWith){
fillWith=fillWith===undefined?null:fillWith;
return this.eachSlice(number,function(slice){
while(slice.length<number){
slice.push(fillWith);
}
return slice;
});
},inject:function(memo,iterator,context){
iterator=iterator.bind(context);
this.each(function(value,index){
memo=iterator(memo,value,index);
});
return memo;
},invoke:function(method){
var args=$A(arguments).slice(1);
return this.map(function(value){
return value[method].apply(value,args);
});
},max:function(iterator,context){
iterator=iterator?iterator.bind(context):Prototype.K;
var result;
this.each(function(value,index){
value=iterator(value,index);
if(result==undefined||value>=result){
result=value;
}
});
return result;
},min:function(iterator,context){
iterator=iterator?iterator.bind(context):Prototype.K;
var result;
this.each(function(value,index){
value=iterator(value,index);
if(result==undefined||value<result){
result=value;
}
});
return result;
},partition:function(iterator,context){
iterator=iterator?iterator.bind(context):Prototype.K;
var trues=[],falses=[];
this.each(function(value,index){
(iterator(value,index)?trues:falses).push(value);
});
return [trues,falses];
},pluck:function(property){
var results=[];
this.each(function(value){
results.push(value[property]);
});
return results;
},reject:function(iterator,context){
iterator=iterator.bind(context);
var results=[];
this.each(function(value,index){
if(!iterator(value,index)){
results.push(value);
}
});
return results;
},sortBy:function(iterator,context){
iterator=iterator.bind(context);
return this.map(function(value,index){
return {value:value,criteria:iterator(value,index)};
}).sort(function(left,right){
var a=left.criteria,b=right.criteria;
return a<b?-1:a>b?1:0;
}).pluck("value");
},toArray:function(){
return this.map();
},zip:function(){
var iterator=Prototype.K,args=$A(arguments);
if(Object.isFunction(args.last())){
iterator=args.pop();
}
var collections=[this].concat(args).map($A);
return this.map(function(value,index){
return iterator(collections.pluck(index));
});
},size:function(){
return this.toArray().length;
},inspect:function(){
return "#<Enumerable:"+this.toArray().inspect()+">";
}};
Object.extend(Enumerable,{map:Enumerable.collect,find:Enumerable.detect,select:Enumerable.findAll,filter:Enumerable.findAll,member:Enumerable.include,entries:Enumerable.toArray,every:Enumerable.all,some:Enumerable.any});
function $A(iterable){
if(!iterable){
return [];
}
if(iterable.toArray){
return iterable.toArray();
}
var length=iterable.length,results=new Array(length);
while(length--){
results[length]=iterable[length];
}
return results;
}
if(Prototype.Browser.WebKit){
function $A(iterable){
if(!iterable){
return [];
}
if(!(Object.isFunction(iterable)&&iterable=="[object NodeList]")&&iterable.toArray){
return iterable.toArray();
}
var length=iterable.length,results=new Array(length);
while(length--){
results[length]=iterable[length];
}
return results;
}
}
Array.from=$A;
Object.extend(Array.prototype,Enumerable);
if(!Array.prototype._reverse){
Array.prototype._reverse=Array.prototype.reverse;
}
Object.extend(Array.prototype,{_each:function(iterator){
for(var i=0,length=this.length;i<length;i++){
iterator(this[i]);
}
},clear:function(){
this.length=0;
return this;
},first:function(){
return this[0];
},last:function(){
return this[this.length-1];
},compact:function(){
return this.select(function(value){
return value!=null;
});
},flatten:function(){
return this.inject([],function(array,value){
return array.concat(Object.isArray(value)?value.flatten():[value]);
});
},without:function(){
var values=$A(arguments);
return this.select(function(value){
return !values.include(value);
});
},reverse:function(inline){
return (inline!==false?this:this.toArray())._reverse();
},reduce:function(){
return this.length>1?this:this[0];
},uniq:function(sorted){
return this.inject([],function(array,value,index){
if(0==index||(sorted?array.last()!=value:!array.include(value))){
array.push(value);
}
return array;
});
},intersect:function(array){
return this.uniq().findAll(function(item){
return array.detect(function(value){
return item===value;
});
});
},clone:function(){
return [].concat(this);
},size:function(){
return this.length;
},inspect:function(){
return "["+this.map(Object.inspect).join(", ")+"]";
},toJSON:function(){
var results=[];
this.each(function(object){
var value=Object.toJSON(object);
if(value!==undefined){
results.push(value);
}
});
return "["+results.join(", ")+"]";
}});
if(Object.isFunction(Array.prototype.forEach)){
Array.prototype._each=Array.prototype.forEach;
}
if(!Array.prototype.indexOf){
Array.prototype.indexOf=function(item,i){
i||(i=0);
var length=this.length;
if(i<0){
i=length+i;
}
for(;i<length;i++){
if(this[i]===item){
return i;
}
}
return -1;
};
}
if(!Array.prototype.lastIndexOf){
Array.prototype.lastIndexOf=function(item,i){
i=isNaN(i)?this.length:(i<0?this.length+i:i)+1;
var n=this.slice(0,i).reverse().indexOf(item);
return (n<0)?n:i-n-1;
};
}
Array.prototype.toArray=Array.prototype.clone;
function $w(string){
if(!Object.isString(string)){
return [];
}
string=string.strip();
return string?string.split(/\s+/):[];
}
if(Prototype.Browser.Opera){
Array.prototype.concat=function(){
var array=[];
for(var i=0,length=this.length;i<length;i++){
array.push(this[i]);
}
for(var i=0,length=arguments.length;i<length;i++){
if(Object.isArray(arguments[i])){
for(var j=0,arrayLength=arguments[i].length;j<arrayLength;j++){
array.push(arguments[i][j]);
}
}else {
array.push(arguments[i]);
}
}
return array;
};
}
Object.extend(Number.prototype,{toColorPart:function(){
return this.toPaddedString(2,16);
},succ:function(){
return this+1;
},times:function(iterator){
$R(0,this,true).each(iterator);
return this;
},toPaddedString:function(length,radix){
var string=this.toString(radix||10);
return "0".times(length-string.length)+string;
},toJSON:function(){
return isFinite(this)?this.toString():"null";
}});
$w("abs round ceil floor").each(function(method){
Number.prototype[method]=Math[method].methodize();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -