Subversion-Projekte lars-tiefland.webanos.faltradxxs.de

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
24 lars 1
/*
2
 * jQuery postMessage Transport Plugin
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
 * https://opensource.org/licenses/MIT
10
 */
11
 
12
/* global define, require */
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';
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
    };
52
 
53
  $.ajaxSetup({
54
    converters: {
55
      'postmessage text': convert,
56
      'postmessage json': convert,
57
      'postmessage html': convert
58
    }
59
  });
60
 
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
                  }
112
                }
113
              });
114
              iframe[0].contentWindow.postMessage(message, target);
115
            })
116
            .appendTo(document.body);
117
        },
118
        abort: function () {
119
          if (iframe) {
120
            iframe.remove();
121
          }
122
        }
123
      };
124
    }
125
  });
126
});