Merge pull request #967 from solgenomics/topic/expression_atlas_export
[sgn.git] / js / jquery / iframe-post-form.js
blob34d64f1dbbdf8075d29aa63770b0d0aa32ec993d
1 /**
2 * jQuery plugin for posting form including file inputs.
3 *
4 * Copyright (c) 2010 - 2011 Ewen Elder
6 * Licensed under the MIT and GPL licenses:
7 * http://www.opensource.org/licenses/mit-license.php
8 * http://www.gnu.org/licenses/gpl.html
10 * @author: Ewen Elder <ewen at jainaewen dot com> <glomainn at yahoo dot co dot uk>
11 * @version: 1.1.1 (2011-07-29)
12 **/
13 (function ($)
17 $('input[type=\"file\"]').each(function(i, it) {
18 $(it).closest('form').attr('enctype', 'multipart/form-data').attr('encoding', 'multipart/form-data');
19 });
21 $.fn.iframePostForm = function (options)
23 var response,
24 returnReponse,
25 element,
26 status = true,
27 iframe;
29 options = $.extend({}, $.fn.iframePostForm.defaults, options);
32 // Add the iframe.
33 if (!$('#' + options.iframeID).length)
35 $('body').append('<iframe id="' + options.iframeID + '" name="' + options.iframeID + '" style="display:none" />');
39 return $(this).each(function ()
41 element = $(this);
44 // Target the iframe.
45 element.attr('target', options.iframeID);
48 // Submit listener.
49 element.submit(function ()
51 // If status is false then abort.
52 status = options.post.apply(this);
54 if (status === false)
56 return status;
60 iframe = $('#' + options.iframeID).load(function ()
62 response = iframe.contents().find('body');
65 if (options.json)
67 returnReponse = $.parseJSON(response.html());
70 else
72 returnReponse = response.html();
76 options.complete.apply(this, [returnReponse]);
78 iframe.unbind('load');
81 setTimeout(function ()
83 response.html('');
84 }, 1);
85 });
86 });
87 });
91 $.fn.iframePostForm.defaults =
93 iframeID : 'iframe-post-form', // Iframe ID.
94 json : false, // Parse server response as a json object.
95 post : function () {}, // Form onsubmit.
96 complete : function (response) {} // After response from the server has been received.
98 })(jQuery);