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

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/lib/OpenLayers/Ajax.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
 * @requires OpenLayers/Request/XMLHttpRequest.js
 
7
 */
 
8
 
 
9
//OpenLayers.ProxyHost = "";
 
10
OpenLayers.ProxyHost = "examples/proxy.cgi?url=";
 
11
 
 
12
/**
 
13
 * Ajax reader for OpenLayers
 
14
 *
 
15
 *  @uri url to do remote XML http get
 
16
 *  @param {String} 'get' format params (x=y&a=b...)
 
17
 *  @who object to handle callbacks for this request
 
18
 *  @complete  the function to be called on success 
 
19
 *  @failure  the function to be called on failure
 
20
 *  
 
21
 *   example usage from a caller:
 
22
 *  
 
23
 *     caps: function(request) {
 
24
 *      -blah-  
 
25
 *     },
 
26
 *  
 
27
 *     OpenLayers.loadURL(url,params,this,caps);
 
28
 *
 
29
 * Notice the above example does not provide an error handler; a default empty
 
30
 * handler is provided which merely logs the error if a failure handler is not 
 
31
 * supplied
 
32
 *
 
33
 */
 
34
 
 
35
 
 
36
/**
 
37
 * Function: OpenLayers.nullHandler
 
38
 * @param {} request
 
39
 */
 
40
OpenLayers.nullHandler = function(request) {
 
41
    OpenLayers.Console.userError(OpenLayers.i18n("unhandledRequest", {'statusText':request.statusText}));
 
42
};
 
43
 
 
44
/** 
 
45
 * APIFunction: loadURL
 
46
 * Background load a document.  For more flexibility in using XMLHttpRequest,
 
47
 *     see the <OpenLayers.Request> methods.
 
48
 *
 
49
 * Parameters:
 
50
 * uri - {String} URI of source doc
 
51
 * params - {String} or {Object} GET params. Either a string in the form
 
52
 *     "?hello=world&foo=bar" (do not forget the leading question mark)
 
53
 *     or an object in the form {'hello': 'world', 'foo': 'bar}
 
54
 * caller - {Object} object which gets callbacks
 
55
 * onComplete - {Function} Optional callback for success.  The callback
 
56
 *     will be called with this set to caller and will receive the request
 
57
 *     object as an argument.  Note that if you do not specify an onComplete
 
58
 *     function, <OpenLayers.nullHandler> will be called (which pops up a 
 
59
 *     user friendly error message dialog).
 
60
 * onFailure - {Function} Optional callback for failure.  In the event of
 
61
 *     a failure, the callback will be called with this set to caller and will
 
62
 *     receive the request object as an argument.  Note that if you do not
 
63
 *     specify an onComplete function, <OpenLayers.nullHandler> will be called
 
64
 *     (which pops up a user friendly error message dialog).
 
65
 *
 
66
 * Returns:
 
67
 * {<OpenLayers.Request.XMLHttpRequest>}  The request object. To abort loading,
 
68
 *     call request.abort().
 
69
 */
 
70
OpenLayers.loadURL = function(uri, params, caller,
 
71
                                  onComplete, onFailure) {
 
72
    
 
73
    if(typeof params == 'string') {
 
74
        params = OpenLayers.Util.getParameters(params);
 
75
    }
 
76
    var success = (onComplete) ? onComplete : OpenLayers.nullHandler;
 
77
    var failure = (onFailure) ? onFailure : OpenLayers.nullHandler;
 
78
    
 
79
    return OpenLayers.Request.GET({
 
80
        url: uri, params: params,
 
81
        success: success, failure: failure, scope: caller
 
82
    });
 
83
};
 
84
 
 
85
/** 
 
86
 * Function: parseXMLString
 
87
 * Parse XML into a doc structure
 
88
 * 
 
89
 * Parameters:
 
90
 * text - {String} 
 
91
 * 
 
92
 * Returns:
 
93
 * {?} Parsed AJAX Responsev
 
94
 */
 
