source: trunk/www.guidonia.net/wp/wp-includes/js/tw-sack.dev.js@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 4.9 KB
Line 
1/* Simple AJAX Code-Kit (SACK) v1.6.1 */
2/* ©2005 Gregory Wild-Smith */
3/* www.twilightuniverse.com */
4/* Software licenced under a modified X11 licence,
5 see documentation or authors website for more details */
6
7function sack(file) {
8 this.xmlhttp = null;
9
10 this.resetData = function() {
11 this.method = "POST";
12 this.queryStringSeparator = "?";
13 this.argumentSeparator = "&";
14 this.URLString = "";
15 this.encodeURIString = true;
16 this.execute = false;
17 this.element = null;
18 this.elementObj = null;
19 this.requestFile = file;
20 this.vars = new Object();
21 this.responseStatus = new Array(2);
22 };
23
24 this.resetFunctions = function() {
25 this.onLoading = function() { };
26 this.onLoaded = function() { };
27 this.onInteractive = function() { };
28 this.onCompletion = function() { };
29 this.onError = function() { };
30 this.onFail = function() { };
31 };
32
33 this.reset = function() {
34 this.resetFunctions();
35 this.resetData();
36 };
37
38 this.createAJAX = function() {
39 try {
40 this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
41 } catch (e1) {
42 try {
43 this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
44 } catch (e2) {
45 this.xmlhttp = null;
46 }
47 }
48
49 if (! this.xmlhttp) {
50 if (typeof XMLHttpRequest != "undefined") {
51 this.xmlhttp = new XMLHttpRequest();
52 } else {
53 this.failed = true;
54 }
55 }
56 };
57
58 this.setVar = function(name, value){
59 this.vars[name] = Array(value, false);
60 };
61
62 this.encVar = function(name, value, returnvars) {
63 if (true == returnvars) {
64 return Array(encodeURIComponent(name), encodeURIComponent(value));
65 } else {
66 this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
67 }
68 }
69
70 this.processURLString = function(string, encode) {
71 encoded = encodeURIComponent(this.argumentSeparator);
72 regexp = new RegExp(this.argumentSeparator + "|" + encoded);
73 varArray = string.split(regexp);
74 for (i = 0; i < varArray.length; i++){
75 urlVars = varArray[i].split("=");
76 if (true == encode){
77 this.encVar(urlVars[0], urlVars[1]);
78 } else {
79 this.setVar(urlVars[0], urlVars[1]);
80 }
81 }
82 }
83
84 this.createURLString = function(urlstring) {
85 if (this.encodeURIString && this.URLString.length) {
86 this.processURLString(this.URLString, true);
87 }
88
89 if (urlstring) {
90 if (this.URLString.length) {
91 this.URLString += this.argumentSeparator + urlstring;
92 } else {
93 this.URLString = urlstring;
94 }
95 }
96
97 // prevents caching of URLString
98 this.setVar("rndval", new Date().getTime());
99
100 urlstringtemp = new Array();
101 for (key in this.vars) {
102 if (false == this.vars[key][1] && true == this.encodeURIString) {
103 encoded = this.encVar(key, this.vars[key][0], true);
104 delete this.vars[key];
105 this.vars[encoded[0]] = Array(encoded[1], true);
106 key = encoded[0];
107 }
108
109 urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
110 }
111 if (urlstring){
112 this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
113 } else {
114 this.URLString += urlstringtemp.join(this.argumentSeparator);
115 }
116 }
117
118 this.runResponse = function() {
119 eval(this.response);
120 }
121
122 this.runAJAX = function(urlstring) {
123 if (this.failed) {
124 this.onFail();
125 } else {
126 this.createURLString(urlstring);
127 if (this.element) {
128 this.elementObj = document.getElementById(this.element);
129 }
130 if (this.xmlhttp) {
131 var self = this;
132 if (this.method == "GET") {
133 totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
134 this.xmlhttp.open(this.method, totalurlstring, true);
135 } else {
136 this.xmlhttp.open(this.method, this.requestFile, true);
137 try {
138 this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
139 } catch (e) { }
140 }
141
142 this.xmlhttp.onreadystatechange = function() {
143 switch (self.xmlhttp.readyState) {
144 case 1:
145 self.onLoading();
146 break;
147 case 2:
148 self.onLoaded();
149 break;
150 case 3:
151 self.onInteractive();
152 break;
153 case 4:
154 self.response = self.xmlhttp.responseText;
155 self.responseXML = self.xmlhttp.responseXML;
156 self.responseStatus[0] = self.xmlhttp.status;
157 self.responseStatus[1] = self.xmlhttp.statusText;
158
159 if (self.execute) {
160 self.runResponse();
161 }
162
163 if (self.elementObj) {
164 elemNodeName = self.elementObj.nodeName;
165 elemNodeName.toLowerCase();
166 if (elemNodeName == "input"
167 || elemNodeName == "select"
168 || elemNodeName == "option"
169 || elemNodeName == "textarea") {
170 self.elementObj.value = self.response;
171 } else {
172 self.elementObj.innerHTML = self.response;
173 }
174 }
175 if (self.responseStatus[0] == "200") {
176 self.onCompletion();
177 } else {
178 self.onError();
179 }
180
181 self.URLString = "";
182 break;
183 }
184 };
185
186 this.xmlhttp.send(this.URLString);
187 }
188 }
189 };
190
191 this.reset();
192 this.createAJAX();
193}
Note: See TracBrowser for help on using the repository browser.