2015-08-18 13:15:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-11-25 16:27:32 +01:00
|
|
|
import fineUploader from 'fineUploader';
|
2015-12-10 17:54:33 +01:00
|
|
|
import MimeTypes from '../../constants/mime_types';
|
|
|
|
|
2016-02-05 15:17:59 +01:00
|
|
|
|
2015-11-25 16:27:32 +01:00
|
|
|
// Re-export qq.status from FineUploader with an additional online
|
|
|
|
// state that we use to keep track of files from S3.
|
2016-02-05 15:17:59 +01:00
|
|
|
export const FileStatus = Object.assign({}, fineUploader.status, {
|
2015-11-25 16:27:32 +01:00
|
|
|
ONLINE: 'online'
|
|
|
|
});
|
|
|
|
|
2015-09-01 14:42:09 +02:00
|
|
|
export const formSubmissionValidation = {
|
|
|
|
/**
|
|
|
|
* Returns a boolean if there has been at least one file uploaded
|
|
|
|
* successfully without it being deleted or canceled.
|
|
|
|
* @param {array of files} files provided by react fine uploader
|
2015-11-25 16:27:32 +01:00
|
|
|
* @return {boolean}
|
2015-09-01 14:42:09 +02:00
|
|
|
*/
|
|
|
|
atLeastOneUploadedFile(files) {
|
2015-11-25 18:58:49 +01:00
|
|
|
files = files.filter((file) => {
|
|
|
|
return file.status !== FileStatus.DELETED &&
|
2016-02-05 15:17:59 +01:00
|
|
|
file.status !== FileStatus.CANCELED &&
|
|
|
|
file.status != FileStatus.UPLOADED_FAILED
|
2015-11-25 18:58:49 +01:00
|
|
|
});
|
|
|
|
|
2016-02-05 15:17:59 +01:00
|
|
|
if (files.length && files[0].status === FileStatus.UPLOAD_SUCCESSFUL) {
|
2015-09-01 14:42:09 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* File submission for the form is optional, but if the user decides to submit a file
|
|
|
|
* the form is not ready until there are no more files currently uploading.
|
|
|
|
* @param {array of files} files files provided by react fine uploader
|
|
|
|
* @return {boolean} [description]
|
|
|
|
*/
|
|
|
|
fileOptional(files) {
|
2016-02-05 15:17:59 +01:00
|
|
|
const uploadingFiles = files.filter((file) => file.status === FileStatus.SUBMITTING);
|
2015-09-01 14:42:09 +02:00
|
|
|
|
2016-02-09 10:20:32 +01:00
|
|
|
return uploadingFiles.length === 0;
|
2015-08-18 13:15:22 +02:00
|
|
|
}
|
2015-09-15 13:30:24 +02:00
|
|
|
};
|
|
|
|
|
2015-09-10 09:54:02 +02:00
|
|
|
/**
|
2015-11-25 18:58:49 +01:00
|
|
|
* Filter function for filtering all deleted, canceled, and failed files
|
2015-09-10 09:54:02 +02:00
|
|
|
* @param {object} file A file from filesToUpload that has status as a prop.
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
export function displayValidFilesFilter(file) {
|
2015-11-25 18:58:49 +01:00
|
|
|
return file.status !== FileStatus.DELETED &&
|
2016-02-05 15:17:59 +01:00
|
|
|
file.status !== FileStatus.CANCELED &&
|
|
|
|
file.status !== FileStatus.UPLOAD_FAILED;
|
2015-09-10 09:54:02 +02:00
|
|
|
}
|
|
|
|
|
2015-11-23 17:59:20 +01:00
|
|
|
/**
|
2015-11-25 18:58:49 +01:00
|
|
|
* Filter function for filtering all files except for deleted, canceled, and failed files
|
2015-11-23 17:59:20 +01:00
|
|
|
* @param {object} file A file from filesToUpload that has status as a prop.
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
export function displayRemovedFilesFilter(file) {
|
2015-11-25 18:58:49 +01:00
|
|
|
return file.status === FileStatus.DELETED ||
|
2016-02-05 15:17:59 +01:00
|
|
|
file.status === FileStatus.CANCELED ||
|
|
|
|
file.status === FileStatus.UPLOAD_FAILED;
|
2015-11-23 17:59:20 +01:00
|
|
|
}
|
|
|
|
|
2015-09-10 09:54:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter function for which files to integrate in the progress process
|
|
|
|
* @param {object} file A file from filesToUpload, that has a status as a prop.
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
export function displayValidProgressFilesFilter(file) {
|
2015-11-25 18:58:49 +01:00
|
|
|
return file.status !== FileStatus.DELETED &&
|
2016-02-05 15:17:59 +01:00
|
|
|
file.status !== FileStatus.CANCELED &&
|
|
|
|
file.status !== FileStatus.UPLOAD_FAILED &&
|
|
|
|
file.status !== FileStatus.ONLINE;
|
2015-09-10 09:54:02 +02:00
|
|
|
}
|
2015-09-15 13:30:24 +02:00
|
|
|
|
2015-09-15 16:35:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fineuploader allows to specify the file extensions that are allowed to upload.
|
|
|
|
* For our self defined input, we can reuse those declarations to restrict which files
|
|
|
|
* the user can pick from his hard drive.
|
|
|
|
*
|
|
|
|
* Takes an array of file extensions (['pdf', 'png', ...]) and transforms them into a string
|
|
|
|
* that can be passed into an html5 input via its 'accept' prop.
|
2015-12-10 17:54:33 +01:00
|
|
|
* @param {array} allowedExtensions Array of strings without a dot prefixed
|
2015-09-15 16:35:45 +02:00
|
|
|
* @return {string} Joined string (comma-separated) of the passed-in array
|
|
|
|
*/
|
|
|
|
export function transformAllowedExtensionsToInputAcceptProp(allowedExtensions) {
|
2015-12-10 17:54:33 +01:00
|
|
|
// Get the mime type of the extension if it's defined or add a dot in front of the extension
|
2015-12-14 11:41:06 +01:00
|
|
|
// This is important for Safari as it doesn't understand just the extension.
|
2016-02-05 17:06:16 +01:00
|
|
|
const prefixedAllowedExtensions = allowedExtensions.map((ext) => {
|
2015-12-10 17:54:33 +01:00
|
|
|
return MimeTypes[ext] || ('.' + ext);
|
|
|
|
});
|
2015-09-15 16:35:45 +02:00
|
|
|
|
|
|
|
// generate a comma separated list to add them to the DOM element
|
|
|
|
// See: http://stackoverflow.com/questions/4328947/limit-file-format-when-using-input-type-file
|
|
|
|
return prefixedAllowedExtensions.join(', ');
|
|
|
|
}
|