⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swato-engine.js

📁 刚开始学习看的
💻 JS
字号:
/*
 *  (c) 2005 Zhijie Chen <zigzag.chen@gmail.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Swato.Engine = Class.create();

Swato.Engine.prototype = {

	initialize: function(url,options) {
		this.url = url;
		this.conn_id = Math.floor(Math.random()*100000000);
		this.options = options;
	},

	call: function(catcher,serviceDotMethodName,params,options) {
		if ( this.url == null ) return;
		if ($("loading_img")) {
			$("loading_img").style.display="" ;
		}
		var serviceId=serviceDotMethodName.substring(0,serviceDotMethodName.indexOf("."));
		var methodName=serviceDotMethodName.substring(serviceDotMethodName.indexOf(".")+1,serviceDotMethodName.length);
		if (document.getElementById(catcher)!=null) {
			new Ajax.Updater(catcher, this.url+"/"+serviceId,
				this._requestOptions(serviceId.hashCode||serviceId,methodName,params,options));
		} else if (catcher!=null){
			var option=new Object();
			Object.extend(option,options);
			option.onComplete=this._onRequestComplete.bind(catcher);
			new Ajax.Request(this.url+"/"+serviceId,
				this._requestOptions(serviceId.hashCode||serviceId,methodName,params,option));
		}
	},

	fetch: function(catcher,url,options) {
		if ( url == null ) return;
		if ($("loading_img")) {
			$("loading_img").style.display="" ;
		}
		if (typeof(debug)== 'function') debug(">>> "+url);
		if (document.getElementById(catcher)!=null) {
			new Ajax.Updater(catcher, url,options);
		} else if (catcher!=null){
			var option=new Object();
			Object.extend(option,options);
			option.onComplete=this._onRequestComplete.bind(catcher);
			new Ajax.Request(url,option);
		}
	},	

//	fetch: function(catcher,remoteObjectID,options) {
//		this.call(catcher,remoteObjectID,'',[],options);
//	},
	
	send: function(catcher,jsObject,remoteObjectID,options) {
		this.call(catcher,remoteObjectID,'',[jsObject],options);
	},

	_requestOptions: function(remoteObjectID,methodName,params,options) {
		var sendingData=
			{"params":params,
			"method":methodName,
			"id":remoteObjectID,
			"conn_id":this.conn_id};
		var requestOptions=new Object();
		Object.extend(requestOptions,this.options);
		Object.extend(requestOptions,options);
		requestOptions.parameters=JSON.stringify(sendingData);
		if (typeof(debug)== 'function') debug(">>> "+requestOptions.parameters);
		return requestOptions;
	},

	_onRequestComplete : function(response) {
		if (response.status != 200) {
			if (typeof(FVL_log)== 'function') FVL_log("Response Status: "+response.status,FVL_ERROR);
			return;
		}
		if ($("loading_img")) {
			$("loading_img").style.display="none" ;
		}
		if (typeof(debug)== 'function')	debug("<<< "+response.responseText);
		var root;
		var result;
		var error;
		//try parse XML
		root=XMLUtil.getDocumentElement(response);
		if (root){ 
			//deal with xml
			var d2j = new JKL.ParseXML.JSON();
			result = d2j.parseDocument( root );
			if (typeof(debug)== 'function') {
				debug("XML parse result: "+JSON.stringify(result));
			}
		}else { 
			//deal with json
			var responseText;
			eval("responseText = " + response.responseText);
			result=responseText.result;
			error=responseText.error;
			this.hashCode=responseText.hashCode;
		}
		if (this.gotError!=null && error!=undefined) {
			this.gotError.apply(this, [error]);
		}
		if (this.gotResult!=null){
			this.gotResult.apply(this, [result]);
		}
	}
};

var XMLUtil = {
	getForIE: function(response){
		var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.loadXML(response.responseText);
		return xmlDoc.documentElement;
	},

	getDocumentElement: function(response) {
		if (response.responseText!=null && response.responseText.charAt(0)=='{') return null;
		if (response.responseXML!=null && response.responseXML.documentElement!=null)
			return response.responseXML.documentElement;
		if (response.documentElement!=null) return response.documentElement;
		return (XMLUtil.getForIE(response));
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -