-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjquery.uploader.js
More file actions
104 lines (77 loc) · 3.14 KB
/
jquery.uploader.js
File metadata and controls
104 lines (77 loc) · 3.14 KB
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
(function ($) {
$.uploader = {
instanceList : {}
};
$.fn.uploader = function (options) {
this.each(function (index) {
var uploaderOptions = $.extend({}, options);
uploaderOptions.buttonElement = this;
var uploader = new Uploader(uploaderOptions);
var uploaderId = $(this).attr('id') ? $(this).attr('id') : uploader.id;
$.uploader.instanceList[uploaderId] = uploader;
});
}
function Uploader (options) {
var that = this;
this.options = $.extend({}, {
url : 'upload.php',
buttonElement : null,
inputName : 'ajax_file',
data : {},
onStart : function () {},
onComplete : function (response) {}
}, options);
this.id = ((new Date()).getTime());
this.$button = $(this.options.buttonElement);
// assemble upload form/frame mechanics
this.$form = $('<form id="uploader_form_'+this.id+
'" action="'+this.options.url+'" method="post" '+
'enctype="multipart/form-data" target="uploader_frame_'+this.id+
'"></form>');
this.$frame = $('<iframe name="uploader_frame_' + this.id + '" src="about:blank" \/>');
this.$frame.css("display", "none");
this.$input = $('<input type="file" name="'+this.options.inputName+'">');
this.$input.on('change', function () {
this.form.submit();
that.$input.attr('disabled', 'disabled');
that.options.onStart.call(that);
});
this.updateFormData = function (formData)
{
that.$form.find('input[type=hidden]').remove();
for (var key in formData) {
that.$form.append('<input type="hidden" name="'+key+'" name="'+key+'" value="'+formData[key]+'"/>');
}
}
this.updateFormData(this.options.data);
// position upload over button
function repositionForm () {
var buttonOffset = that.$button.offset();
that.$form
.width(that.$button.outerWidth())
.height(that.$button.outerHeight())
.css({
overflow : 'hidden',
position : 'absolute',
top : buttonOffset.top+'px',
left : buttonOffset.left+'px',
opacity: '0'
});
}
repositionForm();
this.$button.on('hover', repositionForm);
this.$input.css({
fontSize : (this.$button.outerHeight() + 20) + 'px',
cursor: 'pointer'
});
// attach to document
this.$form.append(this.$input);
$('body').append(this.$form).append(this.$frame);
this.$frame.on('load', function (e) {
var $body = $(window.frames['uploader_frame_'+that.id].document.getElementsByTagName('body')[0]);
that.options.onComplete.call(that, $body.html());
that.$input.removeAttr('disabled');
that.$form.get(0).reset();
});
}
})(jQuery);