95
OpenLayers.parseXMLString = function(text) {
 
96
 
 
97
    //MS sucks, if the server is bad it dies
 
98
    var index = text.indexOf('<');
 
99
    if (index > 0) {
 
100
        text = text.substring(index);
 
101
    }
 
102
 
 
103
    var ajaxResponse = OpenLayers.Util.Try(
 
104
        function() {
 
105
            var xmldom = new ActiveXObject('Microsoft.XMLDOM');
 
106
            xmldom.loadXML(text);
 
107
            return xmldom;
 
108
        },
 
109
        function() {
 
110
            return new DOMParser().parseFromString(text, 'text/xml');
 
111
        },
 
112
        function() {
 
113
            var req = new XMLHttpRequest();
 
114
            req.open("GET", "data:" + "text/xml" +
 
115
                     ";charset=utf-8," + encodeURIComponent(text), false);
 
116
            if (req.overrideMimeType) {
 
117
                req.overrideMimeType("text/xml");
 
118
            }
 
119
            req.send(null);
 
120
            return req.responseXML;
 
121
        }
 
122
    );
 
123
 
 
124
    return ajaxResponse;
 
125
};
 
126
 
 
127
 
 
128
/**
 
129
 * Namespace: OpenLayers.Ajax
 
130
 */
 
131
OpenLayers.Ajax = {
 
132
 
 
133
    /**
 
134
     * Method: emptyFunction
 
135
     */
 
136
    emptyFunction: function () {},
 
137
 
 
138
    /**
 
139
     * Method: getTransport
 
140
     * 
 
141
     * Returns: 
 
142
     * {Object} Transport mechanism for whichever browser we're in, or false if
 
143
     *          none available.
 
144
     */
 
145
    getTransport: function() {
 
146
        return OpenLayers.Util.Try(
 
147
            function() {return new XMLHttpRequest();},
 
148
            function() {return new ActiveXObject('Msxml2.XMLHTTP');},
 
149
            function() {return new ActiveXObject('Microsoft.XMLHTTP');}
 
150
        ) || false;
 
151
    },
 
152
 
 
153
    /**
 
154
     * Property: activeRequestCount
 
155
     * {Integer}
 
156
     */
 
157
    activeRequestCount: 0
 
158
};
 
159
 
 
160
/**
 
161
 * Namespace: OpenLayers.Ajax.Responders
 
162
 * {Object}
 
163
 */
 
164
OpenLayers.Ajax.Responders = {
 
165
  
 
166
    /**
 
167
     * Property: responders
 
168
     * {Array}
 
169
     */
 
170
    responders: [],
 
171
 
 
172
    /**
 
173
     * Method: register
 
174
     *  
 
175
     * Parameters:
 
176
     * responderToAdd - {?}
 
177
     */
 
178
    register: function(responderToAdd) {
 
179
        for (var i = 0; i < this.responders.length; i++){
 
180
            if (responderToAdd == this.responders[i]){
 
181
                return;
 
182
            }
 
183
        }
 
184
        this.responders.push(responderToAdd);
 
185
    },
 
186
 
 
187
    /**
 
188
     * Method: unregister
 
189
     *  
 
190
     * Parameters:
 
191
     * responderToRemove - {?}
 
192
     */
 
193
    unregister: function(responderToRemove) {
 
194
        OpenLayers.Util.removeItem(this.reponders, responderToRemove);
 
195
    },
 
196
 
 
197
    /**
 
198
     * Method: dispatch
 
199
     * 
 
200
     * Parameters:
 
201
     * callback - {?}
 
202
     * request - {?}
 
203
     * transport - {?}
 
204
     */
 
205
    dispatch: function(callback, request, transport) {
 
206
        var responder;
 
207
        for (var i = 0; i < this.responders.length; i++) {
 
208
            responder = this.responders[i];
 
209
     
 
210
            if (responder[callback] && 
 
211
                typeof responder[callback] == 'function') {
 
212
                try {
 
213
                    responder[callback].apply(responder, 
 
214
                                              [request, transport]);
 
215
                } catch (e) {}
 
216
            }
 
217
        }
 
218
    }
 
219
};
 
