PageRenderTime 18ms CodeModel.GetById 10ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/BMTmobile-master/assets/grocery_crud/js/jquery_plugins/jquery.iframe-transport.js

https://gitlab.com/webservers/codeigniter_mobile_dev_server
JavaScript | 165 lines | 112 code | 6 blank | 47 comment | 16 complexity | 2738635b40fd195d7dd5b70f313c453d MD5 | raw file
  1/*
  2 * jQuery Iframe Transport Plugin 1.3
  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    // Helper variable to create unique names for the transport iframes:
 28    var counter = 0;
 29
 30    // The iframe transport accepts three additional options:
 31    // options.fileInput: a jQuery collection of file input fields
 32    // options.paramName: the parameter name for the file form data,
 33    //  overrides the name property of the file input field(s)
 34    // options.formData: an array of objects with name and value properties,
 35    //  equivalent to the return data of .serializeArray(), e.g.:
 36    //  [{name: 'a', value: 1}, {name: 'b', value: 2}]
 37    $.ajaxTransport('iframe', function (options) {
 38        if (options.async && (options.type === 'POST' || options.type === 'GET')) {
 39            var form,
 40                iframe;
 41            return {
 42                send: function (_, completeCallback) {
 43                    form = $('<form style="display:none;"></form>');
 44                    // javascript:false as initial iframe src
 45                    // prevents warning popups on HTTPS in IE6.
 46                    // IE versions below IE8 cannot set the name property of
 47                    // elements that have already been added to the DOM,
 48                    // so we set the name along with the iframe HTML markup:
 49                    iframe = $(
 50                        '<iframe src="javascript:false;" name="iframe-transport-' +
 51                            (counter += 1) + '"></iframe>'
 52                    ).bind('load', function () {
 53                        var fileInputClones;
 54                        iframe
 55                            .unbind('load')
 56                            .bind('load', function () {
 57                                var response;
 58                                // Wrap in a try/catch block to catch exceptions thrown
 59                                // when trying to access cross-domain iframe contents:
 60                                try {
 61                                    response = iframe.contents();
 62                                    // Google Chrome and Firefox do not throw an
 63                                    // exception when calling iframe.contents() on
 64                                    // cross-domain requests, so we unify the response:
 65                                    if (!response.length || !response[0].firstChild) {
 66                                        throw new Error();
 67                                    }
 68                                } catch (e) {
 69                                    response = undefined;
 70                                }
 71                                // The complete callback returns the
 72                                // iframe content document as response object:
 73                                completeCallback(
 74                                    200,
 75                                    'success',
 76                                    {'iframe': response}
 77                                );
 78                                // Fix for IE endless progress bar activity bug
 79                                // (happens on form submits to iframe targets):
 80                                $('<iframe src="javascript:false;"></iframe>')
 81                                    .appendTo(form);
 82                                form.remove();
 83                            });
 84                        form
 85                            .prop('target', iframe.prop('name'))
 86                            .prop('action', options.url)
 87                            .prop('method', options.type);
 88                        if (options.formData) {
 89                            $.each(options.formData, function (index, field) {
 90                                $('<input type="hidden"/>')
 91                                    .prop('name', field.name)
 92                                    .val(field.value)
 93                                    .appendTo(form);
 94                            });
 95                        }
 96                        if (options.fileInput && options.fileInput.length &&
 97                                options.type === 'POST') {
 98                            fileInputClones = options.fileInput.clone();
 99                            // Insert a clone for each file input field:
100                            options.fileInput.after(function (index) {
101                                return fileInputClones[index];
102                            });
103                            if (options.paramName) {
104                                options.fileInput.each(function () {
105                                    $(this).prop('name', options.paramName);
106                                });
107                            }
108                            // Appending the file input fields to the hidden form
109                            // removes them from their original location:
110                            form
111                                .append(options.fileInput)
112                                .prop('enctype', 'multipart/form-data')
113                                // enctype must be set as encoding for IE:
114                                .prop('encoding', 'multipart/form-data');
115                        }
116                        form.submit();
117                        // Insert the file input fields at their original location
118                        // by replacing the clones with the originals:
119                        if (fileInputClones && fileInputClones.length) {
120                            options.fileInput.each(function (index, input) {
121                                var clone = $(fileInputClones[index]);
122                                $(input).prop('name', clone.prop('name'));
123                                clone.replaceWith(input);
124                            });
125                        }
126                    });
127                    form.append(iframe).appendTo(document.body);
128                },
129                abort: function () {
130                    if (iframe) {
131                        // javascript:false as iframe src aborts the request
132                        // and prevents warning popups on HTTPS in IE6.
133                        // concat is used to avoid the "Script URL" JSLint error:
134                        iframe
135                            .unbind('load')
136                            .prop('src', 'javascript'.concat(':false;'));
137                    }
138                    if (form) {
139                        form.remove();
140                    }
141                }
142            };
143        }
144    });
145
146    // The iframe transport returns the iframe content document as response.
147    // The following adds converters from iframe to text, json, html, and script:
148    $.ajaxSetup({
149        converters: {
150            'iframe text': function (iframe) {
151                return $(iframe[0].body).text();
152            },
153            'iframe json': function (iframe) {
154                return $.parseJSON($(iframe[0].body).text());
155            },
156            'iframe html': function (iframe) {
157                return $(iframe[0].body).html();
158            },
159            'iframe script': function (iframe) {
160                return $.globalEval($(iframe[0].body).text());
161            }
162        }
163    });
164
165}));