This commit is contained in:
Juti Noppornpitak 2007-03-18 18:06:26 +00:00
parent 4577f46176
commit 996b8fc56e
1 changed files with 166 additions and 0 deletions

166
ajax.js Normal file
View File

@ -0,0 +1,166 @@
// X-Mode AJAX Core "Yume" 2.0
// Compatible with X-Mode 3 "Yotsuba" and any other external apps.
// Originally developed by Dave Crane, Eric Pascarello and Darren James
// Developed by Juti Noppornpitak
var net = new Object();
net.READY_STATE_UNINITIALIZED = 0;
net.READY_STATE_LOADING = 1;
net.READY_STATE_LOADED = 2;
net.READY_STATE_INTERACTIVE = 3;
net.READY_STATE_COMPLETE = 4;
net.CONNECTION_TERMINATION = "<blink>Terminating the connection...</blink>";
net.core = function(url, console, onload, loading, onerror,
method, data, contentType) {
this.url = url;
this.currurl = url;
this.req = null;
this.console = console;
this.data = (data) ? data : null;
this.contentType = (contentType) ? contentType : null;
this.method = (method) ? method : "GET";
this.onload = (onload) ? onload : this.defaultLoad;
this.loading = (loading) ? loading : this.defaultLoading;
this.onerror = (onerror) ? onerror : this.defaultError;
// Require the user to manually start the connection.
// this.connect(url);
}
net.core.prototype = {
defaultConnect:function() {
this.connect(this.url);
},
connect:function(url, method, data, contentType) {
// Create the requester
if(window.XMLHttpRequest) { // for Mozilla/Safari
this.req = new XMLHttpRequest();
} else if(window.ActiveXObject) { // for MSIE
this.req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(!contentType && method == "POST") {
contentType="application/x-www-form-urlencoded";
}
this.currurl = url;
if(this.req) {
try {
var loader = this;
this.req.onreadystatechange = function() {
loader.process.call(loader);
}
if(!method) { method = "GET"; }
if(!data) { data = null; }
this.req.open(method, this.currurl, true);
if(contentType) {
this.req.setRequestHeader("Content-Type", contentType);
}
this.req.send(data);
} catch(err) {
this.onerror.call(this);
}
}
},
process:function() {
var req = this.req;
var status = req.readyState;
if(status == net.READY_STATE_COMPLETE) {
var httpstatus = req.status;
if(httpstatus == 200 || httpstatus == 0) {
this.onload.call(this);
} else {
this.onerror.call(this);
}
} else {
// handling loading/connecting
this.loading.call(this);
}
},
defaultLoad:function() {
xcie__setElement(this.console,
this.req.responseText);
},
defaultError:function() {
alert("Error while connecting to: " + this.currurl);
},
defaultLoading:function() {
xcie__setElement(this.console, "Loading");
}
}
function xcie__element(id) {
return document.getElementById(id);
}
function xcie__getElement(id) {
if(!xcie__element(id)) return;
return xcie__element(id).innerHTML;
}
function xcie__getElementValue(id) {
if(!xcie__element(id)) return;
return xcie__element(id).value;
}
function xcie__setElementValue(id, value) {
if(!xcie__element(id)) return;
xcie__element(id).value = value;
}
function xcie__setElement(id, value) {
if(!xcie__element(id)) return;
xcie__element(id).innerHTML = value;
}
function xcie__setWindowTitle(value) {
document.title = value;
}
function xcie__setClass(id, nc) {
xcie__element(id).className = nc;
}
function xcie__img(src, align, other) {
return "<img border=\"0\" src=\"" + src + "\" align=\""
+ ((align)?align:"absmiddle")
+ "\" " + ((other)?other:"") + "/>";
}
function xcie__a(label, value, ajax, other, command) {
return "<a " + (!ajax?"href=\"" + value + "\"":"onclick=\""
+ ((command == null)?
"connect('" + net.PREFIX_LINK + value + "');"
:command)
+ "\"")
+ ((other)?" " + other:"") + "\">"
+ label + "</a>";
}
function xcie__in(type, name, value, other, cmd) {
return "<input name=\"" + name + "\" id=\"" + name + "\""
+ " type=\"" + type + "\" value=\"" + ((value)?value:"")
+ "\"" + " onclick=\"" + cmd + "\""
+ ((other)?" " + other:"") + "/>";
}
function addslashes(str) {
// SRC: Stephen Chapman of About.com
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
return str;
}
function stripslashes(str) {
// SRC: Stephen Chapman of About.com
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
return str;
}