-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathFileUploader.js
144 lines (131 loc) · 3.78 KB
/
FileUploader.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
function PunkAveFileUploader(options)
{
var self = this,
uploadUrl = options.uploadUrl,
viewUrl = options.viewUrl,
$el = $(options.el),
uploaderTemplate = _.template($.trim($('#file-uploader-template').html()));
$el.html(uploaderTemplate({}));
var fileTemplate = _.template($.trim($('#file-uploader-file-template').html())),
editor = $el.find('[data-files="1"]'),
thumbnails = $el.find('[data-thumbnails="1"]');
self.uploading = false;
self.errorCallback = 'errorCallback' in options ? options.errorCallback : function( info ) { if (window.console && console.log) { console.log(info) } },
self.addExistingFiles = function(files)
{
_.each(files, function(file) {
appendEditableImage({
// cmsMediaUrl is a global variable set by the underscoreTemplates partial of MediaItems.html.twig
'thumbnail_url': viewUrl + '/thumbnails/' + file,
'url': viewUrl + '/originals/' + file,
'name': file
});
});
};
// Delay form submission until upload is complete.
// Note that you are welcome to examine the
// uploading property yourself if this isn't
// quite right for you
self.delaySubmitWhileUploading = function(sel)
{
$(sel).submit(function(e) {
if (!self.uploading)
{
return true;
}
function attempt()
{
if (self.uploading)
{
setTimeout(attempt, 100);
}
else
{
$(sel).submit();
}
}
attempt();
return false;
});
}
if (options.blockFormWhileUploading)
{
self.blockFormWhileUploading(options.blockFormWhileUploading);
}
if (options.existingFiles)
{
self.addExistingFiles(options.existingFiles);
}
editor.fileupload({
dataType: 'json',
url: uploadUrl,
dropZone: $el.find('[data-dropzone="1"]'),
sequentialUploads: (options.sequentialUploads),
done: function (e, data) {
if (data)
{
_.each(data.result, function(item) {
appendEditableImage(item);
});
}
},
start: function (e) {
$el.find('[data-spinner="1"]').show();
self.uploading = true;
},
stop: function (e) {
$el.find('[data-spinner="1"]').hide();
self.uploading = false;
}
});
// Expects thumbnail_url, url, and name properties. thumbnail_url can be undefined if
// url does not end in gif, jpg, jpeg or png. This is designed to work with the
// result returned by the UploadHandler class on the PHP side
function appendEditableImage(info)
{
if (info.error)
{
self.errorCallback(info);
return;
}
var li = $(fileTemplate(info));
li.find('[data-action="delete"]').click(function(event) {
var file = $(this).closest('[data-name]');
var name = file.attr('data-name');
$.ajax({
type: 'delete',
url: setQueryParameter(uploadUrl, 'file', name),
success: function() {
file.remove();
},
dataType: 'json'
});
return false;
});
thumbnails.append(li);
}
function setQueryParameter(url, param, paramVal)
{
var newAdditionalURL = "";
var tempArray = url.split("?");
var baseURL = tempArray[0];
var additionalURL = tempArray[1];
var temp = "";
if (additionalURL)
{
var tempArray = additionalURL.split("&");
var i;
for (i = 0; i < tempArray.length; i++)
{
if (tempArray[i].split('=')[0] != param )
{
newAdditionalURL += temp + tempArray[i];
temp = "&";
}
}
}
var newTxt = temp + "" + param + "=" + encodeURIComponent(paramVal);
var finalURL = baseURL + "?" + newAdditionalURL + newTxt;
return finalURL;
}
}