This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
main.js
148 lines (135 loc) · 6.04 KB
/
main.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
/*
* jQuery File Upload Plugin JS Example 8.9.1
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2010, Sebastian Tschan
* https://blueimp.net
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/MIT
*/
/* global $, window */
$(function () {
'use strict';
$('.fileupload').each(function() {
var $doc = $(document),
uploadForm = $(this),
dropZone = uploadForm.find('.drag-drop'),
filesList = uploadForm.find('.files'),
errorList = uploadForm.find('.js-errors'),
startButton = uploadForm.find('.fileupload-main-start'),
doneButton = uploadForm.find('.fileupload-done'),
cancelButton = uploadForm.find('.fileupload-cancel'),
closeButton = uploadForm.find('.fileupload-close'),
closeButtons = doneButton.add(cancelButton).add(closeButton),
container = uploadForm.closest('.modal'),
revealButtonId = container.attr('id') + '-toggle-button',
revealButton = $('#' + revealButtonId),
countIndicator = uploadForm.find('.js-file-count'),
resetUploadForm = (function resetUploadForm () {
errorList.empty();
// individual items should clean up any resources they use
filesList.find('.cancel').click();
// remove items that don't have a cancel button
filesList.empty();
startButton.removeClass('is-hidden')
.attr('disabled', true)
.text('Upload Documents');
doneButton.addClass('is-hidden').prop('disabled', true);
cancelButton.removeClass('is-hidden').prop('disabled', false);
dropZone.removeClass('is-hidden');
}),
updateCountIndicator = (function updateCountIndicator (options) {
var numberOfFiles = options.getNumberOfFiles(),
noFiles = numberOfFiles === 0;
// FIXME i18n on this string
countIndicator.text((noFiles ? 'No' : numberOfFiles) + ' document' + (numberOfFiles === 1 ? '' : 's') + ' queued');
// start button should only be enabled if there are files to upload
startButton.attr('disabled', noFiles);
}),
updateUploadCountIndicator = (function updateUploadCountIndicator (options) {
var totalFiles = options.getNumberOfFiles(),
uploadedFiles = options.getNumberOfUploadedFiles();
countIndicator.text('Uploaded ' + uploadedFiles + ' of ' + totalFiles + ' files');
if (uploadedFiles === totalFiles) {
startButton.addClass('is-hidden').prop('disabled', true);
doneButton.removeClass('is-hidden').prop('disabled', false);
cancelButton.addClass('is-hidden').prop('disabled', true);
}
});
// move the container to the end of the body so it is on top of everything
container.appendTo('body');
revealButton.bind('click', resetUploadForm);
closeButtons.bind('click', function (e) {
zanata.modal.hide('#' + container.attr('id'));
});
// prevent default file drop behaviour on the page
$doc.bind('drop dragover', function (e) {
e.preventDefault();
});
dropZone.bind('drop dragleave dragend', function (e) {
e.preventDefault();
dropZone.removeClass('is-active');
});
dropZone.bind('dragover', function (e) {
e.preventDefault();
dropZone.addClass('is-active');
});
var accepted = uploadForm.data('accepted') || '';
var acceptedTypes = (function (commaSeparated) {
var types = commaSeparated.split(',');
$.each(types, function (i, type) {
types[i] = type.trim();
});
return new RegExp('(\.|\/)(' + types.join('|') + ')$', 'i');
})(accepted);
if (accepted.trim().length === 0) {
uploadForm.find('.js-upload-supported').addClass('is-hidden');
} else {
uploadForm.find('.js-upload-not-supported').addClass('is-hidden');
}
uploadForm.fileupload({
sequentialUploads: true,
maxFileSize: 200*1024*1024,
dropZone: dropZone,
beforeAdd: (function beforeAdd (e, data) {
errorList.empty();
startButton.attr('disabled', true);
}),
afterAdd: (function afterAdd (e, data) {
updateCountIndicator(data);
}),
errorList: errorList,
acceptFileTypes: acceptedTypes,
failed: (function updateCount (e, data) {
// update count when removing files
var $this = $(this),
that = $this.data('blueimp-fileupload') ||
$this.data('fileupload'),
options = that.options;
// FIXME may want to make sure this doesn't trigger update while
// uploading. Alternative: make this function aware of
// current uploading state.
updateCountIndicator(options);
}),
completed: (function completed (e, data) {
var $this = $(this),
that = $this.data('blueimp-fileupload') ||
$this.data('fileupload'),
options = that.options;
updateUploadCountIndicator(options);
}),
updateUploadCountIndicator: updateUploadCountIndicator
});
// FIXME may be unnecessary. If necessary, it could just go in the above options
// Enable iframe cross-domain access via redirect option:
uploadForm.fileupload(
'option',
'redirect',
window.location.href.replace(
/\/[^\/]*$/,
'/cors/result.html?%s'
)
);
});
});