220
 
 
221
OpenLayers.Ajax.Responders.register({
 
222
    /** 
 
223
     * Function: onCreate
 
224
     */
 
225
    onCreate: function() {
 
226
        OpenLayers.Ajax.activeRequestCount++;
 
227
    },
 
228
 
 
229
    /**
 
230
     * Function: onComplete
 
231
     */
 
232
     onComplete: function() {
 
233
         OpenLayers.Ajax.activeRequestCount--;
 
234
     }
 
235
});
 
236
 
 
237
/**
 
238
 * Class: OpenLayers.Ajax.Base
 
239
 */
 
240
OpenLayers.Ajax.Base = OpenLayers.Class({
 
241
      
 
242
    /**
 
243
     * Constructor: OpenLayers.Ajax.Base
 
244
     * 
 
245
     * Parameters: 
 
246
     * options - {Object}
 
247
     */
 
248
    initialize: function(options) {
 
249
        this.options = {
 
250
            method:       'post',
 
251
            asynchronous: true,
 
252
            contentType:  'application/xml',
 
253
            parameters:   ''
 
254
        };
 
255
        OpenLayers.Util.extend(this.options, options || {});
 
256
        
 
257
        this.options.method = this.options.method.toLowerCase();
 
258
        
 
259
        if (typeof this.options.parameters == 'string') {
 
260
            this.options.parameters = 
 
261
                OpenLayers.Util.getParameters(this.options.parameters);
 
262
        }
 
263
    }
 
264
});
 
265
 
 
266
/**
 
267
 * Class: OpenLayers.Ajax.Request
 
268
 * *Deprecated*.  Use <OpenLayers.Request> method instead.
 
269
 *
 
270
 * Inherit:
 
271
 *  - <OpenLayers.Ajax.Base>
 
272
 */
 
273
OpenLayers.Ajax.Request = OpenLayers.Class(OpenLayers.Ajax.Base, {
 
274
 
 
275
    /**
 
276
     * Property: _complete
 
277
     *
 
278
     * {Boolean}
 
279
     */
 
280
    _complete: false,
 
281
      
 
282
    /**
 
283
     * Constructor: OpenLayers.Ajax.Request
 
284
     * 
 
285
     * Parameters: 
 
286
     * url - {String}
 
287
     * options - {Object}
 
288
     */
 
289
    initialize: function(url, options) {
 
290
        OpenLayers.Ajax.Base.prototype.initialize.apply(this, [options]);
 
291
        
 
292
        if (OpenLayers.ProxyHost && OpenLayers.String.startsWith(url, "http")) {
 
293
            url = OpenLayers.ProxyHost + encodeURIComponent(url);
 
294
        }
 
295
        
 
296
        this.transport = OpenLayers.Ajax.getTransport();
 
297
        this.request(url);
 
298
    },
 
299
 
 
300
    /**
 
301
     * Method: request
 
302
     * 
 
303
     * Parameters:
 
304
     * url - {String}
 
305
     */
 
306
    request: function(url) {
 
307
        this.url = url;
 
308
        this.method = this.options.method;
 
309
        var params = OpenLayers.Util.extend({}, this.options.parameters);
 
310
        
 
311
        if (this.method != 'get' && this.method != 'post') {
 
312
            // simulate other verbs over post
 
313
            params['_method'] = this.method;
 
314
            this.method = 'post';
 
315
        }
 
316
 
 
317
        this.parameters = params;        
 
318
        
 
319
        if (params = OpenLayers.Util.getParameterString(params)) {
 
320
            // when GET, append parameters to URL
 
321
            if (this.method == 'get') {
 
322
                this.url += ((this.url.indexOf('?') > -1) ? '&' : '?') + params;
 
323
            } else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent)) {
 
324
                params += '&_=';
 
325
            }
 
326
        }
 
327
        try {
 
328
            var response = new OpenLayers.Ajax.Response(this);
 
329
            if (this.options.onCreate) {
 
330
                this.options.onCreate(response);
 
331
            }
 
332
            
 
333
            OpenLayers.Ajax.Responders.dispatch('onCreate', 
 
334
                                                this, 
 
335
                                                response);
 
336
    
 
337
            this.transport.open(this.method.toUpperCase(), 
 
338
                                this.url,
 
339
                                this.options.asynchronous);
 
340
    
 
341
            if (this.options.asynchronous) {
 
342
                window.setTimeout(
 
343
                    OpenLayers.Function.bind(this.respondToReadyState, this, 1),
 
344
                    10);
 
345
            }
 
346
            
 
347
            this.transport.onreadystatechange = 
 
348
                OpenLayers.Function.bind(this.onStateChange, this);    
 
349
            this.setRequestHeaders();
 
350
    
 
351
            this.body =  this.method == 'post' ?
 
352
                (this.options.postBody || params) : null;
 
353
            this.transport.send(this.body);
 
354
    
 
355
            // Force Firefox to handle ready state 4 for synchronous requests
 
356
            if (!this.options.asynchronous && 
 
357
                this.transport.overrideMimeType) {
 
358
                this.onStateChange();
 
359
            }
 
360
        } catch (e) {
 
361
            this.dispatchException(e);
 
362
        }
 
363
    },
 
