diff options
author | Sem <sembrestels@riseup.net> | 2012-07-29 10:15:17 +0200 |
---|---|---|
committer | Sem <sembrestels@riseup.net> | 2012-07-29 10:15:17 +0200 |
commit | d00e34131d6177f6d22eb3cf32c50216820aafba (patch) | |
tree | 8adfab83855f19bc1de2273216707da99bec9c1d /vendors/jquery-file-upload/js/cors/jquery.postmessage-transport.js | |
parent | 24ff6662195222479b4d83d41fa89edc8a3c05d1 (diff) | |
download | elgg-d00e34131d6177f6d22eb3cf32c50216820aafba.tar.gz elgg-d00e34131d6177f6d22eb3cf32c50216820aafba.tar.bz2 |
Added jquery file upload plugin: https://github.com/blueimp/jQuery-File-Upload
Diffstat (limited to 'vendors/jquery-file-upload/js/cors/jquery.postmessage-transport.js')
-rw-r--r-- | vendors/jquery-file-upload/js/cors/jquery.postmessage-transport.js | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/vendors/jquery-file-upload/js/cors/jquery.postmessage-transport.js b/vendors/jquery-file-upload/js/cors/jquery.postmessage-transport.js new file mode 100644 index 000000000..931b6352b --- /dev/null +++ b/vendors/jquery-file-upload/js/cors/jquery.postmessage-transport.js @@ -0,0 +1,117 @@ +/* + * jQuery postMessage Transport Plugin 1.1 + * https://github.com/blueimp/jQuery-File-Upload + * + * Copyright 2011, Sebastian Tschan + * https://blueimp.net + * + * Licensed under the MIT license: + * http://www.opensource.org/licenses/MIT + */ + +/*jslint unparam: true, nomen: true */ +/*global define, window, document */ + +(function (factory) { + 'use strict'; + if (typeof define === 'function' && define.amd) { + // Register as an anonymous AMD module: + define(['jquery'], factory); + } else { + // Browser globals: + factory(window.jQuery); + } +}(function ($) { + 'use strict'; + + var counter = 0, + names = [ + 'accepts', + 'cache', + 'contents', + 'contentType', + 'crossDomain', + 'data', + 'dataType', + 'headers', + 'ifModified', + 'mimeType', + 'password', + 'processData', + 'timeout', + 'traditional', + 'type', + 'url', + 'username' + ], + convert = function (p) { + return p; + }; + + $.ajaxSetup({ + converters: { + 'postmessage text': convert, + 'postmessage json': convert, + 'postmessage html': convert + } + }); + + $.ajaxTransport('postmessage', function (options) { + if (options.postMessage && window.postMessage) { + var iframe, + loc = $('<a>').prop('href', options.postMessage)[0], + target = loc.protocol + '//' + loc.host, + xhrUpload = options.xhr().upload; + return { + send: function (_, completeCallback) { + var message = { + id: 'postmessage-transport-' + (counter += 1) + }, + eventName = 'message.' + message.id; + iframe = $( + '<iframe style="display:none;" src="' + + options.postMessage + '" name="' + + message.id + '"></iframe>' + ).bind('load', function () { + $.each(names, function (i, name) { + message[name] = options[name]; + }); + message.dataType = message.dataType.replace('postmessage ', ''); + $(window).bind(eventName, function (e) { + e = e.originalEvent; + var data = e.data, + ev; + if (e.origin === target && data.id === message.id) { + if (data.type === 'progress') { + ev = document.createEvent('Event'); + ev.initEvent(data.type, false, true); + $.extend(ev, data); + xhrUpload.dispatchEvent(ev); + } else { + completeCallback( + data.status, + data.statusText, + {postmessage: data.result}, + data.headers + ); + iframe.remove(); + $(window).unbind(eventName); + } + } + }); + iframe[0].contentWindow.postMessage( + message, + target + ); + }).appendTo(document.body); + }, + abort: function () { + if (iframe) { + iframe.remove(); + } + } + }; + } + }); + +})); |