|
8 | 8 | */ |
9 | 9 | (function($) { |
10 | 10 |
|
11 | | - var uuid = 0; |
12 | | - |
13 | | - $.fn.uploadimage = function(url, data, callback, type) { |
14 | | - var self = this, inputs, checkbox, checked, |
15 | | - iframeName = 'jquery_upload' + ++uuid, |
16 | | - iframe = $('<iframe name="' + iframeName + '" style="position:absolute;top:-9999px" />').appendTo('body'), |
17 | | - form = '<form target="' + iframeName + '" method="post" enctype="multipart/form-data" />'; |
18 | | - |
19 | | - if ($.isFunction(data)) { |
20 | | - type = callback; |
21 | | - callback = data; |
22 | | - data = {}; |
23 | | - } |
24 | | - |
25 | | - checkbox = $('input:checkbox', this); |
26 | | - checked = $('input:checked', this); |
27 | | - form = self.wrapAll(form).parent('form').attr('action', url); |
28 | | - |
29 | | - // Make sure radios and checkboxes keep original values |
30 | | - // (IE resets checkd attributes when appending) |
31 | | - checkbox.removeAttr('checked'); |
32 | | - checked.attr('checked', true); |
33 | | - |
34 | | - inputs = createInputs(data); |
35 | | - inputs = inputs ? $(inputs).appendTo(form) : null; |
36 | | - |
37 | | - form.submit(function() { |
38 | | - iframe.load(function() { |
39 | | - var data = handleData(this, type), |
40 | | - checked = $('input:checked', self); |
41 | | - |
42 | | - form.after(self).remove(); |
43 | | - checkbox.removeAttr('checked'); |
44 | | - checked.attr('checked', true); |
45 | | - if (inputs) { |
46 | | - inputs.remove(); |
47 | | - } |
48 | | - |
49 | | - setTimeout(function() { |
50 | | - iframe.remove(); |
51 | | - if (type === 'script') { |
52 | | - $.globalEval(data); |
53 | | - } |
54 | | - if (callback) { |
55 | | - callback.call(self, data); |
56 | | - } |
57 | | - }, 0); |
58 | | - }); |
59 | | - }).submit(); |
60 | | - |
61 | | - return this; |
62 | | - }; |
63 | | - |
64 | | - function createInputs(data) { |
65 | | - return $.map(param(data), function(param) { |
66 | | - return '<input type="hidden" name="' + param.name + '" value="' + param.value + '"/>'; |
67 | | - }).join(''); |
| 11 | + var uuid = 0; |
| 12 | + |
| 13 | + $.fn.uploadimage = function(url, data, callback, type) { |
| 14 | + var self = this, inputs, checkbox, checked, |
| 15 | + iframeName = 'jquery_upload' + ++uuid, |
| 16 | + iframe = $('<iframe name="' + iframeName + '" style="position:absolute;top:-9999px" />').appendTo('body'), |
| 17 | + form = '<form target="' + iframeName + '" method="post" enctype="multipart/form-data" />'; |
| 18 | + |
| 19 | + if ($.isFunction(data)) { |
| 20 | + type = callback; |
| 21 | + callback = data; |
| 22 | + data = {}; |
| 23 | + } |
| 24 | + |
| 25 | + checkbox = $('input:checkbox', this); |
| 26 | + checked = $('input:checked', this); |
| 27 | + form = self.wrapAll(form).parent('form').attr('action', url); |
| 28 | + |
| 29 | + // Make sure radios and checkboxes keep original values |
| 30 | + // (IE resets checkd attributes when appending) |
| 31 | + checkbox.removeAttr('checked'); |
| 32 | + checked.attr('checked', true); |
| 33 | + |
| 34 | + inputs = createInputs(data); |
| 35 | + inputs = inputs ? $(inputs).appendTo(form) : null; |
| 36 | + |
| 37 | + form.submit(function() { |
| 38 | + iframe.load(function() { |
| 39 | + var data = handleData(this, type), |
| 40 | + checked = $('input:checked', self); |
| 41 | + |
| 42 | + form.after(self).remove(); |
| 43 | + checkbox.removeAttr('checked'); |
| 44 | + checked.attr('checked', true); |
| 45 | + if (inputs) { |
| 46 | + inputs.remove(); |
68 | 47 | } |
69 | 48 |
|
70 | | - function param(data) { |
71 | | - if ($.isArray(data)) { |
72 | | - return data; |
73 | | - } |
74 | | - var params = []; |
75 | | - |
76 | | - function add(name, value) { |
77 | | - params.push({name:name, value:value}); |
78 | | - } |
79 | | - |
80 | | - if (typeof data === 'object') { |
81 | | - $.each(data, function(name) { |
82 | | - if ($.isArray(this)) { |
83 | | - $.each(this, function() { |
84 | | - add(name, this); |
85 | | - }); |
86 | | - } else { |
87 | | - add(name, $.isFunction(this) ? this() : this); |
88 | | - } |
89 | | - }); |
90 | | - } else if (typeof data === 'string') { |
91 | | - $.each(data.split('&'), function() { |
92 | | - var param = $.map(this.split('='), function(v) { |
93 | | - return decodeURIComponent(v.replace(/\+/g, ' ')); |
94 | | - }); |
95 | | - |
96 | | - add(param[0], param[1]); |
97 | | - }); |
98 | | - } |
99 | | - |
100 | | - return params; |
101 | | - } |
102 | | - |
103 | | - function handleData(iframe, type) { |
104 | | - var data, contents = $(iframe).contents().get(0); |
105 | | - |
106 | | - if ($.isXMLDoc(contents) || contents.XMLDocument) { |
107 | | - return contents.XMLDocument || contents; |
108 | | - } |
109 | | - data = $(contents).find('body').html(); |
110 | | - |
111 | | - switch (type) { |
112 | | - case 'xml': |
113 | | - data = parseXml(data); |
114 | | - break; |
115 | | - case 'json': |
116 | | - data = window.eval('(' + data + ')'); |
117 | | - break; |
118 | | - } |
119 | | - return data; |
120 | | - } |
121 | | - |
122 | | - function parseXml(text) { |
123 | | - if (window.DOMParser) { |
124 | | - return new DOMParser().parseFromString(text, 'application/xml'); |
125 | | - } else { |
126 | | - var xml = new ActiveXObject('Microsoft.XMLDOM'); |
127 | | - xml.async = false; |
128 | | - xml.loadXML(text); |
129 | | - return xml; |
130 | | - } |
| 49 | + setTimeout(function() { |
| 50 | + iframe.remove(); |
| 51 | + if (type === 'script') { |
| 52 | + $.globalEval(data); |
| 53 | + } |
| 54 | + if (callback) { |
| 55 | + callback.call(self, data); |
| 56 | + } |
| 57 | + }, 0); |
| 58 | + }); |
| 59 | + }).submit(); |
| 60 | + |
| 61 | + return this; |
| 62 | + }; |
| 63 | + |
| 64 | + function createInputs(data) { |
| 65 | + return $.map(param(data), function(param) { |
| 66 | + return '<input type="hidden" name="' + param.name + '" value="' + param.value + '"/>'; |
| 67 | + }).join(''); |
| 68 | + } |
| 69 | + |
| 70 | + function param(data) { |
| 71 | + if ($.isArray(data)) { |
| 72 | + return data; |
| 73 | + } |
| 74 | + var params = []; |
| 75 | + |
| 76 | + function add(name, value) { |
| 77 | + params.push({name:name, value:value}); |
| 78 | + } |
| 79 | + |
| 80 | + if (typeof data === 'object') { |
| 81 | + $.each(data, function(name) { |
| 82 | + if ($.isArray(this)) { |
| 83 | + $.each(this, function() { |
| 84 | + add(name, this); |
| 85 | + }); |
| 86 | + } else { |
| 87 | + add(name, $.isFunction(this) ? this() : this); |
131 | 88 | } |
| 89 | + }); |
| 90 | + } else if (typeof data === 'string') { |
| 91 | + $.each(data.split('&'), function() { |
| 92 | + var param = $.map(this.split('='), function(v) { |
| 93 | + return decodeURIComponent(v.replace(/\+/g, ' ')); |
| 94 | + }); |
| 95 | + |
| 96 | + add(param[0], param[1]); |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + return params; |
| 101 | + } |
| 102 | + |
| 103 | + function handleData(iframe, type) { |
| 104 | + var data, contents = $(iframe).contents().get(0); |
| 105 | + |
| 106 | + if ($.isXMLDoc(contents) || contents.XMLDocument) { |
| 107 | + return contents.XMLDocument || contents; |
| 108 | + } |
| 109 | + data = $(contents).find('body').html(); |
| 110 | + |
| 111 | + switch (type) { |
| 112 | + case 'xml': |
| 113 | + data = parseXml(data); |
| 114 | + break; |
| 115 | + case 'json': |
| 116 | + data = JSON.parse(data); |
| 117 | + break; |
| 118 | + } |
| 119 | + return data; |
| 120 | + } |
| 121 | + |
| 122 | + function parseXml(text) { |
| 123 | + if (window.DOMParser) { |
| 124 | + return new DOMParser().parseFromString(text, 'application/xml'); |
| 125 | + } else { |
| 126 | + var xml = new ActiveXObject('Microsoft.XMLDOM'); |
| 127 | + xml.async = false; |
| 128 | + xml.loadXML(text); |
| 129 | + return xml; |
| 130 | + } |
| 131 | + } |
132 | 132 |
|
133 | 133 | })(jQuery); |
0 commit comments