/
jquery.uploadzone.js
111 lines (97 loc) · 2.77 KB
/
jquery.uploadzone.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
/* Image upload plugin w/ drag'n'drop support | by @SeinopSys | for gh:ponydevs/MLPVC-RR */
/* global File */
(function(){
'use strict';
let defaults = {
requestKey: 'file',
title: 'Image upload',
accept: 'image/*',
target: '',
helper: true,
};
$.fn.uploadZone = function(opt){
opt = $.extend(true,{},defaults,opt);
let title = opt.title,
$this = $(this).first(),
$input = $.mk('input').attr({
'type': 'file',
'name': opt.requestKey,
'accept': opt.accept,
}),
$helper;
if (opt.helper)
$helper = $.mk('div').addClass('helper');
$input.on('set-image',function(_, response){
const actions = function(){
$this.removeClass('uploading');
if (response.path){
$input.prev().attr('href', response.path).children('img').fadeTo(200,0,function(){
let $image = $(this);
$this.addClass('loading');
$image.attr({ src: response.path, alt: '' }).on('load',function(){
$this.removeClass('loading');
$image.fadeTo(200,1);
});
$this.trigger('uz-uploadfinish', [response]);
});
return;
}
$this.trigger('uz-uploadfinish', [response]);
};
if (response.keep_dialog === true)
actions();
else $.Dialog.close(actions);
});
$input.on('dragenter dragleave', function(e){
e.stopPropagation();
e.preventDefault();
$this[e.type === 'dragenter' ? 'addClass' : 'removeClass']('drop');
});
$input.on('change drop', function(e){
let files = e.target.files || e.originalEvent.dataTransfer.files;
if (typeof files[0] === 'undefined' || !(files[0] instanceof File))
return true;
$this.trigger('uz-uploadstart').removeClass('drop').addClass('uploading');
let fd = new FormData();
fd.append(opt.requestKey, files[0]);
let ajaxOpts = {
url: opt.target,
type: "POST",
contentType: false,
processData: false,
cache: false,
data: fd,
success: $.mkAjaxHandler(function(){
if (this.status)
$input.trigger('set-image', [this]);
else {
$.Dialog.fail(title,this.message);
$this.trigger('uz-uploadfinish');
}
}),
complete: function(){
$this.removeClass('uploading');
if (opt.helper)
$helper.removeAttr('data-progress');
$input.val('');
}
};
if (opt.helper) ajaxOpts.xhr = function () {
let xhrobj = $.ajaxSettings.xhr();
if (xhrobj.upload)
xhrobj.upload.addEventListener('progress', event => {
if (!event.lengthComputable || !opt.helper) return true;
let complete = event.loaded || event.position,
total = event.total;
$helper.attr('data-progress', Math.round(complete / total * 100));
}, false);
return xhrobj;
};
$.ajax(ajaxOpts);
});
$this.append($input);
if (opt.helper)
$this.append($helper);
return $this;
};
})(jQuery);