364
 
 
365
    /**
 
366
     * Method: onStateChange
 
367
     */
 
368
    onStateChange: function() {
 
369
        var readyState = this.transport.readyState;
 
370
        if (readyState > 1 && !((readyState == 4) && this._complete)) {
 
371
            this.respondToReadyState(this.transport.readyState);
 
372
        }
 
373
    },
 
374
     
 
375
    /**
 
376
     * Method: setRequestHeaders
 
377
     */
 
378
    setRequestHeaders: function() {
 
379
        var headers = {
 
380
            'X-Requested-With': 'XMLHttpRequest',
 
381
            'Accept': 'text/javascript, text/html, application/xml, text/xml, */*',
 
382
            'OpenLayers': true
 
383
        };
 
384
 
 
385
        if (this.method == 'post') {
 
386
            headers['Content-type'] = this.options.contentType +
 
387
                (this.options.encoding ? '; charset=' + this.options.encoding : '');
 
388
    
 
389
            /* Force "Connection: close" for older Mozilla browsers to work
 
390
             * around a bug where XMLHttpRequest sends an incorrect
 
391
             * Content-length header. See Mozilla Bugzilla #246651.
 
392
             */
 
393
            if (this.transport.overrideMimeType &&
 
394
                (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005) {
 
395
                headers['Connection'] = 'close';
 
396
            }
 
397
        }
 
398
        // user-defined headers
 
399
        if (typeof this.options.requestHeaders == 'object') {    
 
400
            var extras = this.options.requestHeaders;
 
401
            
 
402
            if (typeof extras.push == 'function') {
 
403
                for (var i = 0, length = extras.length; i < length; i += 2) {
 
404
                    headers[extras[i]] = extras[i+1];
 
405
                }
 
406
            } else {
 
407
                for (var i in extras) {
 
408
                    headers[i] = extras[i];
 
409
                }
 
410
            }
 
411
        }
 
412
        
 
413
        for (var name in headers) {
 
414
            this.transport.setRequestHeader(name, headers[name]);
 
415
        }
 
416
    },
 
417
    
 
418
    /**
 
419
     * Method: success
 
420
     *
 
421
     * Returns:
 
422
     * {Boolean} - 
 
423
     */
 
424
    success: function() {
 
425
        var status = this.getStatus();
 
426
        return !status || (status >=200 && status < 300);
 
427
    },
 
428
    
 
429
    /**
 
430
     * Method: getStatus
 
431
     *
 
432
     * Returns:
 
433
     * {Integer} - Status
 
434
     */
 
435
    getStatus: function() {
 
436
        try {
 
437
            return this.transport.status || 0;
 
438
        } catch (e) {
 
439
            return 0;
 
440
        }
 
441
    },
 
442
 
 
443
    /**
 
444
     * Method: respondToReadyState
 
445
     *
 
446
     * Parameters:
 
447
     * readyState - {?}
 
448
     */
 
449
    respondToReadyState: function(readyState) {
 
450
        var state = OpenLayers.Ajax.Request.Events[readyState];
 
451
        var response = new OpenLayers.Ajax.Response(this);
 
452
    
 
453
        if (state == 'Complete') {
 
454
            try {
 
455
                this._complete = true;
 
456
                (this.options['on' + response.status] ||
 
457
                    this.options['on' + (this.success() ? 'Success' : 'Failure')] ||
 
458
                    OpenLayers.Ajax.emptyFunction)(response);
 
459
            } catch (e) {
 
460
                this.dispatchException(e);
 
461
            }
 
462
    
 
463
            var contentType = response.getHeader('Content-type');
 
464
        }
 
465
    
 
466
        try {
 
467
            (this.options['on' + state] || 
 
468
             OpenLayers.Ajax.emptyFunction)(response);
 
469
             OpenLayers.Ajax.Responders.dispatch('on' + state, 
 
470
                                                 this, 
 
471
                                                 response);
 
472
        } catch (e) {
 
473
            this.dispatchException(e);
 
474
        }
 
475
    
 
476
        if (state == 'Complete') {
 
477
            // avoid memory leak in MSIE: clean up
 
478
            this.transport.onreadystatechange = OpenLayers.Ajax.emptyFunction;
 
479
        }
 
480
    },
 
481
    
 
482
    /**
 
483
     * Method: getHeader
 
484
     * 
 
485
     * Parameters:
 
486
     * name - {String} Header name
 
487
     *
 
488
     * Returns:
 
489
     * {?} - response header for the given name
 
490
     */
 
491
    getHeader: function(name) {
 
492
        try {
 
493
            return this.transport.getResponseHeader(name);
 
494
        } catch (e) {
 
495
            return null;
 
496
        }
 
497
    },
 
498
 
 
499
    /**
 
500
     * Method: dispatchException
 
501
     * If the optional onException function is set, execute it
 
502
     * and then dispatch the call to any other listener registered
 
503
     * for onException.
 
504
     * 
 
505
     * If no optional onException function is set, we suspect that
 
506
     * the user may have also not used
 
507
     * OpenLayers.Ajax.Responders.register to register a listener
 
508
     * for the onException call.  To make sure that something
 
509
     * gets done with this exception, only dispatch the call if there
 
510
     * are listeners.
 
511
     *
 
512
     * If you explicitly want to swallow exceptions, set
 
513
     * request.options.onException to an empty function (function(){})
 
514
     * or register an empty function with <OpenLayers.Ajax.Responders>
 
515
     * for onException.
 
516
     * 
 
517
     * Parameters:
 
518
     * exception - {?}
 
519
     */
 
520
    dispatchException: function(exception) {
 
521
        var handler = this.options.onException;
 
522
        if(handler) {
 
523
            // call options.onException and alert any other listeners
 
524
            handler(this, exception);
 
525
            OpenLayers.Ajax.Responders.dispatch('onException', this, exception);
 
526
        } else {
 
527
            // check if there are any other listeners
 
528
            var listener = false;
 
529
            var responders = OpenLayers.Ajax.Responders.responders;
 
530
            for (var i = 0; i < responders.length; i++) {
 
531
                if(responders[i].onException) {
 
532
                    listener = true;
 
533
                    break;
 
534
                }
 
535
            }
 
536
            if(listener) {
 
537
                // call all listeners
 
538
                OpenLayers.Ajax.Responders.dispatch('onException', this, exception);
 
539
            } else {
 
540
                // let the exception through
 
541
                throw exception;
 
542
            }
 
543
        }
 
544
    }
 
545
});
 
