📄 prototype.js
字号:
});
function $H(object){
return new Hash(object);
}
var Hash=Class.create(Enumerable,(function(){
if(function(){
var i=0,Test=function(value){
this.key=value;
};
Test.prototype.key="foo";
for(var property in new Test("bar")){
i++;
}
return i>1;
}()){
function each(iterator){
var cache=[];
for(var key in this._object){
var value=this._object[key];
if(cache.include(key)){
continue ;
}
cache.push(key);
var pair=[key,value];
pair.key=key;
pair.value=value;
iterator(pair);
}
}
}else {
function each(iterator){
for(var key in this._object){
var value=this._object[key],pair=[key,value];
pair.key=key;
pair.value=value;
iterator(pair);
}
}
}
function toQueryPair(key,value){
if(Object.isUndefined(value)){
return key;
}
return key+"="+encodeURIComponent(String.interpret(value));
}
return {initialize:function(object){
this._object=Object.isHash(object)?object.toObject():Object.clone(object);
},_each:each,set:function(key,value){
return this._object[key]=value;
},get:function(key){
return this._object[key];
},unset:function(key){
var value=this._object[key];
delete this._object[key];
return value;
},toObject:function(){
return Object.clone(this._object);
},keys:function(){
return this.pluck("key");
},values:function(){
return this.pluck("value");
},index:function(value){
var match=this.detect(function(pair){
return pair.value===value;
});
return match&&match.key;
},merge:function(object){
return this.clone().update(object);
},update:function(object){
return new Hash(object).inject(this,function(result,pair){
result.set(pair.key,pair.value);
return result;
});
},toQueryString:function(){
return this.map(function(pair){
var key=encodeURIComponent(pair.key),values=pair.value;
if(values&&typeof values=="object"){
if(Object.isArray(values)){
return values.map(toQueryPair.curry(key)).join("&");
}
}
return toQueryPair(key,values);
}).join("&");
},inspect:function(){
return "#<Hash:{"+this.map(function(pair){
return pair.map(Object.inspect).join(": ");
}).join(", ")+"}>";
},toJSON:function(){
return Object.toJSON(this.toObject());
},clone:function(){
return new Hash(this);
}};
})());
Hash.prototype.toTemplateReplacements=Hash.prototype.toObject;
Hash.from=$H;
var ObjectRange=Class.create(Enumerable,{initialize:function(start,end,exclusive){
this.start=start;
this.end=end;
this.exclusive=exclusive;
},_each:function(iterator){
var value=this.start;
while(this.include(value)){
iterator(value);
value=value.succ();
}
},include:function(value){
if(value<this.start){
return false;
}
if(this.exclusive){
return value<this.end;
}
return value<=this.end;
}});
var $R=function(start,end,exclusive){
return new ObjectRange(start,end,exclusive);
};
var Ajax={getTransport:function(){
return Try.these(function(){
return new XMLHttpRequest();
},function(){
return new ActiveXObject("Msxml2.XMLHTTP");
},function(){
return new ActiveXObject("Microsoft.XMLHTTP");
})||false;
},activeRequestCount:0};
Ajax.Responders={responders:[],_each:function(iterator){
this.responders._each(iterator);
},register:function(responder){
if(!this.include(responder)){
this.responders.push(responder);
}
},unregister:function(responder){
this.responders=this.responders.without(responder);
},dispatch:function(callback,request,transport,json){
this.each(function(responder){
if(Object.isFunction(responder[callback])){
try{
responder[callback].apply(responder,[request,transport,json]);
}
catch(e){
}
}
});
}};
Object.extend(Ajax.Responders,Enumerable);
Ajax.Responders.register({onCreate:function(){
Ajax.activeRequestCount++;
},onComplete:function(){
Ajax.activeRequestCount--;
}});
Ajax.Base=Class.create({initialize:function(options){
this.options={method:"post",asynchronous:true,contentType:"application/x-www-form-urlencoded",encoding:"UTF-8",parameters:"",evalJSON:true,evalJS:true};
Object.extend(this.options,options||{});
this.options.method=this.options.method.toLowerCase();
if(Object.isString(this.options.parameters)){
this.options.parameters=this.options.parameters.toQueryParams();
}
}});
Ajax.Request=Class.create(Ajax.Base,{_complete:false,initialize:function($super,url,options){
$super(options);
this.transport=Ajax.getTransport();
this.request(url);
},request:function(url){
this.url=url;
this.method=this.options.method;
var params=Object.clone(this.options.parameters);
if(!["get","post"].include(this.method)){
params["_method"]=this.method;
this.method="post";
}
this.parameters=params;
if(params=Object.toQueryString(params)){
if(this.method=="get"){
this.url+=(this.url.include("?")?"&":"?")+params;
}else {
if(/Konqueror|Safari|KHTML/.test(navigator.userAgent)){
params+="&_=";
}
}
}
try{
var response=new Ajax.Response(this);
if(this.options.onCreate){
this.options.onCreate(response);
}
Ajax.Responders.dispatch("onCreate",this,response);
this.transport.open(this.method.toUpperCase(),this.url,this.options.asynchronous);
if(this.options.asynchronous){
this.respondToReadyState.bind(this).defer(1);
}
this.transport.onreadystatechange=this.onStateChange.bind(this);
this.setRequestHeaders();
this.body=this.method=="post"?(this.options.postBody||params):null;
this.transport.send(this.body);
if(!this.options.asynchronous&&this.transport.overrideMimeType){
this.onStateChange();
}
}
catch(e){
this.dispatchException(e);
}
},onStateChange:function(){
var readyState=this.transport.readyState;
if(readyState>1&&!((readyState==4)&&this._complete)){
this.respondToReadyState(this.transport.readyState);
}
},setRequestHeaders:function(){
var headers={"X-Requested-With":"XMLHttpRequest","X-Prototype-Version":Prototype.Version,"Accept":"text/javascript, text/html, application/xml, text/xml, */*"};
if(this.method=="post"){
headers["Content-type"]=this.options.contentType+(this.options.encoding?"; charset="+this.options.encoding:"");
if(this.transport.overrideMimeType&&(navigator.userAgent.match(/Gecko\/(\d{4})/)||[0,2005])[1]<2005){
headers["Connection"]="close";
}
}
if(typeof this.options.requestHeaders=="object"){
var extras=this.options.requestHeaders;
if(Object.isFunction(extras.push)){
for(var i=0,length=extras.length;i<length;i+=2){
headers[extras[i]]=extras[i+1];
}
}else {
$H(extras).each(function(pair){
headers[pair.key]=pair.value;
});
}
}
for(var name in headers){
this.transport.setRequestHeader(name,headers[name]);
}
},success:function(){
var status=this.getStatus();
return !status||(status>=200&&status<300);
},getStatus:function(){
try{
return this.transport.status||0;
}
catch(e){
return 0;
}
},respondToReadyState:function(readyState){
var state=Ajax.Request.Events[readyState],response=new Ajax.Response(this);
if(state=="Complete"){
try{
this._complete=true;
(this.options["on"+response.status]||this.options["on"+(this.success()?"Success":"Failure")]||Prototype.emptyFunction)(response,response.headerJSON);
}
catch(e){
this.dispatchException(e);
}
var contentType=response.getHeader("Content-type");
if(this.options.evalJS=="force"||(this.options.evalJS&&contentType&&contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i))){
this.evalResponse();
}
}
try{
(this.options["on"+state]||Prototype.emptyFunction)(response,response.headerJSON);
Ajax.Responders.dispatch("on"+state,this,response,response.headerJSON);
}
catch(e){
this.dispatchException(e);
}
if(state=="Complete"){
this.transport.onreadystatechange=Prototype.emptyFunction;
}
},getHeader:function(name){
try{
return this.transport.getResponseHeader(name);
}
catch(e){
return null;
}
},evalResponse:function(){
try{
return eval((this.transport.responseText||"").unfilterJSON());
}
catch(e){
this.dispatchException(e);
}
},dispatchException:function(exception){
(this.options.onException||Prototype.emptyFunction)(this,exception);
Ajax.Responders.dispatch("onException",this,exception);
}});
Ajax.Request.Events=["Uninitialized","Loading","Loaded","Interactive","Complete"];
Ajax.Response=Class.create({initialize:function(request){
this.request=request;
var transport=this.transport=request.transport,readyState=this.readyState=transport.readyState;
if((readyState>2&&!Prototype.Browser.IE)||readyState==4){
this.status=this.getStatus();
this.statusText=this.getStatusText();
this.responseText=String.interpret(transport.responseText);
this.headerJSON=this._getHeaderJSON();
}
if(readyState==4){
var xml=transport.responseXML;
this.responseXML=xml===undefined?null:xml;
this.responseJSON=this._getResponseJSON();
}
},status:0,statusText:"",getStatus:Ajax.Request.prototype.getStatus,getStatusText:function(){
try{
return this.transport.statusText||"";
}
catch(e){
return "";
}
},getHeader:Ajax.Request.prototype.getHeader,getAllHeaders:function(){
try{
return this.getAllResponseHeaders();
}
catch(e){
return null;
}
},getResponseHeader:function(name){
return this.transport.getResponseHeader(name);
},getAllResponseHeaders:function(){
return this.transport.getAllResponseHeaders();
},_getHeaderJSON:function(){
var json=this.getHeader("X-JSON");
if(!json){
return null;
}
json=decodeURIComponent(escape(json));
try{
return json.evalJSON(this.request.options.sanitizeJSON);
}
catch(e){
this.request.dispatchException(e);
}
},_getResponseJSON:function(){
var options=this.request.options;
if(!options.evalJSON||(options.evalJSON!="force"&&!(this.getHeader("Content-type")||"").include("application/json"))){
return null;
}
try{
return this.transport.responseText.evalJSON(options.sanitizeJSON);
}
catch(e){
this.request.dispatchException(e);
}
}});
Ajax.Updater=Class.create(Ajax.Request,{initialize:function($super,container,url,options){
this.container={success:(container.success||container),failure:(container.failure||(container.success?null:container))};
options=options||{};
var onComplete=options.onComplete;
options.onComplete=(function(response,param){
this.updateContent(response.responseText);
if(Object.isFunction(onComplete)){
onComplete(response,param);
}
}).bind(this);
$super(url,options);
},updateContent:function(responseText){
var receiver=this.container[this.success()?"success":"failure"],options=this.options;
if(!options.evalScripts){
responseText=responseText.stripScripts();
}
if(receiver=$(receiver)){
if(options.insertion){
if(Object.isString(options.insertion)){
var insertion={};
insertion[options.insertion]=responseText;
receiver.insert(insertion);
}else {
options.insertion(receiver,responseText);
}
}else {
receiver.update(responseText);
}
}
if(this.success()){
if(this.onComplete){
this.onComplete.bind(this).defer();
}
}
}});
Ajax.PeriodicalUpdater=Class.create(Ajax.Base,{initialize:function($super,container,url,options){
$super(options);
this.onComplete=this.options.onComplete;
this.frequency=(this.options.frequency||2);
this.decay=(this.options.decay||1);
this.updater={};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -