spee.ch/public/assets/js/publishFileFunctions.js

103 lines
4.2 KiB
JavaScript
Raw Normal View History

2017-10-02 20:47:12 +02:00
/* publish functions */
2017-10-03 19:05:09 +02:00
function cancelPublish () {
window.location.href = '/';
}
2017-10-02 20:47:12 +02:00
// When a file is selected for publish, validate that file and
// stage it so it will be ready when the publish button is clicked.
function previewAndStageFile(selectedFile){
const publishForm = document.getElementById('publish-form-wrapper');
2017-10-03 23:52:50 +02:00
const assetPreview = document.getElementById('asset-preview-target');
2017-10-04 18:05:35 +02:00
const primaryDropzone = document.getElementById('primary-dropzone-wrapper');
2017-10-03 19:05:09 +02:00
const previewReader = new FileReader();
const nameInput = document.getElementById('claim-name-input');
2017-10-10 03:29:40 +02:00
const fileSelectionInputError = document.getElementById('input-error-file-selection');
const thumbnailSelectionTool = document.getElementById('publish-thumbnail');
const thumbnailSelectionInput = document.getElementById('claim-thumbnail-input');
2017-10-02 20:47:12 +02:00
// validate the file's name, type, and size
try {
2017-10-03 19:05:09 +02:00
console.log('validating file');
2017-10-02 20:47:12 +02:00
validateFile(selectedFile);
} catch (error) {
2017-10-03 19:05:09 +02:00
console.log('file validation failed with error:', error);
2017-10-10 03:29:40 +02:00
showError(fileSelectionInputError, error.message);
2017-10-02 20:47:12 +02:00
return;
}
// set the image preview, if an image was provided
2017-10-03 19:05:09 +02:00
console.log('file type:', selectedFile.type)
if (selectedFile.type !== 'video/mp4') {
if (selectedFile.type === 'image/gif') {
2017-10-03 23:52:50 +02:00
assetPreview.innerHTML = `<h2>loading preview...</h2>`
2017-10-03 19:05:09 +02:00
}
2017-10-02 20:47:12 +02:00
previewReader.readAsDataURL(selectedFile);
previewReader.onloadend = function () {
2017-10-03 23:52:50 +02:00
assetPreview.innerHTML = '<img id="asset-preview" src="' + previewReader.result + '" alt="image preview"/>';
2017-10-02 20:47:12 +02:00
};
2017-10-10 03:29:40 +02:00
// clear & hide the thumbnail selection input
thumbnailSelectionInput.value = '';
thumbnailSelectionTool.hidden = true;
2017-10-03 19:05:09 +02:00
} else {
2017-10-10 03:29:40 +02:00
assetPreview.innerHTML = `<img id="asset-preview" src="/assets/img/black_video_play.jpg"/>`;
// clear & show the thumbnail selection input
thumbnailSelectionInput.value = '';
thumbnailSelectionTool.hidden = false;
2017-10-02 20:47:12 +02:00
}
// hide the drop zone
2017-10-04 18:05:35 +02:00
primaryDropzone.hidden = true;
2017-10-02 20:47:12 +02:00
publishForm.hidden = false;
// set the name input value to the image name if none is set yet
if (nameInput.value === "") {
var filename = selectedFile.name.substring(0, selectedFile.name.indexOf('.'))
nameInput.value = cleanseClaimName(filename);
checkClaimName(nameInput.value);
}
// store the selected file for upload
stagedFiles = [selectedFile];
}
// Validate the publish submission and then trigger publishing.
2017-10-03 19:05:09 +02:00
function publishStagedFile(event) {
// prevent default so this script can handle submission
event.preventDefault();
// declare variables
2017-10-03 19:05:09 +02:00
const claimName = document.getElementById('claim-name-input').value;
let channelName = document.getElementById('channel-name-select').value;
2017-10-10 03:29:40 +02:00
const fileSelectionInputError = document.getElementById('input-error-file-selection');
2017-10-03 19:05:09 +02:00
const claimNameError = document.getElementById('input-error-claim-name');
const channelSelectError = document.getElementById('input-error-channel-select');
const publishSubmitError = document.getElementById('input-error-publish-submit');
let anonymousOrInChannel;
2017-10-10 03:29:40 +02:00
// replace channelName with 'anonymous' if appropriate
const radios = document.getElementsByName('anonymous-or-channel');
for (let i = 0; i < radios.length; i++) {
if (radios[i].checked) {
// do whatever you want with the checked radio
anonymousOrInChannel = radios[i].value;
// only one radio can be logically checked, don't check the rest
break;
}
}
2017-10-10 03:29:40 +02:00
if (anonymousOrInChannel === 'anonymous') {
channelName = null;
};
console.log('channel name:', channelName);
2017-10-02 20:47:12 +02:00
// validate, submit, and handle response
validateFilePublishSubmission(stagedFiles, claimName, channelName)
.then(() => {
uploader.submitFiles(stagedFiles);
})
.catch(error => {
if (error.name === 'FileError') {
2017-10-10 03:29:40 +02:00
showError(fileSelectionInputError, error.message);
2017-10-02 20:47:12 +02:00
} else if (error.name === 'NameError') {
2017-10-03 19:05:09 +02:00
showError(claimNameError, error.message);
2017-10-02 20:47:12 +02:00
} else if (error.name === 'ChannelNameError'){
console.log(error);
2017-10-03 19:05:09 +02:00
showError(channelSelectError, error.message);
2017-10-02 20:47:12 +02:00
} else {
2017-10-03 19:05:09 +02:00
showError(publishSubmitError, error.message);
2017-10-02 20:47:12 +02:00
}
return;
})
};