PageRenderTime 117ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/js/tw-sack.js

https://bitbucket.org/solidcode_bridge/bfm_learning_centre
JavaScript | 193 lines | 165 code | 22 blank | 6 comment | 45 complexity | b989a5bd84f6ebcbc1393ec003e6e991 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, MPL-2.0-no-copyleft-exception, Apache-2.0, JSON
  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. function sack(file) {
  7. this.xmlhttp = null;
  8. this.resetData = function() {
  9. this.method = "POST";
  10. this.queryStringSeparator = "?";
  11. this.argumentSeparator = "&";
  12. this.URLString = "";
  13. this.encodeURIString = true;
  14. this.execute = false;
  15. this.element = null;
  16. this.elementObj = null;
  17. this.requestFile = file;
  18. this.vars = new Object();
  19. this.responseStatus = new Array(2);
  20. };
  21. this.resetFunctions = function() {
  22. this.onLoading = function() { };
  23. this.onLoaded = function() { };
  24. this.onInteractive = function() { };
  25. this.onCompletion = function() { };
  26. this.onError = function() { };
  27. this.onFail = function() { };
  28. };
  29. this.reset = function() {
  30. this.resetFunctions();
  31. this.resetData();
  32. };
  33. this.createAJAX = function() {
  34. try {
  35. this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  36. } catch (e1) {
  37. try {
  38. this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  39. } catch (e2) {
  40. this.xmlhttp = null;
  41. }
  42. }
  43. if (! this.xmlhttp) {
  44. if (typeof XMLHttpRequest != "undefined") {
  45. this.xmlhttp = new XMLHttpRequest();
  46. } else {
  47. this.failed = true;
  48. }
  49. }
  50. };
  51. this.setVar = function(name, value){
  52. this.vars[name] = Array(value, false);
  53. };
  54. this.encVar = function(name, value, returnvars) {
  55. if (true == returnvars) {
  56. return Array(encodeURIComponent(name), encodeURIComponent(value));
  57. } else {
  58. this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
  59. }
  60. }
  61. this.processURLString = function(string, encode) {
  62. encoded = encodeURIComponent(this.argumentSeparator);
  63. regexp = new RegExp(this.argumentSeparator + "|" + encoded);
  64. varArray = string.split(regexp);
  65. for (i = 0; i < varArray.length; i++){
  66. urlVars = varArray[i].split("=");
  67. if (true == encode){
  68. this.encVar(urlVars[0], urlVars[1]);
  69. } else {
  70. this.setVar(urlVars[0], urlVars[1]);
  71. }
  72. }
  73. }
  74. this.createURLString = function(urlstring) {
  75. if (this.encodeURIString && this.URLString.length) {
  76. this.processURLString(this.URLString, true);
  77. }
  78. if (urlstring) {
  79. if (this.URLString.length) {
  80. this.URLString += this.argumentSeparator + urlstring;
  81. } else {
  82. this.URLString = urlstring;
  83. }
  84. }
  85. // prevents caching of URLString
  86. this.setVar("rndval", new Date().getTime());
  87. urlstringtemp = new Array();
  88. for (key in this.vars) {
  89. if (false == this.vars[key][1] && true == this.encodeURIString) {
  90. encoded = this.encVar(key, this.vars[key][0], true);
  91. delete this.vars[key];
  92. this.vars[encoded[0]] = Array(encoded[1], true);
  93. key = encoded[0];
  94. }
  95. urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
  96. }
  97. if (urlstring){
  98. this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
  99. } else {
  100. this.URLString += urlstringtemp.join(this.argumentSeparator);
  101. }
  102. }
  103. this.runResponse = function() {
  104. eval(this.response);
  105. }
  106. this.runAJAX = function(urlstring) {
  107. if (this.failed) {
  108. this.onFail();
  109. } else {
  110. this.createURLString(urlstring);
  111. if (this.element) {
  112. this.elementObj = document.getElementById(this.element);
  113. }
  114. if (this.xmlhttp) {
  115. var self = this;
  116. if (this.method == "GET") {
  117. totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
  118. this.xmlhttp.open(this.method, totalurlstring, true);
  119. } else {
  120. this.xmlhttp.open(this.method, this.requestFile, true);
  121. try {
  122. this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
  123. } catch (e) { }
  124. }
  125. this.xmlhttp.onreadystatechange = function() {
  126. switch (self.xmlhttp.readyState) {
  127. case 1:
  128. self.onLoading();
  129. break;
  130. case 2:
  131. self.onLoaded();
  132. break;
  133. case 3:
  134. self.onInteractive();
  135. break;
  136. case 4:
  137. self.response = self.xmlhttp.responseText;
  138. self.responseXML = self.xmlhttp.responseXML;
  139. self.responseStatus[0] = self.xmlhttp.status;
  140. self.responseStatus[1] = self.xmlhttp.statusText;
  141. if (self.execute) {
  142. self.runResponse();
  143. }
  144. if (self.elementObj) {
  145. elemNodeName = self.elementObj.nodeName;
  146. elemNodeName.toLowerCase();
  147. if (elemNodeName == "input"
  148. || elemNodeName == "select"
  149. || elemNodeName == "option"
  150. || elemNodeName == "textarea") {
  151. self.elementObj.value = self.response;
  152. } else {
  153. self.elementObj.innerHTML = self.response;
  154. }
  155. }
  156. if (self.responseStatus[0] == "200") {
  157. self.onCompletion();
  158. } else {
  159. self.onError();
  160. }
  161. self.URLString = "";
  162. break;
  163. }
  164. };
  165. this.xmlhttp.send(this.URLString);
  166. }
  167. }
  168. };
  169. this.reset();
  170. this.createAJAX();
  171. }