scala-news-reader
rss/atom news reader in scala
git clone https://9o.is/git/scala-news-reader.git
jquery.iframe-transport.js
(10048B)
1 /*
2 * jQuery Iframe Transport Plugin 1.7
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
12 /*jslint unparam: true, nomen: true */
13 /*global define, window, document */
14
15 (function (factory) {
16 'use strict';
17 if (typeof define === 'function' && define.amd) {
18 // Register as an anonymous AMD module:
19 define(['jquery'], factory);
20 } else {
21 // Browser globals:
22 factory(window.jQuery);
23 }
24 }(function ($) {
25 'use strict';
26
27 // Helper variable to create unique names for the transport iframes:
28 var counter = 0;
29
30 // The iframe transport accepts three additional options:
31 // options.fileInput: a jQuery collection of file input fields
32 // options.paramName: the parameter name for the file form data,
33 // overrides the name property of the file input field(s),
34 // can be a string or an array of strings.
35 // options.formData: an array of objects with name and value properties,
36 // equivalent to the return data of .serializeArray(), e.g.:
37 // [{name: 'a', value: 1}, {name: 'b', value: 2}]
38 $.ajaxTransport('iframe', function (options) {
39 if (options.async) {
40 var form,
41 iframe,
42 addParamChar;
43 return {
44 send: function (_, completeCallback) {
45 form = $('<form style="display:none;"></form>');
46 form.attr('accept-charset', options.formAcceptCharset);
47 addParamChar = /\?/.test(options.url) ? '&' : '?';
48 // XDomainRequest only supports GET and POST:
49 if (options.type === 'DELETE') {
50 options.url = options.url + addParamChar + '_method=DELETE';
51 options.type = 'POST';
52 } else if (options.type === 'PUT') {
53 options.url = options.url + addParamChar + '_method=PUT';
54 options.type = 'POST';
55 } else if (options.type === 'PATCH') {
56 options.url = options.url + addParamChar + '_method=PATCH';
57 options.type = 'POST';
58 }
59 // javascript:false as initial iframe src
60 // prevents warning popups on HTTPS in IE6.
61 // IE versions below IE8 cannot set the name property of
62 // elements that have already been added to the DOM,
63 // so we set the name along with the iframe HTML markup:
64 counter += 1;
65 iframe = $(
66 '<iframe src="javascript:false;" name="iframe-transport-' +
67 counter + '"></iframe>'
68 ).bind('load', function () {
69 var fileInputClones,
70 paramNames = $.isArray(options.paramName) ?
71 options.paramName : [options.paramName];
72 iframe
73 .unbind('load')
74 .bind('load', function () {
75 var response;
76 // Wrap in a try/catch block to catch exceptions thrown
77 // when trying to access cross-domain iframe contents:
78 try {
79 response = iframe.contents();
80 // Google Chrome and Firefox do not throw an
81 // exception when calling iframe.contents() on
82 // cross-domain requests, so we unify the response:
83 if (!response.length || !response[0].firstChild) {
84 throw new Error();
85 }
86 } catch (e) {
87 response = undefined;
88 }
89 // The complete callback returns the
90 // iframe content document as response object:
91 completeCallback(
92 200,
93 'success',
94 {'iframe': response}
95 );
96 // Fix for IE endless progress bar activity bug
97 // (happens on form submits to iframe targets):
98 $('<iframe src="javascript:false;"></iframe>')
99 .appendTo(form);
100 window.setTimeout(function () {
101 // Removing the form in a setTimeout call
102 // allows Chrome's developer tools to display
103 // the response result
104 form.remove();
105 }, 0);
106 });
107 form
108 .prop('target', iframe.prop('name'))
109 .prop('action', options.url)
110 .prop('method', options.type);
111 if (options.formData) {
112 $.each(options.formData, function (index, field) {
113 $('<input type="hidden"/>')
114 .prop('name', field.name)
115 .val(field.value)
116 .appendTo(form);
117 });
118 }
119 if (options.fileInput && options.fileInput.length &&
120 options.type === 'POST') {
121 fileInputClones = options.fileInput.clone();
122 // Insert a clone for each file input field:
123 options.fileInput.after(function (index) {
124 return fileInputClones[index];
125 });
126 if (options.paramName) {
127 options.fileInput.each(function (index) {
128 $(this).prop(
129 'name',
130 paramNames[index] || options.paramName
131 );
132 });
133 }
134 // Appending the file input fields to the hidden form
135 // removes them from their original location:
136 form
137 .append(options.fileInput)
138 .prop('enctype', 'multipart/form-data')
139 // enctype must be set as encoding for IE:
140 .prop('encoding', 'multipart/form-data');
141 }
142 form.submit();
143 // Insert the file input fields at their original location
144 // by replacing the clones with the originals:
145 if (fileInputClones && fileInputClones.length) {
146 options.fileInput.each(function (index, input) {
147 var clone = $(fileInputClones[index]);
148 $(input).prop('name', clone.prop('name'));
149 clone.replaceWith(input);
150 });
151 }
152 });
153 form.append(iframe).appendTo(document.body);
154 },
155 abort: function () {
156 if (iframe) {
157 // javascript:false as iframe src aborts the request
158 // and prevents warning popups on HTTPS in IE6.
159 // concat is used to avoid the "Script URL" JSLint error:
160 iframe
161 .unbind('load')
162 .prop('src', 'javascript'.concat(':false;'));
163 }
164 if (form) {
165 form.remove();
166 }
167 }
168 };
169 }
170 });
171
172 // The iframe transport returns the iframe content document as response.
173 // The following adds converters from iframe to text, json, html, xml
174 // and script.
175 // Please note that the Content-Type for JSON responses has to be text/plain
176 // or text/html, if the browser doesn't include application/json in the
177 // Accept header, else IE will show a download dialog.
178 // The Content-Type for XML responses on the other hand has to be always
179 // application/xml or text/xml, so IE properly parses the XML response.
180 // See also
181 // https://github.com/blueimp/jQuery-File-Upload/wiki/Setup#content-type-negotiation
182 $.ajaxSetup({
183 converters: {
184 'iframe text': function (iframe) {
185 return iframe && $(iframe[0].body).text();
186 },
187 'iframe json': function (iframe) {
188 return iframe && $.parseJSON($(iframe[0].body).text());
189 },
190 'iframe html': function (iframe) {
191 return iframe && $(iframe[0].body).html();
192 },
193 'iframe xml': function (iframe) {
194 var xmlDoc = iframe && iframe[0];
195 return xmlDoc && $.isXMLDoc(xmlDoc) ? xmlDoc :
196 $.parseXML((xmlDoc.XMLDocument && xmlDoc.XMLDocument.xml) ||
197 $(xmlDoc.body).html());
198 },
199 'iframe script': function (iframe) {
200 return iframe && $.globalEval($(iframe[0].body).text());
201 }
202 }
203 });
204
205 }));