aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/blueimp/jquery-file-upload/js/cors/jquery.postmessage-transport.js
blob: 0a3d9fe45014545a8c8fb03979bdf6b7ca264bbb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * jQuery postMessage Transport Plugin
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2011, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * https://opensource.org/licenses/MIT
 */

/* global define, require */

(function(factory) {
  'use strict';
  if (typeof define === 'function' && define.amd) {
    // Register as an anonymous AMD module:
    define(['jquery'], factory);
  } else if (typeof exports === 'object') {
    // Node/CommonJS:
    factory(require('jquery'));
  } 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;
      // IE always includes the port for the host property of a link
      // element, but not in the location.host or origin property for the
      // default http port 80 and https port 443, so we strip it:
      if (/^(http:\/\/.+:80)|(https:\/\/.+:443)$/.test(target)) {
        target = target.replace(/:(80|443)$/, '');
      }
      return {
        send: function(_, completeCallback) {
          counter += 1;
          var message = {
              id: 'postmessage-transport-' + counter
            },
            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(event) {
                var e = event.originalEvent;
                var data = e.data;
                var 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();
          }
        }
      };
    }
  });
});