~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/lib/OpenLayers/Request.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
 
2
 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 
3
 * full text of the license. */
 
4
 
 
5
/**
 
6
 * Namespace: OpenLayers.Request
 
7
 * The OpenLayers.Request namespace contains convenience methods for working
 
8
 *     with XMLHttpRequests.  These methods work with a cross-browser
 
9
 *     W3C compliant <OpenLayers.Request.XMLHttpRequest> class.
 
10
 */
 
11
OpenLayers.Request = {
 
12
    
 
13
    /**
 
14
     * Constant: DEFAULT_CONFIG
 
15
     * {Object} Default configuration for all requests.
 
16
     */
 
17
    DEFAULT_CONFIG: {
 
18
        method: "GET",
 
19
        url: window.location.href,
 
20
        async: true,
 
21
        user: undefined,
 
22
        password: undefined,
 
23
        params: null,
 
24
        proxy: OpenLayers.ProxyHost,
 
25
        headers: {},
 
26
        data: null,
 
27
        callback: function() {},
 
28
        success: null,
 
29
        failure: null,
 
30
        scope: null
 
31
    },
 
32
    
 
33
    /**
 
34
     * APIMethod: issue
 
35
     * Create a new XMLHttpRequest object, open it, set any headers, bind
 
36
     *     a callback to done state, and send any data.  It is recommended that
 
37
     *     you use one <GET>, <POST>, <PUT>, <DELETE>, <OPTIONS>, or <HEAD>.
 
38
     *     This method is only documented to provide detail on the configuration
 
39
     *     options available to all request methods.
 
40
     *
 
41
     * Parameters:
 
42
     * config - {Object} Object containing properties for configuring the
 
43
     *     request.  Allowed configuration properties are described below.
 
44
     *     This object is modified and should not be reused.
 
45
     *
 
46
     * Allowed config properties:
 
47
     * method - {String} One of GET, POST, PUT, DELETE, HEAD, or
 
48
     *     OPTIONS.  Default is GET.
 
49
     * url - {String} URL for the request.
 
50
     * async - {Boolean} Open an asynchronous request.  Default is true.
 
51
     * user - {String} User for relevant authentication scheme.  Set
 
52
     *     to null to clear current user.
 
53
     * password - {String} Password for relevant authentication scheme.
 
54
     *     Set to null to clear current password.
 
55
     * proxy - {String} Optional proxy.  Defaults to
 
56
     *     <OpenLayers.ProxyHost>.
 
57
     * params - {Object} Any key:value pairs to be appended to the
 
58
     *     url as a query string.  Assumes url doesn't already include a query
 
59
     *     string or hash.  Typically, this is only appropriate for <GET>
 
60
     *     requests where the query string will be appended to the url.
 
61
     *     Parameter values that are arrays will be
 
62
     *     concatenated with a comma (note that this goes against form-encoding)
 
63
     *     as is done with <OpenLayers.Util.getParameterString>.
 
64
     * headers - {Object} Object with header:value pairs to be set on
 
65
     *     the request.
 
66
     * data - {String | Document} Optional data to send with the request.
 
67
     *     Typically, this is only used with <POST> and <PUT> requests.
 
68
     *     Make sure to provide the appropriate "Content-Type" header for your
 
69
     *     data.  For <POST> and <PUT> requests, the content type defaults to
 
70
     *     "application-xml".  If your data is a different content type, or
 
71
     *     if you are using a different HTTP method, set the "Content-Type"
 
72
     *     header to match your data type.
 
73
     * callback - {Function} Function to call when request is done.
 
74
     *     To determine if the request failed, check request.status (200
 
75
     *     indicates success).
 
76
     * success - {Function} Optional function to call if request status is in
 
77
     *     the 200s.  This will be called in addition to callback above and
 
78
     *     would typically only be used as an alternative.
 
79
     * failure - {Function} Optional function to call if request status is not
 
80
     *     in the 200s.  This will be called in addition to callback above and
 
81
     *     would typically only be used as an alternative.
 
82
     * scope - {Object} If callback is a public method on some object,
 
83
     *     set the scope to that object.
 
84
     *
 
85
     * Returns:
 
86
     * {XMLHttpRequest} Request object.  To abort the request before a response
 
87
     *     is received, call abort() on the request object.
 
88
     */
 
89
    issue: function(config) {        
 
90
        // apply default config - proxy host may have changed
 
91
        var defaultConfig = OpenLayers.Util.extend(
 
92
            this.DEFAULT_CONFIG,
 
93
            {proxy: OpenLayers.ProxyHost}
 
94
        );
 
95
        config = OpenLayers.Util.applyDefaults(config, defaultConfig);
 
96
 
 
97
        // create request, open, and set headers
 
98
        var request = new OpenLayers.Request.XMLHttpRequest();
 
99
        var url = config.url;
 
100
        if(config.params) {
 
101
            var paramString = OpenLayers.Util.getParameterString(config.params);
 
102
            if(paramString.length > 0) {
 
103
                var separator = (url.indexOf('?') > -1) ? '&' : '?';
 
104
                url += separator + paramString;
 
105
            }
 
106
        }
 
107
        if(config.proxy && (url.indexOf("http") == 0)) {
 
108
            url = config.proxy + encodeURIComponent(url);
 
109
        }
 
110
        request.open(
 
111
            config.method, url, config.async, config.user, config.password
 
112
        );
 
113
        for(var header in config.headers) {
 
114
            request.setRequestHeader(header, config.headers[header]);
 
115
        }
 
116
 
 
117
        // bind callbacks to readyState 4 (done)
 
118
        var complete = (config.scope) ?
 
119
            OpenLayers.Function.bind(config.callback, config.scope) :
 
120
            config.callback;
 
121
        
 
122
        // optional success callback
 
123
        var success;
 
124
        if(config.success) {
 
125
            success = (config.scope) ?
 
126
                OpenLayers.Function.bind(config.success, config.scope) :
 
127
                config.success;
 
128
        }
 
129
 
 
130
        // optional failure callback
 
131
        var failure;
 
132
        if(config.failure) {
 
133
            failure = (config.scope) ?
 
134
                OpenLayers.Function.bind(config.failure, config.scope) :
 
135
                config.failure;
 
136
        }
 
137
         
 
138
        request.onreadystatechange = function() {
 
139
            if(request.readyState == OpenLayers.Request.XMLHttpRequest.DONE) {
 
140
                complete(request);
 
141
                if(success && (!request.status ||
 
142
                   (request.status >= 200 && request.status < 300))) {
 
143
                    success(request);
 
144
                }
 
145
                if(failure && (request.status &&
 
146
                   (request.status < 200 || request.status >= 300))) {
 
147
                    failure(request);
 
148
                }
 
149
            }
 
150
        };
 
151
        
 
152
        // send request (optionally with data) and return
 
153
        request.send(config.data);
 
154
        return request;
 
155
    },
 
156
    
 
157
    /**
 
158
     * APIMethod: GET
 
159
     * Send an HTTP GET request.  Additional configuration properties are
 
160
     *     documented in the <issue> method, with the method property set
 
161
     *     to GET.
 
162
     *
 
163
     * Parameters:
 
164
     * config - {Object} Object with properties for configuring the request.
 
165
     *     See the <issue> method for documentation of allowed properties.
 
166
     *     This object is modified and should not be reused.
 
167
     * 
 
168
     * Returns:
 
169
     * {XMLHttpRequest} Request object.
 
170
     */
 
171
    GET: function(config) {
 
172
        config = OpenLayers.Util.extend(config, {method: "GET"});
 
173
        return OpenLayers.Request.issue(config);
 
174
    },
 
175
    
 
176
    /**
 
177
     * APIMethod: POST
 
178
     * Send a POST request.  Additional configuration properties are
 
179
     *     documented in the <issue> method, with the method property set
 
180
     *     to POST and "Content-Type" header set to "application/xml".
 
181
     *
 
182
     * Parameters:
 
183
     * config - {Object} Object with properties for configuring the request.
 
184
     *     See the <issue> method for documentation of allowed properties.  The
 
185
     *     default "Content-Type" header will be set to "application-xml" if
 
186
     *     none is provided.  This object is modified and should not be reused.
 
187
     * 
 
188
     * Returns:
 
189
     * {XMLHttpRequest} Request object.
 
190
     */
 
191
    POST: function(config) {
 
192
        config = OpenLayers.Util.extend(config, {method: "POST"});
 
193
        // set content type to application/xml if it isn't already set
 
194
        config.headers = config.headers ? config.headers : {};
 
195
        if(!("CONTENT-TYPE" in OpenLayers.Util.upperCaseObject(config.headers))) {
 
196
            config.headers["Content-Type"] = "application/xml";
 
197
        }
 
198
        return OpenLayers.Request.issue(config);
 
199
    },
 
200
    
 
201
    /**
 
202
     * APIMethod: PUT
 
203
     * Send an HTTP PUT request.  Additional configuration properties are
 
204
     *     documented in the <issue> method, with the method property set
 
205
     *     to PUT and "Content-Type" header set to "application/xml".
 
206
     *
 
207
     * Parameters:
 
208
     * config - {Object} Object with properties for configuring the request.
 
209
     *     See the <issue> method for documentation of allowed properties.  The
 
210
     *     default "Content-Type" header will be set to "application-xml" if
 
211
     *     none is provided.  This object is modified and should not be reused.
 
212
     * 
 
213
     * Returns:
 
214
     * {XMLHttpRequest} Request object.
 
215
     */
 
216
    PUT: function(config) {
 
217
        config = OpenLayers.Util.extend(config, {method: "PUT"});
 
218
        // set content type to application/xml if it isn't already set
 
219
        config.headers = config.headers ? config.headers : {};
 
220
        if(!("CONTENT-TYPE" in OpenLayers.Util.upperCaseObject(config.headers))) {
 
221
            config.headers["Content-Type"] = "application/xml";
 
222
        }
 
223
        return OpenLayers.Request.issue(config);
 
224
    },
 
225
    
 
226
    /**
 
227
     * APIMethod: DELETE
 
228
     * Send an HTTP DELETE request.  Additional configuration properties are
 
229
     *     documented in the <issue> method, with the method property set
 
230
     *     to DELETE.
 
231
     *
 
232
     * Parameters:
 
233
     * config - {Object} Object with properties for configuring the request.
 
234
     *     See the <issue> method for documentation of allowed properties.
 
235
     *     This object is modified and should not be reused.
 
236
     * 
 
237
     * Returns:
 
238
     * {XMLHttpRequest} Request object.
 
239
     */
 
240
    DELETE: function(config) {
 
241
        config = OpenLayers.Util.extend(config, {method: "DELETE"});
 
242
        return OpenLayers.Request.issue(config);
 
243
    },
 
244
  
 
245
    /**
 
246
     * APIMethod: HEAD
 
247
     * Send an HTTP HEAD request.  Additional configuration properties are
 
248
     *     documented in the <issue> method, with the method property set
 
249
     *     to HEAD.
 
250
     *
 
251
     * Parameters:
 
252
     * config - {Object} Object with properties for configuring the request.
 
253
     *     See the <issue> method for documentation of allowed properties.
 
254
     *     This object is modified and should not be reused.
 
255
     * 
 
256
     * Returns:
 
257
     * {XMLHttpRequest} Request object.
 
258
     */
 
259
    HEAD: function(config) {
 
260
        config = OpenLayers.Util.extend(config, {method: "HEAD"});
 
261
        return OpenLayers.Request.issue(config);
 
262
    },
 
263
    
 
264
    /**
 
265
     * APIMethod: OPTIONS
 
266
     * Send an HTTP OPTIONS request.  Additional configuration properties are
 
267
     *     documented in the <issue> method, with the method property set
 
268
     *     to OPTIONS.
 
269
     *
 
270
     * Parameters:
 
271
     * config - {Object} Object with properties for configuring the request.
 
272
     *     See the <issue> method for documentation of allowed properties.
 
273
     *     This object is modified and should not be reused.
 
274
     * 
 
275
     * Returns:
 
276
     * {XMLHttpRequest} Request object.
 
277
     */
 
278
    OPTIONS: function(config) {
 
279
        config = OpenLayers.Util.extend(config, {method: "OPTIONS"});
 
280
        return OpenLayers.Request.issue(config);
 
281
    }
 
282
 
 
283
};