i/o for single image, js update (CORS+generalize)
[face-privacy-filter.git] / web_demo / jquery-ajax-native.js
1 //     jQuery Ajax Native Plugin
2
3 //     (c) 2015 Tarik Zakaria Benmerar, Acigna Inc.
4 //      jQuery Ajax Native Plugin may be freely distributed under the MIT license.
5 (function (root, factory) {
6     if (typeof define === 'function' && define.amd) {
7         // AMD. Register as an anonymous module.
8         define(['jquery'], factory);
9     } else if (typeof exports === 'object') {
10         // Node. Does not work with strict CommonJS, but
11         // only CommonJS-like environments that support module.exports,
12         // like Node.
13         module.exports = factory(require('jquery'));
14     } else {
15         // Browser globals (root is window)
16         factory(root.jQuery);
17     }
18 }(this, function ( $ ) {
19     var ajaxSettings = $.ajaxSettings;
20     ajaxSettings.responseFields.native = 'responseNative';
21     ajaxSettings.converters[ '* native' ] = true;
22     var support = {},
23         xhrId = 0,
24         xhrSuccessStatus = {
25             // file protocol always yields status code 0, assume 200
26             0: 200,
27             // Support: IE9
28             // #1450: sometimes IE returns 1223 when it should be 204
29             1223: 204
30         },
31         xhrCallbacks = {},
32         xhrSupported = jQuery.ajaxSettings.xhr();
33     // Support: IE9
34     // Open requests must be manually aborted on unload (#5280)
35     if ( window.ActiveXObject ) {
36         $( window ).on( "unload", function() {
37             for ( var key in xhrCallbacks ) {
38                 xhrCallbacks[ key ]();
39             }
40         });
41     }
42     support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
43     support.ajax = xhrSupported = !!xhrSupported;
44
45     //Native Data Type Ajax Transport
46     $.ajaxTransport('native', function ( options ) {
47         var callback;
48         // Cross domain only allowed if supported through XMLHttpRequest
49         if ( support.cors || xhrSupported && !options.crossDomain ) {
50             return {
51                 send: function( headers, complete ) {
52                     var i,
53                         xhr = options.xhr(),
54                         id = ++xhrId,
55                         responses = {};
56
57                     xhr.open( options.type, options.url, options.async, options.username, options.password );
58
59                     // Apply custom fields if provided
60                     if ( options.xhrFields ) {
61                         for ( i in options.xhrFields ) {
62                             xhr[ i ] = options.xhrFields[ i ];
63                         }
64                     }
65
66                     // Override mime type if needed
67                     if ( options.mimeType && xhr.overrideMimeType ) {
68                         xhr.overrideMimeType( options.mimeType );
69                     }
70
71                     // X-Requested-With header
72                     // For cross-domain requests, seeing as conditions for a preflight are
73                     // akin to a jigsaw puzzle, we simply never set it to be sure.
74                     // (it can always be set on a per-request basis or even using ajaxSetup)
75                     // For same-domain requests, won't change header if already provided.
76                     if ( !options.crossDomain && !headers["X-Requested-With"] ) {
77                         headers["X-Requested-With"] = "XMLHttpRequest";
78                     }
79
80                     // Set headers
81                     for ( i in headers ) {
82                         xhr.setRequestHeader( i, headers[ i ] );
83                     }
84
85                     // Callback
86                     callback = function( type ) {
87                         return function() {
88                             if ( callback ) {
89                                 delete xhrCallbacks[ id ];
90                                 callback = xhr.onload = xhr.onerror = null;
91
92                                 if ( type === "abort" ) {
93                                     xhr.abort();
94                                 } else if ( type === "error" ) {
95                                     complete(
96                                         // file: protocol always yields status 0; see #8605, #14207
97                                         xhr.status,
98                                         xhr.statusText
99                                     );
100                                 } else {
101                                     // The native response associated with the responseType
102                                     // Stored in the xhr.response attribute (XHR2 Spec)
103                                     if ( xhr.response ) {
104                                         responses.native = xhr.response;
105                                     }
106
107                                     complete(
108                                         xhrSuccessStatus[ xhr.status ] || xhr.status,
109                                         xhr.statusText,
110                                         responses,
111                                         xhr.getAllResponseHeaders()
112                                     );
113                                 }
114                             }
115                         };
116                     };
117
118                     // Listen to events
119                     xhr.onload = callback();
120                     xhr.onerror = callback("error");
121
122                     // Create the abort callback
123                     callback = xhrCallbacks[ id ] = callback("abort");
124
125                     try {
126                         // Do send the request (this may raise an exception)
127                         xhr.send( options.hasContent && options.data || null );
128                     } catch ( e ) {
129                         // #14683: Only rethrow if this hasn't been notified as an error yet
130                         if ( callback ) {
131                             throw e;
132                         }
133                     }
134                 },
135
136                 abort: function() {
137                     if ( callback ) {
138                         callback();
139                     }
140                 }
141             };
142         }
143     });
144
145
146     //$.getNative wrapper
147     $.getNative = function ( url, callback ) {
148         return $.ajax({
149             dataType: 'native',
150             url: url,
151             xhrFields: {
152                 responseType: 'arraybuffer'
153             },
154             success: callback
155         });
156     };
157
158     //$.getBlob wrapper
159     $.getBlob = function ( url, callback ) {
160         return $.ajax({
161             dataType: 'native',
162             url: url,
163             xhrFields: {
164                 responseType: 'blob'
165             },
166             success: callback
167         });
168     };
169
170     //Return the jQuery Object
171     return $;
172
173 }));