|
1 | 1 | /*
|
2 | 2 | * jQuery postMessage Transport Plugin
|
3 | 3 | * https://github.com/blueimp/jQuery-File-Upload
|
4 |
| - * |
| 4 | + * Version: 10.32.0 |
5 | 5 | * Copyright 2011, Sebastian Tschan
|
6 | 6 | * https://blueimp.net
|
7 | 7 | *
|
8 | 8 | * Licensed under the MIT license:
|
9 |
| - * http://www.opensource.org/licenses/MIT |
| 9 | + * https://opensource.org/licenses/MIT |
10 | 10 | */
|
11 | 11 |
|
12 |
| -/* global define, require, window, document */ |
| 12 | +/* global define, require */ |
13 | 13 |
|
14 |
| -;(function (factory) { |
15 |
| - 'use strict'; |
16 |
| - if (typeof define === 'function' && define.amd) { |
17 |
| - // Register as an anonymous AMD module: |
18 |
| - define(['jquery'], factory); |
19 |
| - } else if (typeof exports === 'object') { |
20 |
| - // Node/CommonJS: |
21 |
| - factory(require('jquery')); |
22 |
| - } else { |
23 |
| - // Browser globals: |
24 |
| - factory(window.jQuery); |
25 |
| - } |
26 |
| -}(function ($) { |
27 |
| - 'use strict'; |
| 14 | +(function (factory) { |
| 15 | + 'use strict'; |
| 16 | + if (typeof define === 'function' && define.amd) { |
| 17 | + // Register as an anonymous AMD module: |
| 18 | + define(['jquery'], factory); |
| 19 | + } else if (typeof exports === 'object') { |
| 20 | + // Node/CommonJS: |
| 21 | + factory(require('jquery')); |
| 22 | + } else { |
| 23 | + // Browser globals: |
| 24 | + factory(window.jQuery); |
| 25 | + } |
| 26 | +})(function ($) { |
| 27 | + 'use strict'; |
28 | 28 |
|
29 |
| - var counter = 0, |
30 |
| - names = [ |
31 |
| - 'accepts', |
32 |
| - 'cache', |
33 |
| - 'contents', |
34 |
| - 'contentType', |
35 |
| - 'crossDomain', |
36 |
| - 'data', |
37 |
| - 'dataType', |
38 |
| - 'headers', |
39 |
| - 'ifModified', |
40 |
| - 'mimeType', |
41 |
| - 'password', |
42 |
| - 'processData', |
43 |
| - 'timeout', |
44 |
| - 'traditional', |
45 |
| - 'type', |
46 |
| - 'url', |
47 |
| - 'username' |
48 |
| - ], |
49 |
| - convert = function (p) { |
50 |
| - return p; |
51 |
| - }; |
| 29 | + var counter = 0, |
| 30 | + names = [ |
| 31 | + 'accepts', |
| 32 | + 'cache', |
| 33 | + 'contents', |
| 34 | + 'contentType', |
| 35 | + 'crossDomain', |
| 36 | + 'data', |
| 37 | + 'dataType', |
| 38 | + 'headers', |
| 39 | + 'ifModified', |
| 40 | + 'mimeType', |
| 41 | + 'password', |
| 42 | + 'processData', |
| 43 | + 'timeout', |
| 44 | + 'traditional', |
| 45 | + 'type', |
| 46 | + 'url', |
| 47 | + 'username' |
| 48 | + ], |
| 49 | + convert = function (p) { |
| 50 | + return p; |
| 51 | + }; |
52 | 52 |
|
53 |
| - $.ajaxSetup({ |
54 |
| - converters: { |
55 |
| - 'postmessage text': convert, |
56 |
| - 'postmessage json': convert, |
57 |
| - 'postmessage html': convert |
58 |
| - } |
59 |
| - }); |
| 53 | + $.ajaxSetup({ |
| 54 | + converters: { |
| 55 | + 'postmessage text': convert, |
| 56 | + 'postmessage json': convert, |
| 57 | + 'postmessage html': convert |
| 58 | + } |
| 59 | + }); |
60 | 60 |
|
61 |
| - $.ajaxTransport('postmessage', function (options) { |
62 |
| - if (options.postMessage && window.postMessage) { |
63 |
| - var iframe, |
64 |
| - loc = $('<a>').prop('href', options.postMessage)[0], |
65 |
| - target = loc.protocol + '//' + loc.host, |
66 |
| - xhrUpload = options.xhr().upload; |
67 |
| - // IE always includes the port for the host property of a link |
68 |
| - // element, but not in the location.host or origin property for the |
69 |
| - // default http port 80 and https port 443, so we strip it: |
70 |
| - if (/^(http:\/\/.+:80)|(https:\/\/.+:443)$/.test(target)) { |
71 |
| - target = target.replace(/:(80|443)$/, ''); |
72 |
| - } |
73 |
| - return { |
74 |
| - send: function (_, completeCallback) { |
75 |
| - counter += 1; |
76 |
| - var message = { |
77 |
| - id: 'postmessage-transport-' + counter |
78 |
| - }, |
79 |
| - eventName = 'message.' + message.id; |
80 |
| - iframe = $( |
81 |
| - '<iframe style="display:none;" src="' + |
82 |
| - options.postMessage + '" name="' + |
83 |
| - message.id + '"></iframe>' |
84 |
| - ).bind('load', function () { |
85 |
| - $.each(names, function (i, name) { |
86 |
| - message[name] = options[name]; |
87 |
| - }); |
88 |
| - message.dataType = message.dataType.replace('postmessage ', ''); |
89 |
| - $(window).bind(eventName, function (e) { |
90 |
| - e = e.originalEvent; |
91 |
| - var data = e.data, |
92 |
| - ev; |
93 |
| - if (e.origin === target && data.id === message.id) { |
94 |
| - if (data.type === 'progress') { |
95 |
| - ev = document.createEvent('Event'); |
96 |
| - ev.initEvent(data.type, false, true); |
97 |
| - $.extend(ev, data); |
98 |
| - xhrUpload.dispatchEvent(ev); |
99 |
| - } else { |
100 |
| - completeCallback( |
101 |
| - data.status, |
102 |
| - data.statusText, |
103 |
| - {postmessage: data.result}, |
104 |
| - data.headers |
105 |
| - ); |
106 |
| - iframe.remove(); |
107 |
| - $(window).unbind(eventName); |
108 |
| - } |
109 |
| - } |
110 |
| - }); |
111 |
| - iframe[0].contentWindow.postMessage( |
112 |
| - message, |
113 |
| - target |
114 |
| - ); |
115 |
| - }).appendTo(document.body); |
116 |
| - }, |
117 |
| - abort: function () { |
118 |
| - if (iframe) { |
119 |
| - iframe.remove(); |
120 |
| - } |
| 61 | + $.ajaxTransport('postmessage', function (options) { |
| 62 | + if (options.postMessage && window.postMessage) { |
| 63 | + var iframe, |
| 64 | + loc = $('<a></a>').prop('href', options.postMessage)[0], |
| 65 | + target = loc.protocol + '//' + loc.host, |
| 66 | + xhrUpload = options.xhr().upload; |
| 67 | + // IE always includes the port for the host property of a link |
| 68 | + // element, but not in the location.host or origin property for the |
| 69 | + // default http port 80 and https port 443, so we strip it: |
| 70 | + if (/^(http:\/\/.+:80)|(https:\/\/.+:443)$/.test(target)) { |
| 71 | + target = target.replace(/:(80|443)$/, ''); |
| 72 | + } |
| 73 | + return { |
| 74 | + send: function (_, completeCallback) { |
| 75 | + counter += 1; |
| 76 | + var message = { |
| 77 | + id: 'postmessage-transport-' + counter |
| 78 | + }, |
| 79 | + eventName = 'message.' + message.id; |
| 80 | + iframe = $( |
| 81 | + '<iframe style="display:none;" src="' + |
| 82 | + options.postMessage + |
| 83 | + '" name="' + |
| 84 | + message.id + |
| 85 | + '"></iframe>' |
| 86 | + ) |
| 87 | + .on('load', function () { |
| 88 | + $.each(names, function (i, name) { |
| 89 | + message[name] = options[name]; |
| 90 | + }); |
| 91 | + message.dataType = message.dataType.replace('postmessage ', ''); |
| 92 | + $(window).on(eventName, function (event) { |
| 93 | + var e = event.originalEvent; |
| 94 | + var data = e.data; |
| 95 | + var ev; |
| 96 | + if (e.origin === target && data.id === message.id) { |
| 97 | + if (data.type === 'progress') { |
| 98 | + ev = document.createEvent('Event'); |
| 99 | + ev.initEvent(data.type, false, true); |
| 100 | + $.extend(ev, data); |
| 101 | + xhrUpload.dispatchEvent(ev); |
| 102 | + } else { |
| 103 | + completeCallback( |
| 104 | + data.status, |
| 105 | + data.statusText, |
| 106 | + { postmessage: data.result }, |
| 107 | + data.headers |
| 108 | + ); |
| 109 | + iframe.remove(); |
| 110 | + $(window).off(eventName); |
| 111 | + } |
121 | 112 | }
|
122 |
| - }; |
| 113 | + }); |
| 114 | + iframe[0].contentWindow.postMessage(message, target); |
| 115 | + }) |
| 116 | + .appendTo(document.body); |
| 117 | + }, |
| 118 | + abort: function () { |
| 119 | + if (iframe) { |
| 120 | + iframe.remove(); |
| 121 | + } |
123 | 122 | }
|
124 |
| - }); |
125 |
| - |
126 |
| -})); |
| 123 | + }; |
| 124 | + } |
| 125 | + }); |
| 126 | +}); |
0 commit comments