initial commit
[namibia] / public / scripts / blueimp-jQuery-File-Upload / js / cors / jquery.xdr-transport.js
1 /*
2  * jQuery XDomainRequest Transport Plugin 1.1.2
3  * https://github.com/blueimp/jQuery-File-Upload
4  *
5  * Copyright 2011, Sebastian Tschan
6  * https://blueimp.net
7  *
8  * Licensed under the MIT license:
9  * http://www.opensource.org/licenses/MIT
10  *
11  * Based on Julian Aubourg's ajaxHooks xdr.js:
12  * https://github.com/jaubourg/ajaxHooks/
13  */
14
15 /*jslint unparam: true */
16 /*global define, window, XDomainRequest */
17
18 (function (factory) {
19     'use strict';
20     if (typeof define === 'function' && define.amd) {
21         // Register as an anonymous AMD module:
22         define(['jquery'], factory);
23     } else {
24         // Browser globals:
25         factory(window.jQuery);
26     }
27 }(function ($) {
28     'use strict';
29     if (window.XDomainRequest && !$.support.cors) {
30         $.ajaxTransport(function (s) {
31             if (s.crossDomain && s.async) {
32                 if (s.timeout) {
33                     s.xdrTimeout = s.timeout;
34                     delete s.timeout;
35                 }
36                 var xdr;
37                 return {
38                     send: function (headers, completeCallback) {
39                         function callback(status, statusText, responses, responseHeaders) {
40                             xdr.onload = xdr.onerror = xdr.ontimeout = $.noop;
41                             xdr = null;
42                             completeCallback(status, statusText, responses, responseHeaders);
43                         }
44                         xdr = new XDomainRequest();
45                         // XDomainRequest only supports GET and POST:
46                         if (s.type === 'DELETE') {
47                             s.url = s.url + (/\?/.test(s.url) ? '&' : '?') +
48                                 '_method=DELETE';
49                             s.type = 'POST';
50                         } else if (s.type === 'PUT') {
51                             s.url = s.url + (/\?/.test(s.url) ? '&' : '?') +
52                                 '_method=PUT';
53                             s.type = 'POST';
54                         }
55                         xdr.open(s.type, s.url);
56                         xdr.onload = function () {
57                             callback(
58                                 200,
59                                 'OK',
60                                 {text: xdr.responseText},
61                                 'Content-Type: ' + xdr.contentType
62                             );
63                         };
64                         xdr.onerror = function () {
65                             callback(404, 'Not Found');
66                         };
67                         if (s.xdrTimeout) {
68                             xdr.ontimeout = function () {
69                                 callback(0, 'timeout');
70                             };
71                             xdr.timeout = s.xdrTimeout;
72                         }
73                         xdr.send((s.hasContent && s.data) || null);
74                     },
75                     abort: function () {
76                         if (xdr) {
77                             xdr.onerror = $.noop();
78                             xdr.abort();
79                         }
80                     }
81                 };
82             }
83         });
84     }
85 }));