546
 
 
547
/** 
 
548
 * Property: Events
 
549
 * {Array(String)}
 
550
 */
 
551
OpenLayers.Ajax.Request.Events =
 
552
  ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
 
553
 
 
554
/**
 
555
 * Class: OpenLayers.Ajax.Response
 
556
 */
 
557
OpenLayers.Ajax.Response = OpenLayers.Class({
 
558
 
 
559
    /**
 
560
     * Property: status
 
561
     *
 
562
     * {Integer}
 
563
     */
 
564
    status: 0,
 
565
    
 
566
 
 
567
    /**
 
568
     * Property: statusText
 
569
     *
 
570
     * {String}
 
571
     */
 
572
    statusText: '',
 
573
      
 
574
    /**
 
575
     * Constructor: OpenLayers.Ajax.Response
 
576
     * 
 
577
     * Parameters: 
 
578
     * request - {Object}
 
579
     */
 
580
    initialize: function(request) {
 
581
        this.request = request;
 
582
        var transport = this.transport = request.transport,
 
583
            readyState = this.readyState = transport.readyState;
 
584
        
 
585
        if ((readyState > 2 &&
 
586
            !(!!(window.attachEvent && !window.opera))) ||
 
587
            readyState == 4) {
 
588
            this.status       = this.getStatus();
 
589
            this.statusText   = this.getStatusText();
 
590
            this.responseText = transport.responseText == null ?
 
591
                '' : String(transport.responseText);
 
592
        }
 
593
        
 
594
        if(readyState == 4) {
 
595
            var xml = transport.responseXML;
 
596
            this.responseXML  = xml === undefined ? null : xml;
 
597
        }
 
598
    },
 
599
    
 
600
    /**
 
601
     * Method: getStatus
 
602
     */
 
603
    getStatus: OpenLayers.Ajax.Request.prototype.getStatus,
 
604
    
 
605
    /**
 
606
     * Method: getStatustext
 
607
     *
 
608
     * Returns:
 
609
     * {String} - statusText
 
610
     */
 
611
    getStatusText: function() {
 
612
        try {
 
613
            return this.transport.statusText || '';
 
614
        } catch (e) {
 
615
            return '';
 
616
        }
 
617
    },
 
618
    
 
619
    /**
 
620
     * Method: getHeader
 
621
     */
 
622
    getHeader: OpenLayers.Ajax.Request.prototype.getHeader,
 
623
    
 
624
    /** 
 
625
     * Method: getResponseHeader
 
626
     *
 
627
     * Returns:
 
628
     * {?} - response header for given name
 
629
     */
 
630
    getResponseHeader: function(name) {
 
631
        return this.transport.getResponseHeader(name);
 
632
    }
 
633
});
 
