forked from andreyfedoseev/jquery-ocupload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.ocupload.js
277 lines (241 loc) · 7.6 KB
/
jquery.ocupload.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
One Click Upload - jQuery Plugin
--------------------------------
Copyright (c) 2008 Michael Mitchell - http://www.michaelmitchell.co.nz
Copyright (c) 2011 Andrey Fedoseev <[email protected]> -
http://andreyfedoseev.name
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
(function ($) {
$.fn.upload = function (options) {
/** Merge the user's options with our defaults */
options = $.extend({
name: 'file',
enctype: 'multipart/form-data',
action: '',
autoSubmit: true,
onSubmit: function () {},
onComplete: function () {},
onSelect: function () {},
params: {}
}, options);
return new $.ocupload(this, options);
};
$.ocupload = function (element, options) {
/** Fix scope problems */
var self = this;
/** A unique id so we can find our elements later */
var id = new Date().getTime().toString().substr(8);
/** Upload iframe */
var iframe = $("<iframe></iframe>", {
id: "iframe" + id,
name: "iframe" + id
}).css({
display: "none"
});
/** Form */
var form = $("<form></form>", {
method: "post",
enctype: options.enctype,
action: options.action,
target: "iframe" + id
}).css({
margin: 0,
padding: 0
});
/** Get cursor type from the object ocupload was assigned to */
/** TODO: Add parameter to init? cursor: auto, cursor: pointer etc */
var element_cursor = element.css('cursor');
/** File Input */
var input = $("<input>", {
name: options.name,
"type": "file"
}).css({
position: 'absolute',
display: 'block',
cursor: element_cursor,
opacity: 0
});
/** Put everything together */
element.wrap("<div></div>");
form.append(input);
element.after(form);
element.after(iframe);
/** Find the container and make it nice and snug */
var container = element.parent().css({
position: 'relative',
height: element.outerHeight() + 'px',
width: element.outerWidth() + 'px',
overflow: 'hidden',
cursor: element_cursor,
margin: 0,
padding: 0
});
/** Get input dimensions so we can put it in the right place */
var input_height = input.outerHeight(element.outerHeight());
var input_width = input.outerWidth(element.outerWidth());
input.css({
margin: "0",
padding: "0",
top: "0",
left: "0"
});
/** Move the input with the mouse to make sure it get clicked! */
container.mousemove(function (e) {
input.css({
top: e.pageY - container.offset().top - (input_height / 2) + 'px',
left: e.pageX - container.offset().left - input_width + 30 + 'px'
});
});
function onChange() {
/** Do something when a file is selected. */
self.onSelect();
/** Submit the form automaticly after selecting the file */
if (self.autoSubmit) {
self.submit();
}
}
/** Watch for file selection */
input.change(onChange);
/** Methods */
$.extend(this, {
autoSubmit: true,
onSubmit: options.onSubmit,
onComplete: options.onComplete,
onSelect: options.onSelect,
/** get filename */
filename: function () {
return input.attr('value');
},
/** get/set params */
params: function (params) {
params = params ? params : false;
if (params) {
options.params = $.extend(options.params, params);
}
else {
return options.params;
}
},
/** get/set name */
name: function (name) {
name = name ? name : false;
if (name) {
input.attr('name', value);
}
else {
return input.attr('name');
}
},
/** get/set action */
action: function (action) {
action = action ? action : false;
if (action) {
form.attr('action', action);
}
else {
return form.attr('action');
}
},
/** get/set enctype */
enctype: function (enctype) {
enctype = enctype ? enctype : false;
if (enctype) {
form.attr('enctype', enctype);
}
else {
return form.attr('enctype');
}
},
/** set options */
set: function (obj, value) {
value = value ? value : false;
function option(action, value) {
switch (action) {
case 'name':
self.name(value);
break;
case 'action':
self.action(value);
break;
case 'enctype':
self.enctype(value);
break;
case 'params':
self.params(value);
break;
case 'autoSubmit':
self.autoSubmit = value;
break;
case 'onSubmit':
self.onSubmit = value;
break;
case 'onComplete':
self.onComplete = value;
break;
case 'onSelect':
self.onSelect = value;
break;
default:
throw new Error('[jQuery.ocupload.set] \'' + action + '\' is ' +
'an invalid option.');
}
}
if (value) {
option(obj, value);
}
else {
$.each(obj,
function (key, value) {
option(key, value);
});
}
},
/** Submit the form */
submit: function () {
/** Do something before we upload */
this.onSubmit();
/** add additional parameters before sending */
$.each(options.params, function (key, value) {
field = form.find("input:hidden[name='" + key + "']");
if (!field.length) {
field = $('<input type="hidden">').attr("name",
key).appendTo(form);
}
field.val(value);
});
/** Submit the actual form */
form.submit();
/** Do something after we are finished uploading */
iframe.unbind().load(function () {
/** Get a response from the server in plain text */
var myFrame = document.getElementById(iframe.attr('name'));
var response = $(myFrame.contentWindow.document.body).text();
/** Do something on complete */
self.onComplete(response);
//done :D
// Reset file input field
input.wrap("<div></div>");
var wrapper = input.parent();
input = $(wrapper.html());
input.change(onChange);
wrapper.replaceWith(input);
});
}
});
};
})(jQuery);