-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.js
177 lines (177 loc) · 4.13 KB
/
ajax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
Object.prototype.extend = function(dest, src){
for (var elem in src) {
dest[elem] = src[elem];
}
return dest;
}
function trim(stringToTrim) {
if(stringToTrim != undefined)
return stringToTrim.replace(/^\s+|\s+$/g,"");
}
/**
* Create a new Ajax Request
* Copyright Philipp Rehs
* @param {Object} options
*/
function Ajax(options){
this.options = {
method: 'post',
url: null,
parameter: null,
contentType: 'application/x-www-form-urlencoded',
ResponseHandle: {
action: 'empty'
},
forceMime: null,
}
this.options = Object.extend(this.options, options || {});
this.ajax = this.init();
this.headers = new Object();
this.expectedHeaders = new Object;
this.expectedHeaders['Content-Type'] = 'text/html';
this.headers_received = false;
this.state = 0;
this.status = 0;
}
/**
* Start the request
*/
Ajax.prototype.go = function() {
if(this.options['url'] != null){
if(this.options['method'].toLowerCase() != "get"){
this.options['method'] = "post";
}
this.ajax.open(this.options['method'].toUpperCase(), this.options['url'], true);
this.ajax.setRequestHeader('Content-Type',this.options['contentType']);
this.ajax.onreadystatechange = this.onreadychange(this);
if(this.options['forceMime'] != null){
this.ajax.overrideMimeType(this.options['forceMime']);
}
this.ajax.send(this.options['parameter']);
}
else{
return false;
}
};
/**
* Set an expected header
* @param String name
* @param String value
*/
Ajax.prototype.setExpectedHeader = function(name, value){
this.expectedHeaders[name] = value;
}
Ajax.prototype.stop = function(){
this.ajax.abort();
}
/**
* Call a request after a interval again
* @param integer interval Interval in milliseconds
* @param boolean now Start the first request now
* @return integer
*/
Ajax.prototype.interval = function (interval, now){
if(now == true){
this.go();
}
return setInterval(this.getReferenceFunction(this,'go'),interval);
}
/**
* Create a function for the delayed / interval functions
* @param {Object} parent
* @param String type
* @return function
*/
Ajax.prototype.getReferenceFunction = function(parent, type){
switch(type){
case 'go':
return function(){
parent.go()
};
case 'result':
return function(){
parent.ResultHandle.call(parent,parent.ajax);
};
default:
return false;
}
}
/**
* Call the request after a delay
* @param delay interval The Timeout in milliseconds
* @return integer
*/
Ajax.prototype.delayed = function(delay){
return setTimeout(this.getReferenceFunction(this,'go'),delay);
}
/**
* Build the XmlHttpRequest
* @return object/boolean
*/
Ajax.prototype.init = function(){
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject('MSXML2.XMLHTTP.6.0');
}
catch(e){
try {
xmlHttp = new ActiveXObject('MSXML3.XMLHTTP');
}
catch(e){
xmlHttp = new XMLHttpRequest();
}
}
return xmlHttp;
};
Ajax.prototype.getHeader = function(name){
return (this.headers[name.toLowerCase()] || '');
}
/**
* Create the on ready change function
* @param {Object} parent
* @return function
*/
Ajax.prototype.onreadychange = function(parent){
return function(){
if (typeof parent.ajax.status != "unknown") {
parent.state = parent.ajax.readyState
parent.status = parent.ajax.status;
}
if(parent.ajax.readyState == 4){
return parent.ResultHandle.call(parent,parent.ajax);
}
}
}
/**
* Handle the response
* All Parameter from the parameter object can be called via this.
* @param {Object} http
*/
Ajax.prototype.ResultHandle = function(http){
parameter = this.options['ResponseHandle'];
switch(parameter.action){
case 'add':
document.getElementById(parameter.id).innerHTML += http.responseText;
break;
case 'replace':
document.getElementById(parameter.id).innerHTML = http.responseText;
break;
case 'formfield':
document.getElementById(parameter.id).value = http.responseText;
break;
case 'return':
return http.responseText;
break;
case 'callback':
parameter['callback'].call(this, http);
break;
case 'execute':
eval(http.responseText);
break;
case 'empty':
break;
default:
alert("Unknown action - "+parameter.action);
break;
}
}