|
| 1 | +/* |
| 2 | + * jQuery postMessage Transport Plugin 1.1 |
| 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 | + var counter = 0, |
| 28 | + names = [ |
| 29 | + 'accepts', |
| 30 | + 'cache', |
| 31 | + 'contents', |
| 32 | + 'contentType', |
| 33 | + 'crossDomain', |
| 34 | + 'data', |
| 35 | + 'dataType', |
| 36 | + 'headers', |
| 37 | + 'ifModified', |
| 38 | + 'mimeType', |
| 39 | + 'password', |
| 40 | + 'processData', |
| 41 | + 'timeout', |
| 42 | + 'traditional', |
| 43 | + 'type', |
| 44 | + 'url', |
| 45 | + 'username' |
| 46 | + ], |
| 47 | + convert = function (p) { |
| 48 | + return p; |
| 49 | + }; |
| 50 | + |
| 51 | + $.ajaxSetup({ |
| 52 | + converters: { |
| 53 | + 'postmessage text': convert, |
| 54 | + 'postmessage json': convert, |
| 55 | + 'postmessage html': convert |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + $.ajaxTransport('postmessage', function (options) { |
| 60 | + if (options.postMessage && window.postMessage) { |
| 61 | + var iframe, |
| 62 | + loc = $('<a>').prop('href', options.postMessage)[0], |
| 63 | + target = loc.protocol + '//' + loc.host, |
| 64 | + xhrUpload = options.xhr().upload; |
| 65 | + return { |
| 66 | + send: function (_, completeCallback) { |
| 67 | + var message = { |
| 68 | + id: 'postmessage-transport-' + (counter += 1) |
| 69 | + }, |
| 70 | + eventName = 'message.' + message.id; |
| 71 | + iframe = $( |
| 72 | + '<iframe style="display:none;" src="' + |
| 73 | + options.postMessage + '" name="' + |
| 74 | + message.id + '"></iframe>' |
| 75 | + ).bind('load', function () { |
| 76 | + $.each(names, function (i, name) { |
| 77 | + message[name] = options[name]; |
| 78 | + }); |
| 79 | + message.dataType = message.dataType.replace('postmessage ', ''); |
| 80 | + $(window).bind(eventName, function (e) { |
| 81 | + e = e.originalEvent; |
| 82 | + var data = e.data, |
| 83 | + ev; |
| 84 | + if (e.origin === target && data.id === message.id) { |
| 85 | + if (data.type === 'progress') { |
| 86 | + ev = document.createEvent('Event'); |
| 87 | + ev.initEvent(data.type, false, true); |
| 88 | + $.extend(ev, data); |
| 89 | + xhrUpload.dispatchEvent(ev); |
| 90 | + } else { |
| 91 | + completeCallback( |
| 92 | + data.status, |
| 93 | + data.statusText, |
| 94 | + {postmessage: data.result}, |
| 95 | + data.headers |
| 96 | + ); |
| 97 | + iframe.remove(); |
| 98 | + $(window).unbind(eventName); |
| 99 | + } |
| 100 | + } |
| 101 | + }); |
| 102 | + iframe[0].contentWindow.postMessage( |
| 103 | + message, |
| 104 | + target |
| 105 | + ); |
| 106 | + }).appendTo(document.body); |
| 107 | + }, |
| 108 | + abort: function () { |
| 109 | + if (iframe) { |
| 110 | + iframe.remove(); |
| 111 | + } |
| 112 | + } |
| 113 | + }; |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | +})); |
0 commit comments