634
 
 
635
 
 
636
/**
 
637
 * Function: getElementsByTagNameNS
 
638
 * 
 
639
 * Parameters:
 
640
 * parentnode - {?}
 
641
 * nsuri - {?}
 
642
 * nsprefix - {?}
 
643
 * tagname - {?}
 
644
 * 
 
645
 * Returns:
 
646
 * {?}
 
647
 */
 
648
OpenLayers.Ajax.getElementsByTagNameNS  = function(parentnode, nsuri, 
 
649
                                                   nsprefix, tagname) {
 
650
    var elem = null;
 
651
    if (parentnode.getElementsByTagNameNS) {
 
652
        elem = parentnode.getElementsByTagNameNS(nsuri, tagname);
 
653
    } else {
 
654
        elem = parentnode.getElementsByTagName(nsprefix + ':' + tagname);
 
655
    }
 
656
    return elem;
 
657
};
 
658
 
 
659
 
 
660
/**
 
661
 * Function: serializeXMLToString
 
662
 * Wrapper function around XMLSerializer, which doesn't exist/work in
 
663
 *     IE/Safari. We need to come up with a way to serialize in those browser:
 
664
 *     for now, these browsers will just fail. #535, #536
 
665
 *
 
666
 * Parameters: 
 
667
 * xmldom {XMLNode} xml dom to serialize
 
668
 * 
 
669
 * Returns:
 
670
 * {?}
 
671
 */
 
672
OpenLayers.Ajax.serializeXMLToString = function(xmldom) {
 
673
    var serializer = new XMLSerializer();
 
674
    var data = serializer.serializeToString(xmldom);
 
675
    return data;
 
676
};