-
Notifications
You must be signed in to change notification settings - Fork 10
/
uploadkit.js
233 lines (188 loc) · 7.23 KB
/
uploadkit.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
var UKEventType = {
FileUploaded: 'UKFileUploaded',
UploadComplete: 'UKUploadComplete',
UploadError: 'UKUploadError'
};
var UploadKit = function(input) {
if (!window['plupload']) {
console.error('Unable to initialize UploadKit; Plupload dependency not found');
return null;
}
var $input = $(input);
var $form = $input.closest('form');
var id = (Date['now']) ? Date.now() : +new Date(); // TODO: Verify this failover works in IE.
var baseUrl = '';
$('script').each(function(index, element) {
var src = $(element).attr('src') || '';
var endIndex = src.indexOf('uploadkit.js');
if (endIndex !== -1) baseUrl = (endIndex === 0) ? './' : src.substring(0, endIndex);
});
var self = this;
var name = this.name = $input.attr('name');
var isMultiple = this.isMultiple = !!$input.attr('multiple');
var uploadUrl = this.uploadUrl = $input.data('uploadUrl') || $form.attr('action');
var maxFileSize = this.maxFileSize = $input.data('maxFileSize') || this.maxFileSize;
var classes = ($input.attr('class') + '').replace(/uk-input/g, '');
var $element = this.$element = $input.wrap('<div id="uk-container-' + id + '" class="uk-container ' + classes + '"/>').parent();
$element.data('uploadKit', this);
$input.data('uploadKit', this);
$input.attr('disabled', true);
var infoHtml = (isMultiple) ?
'<h1>No Files Selected</h1><h2>Browse for files to upload or drag and drop them here</h2>' :
'<h1>No File Selected</h1><h2>Browse for file to upload or drag and drop it here</h2>';
var $info = this.$info = $('<div class="uk-info"/>').html(infoHtml).appendTo($element);
var $table = this.$table = $('<table class="table table-condensed"/>').appendTo($element).hide();
var $thead = this.$thead = $('<thead/>').html('<tr><th/><th/><th>File Name</th><th>Size</th><th>Progress</th></tr>').appendTo($table);
var $tbody = this.$tbody = $('<tbody/>').appendTo($table);
var $browseButton = this.$browseButton = $('<a id="uk-browse-button-' + id + '" class="btn" href="#"/>').html('<i class="icon-file"/>Browse...').appendTo($element);
var $uploadButton = this.$uploadButton = $('<a id="uk-upload-button-' + id + '" class="btn btn-primary" href="#"/>').html('<i class="icon-upload icon-white"/>Upload').appendTo($element).hide();
var uploader = this.uploader = new plupload.Uploader({
runtimes: 'html5,flash,silverlight,gears,browserplus,html4',
container: 'uk-container-' + id,
drop_element: 'uk-container-' + id,
browse_button: 'uk-browse-button-' + id,
file_data_name: name,
multi_selection: isMultiple,
url: uploadUrl,
max_file_size: maxFileSize,
flash_swf_url: baseUrl + 'externals/plupload/js/plupload.flash.swf',
silverlight_xap_url: baseUrl + 'externals/plupload/js/plupload.silverlight.xap',
filters: []
});
var responses = this.responses = [];
uploader.bind('Init', function(uploader, params) {
console.log('Initialized UploadKit uploader with ' + params.runtime + ' runtime');
});
uploader.bind('FilesAdded', function(uploader, files) {
$info.hide();
$table.show();
$uploadButton.removeClass('disabled').show();
var newFiles = files;
if (!isMultiple) {
var existingFiles = uploader.files;
if (existingFiles.length > 0) uploader.removeFile(existingFiles[0]);
if (files.length > 0) newFiles = [files[0]];
$tbody.children('tr.error').remove();
}
for (var i = 0, length = newFiles.length; i < length; i++) {
$tbody.append('<tr id="' + newFiles[i].id + '">' +
'<td class="uk-close-column"><a class="close" title="Remove" href="#">×</a></td>' +
'<td class="uk-icon-column"><i class="icon-file"/></td>' +
'<td>' + newFiles[i].name + '</td>' +
'<td class="uk-size-column">' + plupload.formatSize(newFiles[i].size) + '</td>' +
'<td class="uk-progress-column">' +
'<div class="progress progress-info progress-striped active">' +
'<div class="bar"/>' +
'</div>' +
'</td>' +
'</tr>');
}
});
uploader.bind('FilesRemoved', function(uploader, files) {
var removedFiles = files;
for (var i = 0, length = removedFiles.length; i < length; i++) {
$tbody.children('#' + removedFiles[i].id).remove();
}
});
uploader.bind('BeforeUpload', function(uploader, file) {
var multipartParams = {};
var fields = $form.serializeArray();
for (var i = 0, length = fields.length; i < length; i++) {
var field = fields[i];
var fieldName = field.name;
if (fieldName) multipartParams[fieldName] = field.value;
}
uploader.settings.multipart_params = multipartParams;
});
uploader.bind('UploadProgress', function(uploader, file) {
var $tr = $tbody.find('#' + file.id);
var $bar = $tr.find('.bar');
$bar.css('width', file.percent + '%');
});
uploader.bind('FileUploaded', function(uploader, file, response) {
var $tr = $tbody.find('#' + file.id);
var $progress = $tr.find('.progress');
var $bar = $progress.find('.bar');
$progress.removeClass('progress-info active').addClass('progress-success');
$bar.html('Done');
responses.push(response);
$input.trigger($.Event(UKEventType.FileUploaded, {
uploader: uploader,
file: file,
response: response
}));
});
uploader.bind('UploadComplete', function(uploader, files) {
$input.trigger($.Event(UKEventType.UploadComplete, {
uploader: uploader,
files: files,
responses: responses
}));
});
uploader.bind('Error', function(uploader, error) {
var $tr = $tbody.find('#' + error.file.id).addClass('error');
var $td = $tr.children('.uk-progress-column');
var message;
switch (error.code) {
case -600:
message = 'File size exceeds limit';
break;
default:
message = 'Unknown error occurred';
break;
}
$td.html(message);
if (uploader.files.length === 0) $uploadButton.addClass('disabled');
$input.trigger($.Event(UKEventType.UploadError, {
uploader: uploader,
error: error
}));
});
$tbody.delegate('a.close', 'click', function(evt) {
var $this = $(this);
var $tr = $this.closest('tr');
var id = $tr.attr('id');
var file = uploader.getFile(id);
if (uploader.files.length <= 1) {
$info.show();
$table.hide();
$browseButton.show();
$uploadButton.hide();
}
if (file) {
uploader.removeFile(file);
} else {
$tr.remove();
}
evt.preventDefault();
});
$uploadButton.bind('click', function(evt) {
if ($uploadButton.hasClass('disabled')) return false;
$uploadButton.addClass('disabled');
responses = self.responses = [];
uploader.start();
evt.preventDefault();
});
uploader.init();
};
UploadKit.prototype = {
$element: null,
$info: null,
$table: null,
$thead: null,
$tbody: null,
$browseButton: null,
$uploadButton: null,
name: null,
isMultiple: false,
uploadUrl: null,
maxFileSize: '10mb',
uploader: null,
responses: null
};
$(function() {
var $inputs = $('input.uk-input[type="file"]');
$inputs.each(function(index, element) {
new UploadKit(element);
});
});