PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/public/phpmyadmin/js/openlayers/src/openlayers/lib/OpenLayers/Request.js

https://gitlab.com/qbarbosa/klindev
JavaScript | 358 lines | 136 code | 23 blank | 199 comment | 27 complexity | e614a06cfe304fe374566117f960b625 MD5 | raw file
  1. /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the Clear BSD license.
  3. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Events.js
  7. */
  8. /**
  9. * Namespace: OpenLayers.Request
  10. * The OpenLayers.Request namespace contains convenience methods for working
  11. * with XMLHttpRequests. These methods work with a cross-browser
  12. * W3C compliant <OpenLayers.Request.XMLHttpRequest> class.
  13. */
  14. OpenLayers.Request = {
  15. /**
  16. * Constant: DEFAULT_CONFIG
  17. * {Object} Default configuration for all requests.
  18. */
  19. DEFAULT_CONFIG: {
  20. method: "GET",
  21. url: window.location.href,
  22. async: true,
  23. user: undefined,
  24. password: undefined,
  25. params: null,
  26. proxy: OpenLayers.ProxyHost,
  27. headers: {},
  28. data: null,
  29. callback: function() {},
  30. success: null,
  31. failure: null,
  32. scope: null
  33. },
  34. /**
  35. * APIProperty: events
  36. * {<OpenLayers.Events>} An events object that handles all
  37. * events on the {<OpenLayers.Request>} object.
  38. *
  39. * All event listeners will receive an event object with three properties:
  40. * request - {<OpenLayers.Request.XMLHttpRequest>} The request object.
  41. * config - {Object} The config object sent to the specific request method.
  42. * requestUrl - {String} The request url.
  43. *
  44. * Supported event types:
  45. * complete - Triggered when we have a response from the request, if a
  46. * listener returns false, no further response processing will take
  47. * place.
  48. * success - Triggered when the HTTP response has a success code (200-299).
  49. * failure - Triggered when the HTTP response does not have a success code.
  50. */
  51. events: new OpenLayers.Events(this, null, ["complete", "success", "failure"]),
  52. /**
  53. * APIMethod: issue
  54. * Create a new XMLHttpRequest object, open it, set any headers, bind
  55. * a callback to done state, and send any data. It is recommended that
  56. * you use one <GET>, <POST>, <PUT>, <DELETE>, <OPTIONS>, or <HEAD>.
  57. * This method is only documented to provide detail on the configuration
  58. * options available to all request methods.
  59. *
  60. * Parameters:
  61. * config - {Object} Object containing properties for configuring the
  62. * request. Allowed configuration properties are described below.
  63. * This object is modified and should not be reused.
  64. *
  65. * Allowed config properties:
  66. * method - {String} One of GET, POST, PUT, DELETE, HEAD, or
  67. * OPTIONS. Default is GET.
  68. * url - {String} URL for the request.
  69. * async - {Boolean} Open an asynchronous request. Default is true.
  70. * user - {String} User for relevant authentication scheme. Set
  71. * to null to clear current user.
  72. * password - {String} Password for relevant authentication scheme.
  73. * Set to null to clear current password.
  74. * proxy - {String} Optional proxy. Defaults to
  75. * <OpenLayers.ProxyHost>.
  76. * params - {Object} Any key:value pairs to be appended to the
  77. * url as a query string. Assumes url doesn't already include a query
  78. * string or hash. Typically, this is only appropriate for <GET>
  79. * requests where the query string will be appended to the url.
  80. * Parameter values that are arrays will be
  81. * concatenated with a comma (note that this goes against form-encoding)
  82. * as is done with <OpenLayers.Util.getParameterString>.
  83. * headers - {Object} Object with header:value pairs to be set on
  84. * the request.
  85. * data - {String | Document} Optional data to send with the request.
  86. * Typically, this is only used with <POST> and <PUT> requests.
  87. * Make sure to provide the appropriate "Content-Type" header for your
  88. * data. For <POST> and <PUT> requests, the content type defaults to
  89. * "application-xml". If your data is a different content type, or
  90. * if you are using a different HTTP method, set the "Content-Type"
  91. * header to match your data type.
  92. * callback - {Function} Function to call when request is done.
  93. * To determine if the request failed, check request.status (200
  94. * indicates success).
  95. * success - {Function} Optional function to call if request status is in
  96. * the 200s. This will be called in addition to callback above and
  97. * would typically only be used as an alternative.
  98. * failure - {Function} Optional function to call if request status is not
  99. * in the 200s. This will be called in addition to callback above and
  100. * would typically only be used as an alternative.
  101. * scope - {Object} If callback is a public method on some object,
  102. * set the scope to that object.
  103. *
  104. * Returns:
  105. * {XMLHttpRequest} Request object. To abort the request before a response
  106. * is received, call abort() on the request object.
  107. */
  108. issue: function(config) {
  109. // apply default config - proxy host may have changed
  110. var defaultConfig = OpenLayers.Util.extend(
  111. this.DEFAULT_CONFIG,
  112. {proxy: OpenLayers.ProxyHost}
  113. );
  114. config = OpenLayers.Util.applyDefaults(config, defaultConfig);
  115. // create request, open, and set headers
  116. var request = new OpenLayers.Request.XMLHttpRequest();
  117. var url = config.url;
  118. if(config.params) {
  119. var paramString = OpenLayers.Util.getParameterString(config.params);
  120. if(paramString.length > 0) {
  121. var separator = (url.indexOf('?') > -1) ? '&' : '?';
  122. url += separator + paramString;
  123. }
  124. }
  125. if(config.proxy && (url.indexOf("http") == 0)) {
  126. if(typeof config.proxy == "function") {
  127. url = config.proxy(url);
  128. } else {
  129. url = config.proxy + encodeURIComponent(url);
  130. }
  131. }
  132. request.open(
  133. config.method, url, config.async, config.user, config.password
  134. );
  135. for(var header in config.headers) {
  136. request.setRequestHeader(header, config.headers[header]);
  137. }
  138. var events = this.events;
  139. // we want to execute runCallbacks with "this" as the
  140. // execution scope
  141. var self = this;
  142. request.onreadystatechange = function() {
  143. if(request.readyState == OpenLayers.Request.XMLHttpRequest.DONE) {
  144. var proceed = events.triggerEvent(
  145. "complete",
  146. {request: request, config: config, requestUrl: url}
  147. );
  148. if(proceed !== false) {
  149. self.runCallbacks(
  150. {request: request, config: config, requestUrl: url}
  151. );
  152. }
  153. }
  154. };
  155. // send request (optionally with data) and return
  156. // call in a timeout for asynchronous requests so the return is
  157. // available before readyState == 4 for cached docs
  158. if(config.async === false) {
  159. request.send(config.data);
  160. } else {
  161. window.setTimeout(function(){
  162. if (request._aborted !== true) {
  163. request.send(config.data);
  164. }
  165. }, 0);
  166. }
  167. return request;
  168. },
  169. /**
  170. * Method: runCallbacks
  171. * Calls the complete, success and failure callbacks. Application
  172. * can listen to the "complete" event, have the listener
  173. * display a confirm window and always return false, and
  174. * execute OpenLayers.Request.runCallbacks if the user
  175. * hits "yes" in the confirm window.
  176. *
  177. * Parameters:
  178. * options - {Object} Hash containing request, config and requestUrl keys
  179. */
  180. runCallbacks: function(options) {
  181. var request = options.request;
  182. var config = options.config;
  183. // bind callbacks to readyState 4 (done)
  184. var complete = (config.scope) ?
  185. OpenLayers.Function.bind(config.callback, config.scope) :
  186. config.callback;
  187. // optional success callback
  188. var success;
  189. if(config.success) {
  190. success = (config.scope) ?
  191. OpenLayers.Function.bind(config.success, config.scope) :
  192. config.success;
  193. }
  194. // optional failure callback
  195. var failure;
  196. if(config.failure) {
  197. failure = (config.scope) ?
  198. OpenLayers.Function.bind(config.failure, config.scope) :
  199. config.failure;
  200. }
  201. complete(request);
  202. if (!request.status || (request.status >= 200 && request.status < 300)) {
  203. this.events.triggerEvent("success", options);
  204. if(success) {
  205. success(request);
  206. }
  207. }
  208. if(request.status && (request.status < 200 || request.status >= 300)) {
  209. this.events.triggerEvent("failure", options);
  210. if(failure) {
  211. failure(request);
  212. }
  213. }
  214. },
  215. /**
  216. * APIMethod: GET
  217. * Send an HTTP GET request. Additional configuration properties are
  218. * documented in the <issue> method, with the method property set
  219. * to GET.
  220. *
  221. * Parameters:
  222. * config - {Object} Object with properties for configuring the request.
  223. * See the <issue> method for documentation of allowed properties.
  224. * This object is modified and should not be reused.
  225. *
  226. * Returns:
  227. * {XMLHttpRequest} Request object.
  228. */
  229. GET: function(config) {
  230. config = OpenLayers.Util.extend(config, {method: "GET"});
  231. return OpenLayers.Request.issue(config);
  232. },
  233. /**
  234. * APIMethod: POST
  235. * Send a POST request. Additional configuration properties are
  236. * documented in the <issue> method, with the method property set
  237. * to POST and "Content-Type" header set to "application/xml".
  238. *
  239. * Parameters:
  240. * config - {Object} Object with properties for configuring the request.
  241. * See the <issue> method for documentation of allowed properties. The
  242. * default "Content-Type" header will be set to "application-xml" if
  243. * none is provided. This object is modified and should not be reused.
  244. *
  245. * Returns:
  246. * {XMLHttpRequest} Request object.
  247. */
  248. POST: function(config) {
  249. config = OpenLayers.Util.extend(config, {method: "POST"});
  250. // set content type to application/xml if it isn't already set
  251. config.headers = config.headers ? config.headers : {};
  252. if(!("CONTENT-TYPE" in OpenLayers.Util.upperCaseObject(config.headers))) {
  253. config.headers["Content-Type"] = "application/xml";
  254. }
  255. return OpenLayers.Request.issue(config);
  256. },
  257. /**
  258. * APIMethod: PUT
  259. * Send an HTTP PUT request. Additional configuration properties are
  260. * documented in the <issue> method, with the method property set
  261. * to PUT and "Content-Type" header set to "application/xml".
  262. *
  263. * Parameters:
  264. * config - {Object} Object with properties for configuring the request.
  265. * See the <issue> method for documentation of allowed properties. The
  266. * default "Content-Type" header will be set to "application-xml" if
  267. * none is provided. This object is modified and should not be reused.
  268. *
  269. * Returns:
  270. * {XMLHttpRequest} Request object.
  271. */
  272. PUT: function(config) {
  273. config = OpenLayers.Util.extend(config, {method: "PUT"});
  274. // set content type to application/xml if it isn't already set
  275. config.headers = config.headers ? config.headers : {};
  276. if(!("CONTENT-TYPE" in OpenLayers.Util.upperCaseObject(config.headers))) {
  277. config.headers["Content-Type"] = "application/xml";
  278. }
  279. return OpenLayers.Request.issue(config);
  280. },
  281. /**
  282. * APIMethod: DELETE
  283. * Send an HTTP DELETE request. Additional configuration properties are
  284. * documented in the <issue> method, with the method property set
  285. * to DELETE.
  286. *
  287. * Parameters:
  288. * config - {Object} Object with properties for configuring the request.
  289. * See the <issue> method for documentation of allowed properties.
  290. * This object is modified and should not be reused.
  291. *
  292. * Returns:
  293. * {XMLHttpRequest} Request object.
  294. */
  295. DELETE: function(config) {
  296. config = OpenLayers.Util.extend(config, {method: "DELETE"});
  297. return OpenLayers.Request.issue(config);
  298. },
  299. /**
  300. * APIMethod: HEAD
  301. * Send an HTTP HEAD request. Additional configuration properties are
  302. * documented in the <issue> method, with the method property set
  303. * to HEAD.
  304. *
  305. * Parameters:
  306. * config - {Object} Object with properties for configuring the request.
  307. * See the <issue> method for documentation of allowed properties.
  308. * This object is modified and should not be reused.
  309. *
  310. * Returns:
  311. * {XMLHttpRequest} Request object.
  312. */
  313. HEAD: function(config) {
  314. config = OpenLayers.Util.extend(config, {method: "HEAD"});
  315. return OpenLayers.Request.issue(config);
  316. },
  317. /**
  318. * APIMethod: OPTIONS
  319. * Send an HTTP OPTIONS request. Additional configuration properties are
  320. * documented in the <issue> method, with the method property set
  321. * to OPTIONS.
  322. *
  323. * Parameters:
  324. * config - {Object} Object with properties for configuring the request.
  325. * See the <issue> method for documentation of allowed properties.
  326. * This object is modified and should not be reused.
  327. *
  328. * Returns:
  329. * {XMLHttpRequest} Request object.
  330. */
  331. OPTIONS: function(config) {
  332. config = OpenLayers.Util.extend(config, {method: "OPTIONS"});
  333. return OpenLayers.Request.issue(config);
  334. }
  335. };